Skip to content

Commit e0ef3ef

Browse files
committed
CFI: Handle dyn with no principal
In user-facing Rust, `dyn` always has at least one predicate following it. Unfortunately, because we filter out marker traits from receivers at callsites and `dyn Sync` is, for example, legal, this results in us having `dyn` types with no predicates on occasion in our alias set encoding. This patch handles cases where there are no predicates in a `dyn` type which are relevant to its alias set. Fixes #122998
1 parent 6a92312 commit e0ef3ef

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

compiler/rustc_middle/src/ty/predicate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl<'tcx> ty::List<ty::PolyExistentialPredicate<'tcx>> {
290290
/// have a "trivial" vtable consisting of just the size, alignment,
291291
/// and destructor.
292292
pub fn principal(&self) -> Option<ty::Binder<'tcx, ExistentialTraitRef<'tcx>>> {
293-
self[0]
293+
self.get(0)?
294294
.map_bound(|this| match this {
295295
ExistentialPredicate::Trait(tr) => Some(tr),
296296
_ => None,

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,11 @@ fn transform_predicates<'tcx>(
760760
ty::ExistentialPredicate::AutoTrait(..) => Some(predicate),
761761
})
762762
.collect();
763-
tcx.mk_poly_existential_predicates(&predicates)
763+
if predicates.len() == 0 {
764+
List::empty()
765+
} else {
766+
tcx.mk_poly_existential_predicates(&predicates)
767+
}
764768
}
765769

766770
/// Transforms args for being encoded and used in the substitution dictionary.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Check that dropping a trait object without a principal trait succeeds
2+
3+
//@ needs-sanitizer-cfi
4+
// FIXME(#122848) Remove only-linux once OSX CFI binaries works
5+
//@ only-linux
6+
//@ compile-flags: --crate-type=bin -Cprefer-dynamic=off -Clto -Zsanitizer=cfi
7+
//@ compile-flags: -C target-feature=-crt-static -C codegen-units=1 -C opt-level=0
8+
// FIXME(#118761) Should be run-pass once the labels on drop are compatible.
9+
// This test is being landed ahead of that to test that the compiler doesn't ICE while labeling the
10+
// callsite for a drop, but the vtable doesn't have the correct label yet.
11+
//@ build-pass
12+
13+
struct CustomDrop;
14+
15+
impl Drop for CustomDrop {
16+
fn drop(&mut self) {}
17+
}
18+
19+
fn main() {
20+
let _ = Box::new(CustomDrop) as Box<dyn Send>;
21+
}

0 commit comments

Comments
 (0)