Skip to content

Commit 090d210

Browse files
committed
Reuse result from eval_to_const_value_raw.
1 parent ebaf329 commit 090d210

4 files changed

Lines changed: 43 additions & 12 deletions

File tree

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,40 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
327327
tcx: TyCtxt<'tcx>,
328328
key: ty::PseudoCanonicalInput<'tcx, GlobalId<'tcx>>,
329329
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
330-
if let Some((value, _ty)) = tcx.trivial_const(key.value.instance.def_id()) {
330+
let ty::PseudoCanonicalInput { typing_env, value } = key;
331+
332+
if let Some((value, _ty)) = tcx.trivial_const(value.instance.def_id()) {
331333
return Ok(value);
332334
}
335+
336+
match typing_env.typing_mode().assert_not_erased() {
337+
ty::TypingMode::PostAnalysis => {}
338+
// We are in codegen. It's very likely this constant has been evaluated in PostAnalysis before.
339+
// Try to reuse this evaluation, and only re-run if we hit a `TooGeneric` error.
340+
ty::TypingMode::Codegen => {
341+
let with_postanalysis =
342+
ty::TypingEnv::new(typing_env.param_env, ty::TypingMode::PostAnalysis);
343+
let with_postanalysis =
344+
tcx.eval_to_const_value_raw(with_postanalysis.as_query_input(value));
345+
match with_postanalysis {
346+
Ok(_) | Err(ErrorHandled::Reported(..)) => return with_postanalysis,
347+
Err(ErrorHandled::TooGeneric(_)) => {}
348+
}
349+
}
350+
// Const eval always happens in PostAnalysis or Codegen mode. See the comment in
351+
// `InterpCx::new` for more details.
352+
ty::TypingMode::Coherence
353+
| ty::TypingMode::Analysis { .. }
354+
| ty::TypingMode::Borrowck { .. }
355+
| ty::TypingMode::PostBorrowckAnalysis { .. } => {
356+
if cfg!(debug_assertions) {
357+
bug!(
358+
"Const eval should always happens in PostAnalysis or Codegen mode. See the comment in `InterpCx::new` for more details."
359+
)
360+
}
361+
}
362+
}
363+
333364
tcx.eval_to_allocation_raw(key).map(|val| turn_into_const_value(tcx, val, key))
334365
}
335366

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
error[E0391]: cycle detected when const-evaluating + checking `TRAIT_REF_BAR`
1+
error[E0391]: cycle detected when simplifying constant for the type system `TRAIT_REF_BAR`
2+
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
3+
|
4+
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: ...which requires const-evaluating + checking `TRAIT_REF_BAR`...
28
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:28
39
|
410
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
511
| ^^^^^^^^^^^^^^^^^^^^^
6-
|
712
note: ...which requires simplifying constant for the type system `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`...
813
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:5
914
|
@@ -24,13 +29,8 @@ note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-c
2429
|
2530
LL | const BAR: u32 = TRAIT_REF_BAR;
2631
| ^^^^^^^^^^^^^
27-
note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`...
28-
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
29-
|
30-
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^
32-
= note: ...which again requires const-evaluating + checking `TRAIT_REF_BAR`, completing the cycle
33-
note: cycle used when const-evaluating + checking `TRAIT_REF_BAR`
32+
= note: ...which again requires simplifying constant for the type system `TRAIT_REF_BAR`, completing the cycle
33+
note: cycle used when simplifying constant for the type system `TRAIT_REF_BAR`
3434
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
3535
|
3636
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;

tests/ui/consts/const-eval/const-eval-query-stack.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: please make sure that you have updated to the latest nightly
99

1010
query stack during panic:
1111
#0 [eval_to_allocation_raw] const-evaluating + checking `X`
12-
#1 [eval_to_allocation_raw] const-evaluating + checking `X`
12+
#1 [eval_to_const_value_raw] simplifying constant for the type system `X`
1313
#2 [eval_to_const_value_raw] simplifying constant for the type system `X`
1414
#3 [analysis] running analysis passes on crate `const_eval_query_stack`
1515
end of query stack

tests/ui/consts/recursive-block.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | const { foo::<&T>() }
55
| ^^^^^^^^^^^^^^^
66
|
77
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`recursive_block`)
8-
= note: query depth increased by 1 when computing layout of `foo<&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&i32>`
8+
= note: query depth increased by 1 when computing layout of `foo<&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&i32>`
99

1010
error: aborting due to 1 previous error
1111

0 commit comments

Comments
 (0)