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 <david@davidtw.co>
This commit is contained in:
David Wood 2020-08-18 18:40:03 +01:00
parent 5565241f65
commit 9752787dca
No known key found for this signature in database
GPG Key ID: 2592E76C87381FD9
4 changed files with 19 additions and 21 deletions

View File

@ -291,7 +291,17 @@ pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> Instance<'tcx> {
}
pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
Instance::new(def_id, tcx.empty_substs_for_def_id(def_id))
let substs = InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind {
ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
ty::GenericParamDefKind::Type { .. } => {
bug!("Instance::mono: {:?} has type parameters", def_id)
}
ty::GenericParamDefKind::Const { .. } => {
bug!("Instance::mono: {:?} has const parameters", def_id)
}
});
Instance::new(def_id, substs)
}
#[inline]

View File

@ -6,9 +6,9 @@
use crate::ty::fold::TypeFolder;
use crate::ty::layout::IntegerExt;
use crate::ty::query::TyCtxtAt;
use crate::ty::subst::{GenericArgKind, InternalSubsts, Subst, SubstsRef};
use crate::ty::subst::{GenericArgKind, Subst, SubstsRef};
use crate::ty::TyKind::*;
use crate::ty::{self, DefIdTree, GenericParamDefKind, List, Ty, TyCtxt, TypeFoldable};
use crate::ty::{self, DefIdTree, List, Ty, TyCtxt, TypeFoldable};
use rustc_apfloat::Float as _;
use rustc_ast as ast;
use rustc_attr::{self as attr, SignedInt, UnsignedInt};
@ -509,20 +509,6 @@ pub fn closure_env_ty(
Some(ty::Binder::bind(env_ty))
}
/// Given the `DefId` of some item that has no type or const parameters, make
/// a suitable "empty substs" for it.
pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> SubstsRef<'tcx> {
InternalSubsts::for_item(self, item_def_id, |param, _| match param.kind {
GenericParamDefKind::Lifetime => self.lifetimes.re_erased.into(),
GenericParamDefKind::Type { .. } => {
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
}
GenericParamDefKind::Const { .. } => {
bug!("empty_substs_for_def_id: {:?} has const parameters", item_def_id)
}
})
}
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
pub fn is_static(self, def_id: DefId) -> bool {
self.static_mutability(def_id).is_some()

View File

@ -115,7 +115,6 @@ fn get_symbol_hash<'tcx>(
}
// also include any type parameters (for generic items)
assert!(!substs.has_erasable_regions());
substs.hash_stable(&mut hcx, &mut hasher);
if let Some(instantiating_crate) = instantiating_crate {

View File

@ -6,7 +6,7 @@
use rustc_hir as hir;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{Instance, TyCtxt};
use rustc_middle::ty::{subst::InternalSubsts, Instance, TyCtxt};
use rustc_span::symbol::{sym, Symbol};
const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
@ -36,8 +36,11 @@ fn process_attrs(&mut self, hir_id: hir::HirId) {
let def_id = tcx.hir().local_def_id(hir_id);
for attr in tcx.get_attrs(def_id.to_def_id()).iter() {
if tcx.sess.check_name(attr, SYMBOL_NAME) {
// for now, can only use on monomorphic names
let instance = Instance::mono(tcx, def_id.to_def_id());
let def_id = def_id.to_def_id();
let instance = Instance::new(
def_id,
tcx.erase_regions(&InternalSubsts::identity_for_item(tcx, def_id)),
);
let mangled = tcx.symbol_name(instance);
tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled));
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {