Skip to content

Commit c1181e1

Browse files
Ensure param-env is const before calling eval_to_valtree
1 parent 984eab5 commit c1181e1

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
569569
ty::ConstKind::Unevaluated(uv) => {
570570
let instance = self.resolve(uv.def, uv.substs)?;
571571
let cid = GlobalId { instance, promoted: None };
572-
self.ctfe_query(span, |tcx| tcx.eval_to_valtree(self.param_env.and(cid)))?
573-
.unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"))
572+
self.ctfe_query(span, |tcx| {
573+
tcx.eval_to_valtree(self.param_env.with_const().and(cid))
574+
})?
575+
.unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"))
574576
}
575577
ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => {
576578
span_bug!(self.cur_span(), "unexpected ConstKind in ctfe: {val:?}")

src/test/ui/consts/issue-104396.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// compile-flags: -Zmir-opt-level=3
2+
// check-pass
3+
4+
#![feature(generic_const_exprs)]
5+
//~^ WARN the feature `generic_const_exprs` is incomplete
6+
7+
#[inline(always)]
8+
fn from_fn_1<const N: usize, F: FnMut(usize) -> f32>(mut f: F) -> [f32; N] {
9+
let mut result = [0.0; N];
10+
let mut i = 0;
11+
while i < N {
12+
result[i] = f(i);
13+
i += 1;
14+
}
15+
result
16+
}
17+
18+
pub struct TestArray<const N: usize>
19+
where
20+
[(); N / 2]:,
21+
{
22+
array: [f32; N / 2],
23+
}
24+
25+
impl<const N: usize> TestArray<N>
26+
where
27+
[(); N / 2]:,
28+
{
29+
fn from_fn_2<F: FnMut(usize) -> f32>(f: F) -> Self {
30+
Self { array: from_fn_1(f) }
31+
}
32+
}
33+
34+
fn main() {
35+
TestArray::<4>::from_fn_2(|i| 0.0);
36+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/issue-104396.rs:4:12
3+
|
4+
LL | #![feature(generic_const_exprs)]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+

0 commit comments

Comments
 (0)