Skip to content

Commit dd33766

Browse files
committed
Auto merge of rust-lang#76585 - Aaron1011:ignore-vert-plus, r=petrochenkov
Ignore `|` and `+` tokens during proc-macro pretty-print check Fixes rust-lang#76182 This is an alternative to PR rust-lang#76188 These tokens are not preserved in the AST in certain cases (e.g. a leading `|` in a pattern or a trailing `+` in a trait bound). This PR ignores them entirely during the pretty-print/reparse check to avoid spuriously using the re-parsed tokenstream.
2 parents 04b72b4 + 283d4c4 commit dd33766

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

compiler/rustc_parse/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ pub fn tokenstream_probably_equal_for_proc_macro(
364364
| token::CloseDelim(DelimToken::NoDelim)
365365
// The pretty printer collapses many semicolons into one.
366366
| token::Semi
367+
// We don't preserve leading `|` tokens in patterns, so
368+
// we ignore them entirely
369+
| token::BinOp(token::BinOpToken::Or)
370+
// We don't preserve trailing '+' tokens in trait bounds,
371+
// so we ignore them entirely
372+
| token::BinOp(token::BinOpToken::Plus)
367373
// The pretty printer can turn `$crate` into `::crate_name`
368374
| token::ModSep = token.kind {
369375
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
// aux-build:test-macros.rs
3+
// compile-flags: -Z span-debug
4+
//
5+
// Regression test for issue #76182
6+
// Tests that we properly handle patterns with a leading vert
7+
8+
#![no_std] // Don't load unnecessary hygiene information from std
9+
extern crate std;
10+
11+
extern crate test_macros;
12+
13+
#[test_macros::print_attr]
14+
fn main() {
15+
match () { | () => () }
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
PRINT-ATTR INPUT (DISPLAY): fn main() { match() { | () => () } }
2+
PRINT-ATTR INPUT (DEBUG): TokenStream [
3+
Ident {
4+
ident: "fn",
5+
span: $DIR/issue-76182-leading-vert-pat.rs:14:1: 14:3 (#0),
6+
},
7+
Ident {
8+
ident: "main",
9+
span: $DIR/issue-76182-leading-vert-pat.rs:14:4: 14:8 (#0),
10+
},
11+
Group {
12+
delimiter: Parenthesis,
13+
stream: TokenStream [],
14+
span: $DIR/issue-76182-leading-vert-pat.rs:14:8: 14:10 (#0),
15+
},
16+
Group {
17+
delimiter: Brace,
18+
stream: TokenStream [
19+
Ident {
20+
ident: "match",
21+
span: $DIR/issue-76182-leading-vert-pat.rs:15:5: 15:10 (#0),
22+
},
23+
Group {
24+
delimiter: Parenthesis,
25+
stream: TokenStream [],
26+
span: $DIR/issue-76182-leading-vert-pat.rs:15:11: 15:13 (#0),
27+
},
28+
Group {
29+
delimiter: Brace,
30+
stream: TokenStream [
31+
Punct {
32+
ch: '|',
33+
spacing: Alone,
34+
span: $DIR/issue-76182-leading-vert-pat.rs:15:16: 15:17 (#0),
35+
},
36+
Group {
37+
delimiter: Parenthesis,
38+
stream: TokenStream [],
39+
span: $DIR/issue-76182-leading-vert-pat.rs:15:18: 15:20 (#0),
40+
},
41+
Punct {
42+
ch: '=',
43+
spacing: Joint,
44+
span: $DIR/issue-76182-leading-vert-pat.rs:15:21: 15:23 (#0),
45+
},
46+
Punct {
47+
ch: '>',
48+
spacing: Alone,
49+
span: $DIR/issue-76182-leading-vert-pat.rs:15:21: 15:23 (#0),
50+
},
51+
Group {
52+
delimiter: Parenthesis,
53+
stream: TokenStream [],
54+
span: $DIR/issue-76182-leading-vert-pat.rs:15:24: 15:26 (#0),
55+
},
56+
],
57+
span: $DIR/issue-76182-leading-vert-pat.rs:15:14: 15:28 (#0),
58+
},
59+
],
60+
span: $DIR/issue-76182-leading-vert-pat.rs:14:11: 16:2 (#0),
61+
},
62+
]
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
// aux-build:test-macros.rs
3+
// compile-flags: -Z span-debug
4+
5+
#![no_std] // Don't load unnecessary hygiene information from std
6+
extern crate std;
7+
8+
extern crate test_macros;
9+
10+
#[test_macros::print_attr]
11+
fn foo<T>() where T: Copy + {
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
PRINT-ATTR INPUT (DISPLAY): fn foo < T > () where T : Copy + { }
2+
PRINT-ATTR INPUT (DEBUG): TokenStream [
3+
Ident {
4+
ident: "fn",
5+
span: $DIR/trailing-plus.rs:11:1: 11:3 (#0),
6+
},
7+
Ident {
8+
ident: "foo",
9+
span: $DIR/trailing-plus.rs:11:4: 11:7 (#0),
10+
},
11+
Punct {
12+
ch: '<',
13+
spacing: Alone,
14+
span: $DIR/trailing-plus.rs:11:7: 11:8 (#0),
15+
},
16+
Ident {
17+
ident: "T",
18+
span: $DIR/trailing-plus.rs:11:8: 11:9 (#0),
19+
},
20+
Punct {
21+
ch: '>',
22+
spacing: Alone,
23+
span: $DIR/trailing-plus.rs:11:9: 11:10 (#0),
24+
},
25+
Group {
26+
delimiter: Parenthesis,
27+
stream: TokenStream [],
28+
span: $DIR/trailing-plus.rs:11:10: 11:12 (#0),
29+
},
30+
Ident {
31+
ident: "where",
32+
span: $DIR/trailing-plus.rs:11:13: 11:18 (#0),
33+
},
34+
Ident {
35+
ident: "T",
36+
span: $DIR/trailing-plus.rs:11:19: 11:20 (#0),
37+
},
38+
Punct {
39+
ch: ':',
40+
spacing: Alone,
41+
span: $DIR/trailing-plus.rs:11:20: 11:21 (#0),
42+
},
43+
Ident {
44+
ident: "Copy",
45+
span: $DIR/trailing-plus.rs:11:22: 11:26 (#0),
46+
},
47+
Punct {
48+
ch: '+',
49+
spacing: Alone,
50+
span: $DIR/trailing-plus.rs:11:27: 11:28 (#0),
51+
},
52+
Group {
53+
delimiter: Brace,
54+
stream: TokenStream [],
55+
span: $DIR/trailing-plus.rs:11:29: 12:2 (#0),
56+
},
57+
]

0 commit comments

Comments
 (0)