Skip to content

Commit 2cefdb1

Browse files
committed
Allow raw path-segment kw but not suggest escaping them
1 parent c8bc460 commit 2cefdb1

File tree

5 files changed

+13
-25
lines changed

5 files changed

+13
-25
lines changed

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
435435

436436
let path = if self.parser.parse_cfg_pred {
437437
self.parser.parse_cfg_pred = false;
438-
Path::from_ident(self.parser.parse_ident()?)
438+
self.parser.parse_path(PathStyle::SingleIdent)?
439439
} else {
440440
self.parser.parse_path(PathStyle::Mod)?
441441
};

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast as ast;
22
use rustc_ast::token::{self, MetaVarKind};
33
use rustc_ast::tokenstream::ParserRange;
4-
use rustc_ast::{Attribute, Path, attr};
4+
use rustc_ast::{Attribute, attr};
55
use rustc_errors::codes::*;
66
use rustc_errors::{Diag, PResult};
77
use rustc_span::{BytePos, Span};
@@ -452,7 +452,7 @@ impl<'a> Parser<'a> {
452452

453453
let path = if self.parse_cfg_pred {
454454
self.parse_cfg_pred = false;
455-
Path::from_ident(self.parse_ident()?)
455+
self.parse_path(PathStyle::SingleIdent)?
456456
} else {
457457
self.parse_path(PathStyle::Mod)?
458458
};

compiler/rustc_parse/src/parser/path.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub enum PathStyle {
5757
/// anyway, due to macros), but it is used to avoid weird suggestions about expected
5858
/// tokens when something goes wrong.
5959
Mod,
60+
/// Disallow path segment keywords when parsing cfg preds
61+
SingleIdent,
6062
}
6163

6264
impl PathStyle {
@@ -299,7 +301,12 @@ impl<'a> Parser<'a> {
299301
style: PathStyle,
300302
ty_generics: Option<&Generics>,
301303
) -> PResult<'a, PathSegment> {
302-
let ident = self.parse_path_segment_ident()?;
304+
let ident = if style == PathStyle::SingleIdent {
305+
self.parse_ident()?
306+
} else {
307+
self.parse_path_segment_ident()?
308+
};
309+
303310
let is_args_start = |token: &Token| {
304311
matches!(token.kind, token::Lt | token::Shl | token::OpenParen | token::LArrow)
305312
};

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3082,7 +3082,7 @@ impl Ident {
30823082
/// We see this identifier in a normal identifier position, like variable name or a type.
30833083
/// How was it written originally? Did it use the raw form? Let's try to guess.
30843084
pub fn is_raw_guess(self) -> bool {
3085-
self.name.can_be_raw() && self.is_reserved()
3085+
self.name.can_be_raw() && !self.is_path_segment_keyword() && self.is_reserved()
30863086
}
30873087

30883088
/// Given the name of a lifetime without the first quote (`'`),
@@ -3095,6 +3095,7 @@ impl Ident {
30953095
let name_without_apostrophe = self.without_first_quote();
30963096
name_without_apostrophe.name != self.name
30973097
&& name_without_apostrophe.name.can_be_raw()
3098+
&& !name_without_apostrophe.name.is_path_segment_keyword()
30983099
&& name_without_apostrophe.is_reserved_lifetime()
30993100
}
31003101

tests/ui/cfg/path-kw-as-pred.stderr

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,24 @@ error: expected identifier, found keyword `crate`
2727
|
2828
LL | #[cfg_attr(crate, path = "foo")]
2929
| ^^^^^ expected identifier, found keyword
30-
|
31-
help: escape `crate` to use it as an identifier
32-
|
33-
LL | #[cfg_attr(r#crate, path = "foo")]
34-
| ++
3530

3631
error: expected identifier, found keyword `super`
3732
--> $DIR/path-kw-as-pred.rs:28:12
3833
|
3934
LL | #[cfg_attr(super, path = "foo")]
4035
| ^^^^^ expected identifier, found keyword
41-
|
42-
help: escape `super` to use it as an identifier
43-
|
44-
LL | #[cfg_attr(r#super, path = "foo")]
45-
| ++
4636

4737
error: expected identifier, found keyword `self`
4838
--> $DIR/path-kw-as-pred.rs:30:12
4939
|
5040
LL | #[cfg_attr(self, path = "foo")]
5141
| ^^^^ expected identifier, found keyword
52-
|
53-
help: escape `self` to use it as an identifier
54-
|
55-
LL | #[cfg_attr(r#self, path = "foo")]
56-
| ++
5742

5843
error: expected identifier, found keyword `Self`
5944
--> $DIR/path-kw-as-pred.rs:32:12
6045
|
6146
LL | #[cfg_attr(Self, path = "foo")]
6247
| ^^^^ expected identifier, found keyword
63-
|
64-
help: escape `Self` to use it as an identifier
65-
|
66-
LL | #[cfg_attr(r#Self, path = "foo")]
67-
| ++
6848

6949
error: expected identifier, found keyword `crate`
7050
--> $DIR/path-kw-as-pred.rs:34:22

0 commit comments

Comments
 (0)