Skip to content

Commit 787bd3c

Browse files
Merge #8122
8122: Make bare underscore token an Ident rather than Punct in proc-macro r=edwin0cheng a=kevinmehall In rustc and proc-macro2, a bare `_` token is parsed for procedural macro purposes as `Ident` rather than `Punct` (see rust-lang/rust#48842). This changes rust-analyzer to match rustc's behavior and implementation by handling `_` as an Ident in token trees, but explicitly preventing `$x:ident` from matching it in MBE. proc macro crate: ```rust #[proc_macro] pub fn input(input: proc_macro::TokenStream) -> proc_macro::TokenStream { dbg!(input) } ``` test crate: ```rust test_proc_macro::input!(_); ``` output (rustc): ```rust [test-proc-macro/src/lib.rs:10] input = TokenStream [ Ident { ident: "_", span: #0 bytes(173..174), }, ] ``` output (rust-analyzer before this change): ```rust [test-proc-macro/src/lib.rs:10] input = TokenStream [ Punct { ch: '_', spacing: Joint, span: 4294967295, }, ] ``` output (rust-analyzer after this change): ```rust [test-proc-macro/src/lib.rs:10] input = TokenStream [ Ident { ident: "_", span: 4294967295, }, ] ``` Co-authored-by: Kevin Mehall <[email protected]>
2 parents 090e013 + 0a7f286 commit 787bd3c

File tree

8 files changed

+36
-14
lines changed

8 files changed

+36
-14
lines changed

crates/mbe/src/expander/matcher.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ impl<'a> TtIter<'a> {
762762
fn expect_separator(&mut self, separator: &Separator, idx: usize) -> bool {
763763
let mut fork = self.clone();
764764
let ok = match separator {
765-
Separator::Ident(lhs) if idx == 0 => match fork.expect_ident() {
765+
Separator::Ident(lhs) if idx == 0 => match fork.expect_ident_or_underscore() {
766766
Ok(rhs) => rhs.text == lhs.text,
767767
_ => false,
768768
},
@@ -852,7 +852,7 @@ impl<'a> TtIter<'a> {
852852
if punct.char != '\'' {
853853
return Err(());
854854
}
855-
let ident = self.expect_ident()?;
855+
let ident = self.expect_ident_or_underscore()?;
856856

857857
Ok(tt::Subtree {
858858
delimiter: None,

crates/mbe/src/parser.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,8 @@ fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Resul
177177
Op::Repeat { tokens: MetaTemplate(tokens), separator, kind }
178178
}
179179
tt::TokenTree::Leaf(leaf) => match leaf {
180-
tt::Leaf::Punct(punct) => {
181-
static UNDERSCORE: SmolStr = SmolStr::new_inline("_");
182-
183-
if punct.char != '_' {
184-
return Err(ParseError::Expected("_".to_string()));
185-
}
186-
let name = UNDERSCORE.clone();
187-
let kind = eat_fragment_kind(src, mode)?;
188-
let id = punct.id;
189-
Op::Var { name, kind, id }
180+
tt::Leaf::Punct(_) => {
181+
return Err(ParseError::Expected("ident".to_string()));
190182
}
191183
tt::Leaf::Ident(ident) if ident.text == "crate" => {
192184
// We simply produce identifier `$crate` here. And it will be resolved when lowering ast to Path.

crates/mbe/src/subtree_source.rs

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ fn convert_ident(ident: &tt::Ident) -> TtToken {
150150
let kind = match ident.text.as_ref() {
151151
"true" => T![true],
152152
"false" => T![false],
153+
"_" => UNDERSCORE,
153154
i if i.starts_with('\'') => LIFETIME_IDENT,
154155
_ => SyntaxKind::from_keyword(ident.text.as_str()).unwrap_or(IDENT),
155156
};

crates/mbe/src/syntax_bridge.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ trait TokenConvertor {
350350
return;
351351
}
352352

353-
result.push(if k.is_punct() {
353+
result.push(if k.is_punct() && k != UNDERSCORE {
354354
assert_eq!(range.len(), TextSize::of('.'));
355355
let delim = match k {
356356
T!['('] => Some((tt::DelimiterKind::Parenthesis, T![')'])),
@@ -395,7 +395,9 @@ trait TokenConvertor {
395395
{
396396
tt::Spacing::Alone
397397
}
398-
Some(next) if next.kind().is_punct() => tt::Spacing::Joint,
398+
Some(next) if next.kind().is_punct() && next.kind() != UNDERSCORE => {
399+
tt::Spacing::Joint
400+
}
399401
_ => tt::Spacing::Alone,
400402
};
401403
let char = match token.to_char() {
@@ -415,6 +417,7 @@ trait TokenConvertor {
415417
let leaf: tt::Leaf = match k {
416418
T![true] | T![false] => make_leaf!(Ident),
417419
IDENT => make_leaf!(Ident),
420+
UNDERSCORE => make_leaf!(Ident),
418421
k if k.is_keyword() => make_leaf!(Ident),
419422
k if k.is_literal() => make_leaf!(Literal),
420423
LIFETIME_IDENT => {

crates/mbe/src/tests/expand.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,12 @@ macro_rules! q {
10791079
.assert_expand_items(r#"q![_]"#, r#"0"#);
10801080
}
10811081

1082+
#[test]
1083+
fn test_underscore_lifetime() {
1084+
parse_macro(r#"macro_rules! q { ($a:lifetime) => {0}; }"#)
1085+
.assert_expand_items(r#"q!['_]"#, r#"0"#);
1086+
}
1087+
10821088
#[test]
10831089
fn test_vertical_bar_with_pat() {
10841090
parse_macro(

crates/mbe/src/tests/rule.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ fn test_valid_arms() {
1212
}
1313

1414
check("($i:ident) => ()");
15+
check("($(x),*) => ()");
16+
check("($(x)_*) => ()");
17+
check("($(x)i*) => ()");
1518
check("($($i:ident)*) => ($_)");
1619
check("($($true:ident)*) => ($true)");
1720
check("($($false:ident)*) => ($false)");
@@ -32,6 +35,7 @@ fn test_invalid_arms() {
3235

3336
check("($i) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
3437
check("($i:) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
38+
check("($i:_) => ()", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
3539
}
3640

3741
fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError> {

crates/mbe/src/tt_iter.rs

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ impl<'a> TtIter<'a> {
4949
}
5050

5151
pub(crate) fn expect_ident(&mut self) -> Result<&'a tt::Ident, ()> {
52+
match self.expect_leaf()? {
53+
tt::Leaf::Ident(it) if it.text != "_" => Ok(it),
54+
_ => Err(()),
55+
}
56+
}
57+
58+
pub(crate) fn expect_ident_or_underscore(&mut self) -> Result<&'a tt::Ident, ()> {
5259
match self.expect_leaf()? {
5360
tt::Leaf::Ident(it) => Ok(it),
5461
_ => Err(()),

crates/proc_macro_srv/src/rustc_server.rs

+9
Original file line numberDiff line numberDiff line change
@@ -805,5 +805,14 @@ mod tests {
805805
let t2 = TokenStream::from_str("(a);").unwrap();
806806
assert_eq!(t2.token_trees.len(), 2);
807807
assert_eq!(t2.token_trees[0], subtree_paren_a);
808+
809+
let underscore = TokenStream::from_str("_").unwrap();
810+
assert_eq!(
811+
underscore.token_trees[0],
812+
tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident {
813+
text: "_".into(),
814+
id: tt::TokenId::unspecified(),
815+
}))
816+
);
808817
}
809818
}

0 commit comments

Comments
 (0)