Skip to content

Commit

Permalink
mangling: non-monomorphic #[rustc_symbol_name]
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
davidtwco committed Oct 15, 2020
1 parent 5565241 commit 9752787
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
12 changes: 11 additions & 1 deletion compiler/rustc_middle/src/ty/instance.rs
Expand Up @@ -291,7 +291,17 @@ impl<'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]
Expand Down
18 changes: 2 additions & 16 deletions compiler/rustc_middle/src/ty/util.rs
Expand Up @@ -6,9 +6,9 @@ use crate::mir::interpret::{sign_extend, truncate};
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};
Expand Down Expand Up @@ -509,20 +509,6 @@ impl<'tcx> TyCtxt<'tcx> {
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()
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_symbol_mangling/src/legacy.rs
Expand Up @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_symbol_mangling/src/test.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -36,8 +36,11 @@ impl SymbolNamesTest<'tcx> {
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) {
Expand Down

0 comments on commit 9752787

Please sign in to comment.