Skip to content

Conversation

@aerooneqq
Copy link
Contributor

@aerooneqq aerooneqq commented Dec 18, 2025

This PR adds support for reusing the whole trait with a one-line reuse syntax and is part of the delegation feature #118212:

trait T {
  fn foo(&self);
}

struct S;
impl T for S { ... }

struct Wrapper(S);
reuse impl T for Wrapper { self.0 }

The core idea is that we already have support for glob reuse, so in this scenario we want to transform one-line reuse into a trait impl block with a glob reuse in the following way:

//Before
reuse impl T for Wrapper { self.0 }

//After
impl T for Wrapper {
  reuse T::* { self.0 }
}

It seems like this task can be solved during parsing stage, when we encountered a one-line trait reuse, we can expand into this impl block right away, and the code which was already written to expand glob delegations will take care about the rest. We will copy trait path into glob reuse path.

The implementation of the transformation reuses already existing methods for impl parsing, however, we do not parse inner impl items, instead we parse "inner items" as delegation body. Thus, we do not have to deal with generics, consts, unsafe and other impl related features.

Other syntax possibility is trying to shorten one-line reuse by replacing impl keyword with reuse keyword:

reuse T for Wrapper { self.0 }

In this case implementation may become more complicated, and the syntax more confusing, as keywords such as const or unsafe will precede reuse, and there are also generics:

unsafe reuse<T1, T2> T for Wrapper { self.0 }

In the first (currently implemented) version reuse is placed in the beginning of the item, and it is clear that we will reuse trait implementation, while in the second, shorter version, the reuse keyword may be lost in generics and keywords that may precede impl.

r? @petrochenkov

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 18, 2025
@aerooneqq aerooneqq force-pushed the delegation-one-line-trait-impl branch from 43b3a47 to 829d1bf Compare December 19, 2025 12:59
@petrochenkov petrochenkov added the F-fn_delegation `#![feature(fn_delegation)]` label Dec 19, 2025
// no: `reuse ::path` for compatibility reasons with macro invocations
if self.look_ahead(LOOK_AHEAD_DIST, |t| t.is_path_start() && *t != token::PathSep) {
Some(ReuseKind::Path)
} else if self.look_ahead_check_impl_frontmatter(LOOK_AHEAD_DIST) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why disabling the diagnostics here instead of using the old check_impl_frontmatter?

let deleg = DelegationMac { qself, prefix: path, suffixes, body: body(self)? };
ItemKind::DelegationMac(Box::new(deleg))

Ok(ItemKind::DelegationMac(Box::new(DelegationMac {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Ok(ItemKind::DelegationMac(Box::new(DelegationMac {
ItemKind::DelegationMac(Box::new(DelegationMac {

Ok can be moved out of the condition.

}));
};

if matches!(of_trait.polarity, ImplPolarity::Negative(..)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a semantic error, not a parsing error.
I think we can just generate a negative trait impl here, it will be reported later.

},
kind: AssocItemKind::DelegationMac(Box::new(DelegationMac {
qself: None,
prefix: of_trait.trait_ref.path.clone(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, so you cannot do things like impl my_trait!() for Type {}.
This is good, it means the impl delegation desugaring won't result in duplicating macro calls, and the parsing-time treatment of the feature is not a very bad approximation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Items in the trait path will still get duplicated.
reuse impl Trait<{ struct S; 0 }> for Type {}
->

impl Trait<{ struct S; 0 }> for Type {
  reuse Trait<{ struct S; 0 }>::*;
}

, but that's probably ok because the glob delegation will duplicate the item too (?)

  reuse Trait<{ struct S; 0 }>::foo;
  reuse Trait<{ struct S; 0 }>::bar;
  reuse Trait<{ struct S; 0 }>::baz;

Could you maybe add an AST pretty-printing test for this case?

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-fn_delegation `#![feature(fn_delegation)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants