diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-21 00:10:14 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-21 00:10:14 +0000 |
commit | 787bd3c5516d250245f6070308d689311b638fbe (patch) | |
tree | 3513cb44470b7ff148258bc300402be778c7f097 /crates/mbe/src/tests | |
parent | 090e013161ab5b1679554ddd53683e81e3fe845a (diff) | |
parent | 0a7f28620a7002f47890c2030862052bcbf25cdb (diff) |
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 https://github.com/rust-lang/rust/pull/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]>
Diffstat (limited to 'crates/mbe/src/tests')
-rw-r--r-- | crates/mbe/src/tests/expand.rs | 6 | ||||
-rw-r--r-- | crates/mbe/src/tests/rule.rs | 4 |
2 files changed, 10 insertions, 0 deletions
diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 9dd8ff75b..2cce62781 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs | |||
@@ -1080,6 +1080,12 @@ macro_rules! q { | |||
1080 | } | 1080 | } |
1081 | 1081 | ||
1082 | #[test] | 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 | |||
1088 | #[test] | ||
1083 | fn test_vertical_bar_with_pat() { | 1089 | fn test_vertical_bar_with_pat() { |
1084 | parse_macro( | 1090 | parse_macro( |
1085 | r#" | 1091 | r#" |
diff --git a/crates/mbe/src/tests/rule.rs b/crates/mbe/src/tests/rule.rs index 07277966d..bf48112b3 100644 --- a/crates/mbe/src/tests/rule.rs +++ b/crates/mbe/src/tests/rule.rs | |||
@@ -12,6 +12,9 @@ fn test_valid_arms() { | |||
12 | } | 12 | } |
13 | 13 | ||
14 | check("($i:ident) => ()"); | 14 | check("($i:ident) => ()"); |
15 | check("($(x),*) => ()"); | ||
16 | check("($(x)_*) => ()"); | ||
17 | check("($(x)i*) => ()"); | ||
15 | check("($($i:ident)*) => ($_)"); | 18 | check("($($i:ident)*) => ($_)"); |
16 | check("($($true:ident)*) => ($true)"); | 19 | check("($($true:ident)*) => ($true)"); |
17 | check("($($false:ident)*) => ($false)"); | 20 | check("($($false:ident)*) => ($false)"); |
@@ -32,6 +35,7 @@ fn test_invalid_arms() { | |||
32 | 35 | ||
33 | check("($i) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into())); | 36 | check("($i) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into())); |
34 | check("($i:) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into())); | 37 | check("($i:) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into())); |
38 | check("($i:_) => ()", ParseError::UnexpectedToken("bad fragment specifier 1".into())); | ||
35 | } | 39 | } |
36 | 40 | ||
37 | fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError> { | 41 | fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError> { |