Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4392,6 +4392,7 @@ dependencies = [
name = "rustc_passes"
version = "0.0.0"
dependencies = [
"rustc-demangle",
"rustc_abi",
"rustc_ast",
"rustc_ast_lowering",
Expand All @@ -4407,6 +4408,7 @@ dependencies = [
"rustc_privacy",
"rustc_session",
"rustc_span",
"rustc_symbol_mangling",
"rustc_target",
"rustc_trait_selection",
"tracing",
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,12 +1232,6 @@ pub(crate) fn start_codegen<'tcx>(
tcx.ensure_ok().trigger_delayed_bug(def_id);
}

// Don't run this test assertions when not doing codegen. Compiletest tries to build
// build-fail tests in check mode first and expects it to not give an error in that case.
if tcx.sess.opts.output_types.should_codegen() {
rustc_symbol_mangling::test::report_symbol_names(tcx);
}

// Don't do code generation if there were any errors. Likewise if
// there were any delayed bugs, because codegen will likely cause
// more ICEs, obscuring the original problem.
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_passes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2024"

[dependencies]
# tidy-alphabetical-start
rustc-demangle = "0.1.27"
rustc_abi = { path = "../rustc_abi" }
rustc_ast = { path = "../rustc_ast" }
rustc_ast_lowering = { path = "../rustc_ast_lowering" }
Expand All @@ -20,6 +21,7 @@ rustc_middle = { path = "../rustc_middle" }
rustc_privacy = { path = "../rustc_privacy" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
rustc_target = { path = "../rustc_target" }
rustc_trait_selection = { path = "../rustc_trait_selection" }
tracing = "0.1"
Expand Down
69 changes: 66 additions & 3 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
use rustc_middle::query::Providers;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, TyCtxt, TypingMode};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, GenericArgs, TyCtxt, TypingMode};
use rustc_middle::{bug, span_bug};
use rustc_session::config::CrateType;
use rustc_session::lint;
Expand Down Expand Up @@ -231,6 +232,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
self.check_rustc_must_implement_one_of(*attr_span, fn_names, hir_id,target)
},
Attribute::Parsed(AttributeKind::DoNotRecommend{attr_span}) => {self.check_do_not_recommend(*attr_span, hir_id, target, item)},
Attribute::Parsed(AttributeKind::RustcSymbolName(attr_span)) => {
self.check_rustc_symbol_name(*attr_span, hir_id, target)
}
Attribute::Parsed(AttributeKind::RustcDefPath(attr_span)) => {
self.check_rustc_def_path(*attr_span, hir_id, target);
}
Attribute::Parsed(
// tidy-alphabetical-start
AttributeKind::RustcAllowIncoherentImpl(..)
Expand Down Expand Up @@ -300,7 +307,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::RustcConfusables { .. }
| AttributeKind::RustcConstStabilityIndirect
| AttributeKind::RustcDeallocator
| AttributeKind::RustcDefPath(..)
| AttributeKind::RustcDelayedBugFromInsideQuery
| AttributeKind::RustcDenyExplicitImpl(..)
| AttributeKind::RustcDummy
Expand Down Expand Up @@ -347,7 +353,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::RustcSkipDuringMethodDispatch { .. }
| AttributeKind::RustcSpecializationTrait(..)
| AttributeKind::RustcStdInternalSymbol (..)
| AttributeKind::RustcSymbolName(..)
| AttributeKind::RustcThenThisWouldNeed(..)
| AttributeKind::RustcUnsafeSpecializationMarker(..)
| AttributeKind::RustcVariance
Expand Down Expand Up @@ -507,6 +512,64 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
self.check_mix_no_mangle_export(hir_id, attrs);
}

fn is_valid_symbol_attr(&self, target: &Target) -> bool {
self.tcx.sess.opts.output_types.should_codegen()
&& [
Target::Fn,
Target::Method(MethodKind::TraitImpl),
Target::Method(MethodKind::Inherent),
Target::Method(MethodKind::Trait { body: true }),
Target::ForeignFn,
Target::ForeignStatic,
Target::Impl { of_trait: false },
]
.contains(target)
}

fn check_rustc_symbol_name(&self, attr_span: Span, hir_id: HirId, target: Target) {
use rustc_symbol_mangling::errors::{Kind, TestOutput};
let tcx = self.tcx;
if !self.is_valid_symbol_attr(&target) {
return;
}
let def_id = hir_id.owner.def_id.to_def_id();
let instance = ty::Instance::new_raw(
def_id,
tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)),
);
let mangled = tcx.symbol_name(instance);
tcx.dcx().emit_err(TestOutput {
span: attr_span,
kind: Kind::SymbolName,
content: format!("{mangled}"),
});
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
tcx.dcx().emit_err(TestOutput {
span: attr_span,
kind: Kind::Demangling,
content: format!("{demangling}"),
});
tcx.dcx().emit_err(TestOutput {
span: attr_span,
kind: Kind::DemanglingAlt,
content: format!("{demangling:#}"),
});
}
}

fn check_rustc_def_path(&self, attr_span: Span, hir_id: HirId, target: Target) {
use rustc_symbol_mangling::errors::{Kind, TestOutput};
let tcx = self.tcx;
if !self.is_valid_symbol_attr(&target) {
return;
}
tcx.dcx().emit_err(TestOutput {
span: attr_span,
kind: Kind::DefPath,
content: with_no_trimmed_paths!(tcx.def_path_str(hir_id.owner.def_id)),
});
}

fn check_rustc_must_implement_one_of(
&self,
attr_span: Span,
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_symbol_mangling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ mod legacy;
mod v0;

pub mod errors;
pub mod test;

pub use v0::mangle_internal_symbol;

Expand Down
96 changes: 0 additions & 96 deletions compiler/rustc_symbol_mangling/src/test.rs

This file was deleted.

48 changes: 24 additions & 24 deletions tests/ui/symbol-names/impl1.legacy.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
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>)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; 3] as impl1::main::{{closure}}::Bar>::method::<SYMBOL_HASH>)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling-alt(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; 3] as impl1::main::{{closure}}::Bar>::method)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: def-path(<[&dyn Foo<Assoc = for<'a> extern "C" fn(&'a u8, ...)> + AutoTrait; 3] as main::{closure#1}::Bar>::method)
--> $DIR/impl1.rs:69:13
|
LL | #[rustc_def_path]
| ^^^^^^^^^^^^^^^^^

error: symbol-name(_ZN5impl13foo3Foo3bar17<SYMBOL_HASH>)
--> $DIR/impl1.rs:14:9
|
Expand Down Expand Up @@ -46,29 +70,5 @@ error: def-path(bar::<impl foo::Foo>::baz)
LL | #[rustc_def_path]
| ^^^^^^^^^^^^^^^^^

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>)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; 3] as impl1::main::{{closure}}::Bar>::method::<SYMBOL_HASH>)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling-alt(<[&dyn impl1::Foo+Assoc = extern "C" fn(&u8, ::.)+impl1::AutoTrait; 3] as impl1::main::{{closure}}::Bar>::method)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: def-path(<[&dyn Foo<Assoc = for<'a> extern "C" fn(&'a u8, ...)> + AutoTrait; 3] as main::{closure#1}::Bar>::method)
--> $DIR/impl1.rs:69:13
|
LL | #[rustc_def_path]
| ^^^^^^^^^^^^^^^^^

error: aborting due to 12 previous errors

48 changes: 24 additions & 24 deletions tests/ui/symbol-names/impl1.v0.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
error: symbol-name(_RNvXNCNvCsCRATE_HASH_5impl14mains_0ARDNtB<REF>_3Foop5AssocFG_KCRL0_hvEuNtB<REF>_9AutoTraitEL_j3_NtB<REF>_3Bar6method)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling(<[&dyn impl1[d5591eb39db23cbb]::Foo<Assoc = for<'a> extern "C" fn(&'a u8, ...)> + impl1[d5591eb39db23cbb]::AutoTrait; 3usize] as impl1[d5591eb39db23cbb]::main::{closure#1}::Bar>::method)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling-alt(<[&dyn impl1::Foo<Assoc = for<'a> extern "C" fn(&'a u8, ...)> + impl1::AutoTrait; 3] as impl1::main::{closure#1}::Bar>::method)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: def-path(<[&dyn Foo<Assoc = for<'a> extern "C" fn(&'a u8, ...)> + AutoTrait; 3] as main::{closure#1}::Bar>::method)
--> $DIR/impl1.rs:69:13
|
LL | #[rustc_def_path]
| ^^^^^^^^^^^^^^^^^

error: symbol-name(_RNvMNtCsCRATE_HASH_5impl13fooNtB<REF>_3Foo3bar)
--> $DIR/impl1.rs:14:9
|
Expand Down Expand Up @@ -46,29 +70,5 @@ error: def-path(bar::<impl foo::Foo>::baz)
LL | #[rustc_def_path]
| ^^^^^^^^^^^^^^^^^

error: symbol-name(_RNvXNCNvCsCRATE_HASH_5impl14mains_0ARDNtB<REF>_3Foop5AssocFG_KCRL0_hvEuNtB<REF>_9AutoTraitEL_j3_NtB<REF>_3Bar6method)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling(<[&dyn impl1[d5591eb39db23cbb]::Foo<Assoc = for<'a> extern "C" fn(&'a u8, ...)> + impl1[d5591eb39db23cbb]::AutoTrait; 3usize] as impl1[d5591eb39db23cbb]::main::{closure#1}::Bar>::method)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling-alt(<[&dyn impl1::Foo<Assoc = for<'a> extern "C" fn(&'a u8, ...)> + impl1::AutoTrait; 3] as impl1::main::{closure#1}::Bar>::method)
--> $DIR/impl1.rs:62:13
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: def-path(<[&dyn Foo<Assoc = for<'a> extern "C" fn(&'a u8, ...)> + AutoTrait; 3] as main::{closure#1}::Bar>::method)
--> $DIR/impl1.rs:69:13
|
LL | #[rustc_def_path]
| ^^^^^^^^^^^^^^^^^

error: aborting due to 12 previous errors

Loading