Skip to content

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
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Jun 29, 2025

Fixes #143150

r? compiler

@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 Jun 29, 2025
@fmease fmease self-assigned this Jun 29, 2025
@rust-log-analyzer

This comment has been minimized.

@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 Jun 29, 2025
@xizheyin xizheyin force-pushed the 143150 branch 2 times, most recently from c9addca to c4edd71 Compare June 30, 2025 03:40
Copy link
Contributor Author

@xizheyin xizheyin left a comment

Choose a reason for hiding this comment

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

@rustbot ready

}
return fmt::Display::fmt(&lifetime, f);
}

if self.is_raw {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When Ident starts with ', the contents are printed recursively.

Comment on lines +2880 to +2889
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()
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are two keywords that need to be excluded here.

@@ -2176,7 +2176,7 @@ pub(crate) struct KeywordLifetime {
pub(crate) struct InvalidLabel {
#[primary_span]
pub span: Span,
pub name: Symbol,
pub name: String,
Copy link
Contributor Author

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 call to_ident_string, which will print 'break as 'r#break in the following example.

fn main() {
    'break: loop { //~ ERROR invalid label name `'break`
    }
}

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.

Copy link
Member

@fmease fmease Jun 30, 2025

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)

Copy link
Member

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 of String here (doesn't make much of a difference here tho) (we could actually suggest escaping via r# (if we have Rust >= 2021) here but that's for another PR)

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 found the implementation of Symbol as diagnostic arguments.

impl IntoDiagArg for Symbol {
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
self.to_ident_string().into_diag_arg(path)
}
}

It will be transformed to Ident, so if I use Symbol here, It prints invalid label name 'r#break. I may have to use String.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 30, 2025
@rust-log-analyzer

This comment has been minimized.

Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
@xizheyin xizheyin changed the title Use code snippet to get lifetime literal name instead of lifetime.ident Add lifetime-aware support for Display impl of Ident Jun 30, 2025
@xizheyin xizheyin force-pushed the 143150 branch 2 times, most recently from d6c180d to 9306466 Compare June 30, 2025 09:12
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
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.

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