Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 18751a7

Browse files
committed
safe transmute: gracefully handle const params of wrong types
ref: https://github.com/rust-lang/rust/pull/92268/files#r925244819
1 parent bc4a1de commit 18751a7

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
288288
.substs
289289
.const_at(i)
290290
.try_eval_bool(self.tcx(), obligation.param_env)
291-
.unwrap()
291+
.unwrap_or(true)
292292
};
293293

294294
let src_and_dst = predicate.map_bound(|p| rustc_transmute::Types {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! The implementation must behave well if const values of wrong types are
2+
//! provided.
3+
4+
#![crate_type = "lib"]
5+
#![feature(transmutability)]
6+
#![allow(dead_code, incomplete_features, non_camel_case_types)]
7+
8+
mod assert {
9+
use std::mem::BikeshedIntrinsicFrom;
10+
11+
pub fn is_transmutable<
12+
Src,
13+
Dst,
14+
Context,
15+
const ASSUME_ALIGNMENT: bool,
16+
const ASSUME_LIFETIMES: bool,
17+
const ASSUME_VALIDITY: bool,
18+
const ASSUME_VISIBILITY: bool,
19+
>()
20+
where
21+
Dst: BikeshedIntrinsicFrom<
22+
Src,
23+
Context,
24+
ASSUME_ALIGNMENT,
25+
ASSUME_LIFETIMES,
26+
ASSUME_VALIDITY,
27+
ASSUME_VISIBILITY,
28+
>,
29+
{}
30+
}
31+
32+
fn test() {
33+
struct Context;
34+
#[repr(C)] struct Src;
35+
#[repr(C)] struct Dst;
36+
assert::is_transmutable::<Src, Dst, Context, {0u8}, false, false, false>(); //~ ERROR mismatched types
37+
assert::is_transmutable::<Src, Dst, Context, false, {0u8}, false, false>(); //~ ERROR mismatched types
38+
assert::is_transmutable::<Src, Dst, Context, false, false, {0u8}, false>(); //~ ERROR mismatched types
39+
assert::is_transmutable::<Src, Dst, Context, false, false, false, {0u8}>(); //~ ERROR mismatched types
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/wrong-type-assume.rs:36:51
3+
|
4+
LL | assert::is_transmutable::<Src, Dst, Context, {0u8}, false, false, false>();
5+
| ^^^ expected `bool`, found `u8`
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/wrong-type-assume.rs:37:58
9+
|
10+
LL | assert::is_transmutable::<Src, Dst, Context, false, {0u8}, false, false>();
11+
| ^^^ expected `bool`, found `u8`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/wrong-type-assume.rs:38:65
15+
|
16+
LL | assert::is_transmutable::<Src, Dst, Context, false, false, {0u8}, false>();
17+
| ^^^ expected `bool`, found `u8`
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/wrong-type-assume.rs:39:72
21+
|
22+
LL | assert::is_transmutable::<Src, Dst, Context, false, false, false, {0u8}>();
23+
| ^^^ expected `bool`, found `u8`
24+
25+
error: aborting due to 4 previous errors
26+
27+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)