1
Fork 0

Properly record metavar spans for other expansions other than TT

This commit is contained in:
Michael Goulet 2024-12-18 18:28:08 +00:00
parent 4c40c89c26
commit 28a997fa44
10 changed files with 58 additions and 19 deletions

View file

@ -282,11 +282,13 @@ pub(super) fn transcribe<'a>(
} }
MatchedSingle(ParseNtResult::Ident(ident, is_raw)) => { MatchedSingle(ParseNtResult::Ident(ident, is_raw)) => {
marker.visit_span(&mut sp); marker.visit_span(&mut sp);
with_metavar_spans(|mspans| mspans.insert(ident.span, sp));
let kind = token::NtIdent(*ident, *is_raw); let kind = token::NtIdent(*ident, *is_raw);
TokenTree::token_alone(kind, sp) TokenTree::token_alone(kind, sp)
} }
MatchedSingle(ParseNtResult::Lifetime(ident, is_raw)) => { MatchedSingle(ParseNtResult::Lifetime(ident, is_raw)) => {
marker.visit_span(&mut sp); marker.visit_span(&mut sp);
with_metavar_spans(|mspans| mspans.insert(ident.span, sp));
let kind = token::NtLifetime(*ident, *is_raw); let kind = token::NtLifetime(*ident, *is_raw);
TokenTree::token_alone(kind, sp) TokenTree::token_alone(kind, sp)
} }
@ -295,6 +297,8 @@ pub(super) fn transcribe<'a>(
// `Delimiter::Invisible` to maintain parsing priorities. // `Delimiter::Invisible` to maintain parsing priorities.
// `Interpolated` is currently used for such groups in rustc parser. // `Interpolated` is currently used for such groups in rustc parser.
marker.visit_span(&mut sp); marker.visit_span(&mut sp);
let use_span = nt.use_span();
with_metavar_spans(|mspans| mspans.insert(use_span, sp));
TokenTree::token_alone(token::Interpolated(Lrc::clone(nt)), sp) TokenTree::token_alone(token::Interpolated(Lrc::clone(nt)), sp)
} }
MatchedSeq(..) => { MatchedSeq(..) => {

View file

@ -11,7 +11,7 @@ use rustc_session::errors::report_lit_error;
use rustc_session::lint::BuiltinLintDiag; use rustc_session::lint::BuiltinLintDiag;
use rustc_session::lint::builtin::{ILL_FORMED_ATTRIBUTE_INPUT, UNSAFE_ATTR_OUTSIDE_UNSAFE}; use rustc_session::lint::builtin::{ILL_FORMED_ATTRIBUTE_INPUT, UNSAFE_ATTR_OUTSIDE_UNSAFE};
use rustc_session::parse::ParseSess; use rustc_session::parse::ParseSess;
use rustc_span::{BytePos, Span, Symbol, sym}; use rustc_span::{Span, Symbol, sym};
use crate::{errors, parse_in}; use crate::{errors, parse_in};
@ -164,11 +164,7 @@ pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr:
// wrapping it in `unsafe(...)`. Otherwise, we suggest putting the // wrapping it in `unsafe(...)`. Otherwise, we suggest putting the
// `unsafe(`, `)` right after and right before the opening and closing // `unsafe(`, `)` right after and right before the opening and closing
// square bracket respectively. // square bracket respectively.
let diag_span = if attr_item.span().can_be_used_for_suggestions() { let diag_span = attr_item.span();
attr_item.span()
} else {
attr.span.with_lo(attr.span.lo() + BytePos(2)).with_hi(attr.span.hi() - BytePos(1))
};
if attr.span.at_least_rust_2024() { if attr.span.at_least_rust_2024() {
psess.dcx().emit_err(errors::UnsafeAttrOutsideUnsafe { psess.dcx().emit_err(errors::UnsafeAttrOutsideUnsafe {

View file

@ -2,7 +2,7 @@ error: `if let` assigns a shorter lifetime since Edition 2024
--> $DIR/lint-if-let-rescope-with-macro.rs:12:12 --> $DIR/lint-if-let-rescope-with-macro.rs:12:12
| |
LL | if let $p = $e { $($conseq)* } else { $($alt)* } LL | if let $p = $e { $($conseq)* } else { $($alt)* }
| ^^^ | ^^^^^^^^^^^
... ...
LL | / edition_2021_if_let! { LL | / edition_2021_if_let! {
LL | | Some(_value), LL | | Some(_value),

View file

@ -2,7 +2,7 @@ warning: irrefutable `if let` pattern
--> $DIR/if-let.rs:6:16 --> $DIR/if-let.rs:6:16
| |
LL | if let $p = $e $b LL | if let $p = $e $b
| ^^^ | ^^^^^^^^^^^
... ...
LL | / foo!(a, 1, { LL | / foo!(a, 1, {
LL | | println!("irrefutable pattern"); LL | | println!("irrefutable pattern");
@ -18,7 +18,7 @@ warning: irrefutable `if let` pattern
--> $DIR/if-let.rs:6:16 --> $DIR/if-let.rs:6:16
| |
LL | if let $p = $e $b LL | if let $p = $e $b
| ^^^ | ^^^^^^^^^^^
... ...
LL | / bar!(a, 1, { LL | / bar!(a, 1, {
LL | | println!("irrefutable pattern"); LL | | println!("irrefutable pattern");

View file

@ -2,7 +2,7 @@ warning: irrefutable `while let` pattern
--> $DIR/while-let-2.rs:7:19 --> $DIR/while-let-2.rs:7:19
| |
LL | while let $p = $e $b LL | while let $p = $e $b
| ^^^ | ^^^^^^^^^^^
... ...
LL | / foo!(_a, 1, { LL | / foo!(_a, 1, {
LL | | println!("irrefutable pattern"); LL | | println!("irrefutable pattern");
@ -18,7 +18,7 @@ warning: irrefutable `while let` pattern
--> $DIR/while-let-2.rs:7:19 --> $DIR/while-let-2.rs:7:19
| |
LL | while let $p = $e $b LL | while let $p = $e $b
| ^^^ | ^^^^^^^^^^^
... ...
LL | / bar!(_a, 1, { LL | / bar!(_a, 1, {
LL | | println!("irrefutable pattern"); LL | | println!("irrefutable pattern");

View file

@ -615,7 +615,7 @@ warning: ambiguous wide pointer comparison, the comparison includes metadata whi
--> $DIR/wide_pointer_comparisons.rs:169:37 --> $DIR/wide_pointer_comparisons.rs:169:37
| |
LL | ($a:expr, $b:expr) => { $a == $b } LL | ($a:expr, $b:expr) => { $a == $b }
| ^^ | ^^^^^^^^
... ...
LL | cmp!(&a, &b); LL | cmp!(&a, &b);
| ------------ in this macro invocation | ------------ in this macro invocation

View file

@ -30,7 +30,7 @@ error: `mut` must be followed by a named binding
--> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:13:13 --> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:13:13
| |
LL | let mut $eval = (); LL | let mut $eval = ();
| ^^^ | ^^^^
... ...
LL | mac2! { does_not_exist!() } LL | mac2! { does_not_exist!() }
| --------------------------- in this macro invocation | --------------------------- in this macro invocation
@ -40,7 +40,7 @@ LL | mac2! { does_not_exist!() }
help: remove the `mut` prefix help: remove the `mut` prefix
| |
LL - let mut $eval = (); LL - let mut $eval = ();
LL + let $eval = (); LL + let $eval = ();
| |
error: cannot find macro `does_not_exist` in this scope error: cannot find macro `does_not_exist` in this scope

View file

@ -40,6 +40,15 @@ macro_rules! meta2 {
} }
} }
macro_rules! with_cfg_attr {
() => {
#[cfg_attr(all(), unsafe(link_section = ".custom_section"))]
//~^ ERROR: unsafe attribute used without unsafe
//~| WARN this is accepted in the current edition
pub extern "C" fn abc() {}
};
}
tt!([unsafe(no_mangle)]); tt!([unsafe(no_mangle)]);
//~^ ERROR: unsafe attribute used without unsafe //~^ ERROR: unsafe attribute used without unsafe
//~| WARN this is accepted in the current edition //~| WARN this is accepted in the current edition
@ -52,6 +61,8 @@ meta2!(unsafe(export_name = "baw"));
//~| WARN this is accepted in the current edition //~| WARN this is accepted in the current edition
ident2!(export_name, "bars"); ident2!(export_name, "bars");
with_cfg_attr!();
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
//~^ ERROR: unsafe attribute used without unsafe //~^ ERROR: unsafe attribute used without unsafe
//~| WARN this is accepted in the current edition //~| WARN this is accepted in the current edition

View file

@ -40,6 +40,15 @@ macro_rules! meta2 {
} }
} }
macro_rules! with_cfg_attr {
() => {
#[cfg_attr(all(), link_section = ".custom_section")]
//~^ ERROR: unsafe attribute used without unsafe
//~| WARN this is accepted in the current edition
pub extern "C" fn abc() {}
};
}
tt!([no_mangle]); tt!([no_mangle]);
//~^ ERROR: unsafe attribute used without unsafe //~^ ERROR: unsafe attribute used without unsafe
//~| WARN this is accepted in the current edition //~| WARN this is accepted in the current edition
@ -52,6 +61,8 @@ meta2!(export_name = "baw");
//~| WARN this is accepted in the current edition //~| WARN this is accepted in the current edition
ident2!(export_name, "bars"); ident2!(export_name, "bars");
with_cfg_attr!();
#[no_mangle] #[no_mangle]
//~^ ERROR: unsafe attribute used without unsafe //~^ ERROR: unsafe attribute used without unsafe
//~| WARN this is accepted in the current edition //~| WARN this is accepted in the current edition

View file

@ -1,5 +1,5 @@
error: unsafe attribute used without unsafe error: unsafe attribute used without unsafe
--> $DIR/unsafe-attributes-fix.rs:43:6 --> $DIR/unsafe-attributes-fix.rs:52:6
| |
LL | tt!([no_mangle]); LL | tt!([no_mangle]);
| ^^^^^^^^^ usage of unsafe attribute | ^^^^^^^^^ usage of unsafe attribute
@ -34,7 +34,7 @@ LL | #[unsafe($e)]
| +++++++ + | +++++++ +
error: unsafe attribute used without unsafe error: unsafe attribute used without unsafe
--> $DIR/unsafe-attributes-fix.rs:47:7 --> $DIR/unsafe-attributes-fix.rs:56:7
| |
LL | meta!(no_mangle); LL | meta!(no_mangle);
| ^^^^^^^^^ usage of unsafe attribute | ^^^^^^^^^ usage of unsafe attribute
@ -47,7 +47,7 @@ LL | meta!(unsafe(no_mangle));
| +++++++ + | +++++++ +
error: unsafe attribute used without unsafe error: unsafe attribute used without unsafe
--> $DIR/unsafe-attributes-fix.rs:50:8 --> $DIR/unsafe-attributes-fix.rs:59:8
| |
LL | meta2!(export_name = "baw"); LL | meta2!(export_name = "baw");
| ^^^^^^^^^^^ usage of unsafe attribute | ^^^^^^^^^^^ usage of unsafe attribute
@ -77,7 +77,24 @@ LL | #[unsafe($e = $l)]
| +++++++ + | +++++++ +
error: unsafe attribute used without unsafe error: unsafe attribute used without unsafe
--> $DIR/unsafe-attributes-fix.rs:55:3 --> $DIR/unsafe-attributes-fix.rs:45:27
|
LL | #[cfg_attr(all(), link_section = ".custom_section")]
| ^^^^^^^^^^^^ usage of unsafe attribute
...
LL | with_cfg_attr!();
| ---------------- in this macro invocation
|
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-attributes.html>
= note: this error originates in the macro `with_cfg_attr` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap the attribute in `unsafe(...)`
|
LL | #[cfg_attr(all(), unsafe(link_section = ".custom_section"))]
| +++++++ +
error: unsafe attribute used without unsafe
--> $DIR/unsafe-attributes-fix.rs:66:3
| |
LL | #[no_mangle] LL | #[no_mangle]
| ^^^^^^^^^ usage of unsafe attribute | ^^^^^^^^^ usage of unsafe attribute
@ -89,5 +106,5 @@ help: wrap the attribute in `unsafe(...)`
LL | #[unsafe(no_mangle)] LL | #[unsafe(no_mangle)]
| +++++++ + | +++++++ +
error: aborting due to 6 previous errors error: aborting due to 7 previous errors