Skip to content

Commit 3f6e4db

Browse files
committed
Reuse result from eval_to_const_value_raw.
1 parent fde15b0 commit 3f6e4db

File tree

9 files changed

+78
-54
lines changed

9 files changed

+78
-54
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,30 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
286286
tcx: TyCtxt<'tcx>,
287287
key: ty::PseudoCanonicalInput<'tcx, GlobalId<'tcx>>,
288288
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
289+
let ty::PseudoCanonicalInput { typing_env, value } = key;
290+
291+
// Const eval always happens in PostAnalysis or Codegen mode. See the comment in
292+
// `InterpCx::new` for more details.
293+
debug_assert_matches!(
294+
typing_env.typing_mode,
295+
ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen
296+
);
297+
298+
// We are in codegen. It's very likely this constant has been evaluated in PostAnalysis before.
299+
// Try to reuse this evaluation, and only re-run if we hit a `TooGeneric` error.
300+
if let ty::TypingMode::Codegen = typing_env.typing_mode {
301+
let with_postanalysis = ty::TypingEnv {
302+
typing_mode: ty::TypingMode::PostAnalysis,
303+
param_env: typing_env.param_env,
304+
};
305+
let with_postanalysis =
306+
tcx.eval_to_const_value_raw(with_postanalysis.as_query_input(value));
307+
match with_postanalysis {
308+
Ok(_) | Err(ErrorHandled::Reported(..)) => return with_postanalysis,
309+
Err(ErrorHandled::TooGeneric(_)) => {}
310+
}
311+
}
312+
289313
tcx.eval_to_allocation_raw(key).map(|val| turn_into_const_value(tcx, val, key))
290314
}
291315

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.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 `IMPL_REF_BAR`
1+
error[E0391]: cycle detected when simplifying constant for the type system `IMPL_REF_BAR`
2+
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
3+
|
4+
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: ...which requires const-evaluating + checking `IMPL_REF_BAR`...
28
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:27
39
|
410
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
511
| ^^^^^^^^^^^^^^^^^^
6-
|
712
note: ...which requires simplifying constant for the type system `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`...
813
--> $DIR/issue-24949-assoc-const-static-recursion-impl.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 = IMPL_REF_BAR;
2631
| ^^^^^^^^^^^^
27-
note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`...
28-
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
29-
|
30-
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
31-
| ^^^^^^^^^^^^^^^^^^^^^^^
32-
= note: ...which again requires const-evaluating + checking `IMPL_REF_BAR`, completing the cycle
33-
note: cycle used when const-evaluating + checking `IMPL_REF_BAR`
32+
= note: ...which again requires simplifying constant for the type system `IMPL_REF_BAR`, completing the cycle
33+
note: cycle used when simplifying constant for the type system `IMPL_REF_BAR`
3434
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
3535
|
3636
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;

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 this crate
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

tests/ui/issues/issue-17252.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
error[E0391]: cycle detected when const-evaluating + checking `FOO`
2-
--> $DIR/issue-17252.rs:1:20
1+
error[E0391]: cycle detected when simplifying constant for the type system `FOO`
2+
--> $DIR/issue-17252.rs:1:1
33
|
44
LL | const FOO: usize = FOO;
5-
| ^^^
5+
| ^^^^^^^^^^^^^^^^
66
|
7-
note: ...which requires simplifying constant for the type system `FOO`...
8-
--> $DIR/issue-17252.rs:1:1
7+
note: ...which requires const-evaluating + checking `FOO`...
8+
--> $DIR/issue-17252.rs:1:20
99
|
1010
LL | const FOO: usize = FOO;
11-
| ^^^^^^^^^^^^^^^^
12-
= note: ...which again requires const-evaluating + checking `FOO`, completing the cycle
13-
note: cycle used when const-evaluating + checking `FOO`
11+
| ^^^
12+
= note: ...which again requires simplifying constant for the type system `FOO`, completing the cycle
13+
note: cycle used when simplifying constant for the type system `FOO`
1414
--> $DIR/issue-17252.rs:1:1
1515
|
1616
LL | const FOO: usize = FOO;
1717
| ^^^^^^^^^^^^^^^^
1818
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
1919

20-
error[E0391]: cycle detected when const-evaluating + checking `main::BAR`
21-
--> $DIR/issue-17252.rs:6:28
20+
error[E0391]: cycle detected when simplifying constant for the type system `main::BAR`
21+
--> $DIR/issue-17252.rs:6:9
2222
|
2323
LL | const BAR: usize = BAR;
24-
| ^^^
24+
| ^^^^^^^^^^^^^^^^
2525
|
26-
note: ...which requires simplifying constant for the type system `main::BAR`...
27-
--> $DIR/issue-17252.rs:6:9
26+
note: ...which requires const-evaluating + checking `main::BAR`...
27+
--> $DIR/issue-17252.rs:6:28
2828
|
2929
LL | const BAR: usize = BAR;
30-
| ^^^^^^^^^^^^^^^^
31-
= note: ...which again requires const-evaluating + checking `main::BAR`, completing the cycle
32-
note: cycle used when const-evaluating + checking `main::BAR`
30+
| ^^^
31+
= note: ...which again requires simplifying constant for the type system `main::BAR`, completing the cycle
32+
note: cycle used when simplifying constant for the type system `main::BAR`
3333
--> $DIR/issue-17252.rs:6:9
3434
|
3535
LL | const BAR: usize = BAR;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ compile-flags: -Z threads=2
22

3-
const FOO: usize = FOO; //~ERROR cycle detected when const-evaluating + checking `FOO`
3+
const FOO: usize = FOO; //~ERROR cycle detected when simplifying constant for the type system `FOO`
44

55
fn main() {}

tests/ui/parallel-rustc/cycle_crash.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error[E0391]: cycle detected when const-evaluating + checking `FOO`
2-
--> $DIR/cycle_crash.rs:3:20
1+
error[E0391]: cycle detected when simplifying constant for the type system `FOO`
2+
--> $DIR/cycle_crash.rs:3:1
33
|
44
LL | const FOO: usize = FOO;
5-
| ^^^
5+
| ^^^^^^^^^^^^^^^^
66
|
7-
note: ...which requires simplifying constant for the type system `FOO`...
8-
--> $DIR/cycle_crash.rs:3:1
7+
note: ...which requires const-evaluating + checking `FOO`...
8+
--> $DIR/cycle_crash.rs:3:20
99
|
1010
LL | const FOO: usize = FOO;
11-
| ^^^^^^^^^^^^^^^^
12-
= note: ...which again requires const-evaluating + checking `FOO`, completing the cycle
13-
note: cycle used when const-evaluating + checking `FOO`
11+
| ^^^
12+
= note: ...which again requires simplifying constant for the type system `FOO`, completing the cycle
13+
note: cycle used when simplifying constant for the type system `FOO`
1414
--> $DIR/cycle_crash.rs:3:1
1515
|
1616
LL | const FOO: usize = FOO;

tests/ui/recursion/issue-23302-3.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 `A`
1+
error[E0391]: cycle detected when simplifying constant for the type system `A`
2+
--> $DIR/issue-23302-3.rs:1:1
3+
|
4+
LL | const A: i32 = B;
5+
| ^^^^^^^^^^^^
6+
|
7+
note: ...which requires const-evaluating + checking `A`...
28
--> $DIR/issue-23302-3.rs:1:16
39
|
410
LL | const A: i32 = B;
511
| ^
6-
|
712
note: ...which requires simplifying constant for the type system `B`...
813
--> $DIR/issue-23302-3.rs:3:1
914
|
@@ -14,13 +19,8 @@ note: ...which requires const-evaluating + checking `B`...
1419
|
1520
LL | const B: i32 = A;
1621
| ^
17-
note: ...which requires simplifying constant for the type system `A`...
18-
--> $DIR/issue-23302-3.rs:1:1
19-
|
20-
LL | const A: i32 = B;
21-
| ^^^^^^^^^^^^
22-
= note: ...which again requires const-evaluating + checking `A`, completing the cycle
23-
note: cycle used when const-evaluating + checking `A`
22+
= note: ...which again requires simplifying constant for the type system `A`, completing the cycle
23+
note: cycle used when simplifying constant for the type system `A`
2424
--> $DIR/issue-23302-3.rs:1:1
2525
|
2626
LL | const A: i32 = B;

0 commit comments

Comments
 (0)