Skip to content

mangling: mangle impl params w/ v0 scheme #75675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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 <david@davidtw.co>
  • Loading branch information
davidtwco committed Oct 15, 2020
commit fbdfe2c63bb11c91edc8488e7391e9fd2fd415ac
65 changes: 42 additions & 23 deletions compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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<Self::Region, Self::Error> {
Expand Down Expand Up @@ -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>,
Expand All @@ -552,24 +578,16 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
}

fn path_append_impl(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
self,
_: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_: &DisambiguatedDefPathData,
_: Ty<'tcx>,
_: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
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<Self::Path, Self::Error>,
Expand Down Expand Up @@ -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<Self::Path, Self::Error>,
Expand Down
20 changes: 20 additions & 0 deletions src/test/ui/symbol-names/issue-75326.legacy.stderr
Original file line number Diff line number Diff line change
@@ -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(<issue_75326::Foo<I,E> as issue_75326::Iterator2>::next::SYMBOL_HASH)
--> $DIR/issue-75326.rs:43:5
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling-alt(<issue_75326::Foo<I,E> as issue_75326::Iterator2>::next)
--> $DIR/issue-75326.rs:43:5
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

58 changes: 58 additions & 0 deletions src/test/ui/symbol-names/issue-75326.rs
Original file line number Diff line number Diff line change
@@ -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>(I, E);

pub trait Iterator2 {
type Item;

fn next(&mut self) -> Option<Self::Item>;

fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
{
unimplemented!()
}
}

struct Bar;

impl Iterator2 for Bar {
type Item = (u32, u16);

fn next(&mut self) -> Option<Self::Item> {
unimplemented!()
}
}

impl<I, T, E> Iterator2 for Foo<I, E>
where
I: Iterator2<Item = (T, E)>,
{
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(<issue_75326::Foo<I,E> as issue_75326::Iterator2>::next
//[legacy]~| ERROR demangling-alt(<issue_75326::Foo<I,E> as issue_75326::Iterator2>::next)
//[v0]~^^^^ ERROR symbol-name(_RNvXINICs4fqI2P2rA04_11issue_75326s_0pppEINtB5_3FooppENtB5_9Iterator24nextB5_)
//[v0]~| ERROR demangling(<issue_75326[317d481089b8c8fe]::Foo<_, _> as issue_75326[317d481089b8c8fe]::Iterator2>::next)
//[v0]~| ERROR demangling-alt(<issue_75326::Foo<_, _> as issue_75326::Iterator2>::next)
fn next(&mut self) -> Option<Self::Item> {
self.find(|_| true)
}
}

fn main() {
let mut a = Foo(Bar, 1u16);
let _ = a.next();
}
20 changes: 20 additions & 0 deletions src/test/ui/symbol-names/issue-75326.v0.stderr
Original file line number Diff line number Diff line change
@@ -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(<issue_75326[317d481089b8c8fe]::Foo<_, _> as issue_75326[317d481089b8c8fe]::Iterator2>::next)
--> $DIR/issue-75326.rs:43:5
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling-alt(<issue_75326::Foo<_, _> as issue_75326::Iterator2>::next)
--> $DIR/issue-75326.rs:43:5
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors