From 9752787dca8f891634c8802ba196d5eca4c80c27 Mon Sep 17 00:00:00 2001 From: David Wood Date: Tue, 18 Aug 2020 18:40:03 +0100 Subject: [PATCH 1/2] 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 --- compiler/rustc_middle/src/ty/instance.rs | 12 +++++++++++- compiler/rustc_middle/src/ty/util.rs | 18 ++---------------- compiler/rustc_symbol_mangling/src/legacy.rs | 1 - compiler/rustc_symbol_mangling/src/test.rs | 9 ++++++--- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index f8e8c209d37..4a2c97b4a40 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -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] diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 8734acad9b2..b0f0f0ba57f 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -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}; @@ -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() diff --git a/compiler/rustc_symbol_mangling/src/legacy.rs b/compiler/rustc_symbol_mangling/src/legacy.rs index b96e318bd3e..2c9caf73b8e 100644 --- a/compiler/rustc_symbol_mangling/src/legacy.rs +++ b/compiler/rustc_symbol_mangling/src/legacy.rs @@ -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 { diff --git a/compiler/rustc_symbol_mangling/src/test.rs b/compiler/rustc_symbol_mangling/src/test.rs index d2c9b05c560..822a8352934 100644 --- a/compiler/rustc_symbol_mangling/src/test.rs +++ b/compiler/rustc_symbol_mangling/src/test.rs @@ -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 @@ 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) { From fbdfe2c63bb11c91edc8488e7391e9fd2fd415ac Mon Sep 17 00:00:00 2001 From: David Wood Date: Mon, 24 Aug 2020 15:39:31 +0100 Subject: [PATCH 2/2] mangling: encode all impl parameters This commit modifies v0 symbol mangling to include all generic parameters from impl blocks (not just those used in the self type). Signed-off-by: David Wood --- compiler/rustc_symbol_mangling/src/v0.rs | 65 ++++++++++++------- .../ui/symbol-names/issue-75326.legacy.stderr | 20 ++++++ src/test/ui/symbol-names/issue-75326.rs | 58 +++++++++++++++++ .../ui/symbol-names/issue-75326.v0.stderr | 20 ++++++ 4 files changed, 140 insertions(+), 23 deletions(-) create mode 100644 src/test/ui/symbol-names/issue-75326.legacy.stderr create mode 100644 src/test/ui/symbol-names/issue-75326.rs create mode 100644 src/test/ui/symbol-names/issue-75326.v0.stderr diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index 183a11a5277..16d0b86903e 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -259,7 +259,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { } fn print_impl_path( - self, + mut self, impl_def_id: DefId, substs: &'tcx [GenericArg<'tcx>], mut self_ty: Ty<'tcx>, @@ -284,12 +284,37 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { } } - self.path_append_impl( - |cx| cx.print_def_path(parent_def_id, &[]), - &key.disambiguated_data, - self_ty, - impl_trait_ref, - ) + self.push(match impl_trait_ref { + Some(_) => "X", + None => "M", + }); + + // Encode impl generic params if the substitutions contain parameters (implying + // polymorphization is enabled) and this isn't an inherent impl. + if impl_trait_ref.is_some() && substs.iter().any(|a| a.has_param_types_or_consts()) { + self = self.path_generic_args( + |this| { + this.path_append_ns( + |cx| cx.print_def_path(parent_def_id, &[]), + 'I', + key.disambiguated_data.disambiguator as u64, + "", + ) + }, + substs, + )?; + } else { + self.push_disambiguator(key.disambiguated_data.disambiguator as u64); + self = self.print_def_path(parent_def_id, &[])?; + } + + self = self_ty.print(self)?; + + if let Some(trait_ref) = impl_trait_ref { + self = self.print_def_path(trait_ref.def_id, trait_ref.substs)?; + } + + Ok(self) } fn print_region(mut self, region: ty::Region<'_>) -> Result { @@ -538,6 +563,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { self.push_ident(&name); Ok(self) } + fn path_qualified( mut self, self_ty: Ty<'tcx>, @@ -552,24 +578,16 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { } fn path_append_impl( - mut self, - print_prefix: impl FnOnce(Self) -> Result, - disambiguated_data: &DisambiguatedDefPathData, - self_ty: Ty<'tcx>, - trait_ref: Option>, + self, + _: impl FnOnce(Self) -> Result, + _: &DisambiguatedDefPathData, + _: Ty<'tcx>, + _: Option>, ) -> Result { - self.push(match trait_ref { - Some(_) => "X", - None => "M", - }); - self.push_disambiguator(disambiguated_data.disambiguator as u64); - self = print_prefix(self)?; - self = self_ty.print(self)?; - if let Some(trait_ref) = trait_ref { - self = self.print_def_path(trait_ref.def_id, trait_ref.substs)?; - } - Ok(self) + // Inlined into `print_impl_path` + unreachable!() } + fn path_append( self, print_prefix: impl FnOnce(Self) -> Result, @@ -603,6 +621,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> { name.as_ref().map_or("", |s| &s[..]), ) } + fn path_generic_args( mut self, print_prefix: impl FnOnce(Self) -> Result, diff --git a/src/test/ui/symbol-names/issue-75326.legacy.stderr b/src/test/ui/symbol-names/issue-75326.legacy.stderr new file mode 100644 index 00000000000..5f822f6660c --- /dev/null +++ b/src/test/ui/symbol-names/issue-75326.legacy.stderr @@ -0,0 +1,20 @@ +error: symbol-name(_ZN72_$LT$issue_75326..Foo$LT$I$C$E$GT$$u20$as$u20$issue_75326..Iterator2$GT$4next17SYMBOL_HASH) + --> $DIR/issue-75326.rs:43:5 + | +LL | #[rustc_symbol_name] + | ^^^^^^^^^^^^^^^^^^^^ + +error: demangling( as issue_75326::Iterator2>::next::SYMBOL_HASH) + --> $DIR/issue-75326.rs:43:5 + | +LL | #[rustc_symbol_name] + | ^^^^^^^^^^^^^^^^^^^^ + +error: demangling-alt( as issue_75326::Iterator2>::next) + --> $DIR/issue-75326.rs:43:5 + | +LL | #[rustc_symbol_name] + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + diff --git a/src/test/ui/symbol-names/issue-75326.rs b/src/test/ui/symbol-names/issue-75326.rs new file mode 100644 index 00000000000..ce315164cef --- /dev/null +++ b/src/test/ui/symbol-names/issue-75326.rs @@ -0,0 +1,58 @@ +// build-fail +// ignore-tidy-linelength +// revisions: legacy v0 +//[legacy]compile-flags: -Z symbol-mangling-version=legacy +//[v0]compile-flags: -Z symbol-mangling-version=v0 +//[legacy]normalize-stderr-32bit: "h[\d\w]+" -> "SYMBOL_HASH" +//[legacy]normalize-stderr-64bit: "h[\d\w]+" -> "SYMBOL_HASH" + +#![feature(rustc_attrs)] + +pub(crate) struct Foo(I, E); + +pub trait Iterator2 { + type Item; + + fn next(&mut self) -> Option; + + fn find

(&mut self, predicate: P) -> Option + where + Self: Sized, + P: FnMut(&Self::Item) -> bool, + { + unimplemented!() + } +} + +struct Bar; + +impl Iterator2 for Bar { + type Item = (u32, u16); + + fn next(&mut self) -> Option { + unimplemented!() + } +} + +impl Iterator2 for Foo +where + I: Iterator2, +{ + type Item = T; + + #[rustc_symbol_name] + //[legacy]~^ ERROR symbol-name(_ZN72_$LT$issue_75326..Foo$LT$I$C$E$GT$$u20$as$u20$issue_75326..Iterator2$GT$4next + //[legacy]~| ERROR demangling( as issue_75326::Iterator2>::next + //[legacy]~| ERROR demangling-alt( as issue_75326::Iterator2>::next) + //[v0]~^^^^ ERROR symbol-name(_RNvXINICs4fqI2P2rA04_11issue_75326s_0pppEINtB5_3FooppENtB5_9Iterator24nextB5_) + //[v0]~| ERROR demangling( as issue_75326[317d481089b8c8fe]::Iterator2>::next) + //[v0]~| ERROR demangling-alt( as issue_75326::Iterator2>::next) + fn next(&mut self) -> Option { + self.find(|_| true) + } +} + +fn main() { + let mut a = Foo(Bar, 1u16); + let _ = a.next(); +} diff --git a/src/test/ui/symbol-names/issue-75326.v0.stderr b/src/test/ui/symbol-names/issue-75326.v0.stderr new file mode 100644 index 00000000000..59bdfa8ca36 --- /dev/null +++ b/src/test/ui/symbol-names/issue-75326.v0.stderr @@ -0,0 +1,20 @@ +error: symbol-name(_RNvXINICs4fqI2P2rA04_11issue_75326s_0pppEINtB5_3FooppENtB5_9Iterator24nextB5_) + --> $DIR/issue-75326.rs:43:5 + | +LL | #[rustc_symbol_name] + | ^^^^^^^^^^^^^^^^^^^^ + +error: demangling( as issue_75326[317d481089b8c8fe]::Iterator2>::next) + --> $DIR/issue-75326.rs:43:5 + | +LL | #[rustc_symbol_name] + | ^^^^^^^^^^^^^^^^^^^^ + +error: demangling-alt( as issue_75326::Iterator2>::next) + --> $DIR/issue-75326.rs:43:5 + | +LL | #[rustc_symbol_name] + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors +