Skip to content

Select proper span for delegation, update span of child path segment if needed#154002

Closed
aerooneqq wants to merge 1 commit intorust-lang:mainfrom
aerooneqq:glob-last-segment-span
Closed

Select proper span for delegation, update span of child path segment if needed#154002
aerooneqq wants to merge 1 commit intorust-lang:mainfrom
aerooneqq:glob-last-segment-span

Conversation

@aerooneqq
Copy link
Contributor

@aerooneqq aerooneqq commented Mar 17, 2026

This PR selects more appropriate span for delegation, as span related to child segment of delegation path can point to delegee trait function. Moreover, we update span of child segment during AST -> HIR lowering for proper diagnostics.

Created from discussion. Part of #118212.

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 Mar 17, 2026
@petrochenkov petrochenkov added the F-fn_delegation `#![feature(fn_delegation)]` label Mar 17, 2026
// thus those spans can point either to local or external
// trait functions. This `span` is also used for diagnostics purposes, so we want it to point
// to delegation, not to the random trait function, so we perform this check here.
// We could have added similar check in `rustc_resolve::macros::glob_delegation_suffixes`
Copy link
Contributor

Choose a reason for hiding this comment

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

This was my first reaction - we should generate a good span (not referring to a completely different entity) during expansion, when we are building the expanded delegation item AST. Patching these spans during AST lowering doesn't seem right.

What exactly breaks if impl-reuse-pass if the span is changed during expansion?

Copy link
Contributor

Choose a reason for hiding this comment

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

There's also a question of which spans to use exactly.

  • For single delegations expanded from glob delegations I'd set the last segment's span to the star symbol's span.
  • For reuse impl there's no better choice than the whole item span, I guess.

Copy link
Contributor Author

@aerooneqq aerooneqq Mar 18, 2026

Choose a reason for hiding this comment

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

Errors are in macros test. I've tried to change span only for path segment (as in code below) and span of the ident, in both cases there were errors.

Changes
suffixes.iter().map(move |&(ident, rename)| {
     let mut path = deleg.prefix.clone();
     let mut seg_ident = ident;
     if !item_span.contains(seg_ident.span) {
         seg_ident.span = item_span;
     }

     path.segments.push(ast::PathSegment {
         ident: seg_ident,
         id: ast::DUMMY_NODE_ID,
         args: None,
     });

     ast::Item {
         attrs: item.attrs.clone(),
         id: ast::DUMMY_NODE_ID,
         span: if from_glob { item_span } else { ident.span },
         vis: item.vis.clone(),
         kind: Node::delegation_item_kind(Box::new(ast::Delegation {
             id: ast::DUMMY_NODE_ID,
             qself: deleg.qself.clone(),
             path,
             ident: rename.unwrap_or(ident),
             rename,
             body: deleg.body.clone(),
             from_glob,
         })),
         tokens: None,
     }
 })
Errors
error[E0424]: expected value, found module `self`
  --> C:\work\rust\tests\ui\delegation\impl-reuse-pass.rs:177:80
   |
LL |     macro_rules! one_line_reuse { ($self:ident) => { reuse impl Trait for S1 { $self.0 } } }
   |                                                      --------------------------^^^^^----
   |                                                      |                         |
   |                                                      |                         `self` value is a keyword only available in methods with a `self` parameter
   |                                                      `self` not allowed in an implementation
LL |     one_line_reuse!(self);
   |     --------------------- in this macro invocation
   |
   = note: this error originates in the macro `one_line_reuse` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0424]: expected value, found module `self`
  --> C:\work\rust\tests\ui\delegation\impl-reuse-pass.rs:177:80
   |
LL |     macro_rules! one_line_reuse { ($self:ident) => { reuse impl Trait for S1 { $self.0 } } }
   |                                                      --------------------------^^^^^----
   |                                                      |                         |
   |                                                      |                         `self` value is a keyword only available in methods with a `self` parameter
   |                                                      `self` not allowed in an implementation
LL |     one_line_reuse!(self);
   |     --------------------- in this macro invocation
   |
   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
   = note: this error originates in the macro `one_line_reuse` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0424]: expected value, found module `self`
  --> C:\work\rust\tests\ui\delegation\impl-reuse-pass.rs:182:26
   |
LL |     macro_rules! one_line_reuse_expr { ($x:expr) => { reuse impl Trait for S2 { $x } } }
   |                                                       ------------------------------ `self` not allowed in an implementation
LL |     one_line_reuse_expr!(self.0);
   |                          ^^^^ `self` value is a keyword only available in methods with a `self` parameter

error[E0424]: expected value, found module `self`
  --> C:\work\rust\tests\ui\delegation\impl-reuse-pass.rs:182:26
   |
LL |     macro_rules! one_line_reuse_expr { ($x:expr) => { reuse impl Trait for S2 { $x } } }
   |                                                       ------------------------------ `self` not allowed in an implementation
LL |     one_line_reuse_expr!(self.0);
   |                          ^^^^ `self` value is a keyword only available in methods with a `self` parameter
   |
   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0424]: expected value, found module `self`
  --> C:\work\rust\tests\ui\delegation\impl-reuse-pass.rs:187:27
   |
LL |     macro_rules! one_line_reuse_expr2 { ($x:expr) => { reuse impl Trait for s3!() { $x } } }
   |                                                        --------------------------------- `self` not allowed in an implementation
LL |     one_line_reuse_expr2!(self.0);
   |                           ^^^^ `self` value is a keyword only available in methods with a `self` parameter

error[E0424]: expected value, found module `self`
  --> C:\work\rust\tests\ui\delegation\impl-reuse-pass.rs:187:27
   |
LL |     macro_rules! one_line_reuse_expr2 { ($x:expr) => { reuse impl Trait for s3!() { $x } } }
   |                                                        --------------------------------- `self` not allowed in an implementation
LL |     one_line_reuse_expr2!(self.0);
   |                           ^^^^ `self` value is a keyword only available in methods with a `self` parameter
   |
   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 6 previous errors

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll debug the test failure, I'm sure that we should do it during macro expansion.

@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 Mar 17, 2026
@aerooneqq
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 18, 2026
@petrochenkov
Copy link
Contributor

Closing in favor of #154049.

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 18, 2026
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)]` 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