Skip to content

Commit 6538cdb

Browse files
committed
CFI: Remove trait function encoding hack
1 parent 1d81d8e commit 6538cdb

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,44 +1136,5 @@ pub fn typeid_for_instance<'tcx>(
11361136
bug!("typeid_for_instance: couldn't get fn_abi of instance {:?}", instance)
11371137
});
11381138

1139-
// If this instance is a method and self is a reference, get the impl it belongs to
1140-
let impl_def_id = tcx.impl_of_method(instance.def_id());
1141-
if impl_def_id.is_some() && !fn_abi.args.is_empty() && fn_abi.args[0].layout.ty.is_ref() {
1142-
// If this impl is not an inherent impl, get the trait it implements
1143-
if let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id.unwrap()) {
1144-
// Transform the concrete self into a reference to a trait object
1145-
let existential_predicate = trait_ref.map_bound(|trait_ref| {
1146-
ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::erase_self_ty(
1147-
tcx, trait_ref,
1148-
))
1149-
});
1150-
let existential_predicates = tcx.mk_poly_existential_predicates(&[ty::Binder::dummy(
1151-
existential_predicate.skip_binder(),
1152-
)]);
1153-
// Is the concrete self mutable?
1154-
let self_ty = if fn_abi.args[0].layout.ty.is_mutable_ptr() {
1155-
Ty::new_mut_ref(
1156-
tcx,
1157-
tcx.lifetimes.re_erased,
1158-
Ty::new_dynamic(tcx, existential_predicates, tcx.lifetimes.re_erased, ty::Dyn),
1159-
)
1160-
} else {
1161-
Ty::new_imm_ref(
1162-
tcx,
1163-
tcx.lifetimes.re_erased,
1164-
Ty::new_dynamic(tcx, existential_predicates, tcx.lifetimes.re_erased, ty::Dyn),
1165-
)
1166-
};
1167-
1168-
// Replace the concrete self in an fn_abi clone by the reference to a trait object
1169-
let mut fn_abi = fn_abi.clone();
1170-
// HACK(rcvalle): It is okay to not replace or update the entire ArgAbi here because the
1171-
// other fields are never used.
1172-
fn_abi.args[0].layout.ty = self_ty;
1173-
1174-
return typeid_for_fnabi(tcx, &fn_abi, options);
1175-
}
1176-
}
1177-
11781139
typeid_for_fnabi(tcx, fn_abi, options)
11791140
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Check that the type of trait methods on a concrete type are not abstracted
2+
3+
//@ needs-sanitizer-cfi
4+
//@ compile-flags: --crate-type=bin -Cprefer-dynamic=off -Clto -Zsanitizer=cfi
5+
//@ compile-flags: -C codegen-units=1 -C opt-level=0
6+
//@ run-pass
7+
8+
trait Foo {
9+
fn foo(&self);
10+
}
11+
12+
struct S;
13+
14+
impl Foo for S {
15+
fn foo(&self) {}
16+
}
17+
18+
struct S2 {
19+
f: fn(&S)
20+
}
21+
22+
impl S2 {
23+
fn foo(&self, s: &S) {
24+
(self.f)(s)
25+
}
26+
}
27+
28+
fn main() {
29+
S2 { f: <S as Foo>::foo }.foo(&S)
30+
}

0 commit comments

Comments
 (0)