Skip to content

Commit dee7d0e

Browse files
committed
Auto merge of #134478 - compiler-errors:attr-span, r=oli-obk
Properly record metavar spans for other expansions other than TT This properly records metavar spans for nonterminals other than tokentree. This means that we operations like `span.to(other_span)` work correctly for macros. As you can see, other diagnostics involving metavars have improved as a result. Fixes #132908 Alternative to #133270 cc `@ehuss` cc `@petrochenkov`
2 parents b2728d5 + 2de21ad commit dee7d0e

File tree

12 files changed

+105
-34
lines changed

12 files changed

+105
-34
lines changed

compiler/rustc_expand/src/mbe/transcribe.rs

+10-10
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(..) => {
@@ -410,19 +414,15 @@ fn maybe_use_metavar_location(
410414
return orig_tt.clone();
411415
}
412416

413-
let insert = |mspans: &mut FxHashMap<_, _>, s, ms| match mspans.try_insert(s, ms) {
414-
Ok(_) => true,
415-
Err(err) => *err.entry.get() == ms, // Tried to insert the same span, still success
416-
};
417417
marker.visit_span(&mut metavar_span);
418418
let no_collision = match orig_tt {
419419
TokenTree::Token(token, ..) => {
420-
with_metavar_spans(|mspans| insert(mspans, token.span, metavar_span))
420+
with_metavar_spans(|mspans| mspans.insert(token.span, metavar_span))
421421
}
422422
TokenTree::Delimited(dspan, ..) => with_metavar_spans(|mspans| {
423-
insert(mspans, dspan.open, metavar_span)
424-
&& insert(mspans, dspan.close, metavar_span)
425-
&& insert(mspans, dspan.entire(), metavar_span)
423+
mspans.insert(dspan.open, metavar_span)
424+
&& mspans.insert(dspan.close, metavar_span)
425+
&& mspans.insert(dspan.entire(), metavar_span)
426426
}),
427427
};
428428
if no_collision || psess.source_map().is_imported(metavar_span) {
@@ -434,14 +434,14 @@ fn maybe_use_metavar_location(
434434
match orig_tt {
435435
TokenTree::Token(Token { kind, span }, spacing) => {
436436
let span = metavar_span.with_ctxt(span.ctxt());
437-
with_metavar_spans(|mspans| insert(mspans, span, metavar_span));
437+
with_metavar_spans(|mspans| mspans.insert(span, metavar_span));
438438
TokenTree::Token(Token { kind: kind.clone(), span }, *spacing)
439439
}
440440
TokenTree::Delimited(dspan, dspacing, delimiter, tts) => {
441441
let open = metavar_span.with_ctxt(dspan.open.ctxt());
442442
let close = metavar_span.with_ctxt(dspan.close.ctxt());
443443
with_metavar_spans(|mspans| {
444-
insert(mspans, open, metavar_span) && insert(mspans, close, metavar_span)
444+
mspans.insert(open, metavar_span) && mspans.insert(close, metavar_span)
445445
});
446446
let dspan = DelimSpan::from_pair(open, close);
447447
TokenTree::Delimited(dspan, *dspacing, *delimiter, tts.clone())

compiler/rustc_middle/src/hir/map/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::*;
1212
use rustc_hir_pretty as pprust_hir;
1313
use rustc_middle::hir::nested_filter;
1414
use rustc_span::def_id::StableCrateId;
15-
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
15+
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym, with_metavar_spans};
1616

1717
use crate::hir::ModuleItems;
1818
use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
@@ -1117,6 +1117,9 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
11171117
// the fly in the resolver, storing only their accumulated hash in `ResolverGlobalCtxt`,
11181118
// and combining it with other hashes here.
11191119
resolutions.visibilities_for_hashing.hash_stable(&mut hcx, &mut stable_hasher);
1120+
with_metavar_spans(|mspans| {
1121+
mspans.freeze_and_get_read_spans().hash_stable(&mut hcx, &mut stable_hasher);
1122+
});
11201123
stable_hasher.finish()
11211124
});
11221125

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 {

compiler/rustc_span/src/lib.rs

+39-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#![feature(hash_set_entry)]
2626
#![feature(if_let_guard)]
2727
#![feature(let_chains)]
28+
#![feature(map_try_insert)]
2829
#![feature(negative_impls)]
2930
#![feature(read_buf)]
3031
#![feature(round_char_boundary)]
@@ -85,9 +86,9 @@ use std::str::FromStr;
8586
use std::{fmt, iter};
8687

8788
use md5::{Digest, Md5};
88-
use rustc_data_structures::fx::FxHashMap;
8989
use rustc_data_structures::stable_hasher::{Hash64, Hash128, HashStable, StableHasher};
9090
use rustc_data_structures::sync::{FreezeLock, FreezeWriteGuard, Lock, Lrc};
91+
use rustc_data_structures::unord::UnordMap;
9192
use sha1::Sha1;
9293
use sha2::Sha256;
9394

@@ -103,7 +104,7 @@ pub struct SessionGlobals {
103104
span_interner: Lock<span_encoding::SpanInterner>,
104105
/// Maps a macro argument token into use of the corresponding metavariable in the macro body.
105106
/// Collisions are possible and processed in `maybe_use_metavar_location` on best effort basis.
106-
metavar_spans: Lock<FxHashMap<Span, Span>>,
107+
metavar_spans: MetavarSpansMap,
107108
hygiene_data: Lock<hygiene::HygieneData>,
108109

109110
/// The session's source map, if there is one. This field should only be
@@ -177,9 +178,42 @@ pub fn create_default_session_globals_then<R>(f: impl FnOnce() -> R) -> R {
177178
// deserialization.
178179
scoped_tls::scoped_thread_local!(static SESSION_GLOBALS: SessionGlobals);
179180

181+
#[derive(Default)]
182+
pub struct MetavarSpansMap(FreezeLock<UnordMap<Span, (Span, bool)>>);
183+
184+
impl MetavarSpansMap {
185+
pub fn insert(&self, span: Span, var_span: Span) -> bool {
186+
match self.0.write().try_insert(span, (var_span, false)) {
187+
Ok(_) => true,
188+
Err(entry) => entry.entry.get().0 == var_span,
189+
}
190+
}
191+
192+
/// Read a span and record that it was read.
193+
pub fn get(&self, span: Span) -> Option<Span> {
194+
if let Some(mut mspans) = self.0.try_write() {
195+
if let Some((var_span, read)) = mspans.get_mut(&span) {
196+
*read = true;
197+
Some(*var_span)
198+
} else {
199+
None
200+
}
201+
} else {
202+
if let Some((span, true)) = self.0.read().get(&span) { Some(*span) } else { None }
203+
}
204+
}
205+
206+
/// Freeze the set, and return the spans which have been read.
207+
///
208+
/// After this is frozen, no spans that have not been read can be read.
209+
pub fn freeze_and_get_read_spans(&self) -> UnordMap<Span, Span> {
210+
self.0.freeze().items().filter(|(_, (_, b))| *b).map(|(s1, (s2, _))| (*s1, *s2)).collect()
211+
}
212+
}
213+
180214
#[inline]
181-
pub fn with_metavar_spans<R>(f: impl FnOnce(&mut FxHashMap<Span, Span>) -> R) -> R {
182-
with_session_globals(|session_globals| f(&mut session_globals.metavar_spans.lock()))
215+
pub fn with_metavar_spans<R>(f: impl FnOnce(&MetavarSpansMap) -> R) -> R {
216+
with_session_globals(|session_globals| f(&session_globals.metavar_spans))
183217
}
184218

185219
// FIXME: We should use this enum or something like it to get rid of the
@@ -872,8 +906,7 @@ impl Span {
872906

873907
/// Check if you can select metavar spans for the given spans to get matching contexts.
874908
fn try_metavars(a: SpanData, b: SpanData, a_orig: Span, b_orig: Span) -> (SpanData, SpanData) {
875-
let get = |mspans: &FxHashMap<_, _>, s| mspans.get(&s).copied();
876-
match with_metavar_spans(|mspans| (get(mspans, a_orig), get(mspans, b_orig))) {
909+
match with_metavar_spans(|mspans| (mspans.get(a_orig), mspans.get(b_orig))) {
877910
(None, None) => {}
878911
(Some(meta_a), None) => {
879912
let meta_a = meta_a.data();

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),

tests/ui/expr/if/if-let.stderr

+1-1
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");

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

+1-1
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");

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

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

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

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

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)