Skip to content

Commit 185ca0f

Browse files
authored
Rollup merge of #102639 - nnethercote:improve-spans-splitting, r=Aaron1011
Improve spans when splitting multi-char operator tokens for proc macros. When a two-char (or three-char) operator token is split into single-char operator tokens before being passed to a proc macro, the single-char tokens are given the original span of length two (or three). This PR gives them more accurate spans. r? `@Aaron1011` cc `@petrochenkov`
2 parents f86ee78 + 88dab8d commit 185ca0f

15 files changed

+166
-84
lines changed

Diff for: compiler/rustc_expand/src/proc_macro_server.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,20 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
115115
// before that get `joint = true`.
116116
let mut op = |s: &str| {
117117
assert!(s.is_ascii());
118-
trees.extend(s.bytes().enumerate().map(|(idx, ch)| {
119-
let is_final = idx == s.len() - 1;
118+
trees.extend(s.bytes().enumerate().map(|(i, ch)| {
119+
let is_final = i == s.len() - 1;
120+
// Split the token span into single chars. Unless the span
121+
// is an unusual one, e.g. due to proc macro expansion. We
122+
// determine this by assuming any span with a length that
123+
// matches the operator length is a normal one, and any
124+
// span with a different length is an unusual one.
125+
let span = if (span.hi() - span.lo()).to_usize() == s.len() {
126+
let lo = span.lo() + BytePos::from_usize(i);
127+
let hi = lo + BytePos::from_usize(1);
128+
span.with_lo(lo).with_hi(hi)
129+
} else {
130+
span
131+
};
120132
TokenTree::Punct(Punct { ch, joint: if is_final { joint } else { true }, span })
121133
}));
122134
};

Diff for: src/test/ui/lint/redundant-semicolon/redundant-semi-proc-macro.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
TokenStream [Ident { ident: "fn", span: #0 bytes(198..200) }, Ident { ident: "span_preservation", span: #0 bytes(201..218) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(218..220) }, Group { delimiter: Brace, stream: TokenStream [Ident { ident: "let", span: #0 bytes(228..231) }, Ident { ident: "tst", span: #0 bytes(232..235) }, Punct { ch: '=', spacing: Alone, span: #0 bytes(236..237) }, Literal { kind: Integer, symbol: "123", suffix: None, span: #0 bytes(238..241) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(241..242) }, Punct { ch: ';', spacing: Alone, span: #0 bytes(242..243) }, Ident { ident: "match", span: #0 bytes(289..294) }, Ident { ident: "tst", span: #0 bytes(295..298) }, Group { delimiter: Brace, stream: TokenStream [Literal { kind: Integer, symbol: "123", suffix: None, span: #0 bytes(483..486) }, Punct { ch: '=', spacing: Joint, span: #0 bytes(487..489) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(487..489) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(490..492) }, Punct { ch: ',', spacing: Alone, span: #0 bytes(492..493) }, Ident { ident: "_", span: #0 bytes(502..503) }, Punct { ch: '=', spacing: Joint, span: #0 bytes(504..506) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(504..506) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(507..509) }], span: #0 bytes(299..515) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(515..516) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(516..517) }, Punct { ch: ';', spacing: Alone, span: #0 bytes(517..518) }], span: #0 bytes(222..562) }]
1+
TokenStream [Ident { ident: "fn", span: #0 bytes(198..200) }, Ident { ident: "span_preservation", span: #0 bytes(201..218) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(218..220) }, Group { delimiter: Brace, stream: TokenStream [Ident { ident: "let", span: #0 bytes(228..231) }, Ident { ident: "tst", span: #0 bytes(232..235) }, Punct { ch: '=', spacing: Alone, span: #0 bytes(236..237) }, Literal { kind: Integer, symbol: "123", suffix: None, span: #0 bytes(238..241) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(241..242) }, Punct { ch: ';', spacing: Alone, span: #0 bytes(242..243) }, Ident { ident: "match", span: #0 bytes(289..294) }, Ident { ident: "tst", span: #0 bytes(295..298) }, Group { delimiter: Brace, stream: TokenStream [Literal { kind: Integer, symbol: "123", suffix: None, span: #0 bytes(483..486) }, Punct { ch: '=', spacing: Joint, span: #0 bytes(487..488) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(488..489) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(490..492) }, Punct { ch: ',', spacing: Alone, span: #0 bytes(492..493) }, Ident { ident: "_", span: #0 bytes(502..503) }, Punct { ch: '=', spacing: Joint, span: #0 bytes(504..505) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(505..506) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(507..509) }], span: #0 bytes(299..515) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(515..516) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(516..517) }, Punct { ch: ';', spacing: Alone, span: #0 bytes(517..518) }], span: #0 bytes(222..562) }]
22
error: unnecessary trailing semicolon
33
--> $DIR/redundant-semi-proc-macro.rs:9:19
44
|

Diff for: src/test/ui/proc-macro/attr-complex-fn.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
5353
Punct {
5454
ch: '>',
5555
spacing: Joint,
56-
span: $DIR/attr-complex-fn.rs:19:36: 19:38 (#0),
56+
span: $DIR/attr-complex-fn.rs:19:36: 19:37 (#0),
5757
},
5858
Punct {
5959
ch: '>',
6060
spacing: Joint,
61-
span: $DIR/attr-complex-fn.rs:19:36: 19:38 (#0),
61+
span: $DIR/attr-complex-fn.rs:19:37: 19:38 (#0),
6262
},
6363
Punct {
6464
ch: '>',

Diff for: src/test/ui/proc-macro/capture-macro-rules-invoke.stdout

+6-6
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
177177
Punct {
178178
ch: ':',
179179
spacing: Joint,
180-
span: $DIR/capture-macro-rules-invoke.rs:45:16: 45:18 (#0),
180+
span: $DIR/capture-macro-rules-invoke.rs:45:16: 45:17 (#0),
181181
},
182182
Punct {
183183
ch: ':',
184184
spacing: Alone,
185-
span: $DIR/capture-macro-rules-invoke.rs:45:16: 45:18 (#0),
185+
span: $DIR/capture-macro-rules-invoke.rs:45:17: 45:18 (#0),
186186
},
187187
Ident {
188188
ident: "option",
@@ -191,12 +191,12 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
191191
Punct {
192192
ch: ':',
193193
spacing: Joint,
194-
span: $DIR/capture-macro-rules-invoke.rs:45:24: 45:26 (#0),
194+
span: $DIR/capture-macro-rules-invoke.rs:45:24: 45:25 (#0),
195195
},
196196
Punct {
197197
ch: ':',
198198
spacing: Alone,
199-
span: $DIR/capture-macro-rules-invoke.rs:45:24: 45:26 (#0),
199+
span: $DIR/capture-macro-rules-invoke.rs:45:25: 45:26 (#0),
200200
},
201201
Ident {
202202
ident: "Option",
@@ -231,12 +231,12 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
231231
Punct {
232232
ch: ':',
233233
spacing: Joint,
234-
span: $DIR/capture-macro-rules-invoke.rs:46:24: 46:26 (#0),
234+
span: $DIR/capture-macro-rules-invoke.rs:46:24: 46:25 (#0),
235235
},
236236
Punct {
237237
ch: ':',
238238
spacing: Alone,
239-
span: $DIR/capture-macro-rules-invoke.rs:46:24: 46:26 (#0),
239+
span: $DIR/capture-macro-rules-invoke.rs:46:25: 46:26 (#0),
240240
},
241241
Ident {
242242
ident: "path",

Diff for: src/test/ui/proc-macro/debug/dump-debug-span-debug.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
// aux-build:macro-dump-debug.rs
33
// compile-flags: -Z span-debug
44

5+
56
extern crate macro_dump_debug;
67
use macro_dump_debug::dump_debug;
78

89
dump_debug! {
910
ident // ident
1011
r#ident // raw ident
1112
, // alone punct
12-
==> // joint punct
13+
&& // joint punct, two-char op
14+
||> // joint punct, two-char op + one-char op
15+
||<< // joint punct, two-char op + two-char op
16+
..= // joint punct, three-char op
17+
<<=! // joint punct, three-char op + one-char-op
1318
() // empty group
1419
[_] // nonempty group
1520

0 commit comments

Comments
 (0)