Skip to content

Commit beed1d6

Browse files
committed
Move #[rustc_symbol_name] and #[rustc_def_path] processing
1 parent aafb475 commit beed1d6

11 files changed

Lines changed: 151 additions & 170 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4455,6 +4455,7 @@ dependencies = [
44554455
"rustc_privacy",
44564456
"rustc_session",
44574457
"rustc_span",
4458+
"rustc_symbol_mangling",
44584459
"rustc_target",
44594460
"rustc_trait_selection",
44604461
"tracing",

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ const SYMBOL_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
310310
Allow(Target::Fn),
311311
Allow(Target::Method(MethodKind::TraitImpl)),
312312
Allow(Target::Method(MethodKind::Inherent)),
313+
Allow(Target::Method(MethodKind::Trait { body: true })),
313314
Allow(Target::ForeignFn),
314315
Allow(Target::ForeignStatic),
315316
Allow(Target::Impl { of_trait: false }),

compiler/rustc_hir/src/hir.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,9 +1364,6 @@ impl AttributeExt for Attribute {
13641364
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
13651365
Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
13661366
Attribute::Parsed(AttributeKind::CfgTrace(cfgs)) => cfgs[0].1,
1367-
Attribute::Parsed(AttributeKind::RustcSymbolName(span)) => *span,
1368-
Attribute::Parsed(AttributeKind::RustcDefPath(span)) => *span,
1369-
13701367
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
13711368
}
13721369
}

compiler/rustc_interface/src/passes.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,12 +1234,6 @@ pub(crate) fn start_codegen<'tcx>(
12341234
tcx.ensure_ok().trigger_delayed_bug(def_id);
12351235
}
12361236

1237-
// Don't run this test assertions when not doing codegen. Compiletest tries to build
1238-
// build-fail tests in check mode first and expects it to not give an error in that case.
1239-
if tcx.sess.opts.output_types.should_codegen() {
1240-
rustc_symbol_mangling::test::report_symbol_names(tcx);
1241-
}
1242-
12431237
// Don't do code generation if there were any errors. Likewise if
12441238
// there were any delayed bugs, because codegen will likely cause
12451239
// more ICEs, obscuring the original problem.

compiler/rustc_passes/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rustc_middle = { path = "../rustc_middle" }
2121
rustc_privacy = { path = "../rustc_privacy" }
2222
rustc_session = { path = "../rustc_session" }
2323
rustc_span = { path = "../rustc_span" }
24+
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
2425
rustc_target = { path = "../rustc_target" }
2526
rustc_trait_selection = { path = "../rustc_trait_selection" }
2627
tracing = "0.1"

compiler/rustc_passes/src/check_attr.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ use rustc_trait_selection::traits::ObligationCtxt;
5555
use tracing::debug;
5656

5757
use crate::{errors, fluent_generated as fluent};
58-
58+
const SYMBOL_TARGETS: &'static [Target] = &[
59+
Target::Fn,
60+
Target::Method(MethodKind::TraitImpl),
61+
Target::Method(MethodKind::Inherent),
62+
Target::Method(MethodKind::Trait { body: true }),
63+
Target::ForeignFn,
64+
Target::ForeignStatic,
65+
Target::Impl { of_trait: false },
66+
];
5967
#[derive(LintDiagnostic)]
6068
#[diag(passes_diagnostic_diagnostic_on_unimplemented_only_for_traits)]
6169
struct DiagnosticOnUnimplementedOnlyForTraits;
@@ -222,7 +230,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
222230
},
223231
Attribute::Parsed(AttributeKind::RustcMustImplementOneOf { attr_span, fn_names }) => {
224232
self.check_rustc_must_implement_one_of(*attr_span, fn_names, hir_id,target)
225-
},
233+
}
234+
Attribute::Parsed(AttributeKind::RustcSymbolName(attr_span)) => {
235+
self.check_rustc_symbol_name(*attr_span, hir_id, target)
236+
}
237+
Attribute::Parsed(AttributeKind::RustcDefPath(attr_span)) => {
238+
self.check_rustc_def_path(*attr_span, hir_id, target);
239+
}
226240
Attribute::Parsed(
227241
AttributeKind::EiiExternTarget { .. }
228242
| AttributeKind::EiiExternItem
@@ -306,8 +320,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
306320
| AttributeKind::CfgAttrTrace
307321
| AttributeKind::ThreadLocal
308322
| AttributeKind::CfiEncoding { .. }
309-
| AttributeKind::RustcSymbolName(..)
310-
| AttributeKind::RustcDefPath(..)
311323
) => { /* do nothing */ }
312324
Attribute::Unparsed(attr_item) => {
313325
style = Some(attr_item.style);
@@ -450,6 +462,30 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
450462
self.check_mix_no_mangle_export(hir_id, attrs);
451463
}
452464

465+
fn check_rustc_symbol_name(&self, attr_span: Span, hir_id: HirId, target: Target) {
466+
// Don't run this test assertions when not doing codegen. Compiletest tries to build
467+
// build-fail tests in check mode first and expects it to not give an error in that case.
468+
if self.tcx.sess.opts.output_types.should_codegen() && SYMBOL_TARGETS.contains(&target) {
469+
rustc_symbol_mangling::test::process_symbol_name_attr(
470+
self.tcx,
471+
hir_id.owner.def_id,
472+
attr_span,
473+
);
474+
}
475+
}
476+
477+
fn check_rustc_def_path(&self, attr_span: Span, hir_id: HirId, target: Target) {
478+
// Don't run this test assertions when not doing codegen. Compiletest tries to build
479+
// build-fail tests in check mode first and expects it to not give an error in that case.
480+
if self.tcx.sess.opts.output_types.should_codegen() && SYMBOL_TARGETS.contains(&target) {
481+
rustc_symbol_mangling::test::process_def_path_attr(
482+
self.tcx,
483+
hir_id.owner.def_id,
484+
attr_span,
485+
);
486+
}
487+
}
488+
453489
fn check_rustc_must_implement_one_of(
454490
&self,
455491
attr_span: Span,

compiler/rustc_symbol_mangling/src/test.rs

Lines changed: 32 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,45 @@
44
//! def-path. This is used for unit testing the code that generates
55
//! paths etc in all kinds of annoying scenarios.
66
7-
use rustc_hir::Attribute;
8-
use rustc_hir::attrs::AttributeKind;
97
use rustc_hir::def_id::LocalDefId;
108
use rustc_middle::ty::print::with_no_trimmed_paths;
119
use rustc_middle::ty::{GenericArgs, Instance, TyCtxt};
10+
use rustc_span::Span;
1211

1312
use crate::errors::{Kind, TestOutput};
1413

15-
pub fn report_symbol_names(tcx: TyCtxt<'_>) {
16-
// if the `rustc_attrs` feature is not enabled, then the
17-
// attributes we are interested in cannot be present anyway, so
18-
// skip the walk.
19-
if !tcx.features().rustc_attrs() {
20-
return;
14+
#[inline(always)]
15+
pub fn process_symbol_name_attr<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, attr_span: Span) {
16+
let def_id = def_id.to_def_id();
17+
let instance = Instance::new_raw(
18+
def_id,
19+
tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)),
20+
);
21+
let mangled = tcx.symbol_name(instance);
22+
tcx.dcx().emit_err(TestOutput {
23+
span: attr_span,
24+
kind: Kind::SymbolName,
25+
content: format!("{mangled}"),
26+
});
27+
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
28+
tcx.dcx().emit_err(TestOutput {
29+
span: attr_span,
30+
kind: Kind::Demangling,
31+
content: format!("{demangling}"),
32+
});
33+
tcx.dcx().emit_err(TestOutput {
34+
span: attr_span,
35+
kind: Kind::DemanglingAlt,
36+
content: format!("{demangling:#}"),
37+
});
2138
}
22-
23-
tcx.dep_graph.with_ignore(|| {
24-
let mut symbol_names = SymbolNamesTest { tcx };
25-
let crate_items = tcx.hir_crate_items(());
26-
27-
for id in crate_items.free_items() {
28-
symbol_names.process_attrs(id.owner_id.def_id);
29-
}
30-
31-
for id in crate_items.trait_items() {
32-
symbol_names.process_attrs(id.owner_id.def_id);
33-
}
34-
35-
for id in crate_items.impl_items() {
36-
symbol_names.process_attrs(id.owner_id.def_id);
37-
}
38-
39-
for id in crate_items.foreign_items() {
40-
symbol_names.process_attrs(id.owner_id.def_id);
41-
}
42-
})
4339
}
4440

45-
struct SymbolNamesTest<'tcx> {
46-
tcx: TyCtxt<'tcx>,
47-
}
48-
49-
impl SymbolNamesTest<'_> {
50-
fn process_attrs(&mut self, def_id: LocalDefId) {
51-
let tcx = self.tcx;
52-
// The formatting of `tag({})` is chosen so that tests can elect
53-
// to test the entirety of the string, if they choose, or else just
54-
// some subset.
55-
for attr in tcx
56-
.get_all_attrs(def_id)
57-
.into_iter()
58-
.filter(|attr| matches!(attr, Attribute::Parsed(AttributeKind::RustcSymbolName(..))))
59-
{
60-
let def_id = def_id.to_def_id();
61-
let instance = Instance::new_raw(
62-
def_id,
63-
tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)),
64-
);
65-
let mangled = tcx.symbol_name(instance);
66-
tcx.dcx().emit_err(TestOutput {
67-
span: attr.span(),
68-
kind: Kind::SymbolName,
69-
content: format!("{mangled}"),
70-
});
71-
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
72-
tcx.dcx().emit_err(TestOutput {
73-
span: attr.span(),
74-
kind: Kind::Demangling,
75-
content: format!("{demangling}"),
76-
});
77-
tcx.dcx().emit_err(TestOutput {
78-
span: attr.span(),
79-
kind: Kind::DemanglingAlt,
80-
content: format!("{demangling:#}"),
81-
});
82-
}
83-
}
84-
85-
for attr in tcx
86-
.get_all_attrs(def_id)
87-
.into_iter()
88-
.filter(|attr| matches!(attr, Attribute::Parsed(AttributeKind::RustcDefPath(..))))
89-
{
90-
tcx.dcx().emit_err(TestOutput {
91-
span: attr.span(),
92-
kind: Kind::DefPath,
93-
content: with_no_trimmed_paths!(tcx.def_path_str(def_id)),
94-
});
95-
}
96-
}
41+
#[inline(always)]
42+
pub fn process_def_path_attr<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, attr_span: Span) {
43+
tcx.dcx().emit_err(TestOutput {
44+
span: attr_span,
45+
kind: Kind::DefPath,
46+
content: with_no_trimmed_paths!(tcx.def_path_str(def_id)),
47+
});
9748
}

tests/ui/symbol-names/basic.legacy.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error: def-path(main)
2+
--> $DIR/basic.rs:15:1
3+
|
4+
LL | #[rustc_def_path]
5+
| ^^^^^^^^^^^^^^^^^
6+
17
error: symbol-name(_ZN5basic4main17h1dddcfd03744167fE)
28
--> $DIR/basic.rs:8:1
39
|
@@ -16,11 +22,5 @@ error: demangling-alt(basic::main)
1622
LL | #[rustc_symbol_name]
1723
| ^^^^^^^^^^^^^^^^^^^^
1824

19-
error: def-path(main)
20-
--> $DIR/basic.rs:15:1
21-
|
22-
LL | #[rustc_def_path]
23-
| ^^^^^^^^^^^^^^^^^
24-
2525
error: aborting due to 4 previous errors
2626

tests/ui/symbol-names/basic.v0.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error: def-path(main)
2+
--> $DIR/basic.rs:15:1
3+
|
4+
LL | #[rustc_def_path]
5+
| ^^^^^^^^^^^^^^^^^
6+
17
error: symbol-name(_RNvCsCRATE_HASH_5basic4main)
28
--> $DIR/basic.rs:8:1
39
|
@@ -16,11 +22,5 @@ error: demangling-alt(basic::main)
1622
LL | #[rustc_symbol_name]
1723
| ^^^^^^^^^^^^^^^^^^^^
1824

19-
error: def-path(main)
20-
--> $DIR/basic.rs:15:1
21-
|
22-
LL | #[rustc_def_path]
23-
| ^^^^^^^^^^^^^^^^^
24-
2525
error: aborting due to 4 previous errors
2626

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
error: def-path(<[&dyn Foo<Assoc = for<'a> extern "C" fn(&'a u8, ...)> + AutoTrait; 3] as main::{closure#1}::Bar>::method)
2+
--> $DIR/impl1.rs:69:13
3+
|
4+
LL | #[rustc_def_path]
5+
| ^^^^^^^^^^^^^^^^^
6+
7+
error: symbol-name(_ZN209_$LT$$u5b$$RF$dyn$u20$impl1..Foo$u2b$Assoc$u20$$u3d$$u20$extern$u20$$u22$C$u22$$u20$fn$LP$$RF$u8$C$$u20$...$RP$$u2b$impl1..AutoTrait$u3b$$u20$3$u5d$$u20$as$u20$impl1..main..$u7b$$u7b$closure$u7d$$u7d$..Bar$GT$6method17<SYMBOL_HASH>)
8+
--> $DIR/impl1.rs:62:13
9+
|
10+
LL | #[rustc_symbol_name]
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
13+
error: demangling(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; 3] as impl1::main::{{closure}}::Bar>::method::<SYMBOL_HASH>)
14+
--> $DIR/impl1.rs:62:13
15+
|
16+
LL | #[rustc_symbol_name]
17+
| ^^^^^^^^^^^^^^^^^^^^
18+
19+
error: demangling-alt(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; 3] as impl1::main::{{closure}}::Bar>::method)
20+
--> $DIR/impl1.rs:62:13
21+
|
22+
LL | #[rustc_symbol_name]
23+
| ^^^^^^^^^^^^^^^^^^^^
24+
25+
error: def-path(foo::Foo::bar)
26+
--> $DIR/impl1.rs:21:9
27+
|
28+
LL | #[rustc_def_path]
29+
| ^^^^^^^^^^^^^^^^^
30+
131
error: symbol-name(_ZN5impl13foo3Foo3bar17<SYMBOL_HASH>)
232
--> $DIR/impl1.rs:14:9
333
|
@@ -16,8 +46,8 @@ error: demangling-alt(impl1::foo::Foo::bar)
1646
LL | #[rustc_symbol_name]
1747
| ^^^^^^^^^^^^^^^^^^^^
1848

19-
error: def-path(foo::Foo::bar)
20-
--> $DIR/impl1.rs:21:9
49+
error: def-path(bar::<impl foo::Foo>::baz)
50+
--> $DIR/impl1.rs:39:9
2151
|
2252
LL | #[rustc_def_path]
2353
| ^^^^^^^^^^^^^^^^^
@@ -40,35 +70,5 @@ error: demangling-alt(impl1::bar::<impl impl1::foo::Foo>::baz)
4070
LL | #[rustc_symbol_name]
4171
| ^^^^^^^^^^^^^^^^^^^^
4272

43-
error: def-path(bar::<impl foo::Foo>::baz)
44-
--> $DIR/impl1.rs:39:9
45-
|
46-
LL | #[rustc_def_path]
47-
| ^^^^^^^^^^^^^^^^^
48-
49-
error: symbol-name(_ZN209_$LT$$u5b$$RF$dyn$u20$impl1..Foo$u2b$Assoc$u20$$u3d$$u20$extern$u20$$u22$C$u22$$u20$fn$LP$$RF$u8$C$$u20$...$RP$$u2b$impl1..AutoTrait$u3b$$u20$3$u5d$$u20$as$u20$impl1..main..$u7b$$u7b$closure$u7d$$u7d$..Bar$GT$6method17<SYMBOL_HASH>)
50-
--> $DIR/impl1.rs:62:13
51-
|
52-
LL | #[rustc_symbol_name]
53-
| ^^^^^^^^^^^^^^^^^^^^
54-
55-
error: demangling(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; 3] as impl1::main::{{closure}}::Bar>::method::<SYMBOL_HASH>)
56-
--> $DIR/impl1.rs:62:13
57-
|
58-
LL | #[rustc_symbol_name]
59-
| ^^^^^^^^^^^^^^^^^^^^
60-
61-
error: demangling-alt(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; 3] as impl1::main::{{closure}}::Bar>::method)
62-
--> $DIR/impl1.rs:62:13
63-
|
64-
LL | #[rustc_symbol_name]
65-
| ^^^^^^^^^^^^^^^^^^^^
66-
67-
error: def-path(<[&dyn Foo<Assoc = for<'a> extern "C" fn(&'a u8, ...)> + AutoTrait; 3] as main::{closure#1}::Bar>::method)
68-
--> $DIR/impl1.rs:69:13
69-
|
70-
LL | #[rustc_def_path]
71-
| ^^^^^^^^^^^^^^^^^
72-
7373
error: aborting due to 12 previous errors
7474

0 commit comments

Comments
 (0)