Skip to content

Commit 5378c1c

Browse files
committed
Auto merge of #119821 - oli-obk:reveal_all_const_evals, r=lcnr
Always use RevealAll for const eval queries implements what is described in #116803 (comment) Using `UserFacing` for const eval does not make sense anymore, unless we significantly change things like avoiding revealing opaque types. New tests are copied from #101478
2 parents 128148d + 867831a commit 5378c1c

32 files changed

+363
-108
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,10 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
225225
tcx: TyCtxt<'tcx>,
226226
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
227227
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
228-
// see comment in eval_to_allocation_raw_provider for what we're doing here
229-
if key.param_env.reveal() == Reveal::All {
230-
let mut key = key;
231-
key.param_env = key.param_env.with_user_facing();
232-
match tcx.eval_to_const_value_raw(key) {
233-
// try again with reveal all as requested
234-
Err(ErrorHandled::TooGeneric(_)) => {}
235-
// deduplicate calls
236-
other => return other,
237-
}
238-
}
228+
// Const eval always happens in Reveal::All mode in order to be able to use the hidden types of
229+
// opaque types. This is needed for trivial things like `size_of`, but also for using associated
230+
// types that are not specified in the opaque type.
231+
assert_eq!(key.param_env.reveal(), Reveal::All);
239232

240233
// We call `const_eval` for zero arg intrinsics, too, in order to cache their value.
241234
// Catch such calls and evaluate them instead of trying to load a constant's MIR.
@@ -265,24 +258,11 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
265258
tcx: TyCtxt<'tcx>,
266259
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
267260
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
268-
// Because the constant is computed twice (once per value of `Reveal`), we are at risk of
269-
// reporting the same error twice here. To resolve this, we check whether we can evaluate the
270-
// constant in the more restrictive `Reveal::UserFacing`, which most likely already was
271-
// computed. For a large percentage of constants that will already have succeeded. Only
272-
// associated constants of generic functions will fail due to not enough monomorphization
273-
// information being available.
274-
275-
// In case we fail in the `UserFacing` variant, we just do the real computation.
276-
if key.param_env.reveal() == Reveal::All {
277-
let mut key = key;
278-
key.param_env = key.param_env.with_user_facing();
279-
match tcx.eval_to_allocation_raw(key) {
280-
// try again with reveal all as requested
281-
Err(ErrorHandled::TooGeneric(_)) => {}
282-
// deduplicate calls
283-
other => return other,
284-
}
285-
}
261+
// Const eval always happens in Reveal::All mode in order to be able to use the hidden types of
262+
// opaque types. This is needed for trivial things like `size_of`, but also for using associated
263+
// types that are not specified in the opaque type.
264+
265+
assert_eq!(key.param_env.reveal(), Reveal::All);
286266
if cfg!(debug_assertions) {
287267
// Make sure we format the instance even if we do not print it.
288268
// This serves as a regression test against an ICE on printing.

compiler/rustc_middle/src/mir/interpret/queries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'tcx> TyCtxt<'tcx> {
145145
) -> EvalToConstValueResult<'tcx> {
146146
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
147147
// improve caching of queries.
148-
let inputs = self.erase_regions(param_env.and(cid));
148+
let inputs = self.erase_regions(param_env.with_reveal_all_normalized(self).and(cid));
149149
if let Some(span) = span {
150150
// The query doesn't know where it is being invoked, so we need to fix the span.
151151
self.at(span).eval_to_const_value_raw(inputs).map_err(|e| e.with_span(span))
@@ -164,7 +164,7 @@ impl<'tcx> TyCtxt<'tcx> {
164164
) -> EvalToValTreeResult<'tcx> {
165165
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
166166
// improve caching of queries.
167-
let inputs = self.erase_regions(param_env.and(cid));
167+
let inputs = self.erase_regions(param_env.with_reveal_all_normalized(self).and(cid));
168168
debug!(?inputs);
169169
if let Some(span) = span {
170170
// The query doesn't know where it is being invoked, so we need to fix the span.

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`.
99
|
1010
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
1111
| ^^^^^^^^^^^^^^^^^^^^^^^
12-
note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`...
13-
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
14-
|
15-
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
16-
| ^^^^^^^^^^^^^^^^^^^^^^^
1712
note: ...which requires const-evaluating + checking `IMPL_REF_BAR`...
1813
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:27
1914
|

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ note: ...which requires simplifying constant for the type system `DEFAULT_REF_BA
99
|
1010
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: ...which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
13-
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
14-
|
15-
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1712
note: ...which requires const-evaluating + checking `DEFAULT_REF_BAR`...
1813
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:30
1914
|

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`
99
|
1010
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`...
13-
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
14-
|
15-
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^
1712
note: ...which requires const-evaluating + checking `TRAIT_REF_BAR`...
1813
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:28
1914
|

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | const X: i32 = 1 / 0;
77
query stack during panic:
88
#0 [eval_to_allocation_raw] const-evaluating + checking `X`
99
#1 [eval_to_const_value_raw] simplifying constant for the type system `X`
10-
#2 [eval_to_const_value_raw] simplifying constant for the type system `X`
11-
#3 [lint_mod] linting top-level module
12-
#4 [analysis] running analysis passes on this crate
10+
#2 [lint_mod] linting top-level module
11+
#3 [analysis] running analysis passes on this crate
1312
end of query stack

tests/ui/consts/const-size_of-cycle.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ LL | bytes: [u8; std::mem::size_of::<Foo>()]
77
note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`...
88
--> $DIR/const-size_of-cycle.rs:4:17
99
|
10-
LL | bytes: [u8; std::mem::size_of::<Foo>()]
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`...
13-
--> $DIR/const-size_of-cycle.rs:4:17
14-
|
1510
LL | bytes: [u8; std::mem::size_of::<Foo>()]
1611
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1712
= note: ...which requires computing layout of `Foo`...

tests/ui/consts/issue-36163.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ note: ...which requires const-evaluating + checking `A`...
2020
LL | const A: isize = Foo::B as isize;
2121
| ^^^^^^^^^^^^^^^
2222
= note: ...which again requires simplifying constant for the type system `Foo::B::{constant#0}`, completing the cycle
23-
note: cycle used when simplifying constant for the type system `Foo::B::{constant#0}`
24-
--> $DIR/issue-36163.rs:4:9
23+
note: cycle used when collecting item types in top-level module
24+
--> $DIR/issue-36163.rs:1:1
2525
|
26-
LL | B = A,
27-
| ^
26+
LL | / const A: isize = Foo::B as isize;
27+
LL | |
28+
LL | | enum Foo {
29+
LL | | B = A,
30+
LL | | }
31+
LL | |
32+
LL | | fn main() {}
33+
| |____________^
2834
= 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
2935

3036
error: aborting due to 1 previous error

tests/ui/consts/issue-44415.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
77
note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`...
88
--> $DIR/issue-44415.rs:6:17
99
|
10-
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`...
13-
--> $DIR/issue-44415.rs:6:17
14-
|
1510
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
1611
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1712
= note: ...which requires computing layout of `Foo`...

tests/ui/consts/recursive-zst-static.default.stderr

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
error[E0391]: cycle detected when const-evaluating + checking `FOO`
2-
--> $DIR/recursive-zst-static.rs:10:1
3-
|
4-
LL | static FOO: () = FOO;
5-
| ^^^^^^^^^^^^^^
6-
|
7-
note: ...which requires const-evaluating + checking `FOO`...
82
--> $DIR/recursive-zst-static.rs:10:18
93
|
104
LL | static FOO: () = FOO;
115
| ^^^
12-
= note: ...which again requires const-evaluating + checking `FOO`, completing the cycle
6+
|
7+
= note: ...which immediately requires const-evaluating + checking `FOO` again
138
note: cycle used when linting top-level module
149
--> $DIR/recursive-zst-static.rs:10:1
1510
|

0 commit comments

Comments
 (0)