Skip to content

Commit 31443c6

Browse files
author
Lukas Markeffsky
committed
preserve delim spans during macro_rules! expansion if able
1 parent b9e8286 commit 31443c6

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::mbe::transcribe::transcribe;
1010

1111
use rustc_ast as ast;
1212
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind, TokenKind::*};
13-
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
13+
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
1414
use rustc_ast::{NodeId, DUMMY_NODE_ID};
1515
use rustc_ast_pretty::pprust;
1616
use rustc_attr::{self as attr, TransparencyError};
@@ -212,7 +212,6 @@ fn expand_macro<'cx>(
212212
};
213213
let arm_span = rhses[i].span();
214214

215-
let rhs_spans = rhs.tts.iter().map(|t| t.span()).collect::<Vec<_>>();
216215
// rhs has holes ( `$id` and `$(...)` that need filled)
217216
let mut tts = match transcribe(cx, &named_matches, &rhs, rhs_span, transparency) {
218217
Ok(tts) => tts,
@@ -224,12 +223,25 @@ fn expand_macro<'cx>(
224223

225224
// Replace all the tokens for the corresponding positions in the macro, to maintain
226225
// proper positions in error reporting, while maintaining the macro_backtrace.
227-
if rhs_spans.len() == tts.len() {
226+
if tts.len() == rhs.tts.len() {
228227
tts = tts.map_enumerated(|i, tt| {
229228
let mut tt = tt.clone();
230-
let mut sp = rhs_spans[i];
231-
sp = sp.with_ctxt(tt.span().ctxt());
232-
tt.set_span(sp);
229+
let rhs_tt = &rhs.tts[i];
230+
let ctxt = tt.span().ctxt();
231+
match (&mut tt, rhs_tt) {
232+
// preserve the delim spans if able
233+
(
234+
TokenTree::Delimited(target_sp, ..),
235+
mbe::TokenTree::Delimited(source_sp, ..),
236+
) => {
237+
target_sp.open = source_sp.open.with_ctxt(ctxt);
238+
target_sp.close = source_sp.close.with_ctxt(ctxt);
239+
}
240+
_ => {
241+
let sp = rhs_tt.span().with_ctxt(ctxt);
242+
tt.set_span(sp);
243+
}
244+
}
233245
tt
234246
});
235247
}

tests/ui/const-generics/min_const_generics/macro-fail.stderr

+9-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | fn make_marker() -> impl Marker<gimme_a_const!(marker)> {
88
| in this macro invocation
99
...
1010
LL | ($rusty: ident) => {{ let $rusty = 3; *&$rusty }}
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type
11+
| ^ expected type
1212
|
1313
= note: this error originates in the macro `gimme_a_const` (in Nightly builds, run with -Z macro-backtrace for more info)
1414

@@ -22,26 +22,21 @@ LL | Example::<gimme_a_const!(marker)>
2222
| in this macro invocation
2323
...
2424
LL | ($rusty: ident) => {{ let $rusty = 3; *&$rusty }}
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type
25+
| ^ expected type
2626
|
2727
= note: this error originates in the macro `gimme_a_const` (in Nightly builds, run with -Z macro-backtrace for more info)
2828

2929
error: expected type, found `{`
3030
--> $DIR/macro-fail.rs:4:10
3131
|
32-
LL | () => {{
33-
| __________^
34-
LL | |
35-
LL | | const X: usize = 1337;
36-
LL | | X
37-
LL | | }}
38-
| |___^ expected type
32+
LL | () => {{
33+
| ^ expected type
3934
...
40-
LL | let _fail = Example::<external_macro!()>;
41-
| -----------------
42-
| |
43-
| this macro call doesn't expand to a type
44-
| in this macro invocation
35+
LL | let _fail = Example::<external_macro!()>;
36+
| -----------------
37+
| |
38+
| this macro call doesn't expand to a type
39+
| in this macro invocation
4540
|
4641
= note: this error originates in the macro `external_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
4742

tests/ui/imports/import-prefix-macro-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: expected one of `::`, `;`, or `as`, found `{`
22
--> $DIR/import-prefix-macro-1.rs:11:27
33
|
44
LL | ($p: path) => (use $p {S, Z});
5-
| ^^^^^^ expected one of `::`, `;`, or `as`
5+
| ^ expected one of `::`, `;`, or `as`
66
...
77
LL | import! { a::b::c }
88
| ------------------- in this macro invocation

tests/ui/parser/issues/issue-44406.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ LL | foo!(true);
2121
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
2222
help: if `bar` is a struct, use braces as delimiters
2323
|
24-
LL | bar { }
25-
| ~
24+
LL | bar { baz: $rest }
25+
| ~ ~
2626
help: if `bar` is a function, use the arguments directly
2727
|
2828
LL - bar(baz: $rest)

0 commit comments

Comments
 (0)