Skip to content

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

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

Conversation

fee1-dead
Copy link
Member

@fee1-dead fee1-dead commented Aug 4, 2025

This replaces #143185 and fixes #143150

cc @fmease

@rustbot

This comment was marked as outdated.

@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 Aug 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 4, 2025

This PR modifies tests/ui/issues/. If this PR is adding new tests to tests/ui/issues/,
please refrain from doing so, and instead add it to more descriptive subdirectories.

Comment on lines 2508 to 2512
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)
}
Copy link
Member

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?

Copy link
Member Author

@fee1-dead fee1-dead Aug 4, 2025

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.

Copy link
Member

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

Copy link
Member Author

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.

@rust-log-analyzer

This comment has been minimized.

@fee1-dead fee1-dead force-pushed the raw_lifetimes_printing branch 2 times, most recently from 5805f0a to 9705706 Compare August 4, 2025 13:35
@fee1-dead fee1-dead force-pushed the raw_lifetimes_printing branch from 9705706 to b74a97e Compare August 4, 2025 14:07
@bors

This comment was marked as resolved.

@fee1-dead fee1-dead force-pushed the raw_lifetimes_printing branch from b74a97e to 4d45896 Compare August 16, 2025 14:32
@rustbot

This comment was marked as resolved.

@fmease fmease assigned fmease and unassigned SparrowLii Aug 16, 2025
Copy link
Member

@fmease fmease left a 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

View changes since this review

@@ -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 {
Copy link
Member

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)
Copy link
Member

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.

Comment on lines 3038 to 3043
// 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()
Copy link
Member

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.

@fmease fmease 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 Aug 21, 2025
@fee1-dead fee1-dead force-pushed the raw_lifetimes_printing branch from 4d45896 to f3ff3d3 Compare August 22, 2025 05:07
@rustbot
Copy link
Collaborator

rustbot commented Aug 22, 2025

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.

@rust-log-analyzer

This comment has been minimized.

@fee1-dead fee1-dead force-pushed the raw_lifetimes_printing branch from f3ff3d3 to 4970127 Compare August 22, 2025 05:16
@fee1-dead
Copy link
Member Author

@bors r=fmease

@bors
Copy link
Collaborator

bors commented Aug 22, 2025

📌 Commit 4970127 has been approved by fmease

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

undeclared raw lifetime; "consider introducing non-raw lifetime"
7 participants