Skip to content

Commit bb89b62

Browse files
committed
Couple more tests
1 parent 5c945e9 commit bb89b62

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

src/tools/rust-analyzer/crates/proc-macro-srv/src/tests/mod.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,44 @@ use expect_test::expect;
1010
fn test_derive_empty() {
1111
assert_expand(
1212
"DeriveEmpty",
13-
r#"struct S;"#,
13+
r#"struct S { field: &'r#lt fn(u32) -> &'a r#u32 }"#,
1414
expect![[r#"
1515
IDENT 1 struct
1616
IDENT 1 S
17-
PUNCT 1 ; [alone]
17+
GROUP {} 1 1 1
18+
IDENT 1 field
19+
PUNCT 1 : [alone]
20+
PUNCT 1 & [joint]
21+
PUNCT 1 ' [joint]
22+
IDENT 1 r#lt
23+
IDENT 1 fn
24+
GROUP () 1 1 1
25+
IDENT 1 u32
26+
PUNCT 1 - [joint]
27+
PUNCT 1 > [alone]
28+
PUNCT 1 & [joint]
29+
PUNCT 1 ' [joint]
30+
IDENT 1 a
31+
IDENT 1 r#u32
1832
"#]],
1933
expect![[r#"
2034
IDENT 42:Root[0000, 0]@0..6#ROOT2024 struct
2135
IDENT 42:Root[0000, 0]@7..8#ROOT2024 S
22-
PUNCT 42:Root[0000, 0]@8..9#ROOT2024 ; [alone]
36+
GROUP {} 42:Root[0000, 0]@9..10#ROOT2024 42:Root[0000, 0]@46..47#ROOT2024 42:Root[0000, 0]@9..47#ROOT2024
37+
IDENT 42:Root[0000, 0]@11..16#ROOT2024 field
38+
PUNCT 42:Root[0000, 0]@16..17#ROOT2024 : [alone]
39+
PUNCT 42:Root[0000, 0]@18..19#ROOT2024 & [joint]
40+
PUNCT 42:Root[0000, 0]@22..23#ROOT2024 ' [joint]
41+
IDENT 42:Root[0000, 0]@22..24#ROOT2024 r#lt
42+
IDENT 42:Root[0000, 0]@25..27#ROOT2024 fn
43+
GROUP () 42:Root[0000, 0]@27..28#ROOT2024 42:Root[0000, 0]@31..32#ROOT2024 42:Root[0000, 0]@27..32#ROOT2024
44+
IDENT 42:Root[0000, 0]@28..31#ROOT2024 u32
45+
PUNCT 42:Root[0000, 0]@33..34#ROOT2024 - [joint]
46+
PUNCT 42:Root[0000, 0]@34..35#ROOT2024 > [alone]
47+
PUNCT 42:Root[0000, 0]@36..37#ROOT2024 & [joint]
48+
PUNCT 42:Root[0000, 0]@38..39#ROOT2024 ' [joint]
49+
IDENT 42:Root[0000, 0]@38..39#ROOT2024 a
50+
IDENT 42:Root[0000, 0]@42..45#ROOT2024 r#u32
2351
"#]],
2452
);
2553
}
@@ -466,6 +494,30 @@ fn test_attr_macro() {
466494
);
467495
}
468496

497+
#[test]
498+
#[should_panic = "called `Result::unwrap()` on an `Err` value: \"Mismatched token groups\""]
499+
fn test_broken_input_unclosed_delim() {
500+
assert_expand("fn_like_clone_tokens", r###"{"###, expect![[]], expect![[]]);
501+
}
502+
503+
#[test]
504+
#[should_panic = "called `Result::unwrap()` on an `Err` value: \"Unexpected '}'\""]
505+
fn test_broken_input_unopened_delim() {
506+
assert_expand("fn_like_clone_tokens", r###"}"###, expect![[]], expect![[]]);
507+
}
508+
509+
#[test]
510+
#[should_panic = "called `Result::unwrap()` on an `Err` value: \"Expected '}'\""]
511+
fn test_broken_input_mismatched_delim() {
512+
assert_expand("fn_like_clone_tokens", r###"(}"###, expect![[]], expect![[]]);
513+
}
514+
515+
#[test]
516+
#[should_panic = "called `Result::unwrap()` on an `Err` value: \"Invalid identifier: `🪟`\""]
517+
fn test_broken_input_unknowm_token() {
518+
assert_expand("fn_like_clone_tokens", r###"🪟"###, expect![[]], expect![[]]);
519+
}
520+
469521
/// Tests that we find and classify all proc macros correctly.
470522
#[test]
471523
fn list_test_macros() {

src/tools/rust-analyzer/crates/proc-macro-srv/src/token_stream.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ impl<S> TokenStream<S> {
9898
groups.push((proc_macro::Delimiter::Parenthesis, range, vec![]))
9999
}
100100
rustc_lexer::TokenKind::CloseParen if *open_delim != Delimiter::Parenthesis => {
101-
return Err("Expected ')'".to_owned());
101+
return if *open_delim == Delimiter::None {
102+
Err("Unexpected ')'".to_owned())
103+
} else {
104+
Err("Expected ')'".to_owned())
105+
};
102106
}
103107
rustc_lexer::TokenKind::CloseParen => {
104108
let (delimiter, open_range, stream) = groups.pop().unwrap();
@@ -122,7 +126,11 @@ impl<S> TokenStream<S> {
122126
groups.push((proc_macro::Delimiter::Brace, range, vec![]))
123127
}
124128
rustc_lexer::TokenKind::CloseBrace if *open_delim != Delimiter::Brace => {
125-
return Err("Expected '}'".to_owned());
129+
return if *open_delim == Delimiter::None {
130+
Err("Unexpected '}'".to_owned())
131+
} else {
132+
Err("Expected '}'".to_owned())
133+
};
126134
}
127135
rustc_lexer::TokenKind::CloseBrace => {
128136
let (delimiter, open_range, stream) = groups.pop().unwrap();
@@ -146,7 +154,11 @@ impl<S> TokenStream<S> {
146154
groups.push((proc_macro::Delimiter::Bracket, range, vec![]))
147155
}
148156
rustc_lexer::TokenKind::CloseBracket if *open_delim != Delimiter::Bracket => {
149-
return Err("Expected ']'".to_owned());
157+
return if *open_delim == Delimiter::None {
158+
Err("Unexpected ']'".to_owned())
159+
} else {
160+
Err("Expected ']'".to_owned())
161+
};
150162
}
151163
rustc_lexer::TokenKind::CloseBracket => {
152164
let (delimiter, open_range, stream) = groups.pop().unwrap();
@@ -223,10 +235,14 @@ impl<S> TokenStream<S> {
223235
}
224236
rustc_lexer::TokenKind::Whitespace => continue,
225237
rustc_lexer::TokenKind::Frontmatter { .. } => unreachable!(),
226-
rustc_lexer::TokenKind::Unknown => return Err("Unknown token".to_owned()),
227-
rustc_lexer::TokenKind::UnknownPrefix => return Err("Unknown prefix".to_owned()),
238+
rustc_lexer::TokenKind::Unknown => {
239+
return Err(format!("Unknown token: `{}`", &s[range]));
240+
}
241+
rustc_lexer::TokenKind::UnknownPrefix => {
242+
return Err(format!("Unknown prefix: `{}`", &s[range]));
243+
}
228244
rustc_lexer::TokenKind::UnknownPrefixLifetime => {
229-
return Err("Unknown lifetime prefix".to_owned());
245+
return Err(format!("Unknown lifetime prefix: `{}`", &s[range]));
230246
}
231247
// FIXME: Error on edition >= 2024 ... I dont think the proc-macro server can fetch editions currently
232248
// and whose edition is this?
@@ -247,7 +263,9 @@ impl<S> TokenStream<S> {
247263
is_raw: false,
248264
span: span.derive_ranged(range),
249265
})),
250-
rustc_lexer::TokenKind::InvalidIdent => return Err("Invalid identifier".to_owned()),
266+
rustc_lexer::TokenKind::InvalidIdent => {
267+
return Err(format!("Invalid identifier: `{}`", &s[range]));
268+
}
251269
rustc_lexer::TokenKind::RawIdent => {
252270
let range = range.start + 2..range.end;
253271
tokenstream.push(TokenTree::Ident(Ident {

src/tools/rust-analyzer/xtask/src/tidy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ fn check_test_attrs(path: &Path, text: &str) {
194194
"test-utils/src/fixture.rs",
195195
// Generated code from lints contains doc tests in string literals.
196196
"ide-db/src/generated/lints.rs",
197+
"proc-macro-srv/src/tests/mod.rs",
197198
];
198199
if need_panic.iter().any(|p| path.ends_with(p)) {
199200
return;

0 commit comments

Comments
 (0)