Skip to content

Commit 4970127

Browse files
committed
address review comments
1 parent 30bb704 commit 4970127

15 files changed

+30
-35
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,6 @@ parse_invalid_dyn_keyword = invalid `dyn` keyword
468468
parse_invalid_expression_in_let_else = a `{$operator}` expression cannot be directly assigned in `let...else`
469469
parse_invalid_identifier_with_leading_number = identifiers cannot start with a number
470470
471-
parse_invalid_label =
472-
invalid label name `{$name}`
473-
474471
parse_invalid_literal_suffix_on_tuple_index = suffixes on a tuple index are invalid
475472
.label = invalid suffix `{$suffix}`
476473
.tuple_exception_line_1 = `{$suffix}` is *temporarily* accepted on tuple index fields as it was incorrectly accepted on stable for a few releases
@@ -500,6 +497,8 @@ parse_invalid_unicode_escape = invalid unicode character escape
500497
parse_invalid_variable_declaration =
501498
invalid variable declaration
502499
500+
parse_keyword_label = labels cannot use keyword names
501+
503502
parse_keyword_lifetime =
504503
lifetimes cannot use keyword names
505504

compiler/rustc_parse/src/errors.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,11 +2228,10 @@ pub(crate) struct KeywordLifetime {
22282228
}
22292229

22302230
#[derive(Diagnostic)]
2231-
#[diag(parse_invalid_label)]
2232-
pub(crate) struct InvalidLabel {
2231+
#[diag(parse_keyword_label)]
2232+
pub(crate) struct KeywordLabel {
22332233
#[primary_span]
22342234
pub span: Span,
2235-
pub name: String,
22362235
}
22372236

22382237
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,13 +3094,7 @@ impl<'a> Parser<'a> {
30943094
if let Some((ident, is_raw)) = self.token.lifetime() {
30953095
// Disallow `'fn`, but with a better error message than `expect_lifetime`.
30963096
if matches!(is_raw, IdentIsRaw::No) && ident.without_first_quote().is_reserved() {
3097-
self.dcx().emit_err(errors::InvalidLabel {
3098-
span: ident.span,
3099-
// `IntoDiagArg` prints the symbol as if it was an ident,
3100-
// so `'break` is printed as `r#break`. We don't want that
3101-
// here so convert to string eagerly.
3102-
name: ident.without_first_quote().name.to_string(),
3103-
});
3097+
self.dcx().emit_err(errors::KeywordLabel { span: ident.span });
31043098
}
31053099

31063100
self.bump();

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,8 +1479,7 @@ impl<'a> Parser<'a> {
14791479
pub(super) fn expect_lifetime(&mut self) -> Lifetime {
14801480
if let Some((ident, is_raw)) = self.token.lifetime() {
14811481
if matches!(is_raw, IdentIsRaw::No)
1482-
&& ident.without_first_quote().is_reserved()
1483-
&& ![kw::UnderscoreLifetime, kw::StaticLifetime].contains(&ident.name)
1482+
&& ident.without_first_quote().is_reserved_lifetime()
14841483
{
14851484
self.dcx().emit_err(errors::KeywordLifetime { span: ident.span });
14861485
}

compiler/rustc_span/src/symbol.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,13 +3045,17 @@ impl Ident {
30453045
self.name.can_be_raw() && self.is_reserved()
30463046
}
30473047

3048+
/// Given the name of a lifetime without the first quote (`'`),
3049+
/// returns whether the lifetime name is reserved (therefore invalid)
3050+
pub fn is_reserved_lifetime(self) -> bool {
3051+
self.is_reserved() && ![kw::Underscore, kw::Static].contains(&self.name)
3052+
}
3053+
30483054
pub fn is_raw_lifetime_guess(self) -> bool {
3049-
// this should be kept consistent with `Parser::expect_lifetime` found under
3050-
// compiler/rustc_parse/src/parser/ty.rs
30513055
let name_without_apostrophe = self.without_first_quote();
30523056
name_without_apostrophe.name != self.name
3053-
&& ![kw::UnderscoreLifetime, kw::StaticLifetime].contains(&self.name)
3054-
&& name_without_apostrophe.is_raw_guess()
3057+
&& name_without_apostrophe.name.can_be_raw()
3058+
&& name_without_apostrophe.is_reserved_lifetime()
30553059
}
30563060

30573061
pub fn guess_print_mode(self) -> IdentPrintMode {

tests/ui/closures/issue-52437.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
[(); &(&'static: loop { |x| {}; }) as *const _ as usize]
3-
//~^ ERROR: invalid label name `static`
3+
//~^ ERROR: labels cannot use keyword names
44
//~| ERROR: type annotations needed
55
}

tests/ui/closures/issue-52437.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: invalid label name `static`
1+
error: labels cannot use keyword names
22
--> $DIR/issue-52437.rs:2:13
33
|
44
LL | [(); &(&'static: loop { |x| {}; }) as *const _ as usize]

tests/ui/issues/issue-46311.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
2-
'break: loop { //~ ERROR invalid label name `break`
2+
'break: loop { //~ ERROR labels cannot use keyword names
33
}
44
}

tests/ui/issues/issue-46311.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: invalid label name `break`
1+
error: labels cannot use keyword names
22
--> $DIR/issue-46311.rs:2:5
33
|
44
LL | 'break: loop {

tests/ui/label/label-static.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
2-
'static: loop { //~ ERROR invalid label name `static`
3-
break 'static //~ ERROR invalid label name `static`
2+
'static: loop { //~ ERROR labels cannot use keyword names
3+
break 'static //~ ERROR labels cannot use keyword names
44
}
55
}

0 commit comments

Comments
 (0)