Skip to content

Commit 46cc5e9

Browse files
author
Lukas Markeffsky
committed
elaborate why dropping principal in *dyn casts is non-trivial
1 parent b89751b commit 46cc5e9

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -959,8 +959,35 @@ impl<'a, 'tcx> CastCheck<'tcx> {
959959
// dyn Auto -> dyn Auto'? ok.
960960
(None, None) => Ok(CastKind::PtrPtrCast),
961961

962-
// dyn Trait -> dyn Auto? should be ok, but we used to not allow it.
963-
// FIXME: allow this
962+
// dyn Trait -> dyn Auto? not ok (for now).
963+
//
964+
// Although dropping the principal is already allowed for unsizing coercions
965+
// (e.g. `*const (dyn Trait + Auto)` to `*const dyn Auto`), dropping it is
966+
// currently **NOT** allowed for (non-coercion) ptr-to-ptr casts (e.g
967+
// `*const Foo` to `*const Bar` where `Foo` has a `dyn Trait + Auto` tail
968+
// and `Bar` has a `dyn Auto` tail), because the underlying MIR operations
969+
// currently work very differently:
970+
//
971+
// * A MIR unsizing coercion on raw pointers to trait objects (`*const dyn Src`
972+
// to `*const dyn Dst`) is currently equivalent to downcasting the source to
973+
// the concrete sized type that it was originally unsized from first (via a
974+
// ptr-to-ptr cast from `*const Src` to `*const T` with `T: Sized`) and then
975+
// unsizing this thin pointer to the target type (unsizing `*const T` to
976+
// `*const Dst`). In particular, this means that the pointer's metadata
977+
// (vtable) will semantically change, e.g. for const eval and miri, even
978+
// though the vtables will always be merged for codegen.
979+
//
980+
// * A MIR ptr-to-ptr cast is currently equivalent to a transmute and does not
981+
// change the pointer metadata (vtable) at all.
982+
//
983+
// In addition to this potentially surprising difference between coercion and
984+
// non-coercion casts, casting away the principal with a MIR ptr-to-ptr cast
985+
// is currently considered undefined behavior:
986+
//
987+
// As a validity invariant of pointers to trait objects, we currently require
988+
// that the principal of the vtable in the pointer metadata exactly matches
989+
// the principal of the pointee type, where "no principal" is also considered
990+
// a kind of principal.
964991
(Some(_), None) => Err(CastError::DifferingKinds { src_kind, dst_kind }),
965992

966993
// dyn Auto -> dyn Trait? not ok.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! Test that non-coercion casts aren't allowed to drop the principal,
2+
//! because they cannot modify the pointer metadata.
3+
//!
4+
//! We test this in a const context to guard against UB if this is allowed
5+
//! in the future.
6+
7+
trait Trait {}
8+
impl Trait for () {}
9+
10+
struct Wrapper<T: ?Sized>(T);
11+
12+
const OBJECT: *const (dyn Trait + Send) = &();
13+
14+
// coercions are allowed
15+
const _: *const dyn Send = OBJECT as _;
16+
17+
// casts are **not** allowed
18+
const _: *const Wrapper<dyn Send> = OBJECT as _;
19+
//~^ ERROR casting `*const (dyn Trait + Send + 'static)` as `*const Wrapper<dyn Send>` is invalid
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0606]: casting `*const (dyn Trait + Send + 'static)` as `*const Wrapper<dyn Send>` is invalid
2+
--> $DIR/ptr-to-trait-obj-drop-principal.rs:18:37
3+
|
4+
LL | const _: *const Wrapper<dyn Send> = OBJECT as _;
5+
| ^^^^^^^^^^^
6+
|
7+
= note: the trait objects may have different vtables
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0606`.

0 commit comments

Comments
 (0)