Skip to content

Commit 0c5c3bb

Browse files
committed
handle unevaluated consts after monomophize
1 parent 98803c1 commit 0c5c3bb

File tree

5 files changed

+77
-21
lines changed

5 files changed

+77
-21
lines changed

src/librustc_codegen_ssa/mir/constant.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,36 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
4040
&mut self,
4141
constant: &mir::Constant<'tcx>,
4242
) -> Result<ConstValue<'tcx>, ErrorHandled> {
43-
match constant.literal.val {
43+
let const_ = match constant.literal.val {
4444
ty::ConstKind::Unevaluated(def_id, substs, promoted) => {
4545
let substs = self.monomorphize(&substs);
46-
self.cx
47-
.tcx()
48-
.const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None)
49-
.map_err(|err| {
50-
if promoted.is_none() {
51-
self.cx
52-
.tcx()
53-
.sess
54-
.span_err(constant.span, "erroneous constant encountered");
55-
}
56-
err
57-
})
46+
ty::ConstKind::Unevaluated(def_id, substs, promoted)
5847
}
48+
ty::ConstKind::Value(value) => ty::ConstKind::Value(value),
49+
ty::ConstKind::Param(_) => self.monomorphize(&constant.literal).val,
50+
_ => span_bug!(constant.span, "encountered bad Const in codegen: {:?}", constant),
51+
};
52+
53+
match const_ {
54+
ty::ConstKind::Unevaluated(def_id, substs, promoted) => self
55+
.cx
56+
.tcx()
57+
.const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None)
58+
.map_err(|err| {
59+
if promoted.is_none() {
60+
self.cx
61+
.tcx()
62+
.sess
63+
.span_err(constant.span, "erroneous constant encountered");
64+
}
65+
err
66+
}),
5967
ty::ConstKind::Value(value) => Ok(value),
60-
_ => {
61-
let const_ = self.monomorphize(&constant.literal);
62-
if let ty::ConstKind::Value(value) = const_.val {
63-
Ok(value)
64-
} else {
65-
span_bug!(constant.span, "encountered bad ConstKind in codegen: {:?}", const_);
66-
}
67-
}
68+
_ => span_bug!(
69+
constant.span,
70+
"encountered bad ConstKind after monomorphizing: {:?}",
71+
const_
72+
),
6873
}
6974
}
7075

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// run-pass
2+
#![feature(const_generics)]
3+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
4+
5+
const L: usize = 4;
6+
7+
pub trait Print<const N: usize> {
8+
fn print(&self) -> usize {
9+
N
10+
}
11+
}
12+
13+
pub struct Printer;
14+
impl Print<L> for Printer {}
15+
16+
fn main() {
17+
let p = Printer;
18+
assert_eq!(p.print(), 4);
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/issue-70125.rs:2:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-pass
2+
3+
#![feature(const_generics)]
4+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
5+
6+
fn main() {
7+
<()>::foo();
8+
}
9+
10+
trait Foo<const X: usize> {
11+
fn foo() -> usize {
12+
X
13+
}
14+
}
15+
16+
impl Foo<{3}> for () {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/normalization_failure.rs:3:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+

0 commit comments

Comments
 (0)