Skip to content

Commit c097b2c

Browse files
author
Lukas Markeffsky
committed
transmutability: fix ICE when passing wrong ADT to ASSUME
1 parent e060723 commit c097b2c

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

compiler/rustc_transmute/src/lib.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ mod rustc {
8484
use rustc_infer::infer::InferCtxt;
8585
use rustc_macros::TypeVisitable;
8686
use rustc_middle::traits::ObligationCause;
87-
use rustc_middle::ty::{Const, ParamEnv, Ty, TyCtxt, ValTree};
87+
use rustc_middle::ty::{Const, ParamEnv, Ty, TyCtxt};
8888

8989
use super::*;
9090

@@ -139,25 +139,21 @@ mod rustc {
139139

140140
let adt_def = cv.ty.ty_adt_def()?;
141141

142-
assert_eq!(
143-
tcx.require_lang_item(LangItem::TransmuteOpts, None),
144-
adt_def.did(),
145-
"The given `Const` was not marked with the `{}` lang item.",
146-
LangItem::TransmuteOpts.name(),
147-
);
142+
if !tcx.is_lang_item(adt_def.did(), LangItem::TransmuteOpts) {
143+
tcx.dcx().delayed_bug(format!(
144+
"The given `const` was not marked with the `{}` lang item.",
145+
LangItem::TransmuteOpts.name()
146+
));
147+
return Some(Self {
148+
alignment: true,
149+
lifetimes: true,
150+
safety: true,
151+
validity: true,
152+
});
153+
}
148154

149155
let variant = adt_def.non_enum_variant();
150-
let fields = match cv.valtree {
151-
ValTree::Branch(branch) => branch,
152-
_ => {
153-
return Some(Self {
154-
alignment: true,
155-
lifetimes: true,
156-
safety: true,
157-
validity: true,
158-
});
159-
}
160-
};
156+
let fields = cv.valtree.unwrap_branch();
161157

162158
let get_field = |name| {
163159
let (field_idx, _) = variant
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Test that we don't ICE when passing the wrong ADT to ASSUME.
2+
3+
#![feature(adt_const_params)]
4+
#![feature(transmutability)]
5+
6+
use std::marker::ConstParamTy;
7+
use std::mem::TransmuteFrom;
8+
9+
#[derive(ConstParamTy, PartialEq, Eq)]
10+
struct NotAssume;
11+
12+
fn foo<const ASSUME: NotAssume>()
13+
where
14+
u8: TransmuteFrom<u8, ASSUME>, //~ ERROR the constant `ASSUME` is not of type `Assume`
15+
{
16+
}
17+
18+
fn main() {
19+
foo::<{ NotAssume }>();
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: the constant `ASSUME` is not of type `Assume`
2+
--> $DIR/wrong-adt-assume.rs:14:9
3+
|
4+
LL | u8: TransmuteFrom<u8, ASSUME>,
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Assume`, found `NotAssume`
6+
|
7+
note: required by a const generic parameter in `TransmuteFrom`
8+
--> $SRC_DIR/core/src/mem/transmutability.rs:LL:COL
9+
10+
error: aborting due to 1 previous error
11+

0 commit comments

Comments
 (0)