-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add lifetime-aware support for Display
impl of Ident
#143185
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
xizheyin
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
xizheyin:143150
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2473,6 +2473,10 @@ impl Ident { | |
pub fn as_str(&self) -> &str { | ||
self.name.as_str() | ||
} | ||
|
||
pub fn as_lifetime(&self) -> Option<Ident> { | ||
self.name.as_lifetime().map(|sym| Ident::with_dummy_span(sym)) | ||
} | ||
} | ||
|
||
impl PartialEq for Ident { | ||
|
@@ -2542,6 +2546,14 @@ impl IdentPrinter { | |
|
||
impl fmt::Display for IdentPrinter { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
if let Some(lifetime) = self.symbol.as_lifetime() { | ||
f.write_str("'")?; | ||
if self.is_raw { | ||
f.write_str("r#")?; | ||
} | ||
return fmt::Display::fmt(&lifetime, f); | ||
} | ||
|
||
if self.is_raw { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When Ident starts with |
||
f.write_str("r#")?; | ||
} else if self.symbol == kw::DollarCrate { | ||
|
@@ -2633,6 +2645,11 @@ impl Symbol { | |
self == sym::empty | ||
} | ||
|
||
pub fn as_lifetime(self) -> Option<Symbol> { | ||
let name = self.as_str(); | ||
name.strip_prefix("'").map(Symbol::intern) | ||
} | ||
|
||
/// This method is supposed to be used in error messages, so it's expected to be | ||
/// identical to printing the original identifier token written in source code | ||
/// (`token_to_string`, `Ident::to_string`), except that symbols don't keep the rawness flag | ||
|
@@ -2863,7 +2880,13 @@ impl Ident { | |
/// We see this identifier in a normal identifier position, like variable name or a type. | ||
/// How was it written originally? Did it use the raw form? Let's try to guess. | ||
pub fn is_raw_guess(self) -> bool { | ||
self.name.can_be_raw() && self.is_reserved() | ||
if self.name == kw::StaticLifetime || self.name == kw::UnderscoreLifetime { | ||
false | ||
} else if let Some(lifetime) = self.as_lifetime() { | ||
lifetime.is_raw_guess() | ||
} else { | ||
self.name.can_be_raw() && self.is_reserved() | ||
} | ||
Comment on lines
+2883
to
+2889
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two keywords that need to be excluded here. |
||
} | ||
|
||
/// Whether this would be the identifier for a tuple field like `self.0`, as | ||
|
20 changes: 20 additions & 0 deletions
20
tests/ui/lifetimes/lifetime-errors/error-lifetime-name-issue-143150.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//@ edition: 2021 | ||
fn a(_: dyn Trait + 'r#fn) { //~ ERROR use of undeclared lifetime name `'r#fn` [E0261] | ||
|
||
} | ||
|
||
trait Trait {} | ||
|
||
#[derive(Eq, PartialEq)] | ||
struct Test { | ||
a: &'r#fn str, | ||
//~^ ERROR use of undeclared lifetime name `'r#fn` [E0261] | ||
//~| ERROR use of undeclared lifetime name `'r#fn` [E0261] | ||
} | ||
|
||
trait Trait1<T> | ||
where T: for<'a> Trait1<T> + 'r#fn { } //~ ERROR use of undeclared lifetime name `'r#fn` [E0261] | ||
|
||
|
||
|
||
fn main() {} |
56 changes: 56 additions & 0 deletions
56
tests/ui/lifetimes/lifetime-errors/error-lifetime-name-issue-143150.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
error[E0261]: use of undeclared lifetime name `'r#fn` | ||
--> $DIR/error-lifetime-name-issue-143150.rs:2:21 | ||
| | ||
LL | fn a(_: dyn Trait + 'r#fn) { | ||
| ^^^^^ undeclared lifetime | ||
| | ||
help: consider introducing lifetime `'r#fn` here | ||
| | ||
LL | fn a<'r#fn>(_: dyn Trait + 'r#fn) { | ||
| +++++++ | ||
|
||
error[E0261]: use of undeclared lifetime name `'r#fn` | ||
--> $DIR/error-lifetime-name-issue-143150.rs:10:9 | ||
| | ||
LL | a: &'r#fn str, | ||
| ^^^^^ undeclared lifetime | ||
| | ||
help: consider introducing lifetime `'r#fn` here | ||
| | ||
LL | struct Test<'r#fn> { | ||
| +++++++ | ||
|
||
error[E0261]: use of undeclared lifetime name `'r#fn` | ||
--> $DIR/error-lifetime-name-issue-143150.rs:10:9 | ||
| | ||
LL | #[derive(Eq, PartialEq)] | ||
| -- lifetime `'r#fn` is missing in item created through this procedural macro | ||
LL | struct Test { | ||
LL | a: &'r#fn str, | ||
| ^^^^^ undeclared lifetime | ||
| | ||
help: consider introducing lifetime `'r#fn` here | ||
| | ||
LL | struct Test<'r#fn> { | ||
| +++++++ | ||
|
||
error[E0261]: use of undeclared lifetime name `'r#fn` | ||
--> $DIR/error-lifetime-name-issue-143150.rs:16:32 | ||
| | ||
LL | where T: for<'a> Trait1<T> + 'r#fn { } | ||
| ^^^^^ undeclared lifetime | ||
| | ||
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html | ||
help: consider making the bound lifetime-generic with a new `'r#fn` lifetime | ||
| | ||
LL - where T: for<'a> Trait1<T> + 'r#fn { } | ||
LL + where for<'r#fn, 'a> T: Trait1<T> + 'r#fn { } | ||
| | ||
help: consider introducing lifetime `'r#fn` here | ||
| | ||
LL | trait Trait1<'r#fn, T> | ||
| ++++++ | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0261`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The label needs to be converted to
String
, otherwise the error message will callto_ident_string
, which will print'break
as'r#break
in the following example.I haven't checked to see if there are any other label-related errors yet though, there may be similar issues, but it's generally a more borderline case.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Oh interesting.
'break
is actually an invalid label but'r#break
is only a valid label in Rust >=2021) (ignore, not relevant)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you should be able to use
Symbol
instead ofString
here (doesn't make much of a difference here tho) (we could actually suggest escaping viar#
(if we have Rust >= 2021) here but that's for another PR)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found the implementation of
Symbol
as diagnostic arguments.rust/compiler/rustc_errors/src/diagnostic_impls.rs
Lines 165 to 169 in f191420
It will be transformed to
Ident
, so if I useSymbol
here, It printsinvalid label name 'r#break
. I may have to useString
.