Skip to content

Commit 3162956

Browse files
Auto merge of #150949 - Bryntet:port_symbol_mangler_attrs, r=<try>
Port symbol mangler attrs
2 parents 9bc8b40 + beed1d6 commit 3162956

13 files changed

Lines changed: 206 additions & 161 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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,47 @@ impl<S: Stage> SingleAttributeParser<S> for RustcScalableVectorParser {
305305
Some(AttributeKind::RustcScalableVector { element_count: Some(n), span: cx.attr_span })
306306
}
307307
}
308+
309+
const SYMBOL_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
310+
Allow(Target::Fn),
311+
Allow(Target::Method(MethodKind::TraitImpl)),
312+
Allow(Target::Method(MethodKind::Inherent)),
313+
Allow(Target::Method(MethodKind::Trait { body: true })),
314+
Allow(Target::ForeignFn),
315+
Allow(Target::ForeignStatic),
316+
Allow(Target::Impl { of_trait: false }),
317+
]);
318+
319+
pub(crate) struct RustcSymbolName;
320+
321+
impl<S: Stage> SingleAttributeParser<S> for RustcSymbolName {
322+
const ALLOWED_TARGETS: AllowedTargets = SYMBOL_TARGETS;
323+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
324+
const PATH: &[Symbol] = &[sym::rustc_symbol_name];
325+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
326+
const TEMPLATE: AttributeTemplate = template!(Word);
327+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
328+
if let Err(span) = args.no_args() {
329+
cx.expected_no_args(span);
330+
return None;
331+
}
332+
Some(AttributeKind::RustcSymbolName(cx.attr_span))
333+
}
334+
}
335+
336+
pub(crate) struct RustcDefPath;
337+
338+
impl<S: Stage> SingleAttributeParser<S> for RustcDefPath {
339+
const ALLOWED_TARGETS: AllowedTargets = SYMBOL_TARGETS;
340+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
341+
const PATH: &[Symbol] = &[sym::rustc_def_path];
342+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
343+
const TEMPLATE: AttributeTemplate = template!(Word);
344+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
345+
if let Err(span) = args.no_args() {
346+
cx.expected_no_args(span);
347+
return None;
348+
}
349+
Some(AttributeKind::RustcDefPath(cx.attr_span))
350+
}
351+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ use crate::attributes::proc_macro_attrs::{
6262
use crate::attributes::prototype::CustomMirParser;
6363
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
6464
use crate::attributes::rustc_internal::{
65-
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
65+
RustcDefPath, RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
6666
RustcLegacyConstGenericsParser, RustcLintDiagnosticsParser, RustcLintOptDenyFieldAccessParser,
6767
RustcLintOptTyParser, RustcLintQueryInstabilityParser,
6868
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcMustImplementOneOfParser,
6969
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
7070
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
71-
RustcSimdMonomorphizeLaneLimitParser,
71+
RustcSimdMonomorphizeLaneLimitParser, RustcSymbolName,
7272
};
7373
use crate::attributes::semantics::MayDangleParser;
7474
use crate::attributes::stability::{
@@ -215,6 +215,7 @@ attribute_parsers!(
215215
Single<ProcMacroDeriveParser>,
216216
Single<RecursionLimitParser>,
217217
Single<RustcBuiltinMacroParser>,
218+
Single<RustcDefPath>,
218219
Single<RustcForceInlineParser>,
219220
Single<RustcLayoutScalarValidRangeEndParser>,
220221
Single<RustcLayoutScalarValidRangeStartParser>,
@@ -224,6 +225,7 @@ attribute_parsers!(
224225
Single<RustcObjectLifetimeDefaultParser>,
225226
Single<RustcScalableVectorParser>,
226227
Single<RustcSimdMonomorphizeLaneLimitParser>,
228+
Single<RustcSymbolName>,
227229
Single<SanitizeParser>,
228230
Single<ShouldPanicParser>,
229231
Single<SkipDuringMethodDispatchParser>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,9 @@ pub enum AttributeKind {
880880
/// Represents `#[rustc_coherence_is_core]`
881881
RustcCoherenceIsCore(Span),
882882

883+
/// Represents `#[rustc_def_path]`
884+
RustcDefPath(Span),
885+
883886
/// Represents `#[rustc_layout_scalar_valid_range_end]`.
884887
RustcLayoutScalarValidRangeEnd(Box<u128>, Span),
885888

@@ -936,6 +939,9 @@ pub enum AttributeKind {
936939
/// Represents `#[rustc_simd_monomorphize_lane_limit = "N"]`.
937940
RustcSimdMonomorphizeLaneLimit(Limit),
938941

942+
/// Represents `#[rustc_symbol_name]`
943+
RustcSymbolName(Span),
944+
939945
/// Represents `#[sanitize]`
940946
///
941947
/// the on set and off set are distjoint since there's a third option: unset.

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl AttributeKind {
9595
Repr { .. } => No,
9696
RustcBuiltinMacro { .. } => Yes,
9797
RustcCoherenceIsCore(..) => No,
98+
RustcDefPath(..) => No,
9899
RustcLayoutScalarValidRangeEnd(..) => Yes,
99100
RustcLayoutScalarValidRangeStart(..) => Yes,
100101
RustcLegacyConstGenerics { .. } => Yes,
@@ -112,6 +113,7 @@ impl AttributeKind {
112113
RustcScalableVector { .. } => Yes,
113114
RustcShouldNotBeCalledOnConstItems(..) => Yes,
114115
RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate
116+
RustcSymbolName(..) => Yes,
115117
Sanitize { .. } => No,
116118
ShouldPanic { .. } => No,
117119
SkipDuringMethodDispatch { .. } => No,

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 & 2 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
@@ -448,6 +462,30 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
448462
self.check_mix_no_mangle_export(hir_id, attrs);
449463
}
450464

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+
451489
fn check_rustc_must_implement_one_of(
452490
&self,
453491
attr_span: Span,

compiler/rustc_symbol_mangling/src/test.rs

Lines changed: 32 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,85 +7,42 @@
77
use rustc_hir::def_id::LocalDefId;
88
use rustc_middle::ty::print::with_no_trimmed_paths;
99
use rustc_middle::ty::{GenericArgs, Instance, TyCtxt};
10-
use rustc_span::{Symbol, sym};
10+
use rustc_span::Span;
1111

1212
use crate::errors::{Kind, TestOutput};
1313

14-
const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
15-
const DEF_PATH: Symbol = sym::rustc_def_path;
16-
17-
pub fn report_symbol_names(tcx: TyCtxt<'_>) {
18-
// if the `rustc_attrs` feature is not enabled, then the
19-
// attributes we are interested in cannot be present anyway, so
20-
// skip the walk.
21-
if !tcx.features().rustc_attrs() {
22-
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+
});
2338
}
24-
25-
tcx.dep_graph.with_ignore(|| {
26-
let mut symbol_names = SymbolNamesTest { tcx };
27-
let crate_items = tcx.hir_crate_items(());
28-
29-
for id in crate_items.free_items() {
30-
symbol_names.process_attrs(id.owner_id.def_id);
31-
}
32-
33-
for id in crate_items.trait_items() {
34-
symbol_names.process_attrs(id.owner_id.def_id);
35-
}
36-
37-
for id in crate_items.impl_items() {
38-
symbol_names.process_attrs(id.owner_id.def_id);
39-
}
40-
41-
for id in crate_items.foreign_items() {
42-
symbol_names.process_attrs(id.owner_id.def_id);
43-
}
44-
})
45-
}
46-
47-
struct SymbolNamesTest<'tcx> {
48-
tcx: TyCtxt<'tcx>,
4939
}
5040

51-
impl SymbolNamesTest<'_> {
52-
fn process_attrs(&mut self, def_id: LocalDefId) {
53-
let tcx = self.tcx;
54-
// The formatting of `tag({})` is chosen so that tests can elect
55-
// to test the entirety of the string, if they choose, or else just
56-
// some subset.
57-
for attr in tcx.get_attrs(def_id, SYMBOL_NAME) {
58-
let def_id = def_id.to_def_id();
59-
let instance = Instance::new_raw(
60-
def_id,
61-
tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)),
62-
);
63-
let mangled = tcx.symbol_name(instance);
64-
tcx.dcx().emit_err(TestOutput {
65-
span: attr.span(),
66-
kind: Kind::SymbolName,
67-
content: format!("{mangled}"),
68-
});
69-
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
70-
tcx.dcx().emit_err(TestOutput {
71-
span: attr.span(),
72-
kind: Kind::Demangling,
73-
content: format!("{demangling}"),
74-
});
75-
tcx.dcx().emit_err(TestOutput {
76-
span: attr.span(),
77-
kind: Kind::DemanglingAlt,
78-
content: format!("{demangling:#}"),
79-
});
80-
}
81-
}
82-
83-
for attr in tcx.get_attrs(def_id, DEF_PATH) {
84-
tcx.dcx().emit_err(TestOutput {
85-
span: attr.span(),
86-
kind: Kind::DefPath,
87-
content: with_no_trimmed_paths!(tcx.def_path_str(def_id)),
88-
});
89-
}
90-
}
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+
});
9148
}

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

0 commit comments

Comments
 (0)