Skip to content

Commit 9752787

Browse files
committed
mangling: non-monomorphic #[rustc_symbol_name]
This commit adjust `#[rustc_symbol_name]` so that it can be applied to non-monomorphic functions without producing an ICE. Signed-off-by: David Wood <[email protected]>
1 parent 5565241 commit 9752787

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

compiler/rustc_middle/src/ty/instance.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,17 @@ impl<'tcx> Instance<'tcx> {
291291
}
292292

293293
pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
294-
Instance::new(def_id, tcx.empty_substs_for_def_id(def_id))
294+
let substs = InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind {
295+
ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
296+
ty::GenericParamDefKind::Type { .. } => {
297+
bug!("Instance::mono: {:?} has type parameters", def_id)
298+
}
299+
ty::GenericParamDefKind::Const { .. } => {
300+
bug!("Instance::mono: {:?} has const parameters", def_id)
301+
}
302+
});
303+
304+
Instance::new(def_id, substs)
295305
}
296306

297307
#[inline]

compiler/rustc_middle/src/ty/util.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use crate::mir::interpret::{sign_extend, truncate};
66
use crate::ty::fold::TypeFolder;
77
use crate::ty::layout::IntegerExt;
88
use crate::ty::query::TyCtxtAt;
9-
use crate::ty::subst::{GenericArgKind, InternalSubsts, Subst, SubstsRef};
9+
use crate::ty::subst::{GenericArgKind, Subst, SubstsRef};
1010
use crate::ty::TyKind::*;
11-
use crate::ty::{self, DefIdTree, GenericParamDefKind, List, Ty, TyCtxt, TypeFoldable};
11+
use crate::ty::{self, DefIdTree, List, Ty, TyCtxt, TypeFoldable};
1212
use rustc_apfloat::Float as _;
1313
use rustc_ast as ast;
1414
use rustc_attr::{self as attr, SignedInt, UnsignedInt};
@@ -509,20 +509,6 @@ impl<'tcx> TyCtxt<'tcx> {
509509
Some(ty::Binder::bind(env_ty))
510510
}
511511

512-
/// Given the `DefId` of some item that has no type or const parameters, make
513-
/// a suitable "empty substs" for it.
514-
pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> SubstsRef<'tcx> {
515-
InternalSubsts::for_item(self, item_def_id, |param, _| match param.kind {
516-
GenericParamDefKind::Lifetime => self.lifetimes.re_erased.into(),
517-
GenericParamDefKind::Type { .. } => {
518-
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
519-
}
520-
GenericParamDefKind::Const { .. } => {
521-
bug!("empty_substs_for_def_id: {:?} has const parameters", item_def_id)
522-
}
523-
})
524-
}
525-
526512
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
527513
pub fn is_static(self, def_id: DefId) -> bool {
528514
self.static_mutability(def_id).is_some()

compiler/rustc_symbol_mangling/src/legacy.rs

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ fn get_symbol_hash<'tcx>(
115115
}
116116

117117
// also include any type parameters (for generic items)
118-
assert!(!substs.has_erasable_regions());
119118
substs.hash_stable(&mut hcx, &mut hasher);
120119

121120
if let Some(instantiating_crate) = instantiating_crate {

compiler/rustc_symbol_mangling/src/test.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use rustc_hir as hir;
88
use rustc_middle::ty::print::with_no_trimmed_paths;
9-
use rustc_middle::ty::{Instance, TyCtxt};
9+
use rustc_middle::ty::{subst::InternalSubsts, Instance, TyCtxt};
1010
use rustc_span::symbol::{sym, Symbol};
1111

1212
const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
@@ -36,8 +36,11 @@ impl SymbolNamesTest<'tcx> {
3636
let def_id = tcx.hir().local_def_id(hir_id);
3737
for attr in tcx.get_attrs(def_id.to_def_id()).iter() {
3838
if tcx.sess.check_name(attr, SYMBOL_NAME) {
39-
// for now, can only use on monomorphic names
40-
let instance = Instance::mono(tcx, def_id.to_def_id());
39+
let def_id = def_id.to_def_id();
40+
let instance = Instance::new(
41+
def_id,
42+
tcx.erase_regions(&InternalSubsts::identity_for_item(tcx, def_id)),
43+
);
4144
let mangled = tcx.symbol_name(instance);
4245
tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled));
4346
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {

0 commit comments

Comments
 (0)