Skip to content

Commit 30004a8

Browse files
committed
improve nonfatal-parsing test
1 parent b292ff8 commit 30004a8

3 files changed

Lines changed: 102 additions & 49 deletions

File tree

tests/ui/proc-macro/auxiliary/nonfatal-parsing-body.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,38 @@ enum Mode {
1515
OtherWithPanic,
1616
}
1717

18+
fn print_unspanned<T>(s: &str) -> Result<T, LexError>
19+
where
20+
T: FromStr<Err = LexError> + Debug,
21+
{
22+
let t = T::from_str(s);
23+
let mut s = format!("{t:?}");
24+
while let Some((l, r)) = s.split_once("span: #") {
25+
let (_, r) = r.split_once(")").unwrap();
26+
s = format!("{l}span: Span{r}");
27+
}
28+
println!("{s}");
29+
t
30+
}
31+
1832
fn parse<T>(s: &str, mode: Mode)
1933
where
2034
T: FromStr<Err = LexError> + Debug,
2135
{
2236
match mode {
2337
NormalOk => {
24-
let t = T::from_str(s);
25-
println!("{:?}", t);
38+
let t = print_unspanned::<T>(s);
2639
assert!(t.is_ok());
2740
}
2841
NormalErr => {
29-
let t = T::from_str(s);
30-
println!("{:?}", t);
42+
let t = print_unspanned::<T>(s);
3143
assert!(t.is_err());
3244
}
3345
OtherError => {
34-
println!("{:?}", T::from_str(s));
46+
print_unspanned::<T>(s);
3547
}
3648
OtherWithPanic => {
37-
if catch_unwind(|| println!("{:?}", T::from_str(s))).is_ok() {
49+
if catch_unwind(|| print_unspanned::<T>(s)).is_ok() {
3850
eprintln!("{s} did not panic");
3951
}
4052
}
@@ -64,14 +76,18 @@ fn lit(s: &str, mode: Mode) {
6476
}
6577

6678
pub fn run() {
79+
assert_eq!("\'", "'");
6780
// returns Ok(valid instance)
81+
lit("r\"g\"", NormalOk);
82+
lit("r#\"g\"#", NormalOk);
6883
lit("123", NormalOk);
6984
lit("\"ab\"", NormalOk);
7085
lit("\'b\'", NormalOk);
7186
lit("'b'", NormalOk);
7287
lit("b\"b\"", NormalOk);
7388
lit("c\"b\"", NormalOk);
7489
lit("cr\"b\"", NormalOk);
90+
lit("'\\''", NormalOk);
7591
lit("b'b'", NormalOk);
7692
lit("256u8", NormalOk);
7793
lit("-256u8", NormalOk);
@@ -99,10 +115,13 @@ pub fn run() {
99115
NormalOk,
100116
);
101117
stream("/*a*/ //", NormalOk);
118+
lit("\"\"", NormalOk);
119+
stream("", NormalOk);
102120

103121
println!("### ERRORS");
104122

105123
// returns Err(LexError)
124+
lit("", NormalErr);
106125
lit("\'c\'/**/", NormalErr);
107126
lit(" 0", NormalErr);
108127
lit("0 ", NormalErr);
@@ -119,10 +138,12 @@ pub fn run() {
119138
// emits diagnostics and returns LexError
120139
lit("r'r'", OtherError);
121140
lit("c'r'", OtherError);
141+
lit("\u{2000}", OtherError);
122142

123143
// emits diagnostic and returns a seemingly valid tokenstream
124144
stream("r'r'", OtherError);
125145
stream("c'r'", OtherError);
146+
stream("\u{2000}", OtherError);
126147

127148
for parse in [stream as fn(&str, Mode), lit] {
128149
// emits diagnostic(s), then panics

tests/ui/proc-macro/nonfatal-parsing.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ help: consider inserting whitespace here
2222
LL | c 'r'
2323
| +
2424

25+
error: unknown start of token: \u{2000}
26+
--> <proc-macro source code>:1:1
27+
|
28+
LL |  
29+
| ^
30+
|
31+
help: Unicode character ' ' (En Quad) looks like ' ' (Space), but it is not
32+
|
33+
LL |
34+
|
35+
2536
error: prefix `r` is unknown
2637
--> $DIR/nonfatal-parsing.rs:15:5
2738
|
@@ -40,6 +51,19 @@ LL | nonfatal_parsing::run!();
4051
= note: prefixed identifiers and literals are reserved since Rust 2021
4152
= note: this error originates in the macro `nonfatal_parsing::run` (in Nightly builds, run with -Z macro-backtrace for more info)
4253

54+
error: unknown start of token: \u{2000}
55+
--> $DIR/nonfatal-parsing.rs:15:5
56+
|
57+
LL | nonfatal_parsing::run!();
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^
59+
|
60+
= note: this error originates in the macro `nonfatal_parsing::run` (in Nightly builds, run with -Z macro-backtrace for more info)
61+
help: Unicode character ' ' (En Quad) looks like ' ' (Space), but it is not
62+
--> <proc-macro source code>:1:1
63+
|
64+
LL |
65+
|
66+
4367
error: found invalid character; only `#` is allowed in raw string delimitation: \u{0}
4468
--> $DIR/nonfatal-parsing.rs:15:5
4569
|
@@ -175,6 +199,6 @@ error: invalid digit for a base 2 literal
175199
LL | /*a*/ 0b2 //
176200
| ^
177201

178-
error: aborting due to 20 previous errors
202+
error: aborting due to 22 previous errors
179203

180204
For more information about this error, try `rustc --explain E0768`.
Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1-
Ok(Literal { kind: Integer, symbol: "123", suffix: None, span: #44 bytes(361..385) })
2-
Ok(Literal { kind: Str, symbol: "ab", suffix: None, span: #44 bytes(361..385) })
3-
Ok(Literal { kind: Char, symbol: "b", suffix: None, span: #44 bytes(361..385) })
4-
Ok(Literal { kind: Char, symbol: "b", suffix: None, span: #44 bytes(361..385) })
5-
Ok(Literal { kind: ByteStr, symbol: "b", suffix: None, span: #44 bytes(361..385) })
6-
Ok(Literal { kind: CStr, symbol: "b", suffix: None, span: #44 bytes(361..385) })
7-
Ok(Literal { kind: CStrRaw(0), symbol: "b", suffix: None, span: #44 bytes(361..385) })
8-
Ok(Literal { kind: Byte, symbol: "b", suffix: None, span: #44 bytes(361..385) })
9-
Ok(Literal { kind: Integer, symbol: "256", suffix: Some("u8"), span: #44 bytes(361..385) })
10-
Ok(Literal { kind: Integer, symbol: "-256", suffix: Some("u8"), span: #44 bytes(361..385) })
11-
Ok(TokenStream [Punct { ch: '-', spacing: Alone, span: #44 bytes(361..385) }, Literal { kind: Integer, symbol: "256", suffix: Some("u8"), span: #44 bytes(361..385) }])
12-
Ok(Literal { kind: Integer, symbol: "0b11111000000001111", suffix: Some("i16"), span: #44 bytes(361..385) })
13-
Ok(Literal { kind: Integer, symbol: "0xf32", suffix: None, span: #44 bytes(361..385) })
14-
Ok(Literal { kind: Integer, symbol: "0b0", suffix: Some("f32"), span: #44 bytes(361..385) })
15-
Ok(Literal { kind: Float, symbol: "2E4", suffix: None, span: #44 bytes(361..385) })
16-
Ok(Literal { kind: Float, symbol: "2.2E-4", suffix: Some("f64"), span: #44 bytes(361..385) })
17-
Ok(Literal { kind: Integer, symbol: "18", suffix: Some("u8E"), span: #44 bytes(361..385) })
18-
Ok(Literal { kind: Float, symbol: "18.0", suffix: Some("u8E"), span: #44 bytes(361..385) })
19-
Ok(Literal { kind: CStrRaw(1), symbol: "// /* // \n */", suffix: None, span: #44 bytes(361..385) })
20-
Ok(Literal { kind: Char, symbol: "\'", suffix: None, span: #44 bytes(361..385) })
21-
Ok(Literal { kind: Char, symbol: "\'", suffix: None, span: #44 bytes(361..385) })
22-
Ok(Literal { kind: StrRaw(255), symbol: "a", suffix: None, span: #44 bytes(361..385) })
23-
Ok(TokenStream [Ident { ident: "fn", span: #44 bytes(361..385) }, Ident { ident: "main", span: #44 bytes(361..385) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #44 bytes(361..385) }, Group { delimiter: Brace, stream: TokenStream [Ident { ident: "println", span: #44 bytes(361..385) }, Punct { ch: '!', spacing: Alone, span: #44 bytes(361..385) }, Group { delimiter: Parenthesis, stream: TokenStream [Literal { kind: Str, symbol: "Hello, world!", suffix: None, span: #44 bytes(361..385) }], span: #44 bytes(361..385) }], span: #44 bytes(361..385) }])
24-
Ok(TokenStream [Literal { kind: Integer, symbol: "18", suffix: None, span: #44 bytes(361..385) }, Punct { ch: '.', spacing: Alone, span: #44 bytes(361..385) }, Ident { ident: "u8E", span: #44 bytes(361..385) }])
25-
Ok(TokenStream [Literal { kind: Float, symbol: "18.0", suffix: Some("f32"), span: #44 bytes(361..385) }])
26-
Ok(TokenStream [Literal { kind: Float, symbol: "18.0", suffix: Some("f34"), span: #44 bytes(361..385) }])
27-
Ok(TokenStream [Literal { kind: Integer, symbol: "18", suffix: None, span: #44 bytes(361..385) }, Punct { ch: '.', spacing: Alone, span: #44 bytes(361..385) }, Ident { ident: "bu8", span: #44 bytes(361..385) }])
28-
Ok(TokenStream [Literal { kind: Integer, symbol: "3", suffix: None, span: #44 bytes(361..385) }, Literal { kind: Integer, symbol: "4", suffix: None, span: #44 bytes(361..385) }])
29-
Ok(TokenStream [Literal { kind: Char, symbol: "c", suffix: None, span: #44 bytes(361..385) }])
1+
Ok(Literal { kind: StrRaw(0), symbol: "g", suffix: None, span: Span })
2+
Ok(Literal { kind: StrRaw(1), symbol: "g", suffix: None, span: Span })
3+
Ok(Literal { kind: Integer, symbol: "123", suffix: None, span: Span })
4+
Ok(Literal { kind: Str, symbol: "ab", suffix: None, span: Span })
5+
Ok(Literal { kind: Char, symbol: "b", suffix: None, span: Span })
6+
Ok(Literal { kind: Char, symbol: "b", suffix: None, span: Span })
7+
Ok(Literal { kind: ByteStr, symbol: "b", suffix: None, span: Span })
8+
Ok(Literal { kind: CStr, symbol: "b", suffix: None, span: Span })
9+
Ok(Literal { kind: CStrRaw(0), symbol: "b", suffix: None, span: Span })
10+
Ok(Literal { kind: Char, symbol: "\'", suffix: None, span: Span })
11+
Ok(Literal { kind: Byte, symbol: "b", suffix: None, span: Span })
12+
Ok(Literal { kind: Integer, symbol: "256", suffix: Some("u8"), span: Span })
13+
Ok(Literal { kind: Integer, symbol: "-256", suffix: Some("u8"), span: Span })
14+
Ok(TokenStream [Punct { ch: '-', spacing: Alone, span: Span }, Literal { kind: Integer, symbol: "256", suffix: Some("u8"), span: Span }])
15+
Ok(Literal { kind: Integer, symbol: "0b11111000000001111", suffix: Some("i16"), span: Span })
16+
Ok(Literal { kind: Integer, symbol: "0xf32", suffix: None, span: Span })
17+
Ok(Literal { kind: Integer, symbol: "0b0", suffix: Some("f32"), span: Span })
18+
Ok(Literal { kind: Float, symbol: "2E4", suffix: None, span: Span })
19+
Ok(Literal { kind: Float, symbol: "2.2E-4", suffix: Some("f64"), span: Span })
20+
Ok(Literal { kind: Integer, symbol: "18", suffix: Some("u8E"), span: Span })
21+
Ok(Literal { kind: Float, symbol: "18.0", suffix: Some("u8E"), span: Span })
22+
Ok(Literal { kind: CStrRaw(1), symbol: "// /* // \n */", suffix: None, span: Span })
23+
Ok(Literal { kind: Char, symbol: "\'", suffix: None, span: Span })
24+
Ok(Literal { kind: Char, symbol: "\'", suffix: None, span: Span })
25+
Ok(Literal { kind: StrRaw(255), symbol: "a", suffix: None, span: Span })
26+
Ok(TokenStream [Ident { ident: "fn", span: Span }, Ident { ident: "main", span: Span }, Group { delimiter: Parenthesis, stream: TokenStream [], span: Span }, Group { delimiter: Brace, stream: TokenStream [Ident { ident: "println", span: Span }, Punct { ch: '!', spacing: Alone, span: Span }, Group { delimiter: Parenthesis, stream: TokenStream [Literal { kind: Str, symbol: "Hello, world!", suffix: None, span: Span }], span: Span }], span: Span }])
27+
Ok(TokenStream [Literal { kind: Integer, symbol: "18", suffix: None, span: Span }, Punct { ch: '.', spacing: Alone, span: Span }, Ident { ident: "u8E", span: Span }])
28+
Ok(TokenStream [Literal { kind: Float, symbol: "18.0", suffix: Some("f32"), span: Span }])
29+
Ok(TokenStream [Literal { kind: Float, symbol: "18.0", suffix: Some("f34"), span: Span }])
30+
Ok(TokenStream [Literal { kind: Integer, symbol: "18", suffix: None, span: Span }, Punct { ch: '.', spacing: Alone, span: Span }, Ident { ident: "bu8", span: Span }])
31+
Ok(TokenStream [Literal { kind: Integer, symbol: "3", suffix: None, span: Span }, Literal { kind: Integer, symbol: "4", suffix: None, span: Span }])
32+
Ok(TokenStream [Literal { kind: Char, symbol: "c", suffix: None, span: Span }])
33+
Ok(TokenStream [])
34+
Ok(Literal { kind: Str, symbol: "", suffix: None, span: Span })
3035
Ok(TokenStream [])
3136
### ERRORS
37+
Err(LexError("not a literal"))
3238
Err(LexError("comment or whitespace around literal"))
3339
Err(LexError("comment or whitespace around literal"))
3440
Err(LexError("comment or whitespace around literal"))
@@ -42,17 +48,19 @@ Err(LexError("unexpected closing delimiter: `)`"))
4248
Err(LexError("unexpected closing delimiter: `]`"))
4349
Err(LexError("not a literal"))
4450
Err(LexError("not a literal"))
45-
Ok(TokenStream [Ident { ident: "r", span: #44 bytes(361..385) }, Literal { kind: Char, symbol: "r", suffix: None, span: #44 bytes(361..385) }])
46-
Ok(TokenStream [Ident { ident: "c", span: #44 bytes(361..385) }, Literal { kind: Char, symbol: "r", suffix: None, span: #44 bytes(361..385) }])
47-
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "0b2", suffix: None, span: #44 bytes(361..385) }])
48-
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "0b", suffix: Some("f32"), span: #44 bytes(361..385) }])
49-
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "0b0.0", suffix: Some("f32"), span: #44 bytes(361..385) }])
50-
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "'''", suffix: None, span: #44 bytes(361..385) }])
51-
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "'\n'", suffix: None, span: #44 bytes(361..385) }])
52-
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "0b2", suffix: None, span: #44 bytes(361..385) }])
53-
Ok(Literal { kind: ErrWithGuar, symbol: "0b2", suffix: None, span: #44 bytes(361..385) })
54-
Ok(Literal { kind: ErrWithGuar, symbol: "0b", suffix: Some("f32"), span: #44 bytes(361..385) })
55-
Ok(Literal { kind: ErrWithGuar, symbol: "0b0.0", suffix: Some("f32"), span: #44 bytes(361..385) })
56-
Ok(Literal { kind: ErrWithGuar, symbol: "'''", suffix: None, span: #44 bytes(361..385) })
57-
Ok(Literal { kind: ErrWithGuar, symbol: "'\n'", suffix: None, span: #44 bytes(361..385) })
51+
Err(LexError("not a literal"))
52+
Ok(TokenStream [Ident { ident: "r", span: Span }, Literal { kind: Char, symbol: "r", suffix: None, span: Span }])
53+
Ok(TokenStream [Ident { ident: "c", span: Span }, Literal { kind: Char, symbol: "r", suffix: None, span: Span }])
54+
Ok(TokenStream [])
55+
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "0b2", suffix: None, span: Span }])
56+
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "0b", suffix: Some("f32"), span: Span }])
57+
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "0b0.0", suffix: Some("f32"), span: Span }])
58+
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "'''", suffix: None, span: Span }])
59+
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "'\n'", suffix: None, span: Span }])
60+
Ok(TokenStream [Literal { kind: ErrWithGuar, symbol: "0b2", suffix: None, span: Span }])
61+
Ok(Literal { kind: ErrWithGuar, symbol: "0b2", suffix: None, span: Span })
62+
Ok(Literal { kind: ErrWithGuar, symbol: "0b", suffix: Some("f32"), span: Span })
63+
Ok(Literal { kind: ErrWithGuar, symbol: "0b0.0", suffix: Some("f32"), span: Span })
64+
Ok(Literal { kind: ErrWithGuar, symbol: "'''", suffix: None, span: Span })
65+
Ok(Literal { kind: ErrWithGuar, symbol: "'\n'", suffix: None, span: Span })
5866
Err(LexError("comment or whitespace around literal"))

0 commit comments

Comments
 (0)