Skip to content

Commit 2020c09

Browse files
authored
Rollup merge of rust-lang#105847 - compiler-errors:issue-104396, r=oli-obk
Ensure param-env is const before calling `eval_to_valtree` Other queries call `ParamEnv::with_const` *inside* of the query itself (e.g. `const_eval_global_id_for_typeck`), so this could alternatively be moved into the provider of `eval_to_valtree` instead. I don't have a particularly strong opinion, though *theoretically* caching is better if we make the query keys more constrained. I'm not exactly sure how this is an effect of the `-Zmir-opt-level=3` flag. Maybe something about the inliner causes us to inline an unevaluated const into a body where it can be evaluated, but where it has not yet been normalized. This seems likely, since we're inlining `from_fn_1::<{ N / 2 }, _>` in `from_fn_2`, which means that we will need to evaluate that constant during the const prop pass after inlining. Fixes rust-lang#104396
2 parents ec08211 + c1181e1 commit 2020c09

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
@@ -577,8 +577,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
577577
ty::ConstKind::Unevaluated(uv) => {
578578
let instance = self.resolve(uv.def, uv.substs)?;
579579
let cid = GlobalId { instance, promoted: None };
580-
self.ctfe_query(span, |tcx| tcx.eval_to_valtree(self.param_env.and(cid)))?
581-
.unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"))
580+
self.ctfe_query(span, |tcx| {
581+
tcx.eval_to_valtree(self.param_env.with_const().and(cid))
582+
})?
583+
.unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"))
582584
}
583585
ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => {
584586
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)