Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 10 pull requests #132094

Merged
merged 25 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7a3a98d
Fix target_vendor in QNX Neutrino targets
madsmtm Oct 2, 2024
b89751b
clean up `*dyn` casts (with principals)
Oct 18, 2024
ab4222a
Prevent overflowing enum cast from ICEing
clubby789 Oct 18, 2024
98c4d96
Reduce visibility of coverage FFI functions/types
Zalathar Oct 19, 2024
d1bf77e
Pass coverage mappings to LLVM as separate structs
Zalathar Oct 19, 2024
46cc5e9
elaborate why dropping principal in `*dyn` casts is non-trivial
Oct 18, 2024
8f85b90
Rename Receiver -> LegacyReceiver
adetaylor Sep 11, 2024
0304809
stop hashing compile-time constant
RalfJung Oct 23, 2024
2e3091d
Don't allow test revisions that conflict with built in cfgs
clubby789 Oct 19, 2024
dab76ec
fix a couple clippy:complexitys
matthiaskrgr Oct 12, 2024
464f405
fix some manual_map
matthiaskrgr Oct 12, 2024
d84d659
clone range in a more obvious way
matthiaskrgr Oct 12, 2024
4e1b3ab
Print safety correctly in extern static items
compiler-errors Oct 24, 2024
1920c66
Plumb through param_env to note_type_err
compiler-errors Oct 15, 2024
4217b87
Deeply normalize type trace in type error reporting
compiler-errors Oct 15, 2024
9c73bcf
Rollup merge of #130225 - adetaylor:rename-old-receiver, r=wesleywiser
Zalathar Oct 24, 2024
40d7872
Rollup merge of #131169 - madsmtm:target-info-nto-vendor, r=wesleywiser
Zalathar Oct 24, 2024
77f2c57
Rollup merge of #131623 - matthiaskrgr:clippy_sat, r=Nadrieril
Zalathar Oct 24, 2024
ad43be3
Rollup merge of #131756 - compiler-errors:deeply-normalize-type-err, …
Zalathar Oct 24, 2024
4c0bab3
Rollup merge of #131898 - lukas-code:ptr-cast-cleanup, r=compiler-errors
Zalathar Oct 24, 2024
4b02d64
Rollup merge of #131909 - clubby789:enum-overflow-cast, r=compiler-er…
Zalathar Oct 24, 2024
f7f411d
Rollup merge of #131930 - clubby789:revision-cfg-collide, r=jieyouxu
Zalathar Oct 24, 2024
8f354fc
Rollup merge of #131956 - Zalathar:llvm-counters, r=compiler-errors,S…
Zalathar Oct 24, 2024
006a142
Rollup merge of #132076 - RalfJung:feature-hashing, r=nnethercote,Mar…
Zalathar Oct 24, 2024
7e2bbc3
Rollup merge of #132088 - compiler-errors:extern-static, r=jieyouxu
Zalathar Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
elaborate why dropping principal in *dyn casts is non-trivial
  • Loading branch information
Lukas Markeffsky committed Oct 20, 2024
commit 46cc5e9e962fda2f99d4956ef539bf4b79fead26
31 changes: 29 additions & 2 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,35 @@ impl<'a, 'tcx> CastCheck<'tcx> {
// dyn Auto -> dyn Auto'? ok.
(None, None) => Ok(CastKind::PtrPtrCast),

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

// dyn Auto -> dyn Trait? not ok.
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/cast/ptr-to-trait-obj-drop-principal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Test that non-coercion casts aren't allowed to drop the principal,
//! because they cannot modify the pointer metadata.
//!
//! We test this in a const context to guard against UB if this is allowed
//! in the future.

trait Trait {}
impl Trait for () {}

struct Wrapper<T: ?Sized>(T);

const OBJECT: *const (dyn Trait + Send) = &();

// coercions are allowed
const _: *const dyn Send = OBJECT as _;

// casts are **not** allowed
const _: *const Wrapper<dyn Send> = OBJECT as _;
//~^ ERROR casting `*const (dyn Trait + Send + 'static)` as `*const Wrapper<dyn Send>` is invalid

fn main() {}
11 changes: 11 additions & 0 deletions tests/ui/cast/ptr-to-trait-obj-drop-principal.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0606]: casting `*const (dyn Trait + Send + 'static)` as `*const Wrapper<dyn Send>` is invalid
--> $DIR/ptr-to-trait-obj-drop-principal.rs:18:37
|
LL | const _: *const Wrapper<dyn Send> = OBJECT as _;
| ^^^^^^^^^^^
|
= note: the trait objects may have different vtables

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0606`.
Loading