safe transmute: gracefully handle const params of wrong types

ref: https://github.com/rust-lang/rust/pull/92268/files#r925244819
This commit is contained in:
Jack Wrenn 2022-07-20 19:57:55 +00:00
parent bc4a1dea41
commit 18751a708a
3 changed files with 68 additions and 1 deletions

View File

@ -288,7 +288,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
.substs .substs
.const_at(i) .const_at(i)
.try_eval_bool(self.tcx(), obligation.param_env) .try_eval_bool(self.tcx(), obligation.param_env)
.unwrap() .unwrap_or(true)
}; };
let src_and_dst = predicate.map_bound(|p| rustc_transmute::Types { let src_and_dst = predicate.map_bound(|p| rustc_transmute::Types {

View File

@ -0,0 +1,40 @@
//! The implementation must behave well if const values of wrong types are
//! provided.
#![crate_type = "lib"]
#![feature(transmutability)]
#![allow(dead_code, incomplete_features, non_camel_case_types)]
mod assert {
use std::mem::BikeshedIntrinsicFrom;
pub fn is_transmutable<
Src,
Dst,
Context,
const ASSUME_ALIGNMENT: bool,
const ASSUME_LIFETIMES: bool,
const ASSUME_VALIDITY: bool,
const ASSUME_VISIBILITY: bool,
>()
where
Dst: BikeshedIntrinsicFrom<
Src,
Context,
ASSUME_ALIGNMENT,
ASSUME_LIFETIMES,
ASSUME_VALIDITY,
ASSUME_VISIBILITY,
>,
{}
}
fn test() {
struct Context;
#[repr(C)] struct Src;
#[repr(C)] struct Dst;
assert::is_transmutable::<Src, Dst, Context, {0u8}, false, false, false>(); //~ ERROR mismatched types
assert::is_transmutable::<Src, Dst, Context, false, {0u8}, false, false>(); //~ ERROR mismatched types
assert::is_transmutable::<Src, Dst, Context, false, false, {0u8}, false>(); //~ ERROR mismatched types
assert::is_transmutable::<Src, Dst, Context, false, false, false, {0u8}>(); //~ ERROR mismatched types
}

View File

@ -0,0 +1,27 @@
error[E0308]: mismatched types
--> $DIR/wrong-type-assume.rs:36:51
|
LL | assert::is_transmutable::<Src, Dst, Context, {0u8}, false, false, false>();
| ^^^ expected `bool`, found `u8`
error[E0308]: mismatched types
--> $DIR/wrong-type-assume.rs:37:58
|
LL | assert::is_transmutable::<Src, Dst, Context, false, {0u8}, false, false>();
| ^^^ expected `bool`, found `u8`
error[E0308]: mismatched types
--> $DIR/wrong-type-assume.rs:38:65
|
LL | assert::is_transmutable::<Src, Dst, Context, false, false, {0u8}, false>();
| ^^^ expected `bool`, found `u8`
error[E0308]: mismatched types
--> $DIR/wrong-type-assume.rs:39:72
|
LL | assert::is_transmutable::<Src, Dst, Context, false, false, false, {0u8}>();
| ^^^ expected `bool`, found `u8`
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0308`.