Skip to content

Commit cdc764e

Browse files
committed
remove unnecessary iteration over almost all items in the dep graph
instead just check and emit this attribute error through `check_attr.rs`
1 parent 6efa357 commit cdc764e

6 files changed

Lines changed: 70 additions & 106 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,6 +4392,7 @@ dependencies = [
43924392
name = "rustc_passes"
43934393
version = "0.0.0"
43944394
dependencies = [
4395+
"rustc-demangle",
43954396
"rustc_abi",
43964397
"rustc_ast",
43974398
"rustc_ast_lowering",
@@ -4407,6 +4408,7 @@ dependencies = [
44074408
"rustc_privacy",
44084409
"rustc_session",
44094410
"rustc_span",
4411+
"rustc_symbol_mangling",
44104412
"rustc_target",
44114413
"rustc_trait_selection",
44124414
"tracing",

compiler/rustc_interface/src/passes.rs

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

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

compiler/rustc_passes/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
rustc-demangle = "0.1.27"
89
rustc_abi = { path = "../rustc_abi" }
910
rustc_ast = { path = "../rustc_ast" }
1011
rustc_ast_lowering = { path = "../rustc_ast_lowering" }
@@ -20,6 +21,7 @@ rustc_middle = { path = "../rustc_middle" }
2021
rustc_privacy = { path = "../rustc_privacy" }
2122
rustc_session = { path = "../rustc_session" }
2223
rustc_span = { path = "../rustc_span" }
24+
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
2325
rustc_target = { path = "../rustc_target" }
2426
rustc_trait_selection = { path = "../rustc_trait_selection" }
2527
tracing = "0.1"

compiler/rustc_passes/src/check_attr.rs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
3838
use rustc_middle::query::Providers;
3939
use rustc_middle::traits::ObligationCause;
4040
use rustc_middle::ty::error::{ExpectedFound, TypeError};
41-
use rustc_middle::ty::{self, TyCtxt, TypingMode};
41+
use rustc_middle::ty::print::with_no_trimmed_paths;
42+
use rustc_middle::ty::{self, GenericArgs, TyCtxt, TypingMode};
4243
use rustc_middle::{bug, span_bug};
4344
use rustc_session::config::CrateType;
4445
use rustc_session::lint;
@@ -231,6 +232,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
231232
self.check_rustc_must_implement_one_of(*attr_span, fn_names, hir_id,target)
232233
},
233234
Attribute::Parsed(AttributeKind::DoNotRecommend{attr_span}) => {self.check_do_not_recommend(*attr_span, hir_id, target, item)},
235+
Attribute::Parsed(AttributeKind::RustcSymbolName(attr_span)) => {
236+
self.check_rustc_symbol_name(*attr_span, hir_id, target)
237+
}
238+
Attribute::Parsed(AttributeKind::RustcDefPath(attr_span)) => {
239+
self.check_rustc_def_path(*attr_span, hir_id, target);
240+
}
234241
Attribute::Parsed(
235242
// tidy-alphabetical-start
236243
AttributeKind::RustcAllowIncoherentImpl(..)
@@ -300,7 +307,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
300307
| AttributeKind::RustcConfusables { .. }
301308
| AttributeKind::RustcConstStabilityIndirect
302309
| AttributeKind::RustcDeallocator
303-
| AttributeKind::RustcDefPath(..)
304310
| AttributeKind::RustcDelayedBugFromInsideQuery
305311
| AttributeKind::RustcDenyExplicitImpl(..)
306312
| AttributeKind::RustcDummy
@@ -347,7 +353,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
347353
| AttributeKind::RustcSkipDuringMethodDispatch { .. }
348354
| AttributeKind::RustcSpecializationTrait(..)
349355
| AttributeKind::RustcStdInternalSymbol (..)
350-
| AttributeKind::RustcSymbolName(..)
351356
| AttributeKind::RustcThenThisWouldNeed(..)
352357
| AttributeKind::RustcUnsafeSpecializationMarker(..)
353358
| AttributeKind::RustcVariance
@@ -507,6 +512,64 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
507512
self.check_mix_no_mangle_export(hir_id, attrs);
508513
}
509514

515+
fn is_valid_symbol_attr(&self, target: &Target) -> bool {
516+
self.tcx.sess.opts.output_types.should_codegen()
517+
&& [
518+
Target::Fn,
519+
Target::Method(MethodKind::TraitImpl),
520+
Target::Method(MethodKind::Inherent),
521+
Target::Method(MethodKind::Trait { body: true }),
522+
Target::ForeignFn,
523+
Target::ForeignStatic,
524+
Target::Impl { of_trait: false },
525+
]
526+
.contains(target)
527+
}
528+
529+
fn check_rustc_symbol_name(&self, attr_span: Span, hir_id: HirId, target: Target) {
530+
use rustc_symbol_mangling::errors::{Kind, TestOutput};
531+
let tcx = self.tcx;
532+
if !self.is_valid_symbol_attr(&target) {
533+
return;
534+
}
535+
let def_id = hir_id.owner.def_id.to_def_id();
536+
let instance = ty::Instance::new_raw(
537+
def_id,
538+
tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)),
539+
);
540+
let mangled = tcx.symbol_name(instance);
541+
tcx.dcx().emit_err(TestOutput {
542+
span: attr_span,
543+
kind: Kind::SymbolName,
544+
content: format!("{mangled}"),
545+
});
546+
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
547+
tcx.dcx().emit_err(TestOutput {
548+
span: attr_span,
549+
kind: Kind::Demangling,
550+
content: format!("{demangling}"),
551+
});
552+
tcx.dcx().emit_err(TestOutput {
553+
span: attr_span,
554+
kind: Kind::DemanglingAlt,
555+
content: format!("{demangling:#}"),
556+
});
557+
}
558+
}
559+
560+
fn check_rustc_def_path(&self, attr_span: Span, hir_id: HirId, target: Target) {
561+
use rustc_symbol_mangling::errors::{Kind, TestOutput};
562+
let tcx = self.tcx;
563+
if !self.is_valid_symbol_attr(&target) {
564+
return;
565+
}
566+
tcx.dcx().emit_err(TestOutput {
567+
span: attr_span,
568+
kind: Kind::DefPath,
569+
content: with_no_trimmed_paths!(tcx.def_path_str(hir_id.owner.def_id)),
570+
});
571+
}
572+
510573
fn check_rustc_must_implement_one_of(
511574
&self,
512575
attr_span: Span,

compiler/rustc_symbol_mangling/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ mod legacy;
106106
mod v0;
107107

108108
pub mod errors;
109-
pub mod test;
110109

111110
pub use v0::mangle_internal_symbol;
112111

compiler/rustc_symbol_mangling/src/test.rs

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)