Skip to content

Commit 28a997f

Browse files
Properly record metavar spans for other expansions other than TT
1 parent 4c40c89 commit 28a997f

File tree

10 files changed

+58
-19
lines changed

10 files changed

+58
-19
lines changed

Diff for: compiler/rustc_expand/src/mbe/transcribe.rs

+4
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,13 @@ pub(super) fn transcribe<'a>(
282282
}
283283
MatchedSingle(ParseNtResult::Ident(ident, is_raw)) => {
284284
marker.visit_span(&mut sp);
285+
with_metavar_spans(|mspans| mspans.insert(ident.span, sp));
285286
let kind = token::NtIdent(*ident, *is_raw);
286287
TokenTree::token_alone(kind, sp)
287288
}
288289
MatchedSingle(ParseNtResult::Lifetime(ident, is_raw)) => {
289290
marker.visit_span(&mut sp);
291+
with_metavar_spans(|mspans| mspans.insert(ident.span, sp));
290292
let kind = token::NtLifetime(*ident, *is_raw);
291293
TokenTree::token_alone(kind, sp)
292294
}
@@ -295,6 +297,8 @@ pub(super) fn transcribe<'a>(
295297
// `Delimiter::Invisible` to maintain parsing priorities.
296298
// `Interpolated` is currently used for such groups in rustc parser.
297299
marker.visit_span(&mut sp);
300+
let use_span = nt.use_span();
301+
with_metavar_spans(|mspans| mspans.insert(use_span, sp));
298302
TokenTree::token_alone(token::Interpolated(Lrc::clone(nt)), sp)
299303
}
300304
MatchedSeq(..) => {

Diff for: compiler/rustc_parse/src/validate_attr.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_session::errors::report_lit_error;
1111
use rustc_session::lint::BuiltinLintDiag;
1212
use rustc_session::lint::builtin::{ILL_FORMED_ATTRIBUTE_INPUT, UNSAFE_ATTR_OUTSIDE_UNSAFE};
1313
use rustc_session::parse::ParseSess;
14-
use rustc_span::{BytePos, Span, Symbol, sym};
14+
use rustc_span::{Span, Symbol, sym};
1515

1616
use crate::{errors, parse_in};
1717

@@ -164,11 +164,7 @@ pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr:
164164
// wrapping it in `unsafe(...)`. Otherwise, we suggest putting the
165165
// `unsafe(`, `)` right after and right before the opening and closing
166166
// square bracket respectively.
167-
let diag_span = if attr_item.span().can_be_used_for_suggestions() {
168-
attr_item.span()
169-
} else {
170-
attr.span.with_lo(attr.span.lo() + BytePos(2)).with_hi(attr.span.hi() - BytePos(1))
171-
};
167+
let diag_span = attr_item.span();
172168

173169
if attr.span.at_least_rust_2024() {
174170
psess.dcx().emit_err(errors::UnsafeAttrOutsideUnsafe {

Diff for: tests/ui/drop/lint-if-let-rescope-with-macro.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: `if let` assigns a shorter lifetime since Edition 2024
22
--> $DIR/lint-if-let-rescope-with-macro.rs:12:12
33
|
44
LL | if let $p = $e { $($conseq)* } else { $($alt)* }
5-
| ^^^
5+
| ^^^^^^^^^^^
66
...
77
LL | / edition_2021_if_let! {
88
LL | | Some(_value),

Diff for: tests/ui/expr/if/if-let.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: irrefutable `if let` pattern
22
--> $DIR/if-let.rs:6:16
33
|
44
LL | if let $p = $e $b
5-
| ^^^
5+
| ^^^^^^^^^^^
66
...
77
LL | / foo!(a, 1, {
88
LL | | println!("irrefutable pattern");
@@ -18,7 +18,7 @@ warning: irrefutable `if let` pattern
1818
--> $DIR/if-let.rs:6:16
1919
|
2020
LL | if let $p = $e $b
21-
| ^^^
21+
| ^^^^^^^^^^^
2222
...
2323
LL | / bar!(a, 1, {
2424
LL | | println!("irrefutable pattern");

Diff for: tests/ui/for-loop-while/while-let-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: irrefutable `while let` pattern
22
--> $DIR/while-let-2.rs:7:19
33
|
44
LL | while let $p = $e $b
5-
| ^^^
5+
| ^^^^^^^^^^^
66
...
77
LL | / foo!(_a, 1, {
88
LL | | println!("irrefutable pattern");
@@ -18,7 +18,7 @@ warning: irrefutable `while let` pattern
1818
--> $DIR/while-let-2.rs:7:19
1919
|
2020
LL | while let $p = $e $b
21-
| ^^^
21+
| ^^^^^^^^^^^
2222
...
2323
LL | / bar!(_a, 1, {
2424
LL | | println!("irrefutable pattern");

Diff for: tests/ui/lint/wide_pointer_comparisons.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ warning: ambiguous wide pointer comparison, the comparison includes metadata whi
615615
--> $DIR/wide_pointer_comparisons.rs:169:37
616616
|
617617
LL | ($a:expr, $b:expr) => { $a == $b }
618-
| ^^
618+
| ^^^^^^^^
619619
...
620620
LL | cmp!(&a, &b);
621621
| ------------ in this macro invocation

Diff for: tests/ui/parser/issues/issue-65122-mac-invoc-in-mut-patterns.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ error: `mut` must be followed by a named binding
3030
--> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:13:13
3131
|
3232
LL | let mut $eval = ();
33-
| ^^^
33+
| ^^^^
3434
...
3535
LL | mac2! { does_not_exist!() }
3636
| --------------------------- in this macro invocation
@@ -40,7 +40,7 @@ LL | mac2! { does_not_exist!() }
4040
help: remove the `mut` prefix
4141
|
4242
LL - let mut $eval = ();
43-
LL + let $eval = ();
43+
LL + let $eval = ();
4444
|
4545

4646
error: cannot find macro `does_not_exist` in this scope

Diff for: tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-fix.fixed

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ macro_rules! meta2 {
4040
}
4141
}
4242

43+
macro_rules! with_cfg_attr {
44+
() => {
45+
#[cfg_attr(all(), unsafe(link_section = ".custom_section"))]
46+
//~^ ERROR: unsafe attribute used without unsafe
47+
//~| WARN this is accepted in the current edition
48+
pub extern "C" fn abc() {}
49+
};
50+
}
51+
4352
tt!([unsafe(no_mangle)]);
4453
//~^ ERROR: unsafe attribute used without unsafe
4554
//~| WARN this is accepted in the current edition
@@ -52,6 +61,8 @@ meta2!(unsafe(export_name = "baw"));
5261
//~| WARN this is accepted in the current edition
5362
ident2!(export_name, "bars");
5463

64+
with_cfg_attr!();
65+
5566
#[unsafe(no_mangle)]
5667
//~^ ERROR: unsafe attribute used without unsafe
5768
//~| WARN this is accepted in the current edition

Diff for: tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-fix.rs

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ macro_rules! meta2 {
4040
}
4141
}
4242

43+
macro_rules! with_cfg_attr {
44+
() => {
45+
#[cfg_attr(all(), link_section = ".custom_section")]
46+
//~^ ERROR: unsafe attribute used without unsafe
47+
//~| WARN this is accepted in the current edition
48+
pub extern "C" fn abc() {}
49+
};
50+
}
51+
4352
tt!([no_mangle]);
4453
//~^ ERROR: unsafe attribute used without unsafe
4554
//~| WARN this is accepted in the current edition
@@ -52,6 +61,8 @@ meta2!(export_name = "baw");
5261
//~| WARN this is accepted in the current edition
5362
ident2!(export_name, "bars");
5463

64+
with_cfg_attr!();
65+
5566
#[no_mangle]
5667
//~^ ERROR: unsafe attribute used without unsafe
5768
//~| WARN this is accepted in the current edition

Diff for: tests/ui/rust-2024/unsafe-attributes/unsafe-attributes-fix.stderr

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unsafe attribute used without unsafe
2-
--> $DIR/unsafe-attributes-fix.rs:43:6
2+
--> $DIR/unsafe-attributes-fix.rs:52:6
33
|
44
LL | tt!([no_mangle]);
55
| ^^^^^^^^^ usage of unsafe attribute
@@ -34,7 +34,7 @@ LL | #[unsafe($e)]
3434
| +++++++ +
3535

3636
error: unsafe attribute used without unsafe
37-
--> $DIR/unsafe-attributes-fix.rs:47:7
37+
--> $DIR/unsafe-attributes-fix.rs:56:7
3838
|
3939
LL | meta!(no_mangle);
4040
| ^^^^^^^^^ usage of unsafe attribute
@@ -47,7 +47,7 @@ LL | meta!(unsafe(no_mangle));
4747
| +++++++ +
4848

4949
error: unsafe attribute used without unsafe
50-
--> $DIR/unsafe-attributes-fix.rs:50:8
50+
--> $DIR/unsafe-attributes-fix.rs:59:8
5151
|
5252
LL | meta2!(export_name = "baw");
5353
| ^^^^^^^^^^^ usage of unsafe attribute
@@ -77,7 +77,24 @@ LL | #[unsafe($e = $l)]
7777
| +++++++ +
7878

7979
error: unsafe attribute used without unsafe
80-
--> $DIR/unsafe-attributes-fix.rs:55:3
80+
--> $DIR/unsafe-attributes-fix.rs:45:27
81+
|
82+
LL | #[cfg_attr(all(), link_section = ".custom_section")]
83+
| ^^^^^^^^^^^^ usage of unsafe attribute
84+
...
85+
LL | with_cfg_attr!();
86+
| ---------------- in this macro invocation
87+
|
88+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
89+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-attributes.html>
90+
= note: this error originates in the macro `with_cfg_attr` (in Nightly builds, run with -Z macro-backtrace for more info)
91+
help: wrap the attribute in `unsafe(...)`
92+
|
93+
LL | #[cfg_attr(all(), unsafe(link_section = ".custom_section"))]
94+
| +++++++ +
95+
96+
error: unsafe attribute used without unsafe
97+
--> $DIR/unsafe-attributes-fix.rs:66:3
8198
|
8299
LL | #[no_mangle]
83100
| ^^^^^^^^^ usage of unsafe attribute
@@ -89,5 +106,5 @@ help: wrap the attribute in `unsafe(...)`
89106
LL | #[unsafe(no_mangle)]
90107
| +++++++ +
91108

92-
error: aborting due to 6 previous errors
109+
error: aborting due to 7 previous errors
93110

0 commit comments

Comments
 (0)