Skip to content

Commit b217e97

Browse files
committed
Auto merge of #124277 - matthiaskrgr:rollup-zdb93i4, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #123680 (Deny gen keyword in `edition_2024_compat` lints) - #124057 (Fix ICE when ADT tail has type error) - #124168 (Use `DefiningOpaqueTypes::Yes` in rustdoc, where the `InferCtxt` is guaranteed to have no opaque types it can define) - #124197 (Move duplicated code in functions in `tests/rustdoc-gui/notable-trait.goml`) - #124200 (Improve handling of expr->field errors) - #124220 (Miri: detect wrong vtables in wide pointers) - #124266 (remove an unused type from the reentrant lock tests) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 29381b6 + edc13b7 commit b217e97

12 files changed

+116
-30
lines changed

tests/fail/dyn-call-trait-mismatch.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Validation stops this too early.
2+
//@compile-flags: -Zmiri-disable-validation
3+
14
trait T1 {
25
#[allow(dead_code)]
36
fn method1(self: Box<Self>);
@@ -13,5 +16,5 @@ impl T1 for i32 {
1316
fn main() {
1417
let r = Box::new(0) as Box<dyn T1>;
1518
let r2: Box<dyn T2> = unsafe { std::mem::transmute(r) };
16-
r2.method2(); //~ERROR: call on a pointer whose vtable does not match its type
19+
r2.method2(); //~ERROR: using vtable for trait `T1` but trait `T2` was expected
1720
}

tests/fail/dyn-call-trait-mismatch.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: `dyn` call on a pointer whose vtable does not match its type
1+
error: Undefined Behavior: using vtable for trait `T1` but trait `T2` was expected
22
--> $DIR/dyn-call-trait-mismatch.rs:LL:CC
33
|
44
LL | r2.method2();
5-
| ^^^^^^^^^^^^ `dyn` call on a pointer whose vtable does not match its type
5+
| ^^^^^^^^^^^^ using vtable for trait `T1` but trait `T2` was expected
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This upcast is currently forbidden because it involves an invalid value.
2+
// However, if in the future we relax the validity requirements for raw pointer vtables,
3+
// we could consider allowing this again -- the cast itself isn't doing anything wrong,
4+
// only the transmutes needed to set up the testcase are wrong.
5+
6+
use std::fmt;
7+
8+
fn main() {
9+
// vtable_mismatch_nop_cast
10+
let ptr: &dyn fmt::Display = &0;
11+
let ptr: *const (dyn fmt::Debug + Send + Sync) = unsafe { std::mem::transmute(ptr) }; //~ERROR: wrong trait
12+
// Even though the vtable is for the wrong trait, this cast doesn't actually change the needed
13+
// vtable so it should still be allowed -- if we ever allow the line above.
14+
let _ptr2 = ptr as *const dyn fmt::Debug;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: constructing invalid value: wrong trait in wide pointer vtable: expected `std::fmt::Debug + std::marker::Send + std::marker::Sync`, but encountered `std::fmt::Display`
2+
--> $DIR/dyn-upcast-nop-wrong-trait.rs:LL:CC
3+
|
4+
LL | let ptr: *const (dyn fmt::Debug + Send + Sync) = unsafe { std::mem::transmute(ptr) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: wrong trait in wide pointer vtable: expected `std::fmt::Debug + std::marker::Send + std::marker::Sync`, but encountered `std::fmt::Display`
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/dyn-upcast-nop-wrong-trait.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

tests/fail/dyn-upcast-trait-mismatch.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Validation stops this too early.
2+
//@compile-flags: -Zmiri-disable-validation
3+
14
#![feature(trait_upcasting)]
25
#![allow(incomplete_features)]
36

@@ -57,7 +60,7 @@ impl Baz for i32 {
5760

5861
fn main() {
5962
let baz: &dyn Baz = &1;
60-
let baz_fake: &dyn Bar = unsafe { std::mem::transmute(baz) };
61-
let _err = baz_fake as &dyn Foo;
62-
//~^ERROR: upcast on a pointer whose vtable does not match its type
63+
let baz_fake: *const dyn Bar = unsafe { std::mem::transmute(baz) };
64+
let _err = baz_fake as *const dyn Foo;
65+
//~^ERROR: using vtable for trait `Baz` but trait `Bar` was expected
6366
}

tests/fail/dyn-upcast-trait-mismatch.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: upcast on a pointer whose vtable does not match its type
1+
error: Undefined Behavior: using vtable for trait `Baz` but trait `Bar` was expected
22
--> $DIR/dyn-upcast-trait-mismatch.rs:LL:CC
33
|
4-
LL | let _err = baz_fake as &dyn Foo;
5-
| ^^^^^^^^ upcast on a pointer whose vtable does not match its type
4+
LL | let _err = baz_fake as *const dyn Foo;
5+
| ^^^^^^^^ using vtable for trait `Baz` but trait `Bar` was expected
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::mem;
2+
3+
// Make sure we notice the mismatch also if the difference is "only" in the generic
4+
// parameters of the trait.
5+
6+
trait Trait<T> {}
7+
impl<T> Trait<T> for T {}
8+
9+
fn main() {
10+
let x: &dyn Trait<i32> = &0;
11+
let _y: *const dyn Trait<u32> = unsafe { mem::transmute(x) }; //~ERROR: wrong trait
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: constructing invalid value: wrong trait in wide pointer vtable: expected `Trait<u32>`, but encountered `Trait<i32>`
2+
--> $DIR/wrong-dyn-trait-generic.rs:LL:CC
3+
|
4+
LL | let _y: *const dyn Trait<u32> = unsafe { mem::transmute(x) };
5+
| ^^^^^^^^^^^^^^^^^ constructing invalid value: wrong trait in wide pointer vtable: expected `Trait<u32>`, but encountered `Trait<i32>`
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/wrong-dyn-trait-generic.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::{fmt, mem};
2+
3+
fn main() {
4+
let x: &dyn Send = &0;
5+
let _y: *const dyn fmt::Debug = unsafe { mem::transmute(x) }; //~ERROR: wrong trait
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: constructing invalid value: wrong trait in wide pointer vtable: expected `std::fmt::Debug`, but encountered `<trivial>`
2+
--> $DIR/wrong-dyn-trait.rs:LL:CC
3+
|
4+
LL | let _y: *const dyn fmt::Debug = unsafe { mem::transmute(x) };
5+
| ^^^^^^^^^^^^^^^^^ constructing invalid value: wrong trait in wide pointer vtable: expected `std::fmt::Debug`, but encountered `<trivial>`
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/wrong-dyn-trait.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)