Skip to content

Commit fbdfe2c

Browse files
committed
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 <[email protected]>
1 parent 9752787 commit fbdfe2c

File tree

4 files changed

+140
-23
lines changed

4 files changed

+140
-23
lines changed

compiler/rustc_symbol_mangling/src/v0.rs

+42-23
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
259259
}
260260

261261
fn print_impl_path(
262-
self,
262+
mut self,
263263
impl_def_id: DefId,
264264
substs: &'tcx [GenericArg<'tcx>],
265265
mut self_ty: Ty<'tcx>,
@@ -284,12 +284,37 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
284284
}
285285
}
286286

287-
self.path_append_impl(
288-
|cx| cx.print_def_path(parent_def_id, &[]),
289-
&key.disambiguated_data,
290-
self_ty,
291-
impl_trait_ref,
292-
)
287+
self.push(match impl_trait_ref {
288+
Some(_) => "X",
289+
None => "M",
290+
});
291+
292+
// Encode impl generic params if the substitutions contain parameters (implying
293+
// polymorphization is enabled) and this isn't an inherent impl.
294+
if impl_trait_ref.is_some() && substs.iter().any(|a| a.has_param_types_or_consts()) {
295+
self = self.path_generic_args(
296+
|this| {
297+
this.path_append_ns(
298+
|cx| cx.print_def_path(parent_def_id, &[]),
299+
'I',
300+
key.disambiguated_data.disambiguator as u64,
301+
"",
302+
)
303+
},
304+
substs,
305+
)?;
306+
} else {
307+
self.push_disambiguator(key.disambiguated_data.disambiguator as u64);
308+
self = self.print_def_path(parent_def_id, &[])?;
309+
}
310+
311+
self = self_ty.print(self)?;
312+
313+
if let Some(trait_ref) = impl_trait_ref {
314+
self = self.print_def_path(trait_ref.def_id, trait_ref.substs)?;
315+
}
316+
317+
Ok(self)
293318
}
294319

295320
fn print_region(mut self, region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
@@ -538,6 +563,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
538563
self.push_ident(&name);
539564
Ok(self)
540565
}
566+
541567
fn path_qualified(
542568
mut self,
543569
self_ty: Ty<'tcx>,
@@ -552,24 +578,16 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
552578
}
553579

554580
fn path_append_impl(
555-
mut self,
556-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
557-
disambiguated_data: &DisambiguatedDefPathData,
558-
self_ty: Ty<'tcx>,
559-
trait_ref: Option<ty::TraitRef<'tcx>>,
581+
self,
582+
_: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
583+
_: &DisambiguatedDefPathData,
584+
_: Ty<'tcx>,
585+
_: Option<ty::TraitRef<'tcx>>,
560586
) -> Result<Self::Path, Self::Error> {
561-
self.push(match trait_ref {
562-
Some(_) => "X",
563-
None => "M",
564-
});
565-
self.push_disambiguator(disambiguated_data.disambiguator as u64);
566-
self = print_prefix(self)?;
567-
self = self_ty.print(self)?;
568-
if let Some(trait_ref) = trait_ref {
569-
self = self.print_def_path(trait_ref.def_id, trait_ref.substs)?;
570-
}
571-
Ok(self)
587+
// Inlined into `print_impl_path`
588+
unreachable!()
572589
}
590+
573591
fn path_append(
574592
self,
575593
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
@@ -603,6 +621,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
603621
name.as_ref().map_or("", |s| &s[..]),
604622
)
605623
}
624+
606625
fn path_generic_args(
607626
mut self,
608627
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: symbol-name(_ZN72_$LT$issue_75326..Foo$LT$I$C$E$GT$$u20$as$u20$issue_75326..Iterator2$GT$4next17SYMBOL_HASH)
2+
--> $DIR/issue-75326.rs:43:5
3+
|
4+
LL | #[rustc_symbol_name]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
7+
error: demangling(<issue_75326::Foo<I,E> as issue_75326::Iterator2>::next::SYMBOL_HASH)
8+
--> $DIR/issue-75326.rs:43:5
9+
|
10+
LL | #[rustc_symbol_name]
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
13+
error: demangling-alt(<issue_75326::Foo<I,E> as issue_75326::Iterator2>::next)
14+
--> $DIR/issue-75326.rs:43:5
15+
|
16+
LL | #[rustc_symbol_name]
17+
| ^^^^^^^^^^^^^^^^^^^^
18+
19+
error: aborting due to 3 previous errors
20+
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// build-fail
2+
// ignore-tidy-linelength
3+
// revisions: legacy v0
4+
//[legacy]compile-flags: -Z symbol-mangling-version=legacy
5+
//[v0]compile-flags: -Z symbol-mangling-version=v0
6+
//[legacy]normalize-stderr-32bit: "h[\d\w]+" -> "SYMBOL_HASH"
7+
//[legacy]normalize-stderr-64bit: "h[\d\w]+" -> "SYMBOL_HASH"
8+
9+
#![feature(rustc_attrs)]
10+
11+
pub(crate) struct Foo<I, E>(I, E);
12+
13+
pub trait Iterator2 {
14+
type Item;
15+
16+
fn next(&mut self) -> Option<Self::Item>;
17+
18+
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
19+
where
20+
Self: Sized,
21+
P: FnMut(&Self::Item) -> bool,
22+
{
23+
unimplemented!()
24+
}
25+
}
26+
27+
struct Bar;
28+
29+
impl Iterator2 for Bar {
30+
type Item = (u32, u16);
31+
32+
fn next(&mut self) -> Option<Self::Item> {
33+
unimplemented!()
34+
}
35+
}
36+
37+
impl<I, T, E> Iterator2 for Foo<I, E>
38+
where
39+
I: Iterator2<Item = (T, E)>,
40+
{
41+
type Item = T;
42+
43+
#[rustc_symbol_name]
44+
//[legacy]~^ ERROR symbol-name(_ZN72_$LT$issue_75326..Foo$LT$I$C$E$GT$$u20$as$u20$issue_75326..Iterator2$GT$4next
45+
//[legacy]~| ERROR demangling(<issue_75326::Foo<I,E> as issue_75326::Iterator2>::next
46+
//[legacy]~| ERROR demangling-alt(<issue_75326::Foo<I,E> as issue_75326::Iterator2>::next)
47+
//[v0]~^^^^ ERROR symbol-name(_RNvXINICs4fqI2P2rA04_11issue_75326s_0pppEINtB5_3FooppENtB5_9Iterator24nextB5_)
48+
//[v0]~| ERROR demangling(<issue_75326[317d481089b8c8fe]::Foo<_, _> as issue_75326[317d481089b8c8fe]::Iterator2>::next)
49+
//[v0]~| ERROR demangling-alt(<issue_75326::Foo<_, _> as issue_75326::Iterator2>::next)
50+
fn next(&mut self) -> Option<Self::Item> {
51+
self.find(|_| true)
52+
}
53+
}
54+
55+
fn main() {
56+
let mut a = Foo(Bar, 1u16);
57+
let _ = a.next();
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: symbol-name(_RNvXINICs4fqI2P2rA04_11issue_75326s_0pppEINtB5_3FooppENtB5_9Iterator24nextB5_)
2+
--> $DIR/issue-75326.rs:43:5
3+
|
4+
LL | #[rustc_symbol_name]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
7+
error: demangling(<issue_75326[317d481089b8c8fe]::Foo<_, _> as issue_75326[317d481089b8c8fe]::Iterator2>::next)
8+
--> $DIR/issue-75326.rs:43:5
9+
|
10+
LL | #[rustc_symbol_name]
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
13+
error: demangling-alt(<issue_75326::Foo<_, _> as issue_75326::Iterator2>::next)
14+
--> $DIR/issue-75326.rs:43:5
15+
|
16+
LL | #[rustc_symbol_name]
17+
| ^^^^^^^^^^^^^^^^^^^^
18+
19+
error: aborting due to 3 previous errors
20+

0 commit comments

Comments
 (0)