-
Notifications
You must be signed in to change notification settings - Fork 13.7k
print raw lifetime idents with r# #144897
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
base: master
Are you sure you want to change the base?
print raw lifetime idents with r# #144897
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This PR modifies |
compiler/rustc_span/src/symbol.rs
Outdated
if let Some(ident) = self.as_raw_lifetime_name_without_apostrophe() { | ||
write!(f, "'{}", IdentPrinter::new(ident.name, true, None)) | ||
} else { | ||
fmt::Display::fmt(&IdentPrinter::new(self.name, self.is_raw_guess(), None), f) | ||
} |
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.
Why is this not a change in IdentPrinter
instead?
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.
How do you propose us to make the check in IdentPrinter
? It doesn't know about editions because it only accepts the symbol. is_raw_guess
and as_raw_lifetime_name_without_apostrophe
both need the span edition to know whether some identifier is treated as a keyword or not.
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 suppose it could accept edition as a parameter? otherwise I'm not sure what's even the point of it
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.
otherwise I'm not sure what's even the point of it
Just looked at the code again. IdentPrinter is used from two general places. One where one has the token data (sorta at the parsing/ast/proc macro stage) in which rawness of idents are known and do not need to be guessed, and one at a later stage where they do. that's why rawness was originally passed as a bool. To improve the design as you highlighted I chose to change the original bool into an enum, but we still have to guess whether a raw lifetime should be printed here and not inside IdentPrinter.
This comment has been minimized.
This comment has been minimized.
5805f0a
to
9705706
Compare
9705706
to
b74a97e
Compare
This comment was marked as resolved.
This comment was marked as resolved.
b74a97e
to
4d45896
Compare
This comment was marked as resolved.
This comment was marked as resolved.
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.
Thanks! r=me with nits addressed in one way or another
@@ -3090,7 +3090,13 @@ impl<'a> Parser<'a> { | |||
if let Some((ident, is_raw)) = self.token.lifetime() { | |||
// Disallow `'fn`, but with a better error message than `expect_lifetime`. | |||
if matches!(is_raw, IdentIsRaw::No) && ident.without_first_quote().is_reserved() { | |||
self.dcx().emit_err(errors::InvalidLabel { span: ident.span, name: ident.name }); | |||
self.dcx().emit_err(errors::InvalidLabel { |
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.
(alternatively, we could just mimic the diagnostic emitted by expect_lifetime
("lifetimes cannot use keyword names") and just say "labels cannot use keyword names"
.as_str() | ||
.strip_prefix("'") | ||
.expect("only lifetime idents should be passed with RawLifetime mode"); | ||
Symbol::intern(s) |
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.
Edit: Disregard, without_first_quote
is already interning (in is_raw_lifetime_guess
), so it's already interned by this point (I'm not happy about without_first_quote
re-interning, seems wasteful in the happy path).
I would just return s.fmt(f);
instead of (needlessly) interning the "stripped" lifetime. I don't know if we use IdentPrinter
in the happy path at all. If we did this would be a minor perf/mem concern.
compiler/rustc_span/src/symbol.rs
Outdated
// this should be kept consistent with `Parser::expect_lifetime` found under | ||
// compiler/rustc_parse/src/parser/ty.rs | ||
let name_without_apostrophe = self.without_first_quote(); | ||
name_without_apostrophe.name != self.name | ||
&& ![kw::UnderscoreLifetime, kw::StaticLifetime].contains(&self.name) | ||
&& name_without_apostrophe.is_raw_guess() |
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.
Since rustc_span
actually defines Ident
and is_reserved
, you could reasonably also define a canonical "is_reserved_lifetime
" which you can then reuse.
4d45896
to
f3ff3d3
Compare
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
f3ff3d3
to
4970127
Compare
@bors r=fmease |
This replaces #143185 and fixes #143150
cc @fmease