Skip to content

Commit d8cd0a4

Browse files
committed
Try to evaluate constants in legacy mangling
1 parent 6a9899c commit d8cd0a4

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

compiler/rustc_symbol_mangling/src/legacy.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use std::fmt::{self, Write};
22
use std::mem::{self, discriminant};
33

44
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
5-
use rustc_hir::def_id::CrateNum;
5+
use rustc_hir::def_id::{CrateNum, DefId};
66
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
77
use rustc_middle::bug;
88
use rustc_middle::ty::print::{PrettyPrinter, Print, PrintError, Printer};
99
use rustc_middle::ty::{
1010
self, GenericArg, GenericArgKind, Instance, ReifyReason, Ty, TyCtxt, TypeVisitableExt,
11+
TypingEnv,
1112
};
1213
use tracing::debug;
1314

@@ -54,7 +55,12 @@ pub(super) fn mangle<'tcx>(
5455

5556
let hash = get_symbol_hash(tcx, instance, instance_ty, instantiating_crate);
5657

57-
let mut printer = SymbolPrinter { tcx, path: SymbolPath::new(), keep_within_component: false };
58+
let mut printer = SymbolPrinter {
59+
tcx,
60+
path: SymbolPath::new(),
61+
keep_within_component: false,
62+
instance_def_id: ty_def_id,
63+
};
5864
printer
5965
.print_def_path(
6066
def_id,
@@ -204,6 +210,10 @@ struct SymbolPrinter<'tcx> {
204210
tcx: TyCtxt<'tcx>,
205211
path: SymbolPath,
206212

213+
// The item currently being printed. Used to obtain a `TypingEnv`
214+
// for evaluating constants in the signature.
215+
instance_def_id: DefId,
216+
207217
// When `true`, `finalize_pending_component` isn't used.
208218
// This is needed when recursing into `path_qualified`,
209219
// or `path_generic_args`, as any nested paths are
@@ -239,6 +249,13 @@ impl<'tcx> Printer<'tcx> for SymbolPrinter<'tcx> {
239249
self.write_str("[")?;
240250
self.print_type(ty)?;
241251
self.write_str("; ")?;
252+
let size = self
253+
.tcx
254+
.try_normalize_erasing_regions(
255+
TypingEnv::non_body_analysis(self.tcx, self.instance_def_id),
256+
size,
257+
)
258+
.unwrap_or(size);
242259
if let Some(size) = size.try_to_target_usize(self.tcx()) {
243260
write!(self, "{size}")?
244261
} else if let ty::ConstKind::Param(param) = size.kind() {
@@ -272,6 +289,13 @@ impl<'tcx> Printer<'tcx> for SymbolPrinter<'tcx> {
272289
}
273290

274291
fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
292+
let ct = self
293+
.tcx
294+
.try_normalize_erasing_regions(
295+
TypingEnv::non_body_analysis(self.tcx, self.instance_def_id),
296+
ct,
297+
)
298+
.unwrap_or(ct);
275299
// only print integers
276300
match ct.kind() {
277301
ty::ConstKind::Value(ty, ty::ValTree::Leaf(scalar)) if ty.is_integral() => {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -502,19 +502,19 @@ error: demangling-alt(a::b::Type<[T; N]>)
502502
LL | #[rustc_symbol_name]
503503
| ^^^^^^^^^^^^^^^^^^^^
504504

505-
error: symbol-name(_ZN1a1b35Type$LT$$u5b$u8$u3b$$u20$_$u5d$$GT$17h[HASH]E)
505+
error: symbol-name(_ZN1a1b35Type$LT$$u5b$u8$u3b$$u20$0$u5d$$GT$17h[HASH]E)
506506
--> $DIR/types.rs:272:5
507507
|
508508
LL | #[rustc_symbol_name]
509509
| ^^^^^^^^^^^^^^^^^^^^
510510

511-
error: demangling(a::b::Type<[u8; _]>::h[HASH])
511+
error: demangling(a::b::Type<[u8; 0]>::h[HASH])
512512
--> $DIR/types.rs:272:5
513513
|
514514
LL | #[rustc_symbol_name]
515515
| ^^^^^^^^^^^^^^^^^^^^
516516

517-
error: demangling-alt(a::b::Type<[u8; _]>)
517+
error: demangling-alt(a::b::Type<[u8; 0]>)
518518
--> $DIR/types.rs:272:5
519519
|
520520
LL | #[rustc_symbol_name]

tests/ui/symbol-names/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ pub fn b() {
270270
const ZERO: usize = 0;
271271

272272
#[rustc_symbol_name]
273-
//[legacy,verbose-legacy]~^ ERROR symbol-name(_ZN1a1b35Type$LT$$u5b$u8$u3b$$u20$_$u5d$$GT$
274-
//[legacy,verbose-legacy]~| ERROR demangling(a::b::Type<[u8; _]>::
275-
//[legacy,verbose-legacy]~| ERROR demangling-alt(a::b::Type<[u8; _]>)
273+
//[legacy,verbose-legacy]~^ ERROR symbol-name(_ZN1a1b35Type$LT$$u5b$u8$u3b$$u20$0$u5d$$GT$
274+
//[legacy,verbose-legacy]~| ERROR demangling(a::b::Type<[u8; 0]>::
275+
//[legacy,verbose-legacy]~| ERROR demangling-alt(a::b::Type<[u8; 0]>)
276276
//[v0]~^^^^ ERROR symbol-name(_RMsq_NvCsCRATE_HASH_1a1bINtB<REF>_4TypeAhj0_E)
277277
//[v0]~| ERROR ::b::Type<[u8; 0usize]>>)
278278
//[v0]~| ERROR demangling-alt(<a::b::Type<[u8; 0]>>)

tests/ui/symbol-names/types.verbose-legacy.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -502,19 +502,19 @@ error: demangling-alt(a::b::Type<[T; N]>)
502502
LL | #[rustc_symbol_name]
503503
| ^^^^^^^^^^^^^^^^^^^^
504504

505-
error: symbol-name(_ZN1a1b35Type$LT$$u5b$u8$u3b$$u20$_$u5d$$GT$17h[HASH]E)
505+
error: symbol-name(_ZN1a1b35Type$LT$$u5b$u8$u3b$$u20$0$u5d$$GT$17h[HASH]E)
506506
--> $DIR/types.rs:272:5
507507
|
508508
LL | #[rustc_symbol_name]
509509
| ^^^^^^^^^^^^^^^^^^^^
510510

511-
error: demangling(a::b::Type<[u8; _]>::h[HASH])
511+
error: demangling(a::b::Type<[u8; 0]>::h[HASH])
512512
--> $DIR/types.rs:272:5
513513
|
514514
LL | #[rustc_symbol_name]
515515
| ^^^^^^^^^^^^^^^^^^^^
516516

517-
error: demangling-alt(a::b::Type<[u8; _]>)
517+
error: demangling-alt(a::b::Type<[u8; 0]>)
518518
--> $DIR/types.rs:272:5
519519
|
520520
LL | #[rustc_symbol_name]

0 commit comments

Comments
 (0)