Skip to content

Commit bd66ce6

Browse files
authored
Rollup merge of rust-lang#75675 - davidtwco:symbol-mangling-impl-params, r=eddyb
mangling: mangle impl params w/ v0 scheme This PR modifies v0 symbol mangling to include all generic parameters from impl blocks (not just those used in the self type) - an alternative fix to rust-lang#75326. ``` original: _RNCNvXCs4fqI2P2rA04_19impl_param_manglingINtB4_3FooppENtNtNtNtCsfnEnqCNU58Z_4core4iter6traits8iterator8Iterator4next0B4_ // |------------ B4_ ----------------| // _R (N C (N v (X (C ((s 4fqI2p2rA04_) 19impl_param_mangling)) (I (N t B4_ 3Foo) pp E) (N t (N t (N t (N t (C ((s fnEnqCNU58Z_) 4core)) 4iter) 6traits) 8iterator) 8Iterator)) 4next) 0) B4_ modified: _RNvXINICs4fqI2P2rA04_11issue_753260pppEINtB5_3FooppENtNtNtNtCsfnEnqCNU58Z_4core4iter6traits8iterator8Iterator4nextB5_ // _R (N v (X (I (N I (C ((s 4fqI2P2rA04_) 11issue_75326)) 0) ppp E) (I (N t B5_ 3Foo) pp E) (N t (N t (N t (N t (C ((s fnEnqCNU58Z_) 4core)) 4iter) 6traits) 8iterator) 8Iterator)) 4next) B5_ // | ^ | // | | | // | new impl namespace | ``` ~~Submitted as a draft as after some discussion w/ @eddyb, I'm going to do some investigation into (yet more alternative) changes to polymorphization that might remove the necessity for this.~~ r? @eddyb
2 parents 355d81b + fbdfe2c commit bd66ce6

File tree

8 files changed

+159
-44
lines changed

8 files changed

+159
-44
lines changed

compiler/rustc_middle/src/ty/instance.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,17 @@ impl<'tcx> Instance<'tcx> {
291291
}
292292

293293
pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
294-
Instance::new(def_id, tcx.empty_substs_for_def_id(def_id))
294+
let substs = InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind {
295+
ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
296+
ty::GenericParamDefKind::Type { .. } => {
297+
bug!("Instance::mono: {:?} has type parameters", def_id)
298+
}
299+
ty::GenericParamDefKind::Const { .. } => {
300+
bug!("Instance::mono: {:?} has const parameters", def_id)
301+
}
302+
});
303+
304+
Instance::new(def_id, substs)
295305
}
296306

297307
#[inline]

compiler/rustc_middle/src/ty/util.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use crate::mir::interpret::{sign_extend, truncate};
66
use crate::ty::fold::TypeFolder;
77
use crate::ty::layout::IntegerExt;
88
use crate::ty::query::TyCtxtAt;
9-
use crate::ty::subst::{GenericArgKind, InternalSubsts, Subst, SubstsRef};
9+
use crate::ty::subst::{GenericArgKind, Subst, SubstsRef};
1010
use crate::ty::TyKind::*;
11-
use crate::ty::{self, DefIdTree, GenericParamDefKind, List, Ty, TyCtxt, TypeFoldable};
11+
use crate::ty::{self, DefIdTree, List, Ty, TyCtxt, TypeFoldable};
1212
use rustc_apfloat::Float as _;
1313
use rustc_ast as ast;
1414
use rustc_attr::{self as attr, SignedInt, UnsignedInt};
@@ -509,20 +509,6 @@ impl<'tcx> TyCtxt<'tcx> {
509509
Some(ty::Binder::bind(env_ty))
510510
}
511511

512-
/// Given the `DefId` of some item that has no type or const parameters, make
513-
/// a suitable "empty substs" for it.
514-
pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> SubstsRef<'tcx> {
515-
InternalSubsts::for_item(self, item_def_id, |param, _| match param.kind {
516-
GenericParamDefKind::Lifetime => self.lifetimes.re_erased.into(),
517-
GenericParamDefKind::Type { .. } => {
518-
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
519-
}
520-
GenericParamDefKind::Const { .. } => {
521-
bug!("empty_substs_for_def_id: {:?} has const parameters", item_def_id)
522-
}
523-
})
524-
}
525-
526512
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
527513
pub fn is_static(self, def_id: DefId) -> bool {
528514
self.static_mutability(def_id).is_some()

compiler/rustc_symbol_mangling/src/legacy.rs

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ fn get_symbol_hash<'tcx>(
115115
}
116116

117117
// also include any type parameters (for generic items)
118-
assert!(!substs.has_erasable_regions());
119118
substs.hash_stable(&mut hcx, &mut hasher);
120119

121120
if let Some(instantiating_crate) = instantiating_crate {

compiler/rustc_symbol_mangling/src/test.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use rustc_hir as hir;
88
use rustc_middle::ty::print::with_no_trimmed_paths;
9-
use rustc_middle::ty::{Instance, TyCtxt};
9+
use rustc_middle::ty::{subst::InternalSubsts, Instance, TyCtxt};
1010
use rustc_span::symbol::{sym, Symbol};
1111

1212
const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
@@ -36,8 +36,11 @@ impl SymbolNamesTest<'tcx> {
3636
let def_id = tcx.hir().local_def_id(hir_id);
3737
for attr in tcx.get_attrs(def_id.to_def_id()).iter() {
3838
if tcx.sess.check_name(attr, SYMBOL_NAME) {
39-
// for now, can only use on monomorphic names
40-
let instance = Instance::mono(tcx, def_id.to_def_id());
39+
let def_id = def_id.to_def_id();
40+
let instance = Instance::new(
41+
def_id,
42+
tcx.erase_regions(&InternalSubsts::identity_for_item(tcx, def_id)),
43+
);
4144
let mangled = tcx.symbol_name(instance);
4245
tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled));
4346
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {

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)