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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2902,7 +2902,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}
}

if param.ident.name == kw::UnderscoreLifetime {
// 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 {
Comment on lines +2905 to +2914
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.

self.r
.dcx()
.emit_err(errors::UnderscoreLifetimeIsReserved { span: param.ident.span });
Comment on lines +2905 to 2917
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.

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/underscore-lifetime/raw-underscore-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This test is to ensure that the raw underscore lifetime won't emit two duplicate errors.
// See issue #143152

//@ edition: 2021

fn f<'r#_>(){}
//~^ ERROR `_` cannot be a raw lifetime

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/underscore-lifetime/raw-underscore-lifetime.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: `_` cannot be a raw lifetime
--> $DIR/raw-underscore-lifetime.rs:6:6
|
LL | fn f<'r#_>(){}
| ^^^^

error: aborting due to 1 previous error

Loading