Skip to content

Remove duplicate error about raw underscore lifetime #143280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Jul 1, 2025

Fixes #143152

r? @fee1-dead

@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 Jul 1, 2025
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

Can you instead use tcx.sess.psess.raw_identifier_spans to look for the raw lifetime span? I think we populate raw_identifier_spans with raw lifetimes too.

If not, then I don't think this hack should land. Using span_to_snippet here seems fragile.

@compiler-errors
Copy link
Member

r? compiler-errors

@rustbot rustbot assigned compiler-errors and unassigned fee1-dead Jul 1, 2025
@xizheyin
Copy link
Contributor Author

xizheyin commented Jul 1, 2025

Can you instead use tcx.sess.psess.raw_identifier_spans to look for the raw lifetime span? I think we populate raw_identifier_spans with raw lifetimes too.

I have a try on it, and it works.

Comment on lines +2905 to +2914
// To avoid emitting two duplicate errors, we need to check if the span is a raw underscore lifetime, see issue #143152
let is_raw_underscore = self
.r
.tcx
.sess
.psess
.raw_identifier_spans
.iter()
.any(|span| span == param.span());
if param.ident.name == kw::UnderscoreLifetime && !is_raw_underscore {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm wondering if I should split it into 2 parts, as this doesn't seem very aesthetically pleasing taking up so many lines. However it seems to give the wrong impression that the variable in the middle will be used multiple times.

Comment on lines +2905 to 2917
// To avoid emitting two duplicate errors, we need to check if the span is a raw underscore lifetime, see issue #143152
let is_raw_underscore = self
.r
.tcx
.sess
.psess
.raw_identifier_spans
.iter()
.any(|span| span == param.span());
if param.ident.name == kw::UnderscoreLifetime && !is_raw_underscore {
self.r
.dcx()
.emit_err(errors::UnderscoreLifetimeIsReserved { span: param.ident.span });
Copy link
Member

Choose a reason for hiding this comment

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

Use emit_unless, and also we don't need to factor this into a variable.

Suggested change
// To avoid emitting two duplicate errors, we need to check if the span is a raw underscore lifetime, see issue #143152
let is_raw_underscore = self
.r
.tcx
.sess
.psess
.raw_identifier_spans
.iter()
.any(|span| span == param.span());
if param.ident.name == kw::UnderscoreLifetime && !is_raw_underscore {
self.r
.dcx()
.emit_err(errors::UnderscoreLifetimeIsReserved { span: param.ident.span });
self.r
.dcx()
.create_err(errors::UnderscoreLifetimeIsReserved {
span: param.ident.span,
})
// To avoid emitting two duplicate errors, we need to check if the span is a raw underscore lifetime, see issue #143152
.emit_unless(
kw::UnderscoreLifetime
&& self
.r
.tcx
.sess
.psess
.raw_identifier_spans
.iter()
.any(|span| span == param.span()),
);

Copy link
Member

Choose a reason for hiding this comment

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

Also I think the comment could be tweaked; instead of "two duplicate errors", b/c the error is not duplicate, we should say something different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'r#_ gives two confusingly similar errors
4 participants