Skip to content

Commit 5dd0e76

Browse files
committed
Auto merge of #121387 - oli-obk:eager_const_failures_regression, r=<try>
Avoid some unnecessary query invocations. Specifically this inlines `const_eval_poly` and avoids computing the generic params, the param env, normalizing the param env and erasing lifetimes on everything. should fix the perf regression from #121087
2 parents 0987e41 + 56d7af2 commit 5dd0e76

23 files changed

+172
-145
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_hir::lang_items::LangItem;
1111
use rustc_hir::ItemKind;
1212
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1313
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
14+
use rustc_middle::mir::interpret::GlobalId;
1415
use rustc_middle::query::Providers;
1516
use rustc_middle::ty::print::with_no_trimmed_paths;
1617
use rustc_middle::ty::trait_def::TraitSpecializationKind;
@@ -291,9 +292,16 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
291292
check_item_fn(tcx, def_id, item.ident, item.span, sig.decl)
292293
}
293294
hir::ItemKind::Static(ty, ..) => {
295+
tcx.ensure().eval_static_initializer(def_id);
294296
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
295297
}
296-
hir::ItemKind::Const(ty, ..) => {
298+
hir::ItemKind::Const(ty, ast_generics, _) => {
299+
if ast_generics.params.is_empty() {
300+
let instance = ty::Instance::new(def_id.into(), ty::GenericArgs::empty());
301+
let cid = GlobalId { instance, promoted: None };
302+
let param_env = ty::ParamEnv::reveal_all();
303+
tcx.ensure().eval_to_const_value_raw(param_env.and(cid));
304+
}
297305
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
298306
}
299307
hir::ItemKind::Struct(_, ast_generics) => {

compiler/rustc_hir_analysis/src/lib.rs

-11
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,6 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
198198
collect::test_opaque_hidden_types(tcx)?;
199199
}
200200

201-
// Make sure we evaluate all static and (non-associated) const items, even if unused.
202-
// If any of these fail to evaluate, we do not want this crate to pass compilation.
203-
tcx.hir().par_body_owners(|item_def_id| {
204-
let def_kind = tcx.def_kind(item_def_id);
205-
match def_kind {
206-
DefKind::Static(_) => tcx.ensure().eval_static_initializer(item_def_id),
207-
DefKind::Const => tcx.ensure().const_eval_poly(item_def_id.into()),
208-
_ => (),
209-
}
210-
});
211-
212201
// Freeze definitions as we don't add new ones at this point. This improves performance by
213202
// allowing lock-free access to them.
214203
tcx.untracked().definitions.freeze();

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-c
3030
LL | const BAR: u32 = IMPL_REF_BAR;
3131
| ^^^^^^^^^^^^
3232
= note: ...which again requires simplifying constant for the type system `IMPL_REF_BAR`, completing the cycle
33-
= note: cycle used when running analysis passes on this crate
33+
note: cycle used when checking that `IMPL_REF_BAR` is well-formed
34+
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
35+
|
36+
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
37+
| ^^^^^^^^^^^^^^^^^^^^^^^
3438
= 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
3539

3640
error: aborting due to 1 previous error

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-c
3030
LL | const BAR: u32 = TRAIT_REF_BAR;
3131
| ^^^^^^^^^^^^^
3232
= note: ...which again requires simplifying constant for the type system `TRAIT_REF_BAR`, completing the cycle
33-
= note: cycle used when running analysis passes on this crate
33+
note: cycle used when checking that `TRAIT_REF_BAR` is well-formed
34+
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
35+
|
36+
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^
3438
= 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
3539

3640
error: aborting due to 1 previous error

tests/ui/associated-inherent-types/generic-associated-types-bad.item.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `String: Copy` is not satisfied
2-
--> $DIR/generic-associated-types-bad.rs:16:10
2+
--> $DIR/generic-associated-types-bad.rs:16:27
33
|
44
LL | const _: Ty::Pr<String> = String::new();
5-
| ^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
5+
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
66
|
77
note: required by a bound in `Ty::Pr`
88
--> $DIR/generic-associated-types-bad.rs:10:16
@@ -11,10 +11,10 @@ LL | type Pr<T: Copy> = T;
1111
| ^^^^ required by this bound in `Ty::Pr`
1212

1313
error[E0277]: the trait bound `String: Copy` is not satisfied
14-
--> $DIR/generic-associated-types-bad.rs:16:27
14+
--> $DIR/generic-associated-types-bad.rs:16:10
1515
|
1616
LL | const _: Ty::Pr<String> = String::new();
17-
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
17+
| ^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
1818
|
1919
note: required by a bound in `Ty::Pr`
2020
--> $DIR/generic-associated-types-bad.rs:10:16
+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
error[E0080]: evaluation of constant value failed
2-
--> $DIR/const-array-oob.rs:5:19
3-
|
4-
LL | const BLUB: [u32; FOO[4]] = [5, 6];
5-
| ^^^^^^ index out of bounds: the length is 3 but the index is 4
6-
71
error[E0080]: evaluation of constant value failed
82
--> $DIR/const-array-oob.rs:2:20
93
|
104
LL | const BAR: usize = FOO[5];
115
| ^^^^^^ index out of bounds: the length is 3 but the index is 5
126

7+
error[E0080]: evaluation of constant value failed
8+
--> $DIR/const-array-oob.rs:5:19
9+
|
10+
LL | const BLUB: [u32; FOO[4]] = [5, 6];
11+
| ^^^^^^ index out of bounds: the length is 3 but the index is 4
12+
1313
error: aborting due to 2 previous errors
1414

1515
For more information about this error, try `rustc --explain E0080`.

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ 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 [analysis] running analysis passes on this crate
10+
#2 [check_well_formed] checking that `X` is well-formed
11+
#3 [check_mod_type_wf] checking that types are well-formed in top-level module
12+
#4 [analysis] running analysis passes on this crate
1113
end of query stack

tests/ui/consts/const-unsized.stderr

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
11
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
2-
--> $DIR/const-unsized.rs:3:16
2+
--> $DIR/const-unsized.rs:3:35
33
|
44
LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
5-
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
8+
= note: constant expressions must have a statically known size
89

910
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
10-
--> $DIR/const-unsized.rs:3:35
11+
--> $DIR/const-unsized.rs:3:16
1112
|
1213
LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
14+
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
1415
|
1516
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
16-
= note: constant expressions must have a statically known size
1717

1818
error[E0277]: the size for values of type `str` cannot be known at compilation time
19-
--> $DIR/const-unsized.rs:7:18
19+
--> $DIR/const-unsized.rs:7:24
2020
|
2121
LL | const CONST_FOO: str = *"foo";
22-
| ^^^ doesn't have a size known at compile-time
22+
| ^^^^^^ doesn't have a size known at compile-time
2323
|
2424
= help: the trait `Sized` is not implemented for `str`
25+
= note: constant expressions must have a statically known size
2526

2627
error[E0277]: the size for values of type `str` cannot be known at compilation time
27-
--> $DIR/const-unsized.rs:7:24
28+
--> $DIR/const-unsized.rs:7:18
2829
|
2930
LL | const CONST_FOO: str = *"foo";
30-
| ^^^^^^ doesn't have a size known at compile-time
31+
| ^^^ doesn't have a size known at compile-time
3132
|
3233
= help: the trait `Sized` is not implemented for `str`
33-
= note: constant expressions must have a statically known size
3434

3535
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
36-
--> $DIR/const-unsized.rs:11:18
36+
--> $DIR/const-unsized.rs:11:37
3737
|
3838
LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
39-
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
4040
|
4141
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
42+
= note: constant expressions must have a statically known size
4243

4344
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
44-
--> $DIR/const-unsized.rs:11:37
45+
--> $DIR/const-unsized.rs:11:18
4546
|
4647
LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
48+
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
4849
|
4950
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
50-
= note: constant expressions must have a statically known size
5151

5252
error[E0277]: the size for values of type `str` cannot be known at compilation time
53-
--> $DIR/const-unsized.rs:15:20
53+
--> $DIR/const-unsized.rs:15:26
5454
|
5555
LL | static STATIC_BAR: str = *"bar";
56-
| ^^^ doesn't have a size known at compile-time
56+
| ^^^^^^ doesn't have a size known at compile-time
5757
|
5858
= help: the trait `Sized` is not implemented for `str`
59+
= note: constant expressions must have a statically known size
5960

6061
error[E0277]: the size for values of type `str` cannot be known at compilation time
61-
--> $DIR/const-unsized.rs:15:26
62+
--> $DIR/const-unsized.rs:15:20
6263
|
6364
LL | static STATIC_BAR: str = *"bar";
64-
| ^^^^^^ doesn't have a size known at compile-time
65+
| ^^^ doesn't have a size known at compile-time
6566
|
6667
= help: the trait `Sized` is not implemented for `str`
67-
= note: constant expressions must have a statically known size
6868

6969
error[E0161]: cannot move a value of type `str`
7070
--> $DIR/const-unsized.rs:20:48

tests/ui/consts/const_cmp_type_id.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
error[E0131]: `main` function is not allowed to have generic parameters
2-
--> $DIR/const_cmp_type_id.rs:7:14
3-
|
4-
LL | const fn main() {
5-
| ^ `main` cannot have generic parameters
6-
71
error[E0080]: evaluation of constant value failed
82
--> $DIR/const_cmp_type_id.rs:10:22
93
|
104
LL | const _A: bool = TypeId::of::<u8>() < TypeId::of::<u16>();
115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling non-const function `<TypeId as PartialOrd>::lt`
126

7+
error[E0131]: `main` function is not allowed to have generic parameters
8+
--> $DIR/const_cmp_type_id.rs:7:14
9+
|
10+
LL | const fn main() {
11+
| ^ `main` cannot have generic parameters
12+
1313
error[E0308]: mismatched types
1414
--> $DIR/const_cmp_type_id.rs:8:13
1515
|

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ note: ...which requires evaluating initializer of static `B`...
1616
LL | static B: () = A;
1717
| ^
1818
= note: ...which again requires evaluating initializer of static `A`, completing the cycle
19-
= note: cycle used when running analysis passes on this crate
19+
note: cycle used when checking that `A` is well-formed
20+
--> $DIR/recursive-zst-static.rs:13:1
21+
|
22+
LL | static A: () = B;
23+
| ^^^^^^^^^^^^
2024
= 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
2125

2226
error: aborting due to 2 previous errors

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ note: ...which requires evaluating initializer of static `B`...
1616
LL | static B: () = A;
1717
| ^
1818
= note: ...which again requires evaluating initializer of static `A`, completing the cycle
19-
= note: cycle used when running analysis passes on this crate
19+
note: cycle used when checking that `A` is well-formed
20+
--> $DIR/recursive-zst-static.rs:13:1
21+
|
22+
LL | static A: () = B;
23+
| ^^^^^^^^^^^^
2024
= 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
2125

2226
error: aborting due to 2 previous errors

tests/ui/error-codes/E0017.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
warning: creating a mutable reference to mutable static is discouraged
2-
--> $DIR/E0017.rs:18:52
3-
|
4-
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
5-
| ^^^^^^ mutable reference to mutable static
6-
|
7-
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
8-
= note: this will be a hard error in the 2024 edition
9-
= note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
10-
= note: `#[warn(static_mut_refs)]` on by default
11-
help: use `addr_of_mut!` instead to create a raw pointer
12-
|
13-
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { addr_of_mut!(M) };
14-
| ~~~~~~~~~~~~~~~
15-
161
warning: taking a mutable reference to a `const` item
172
--> $DIR/E0017.rs:10:30
183
|
@@ -60,6 +45,21 @@ error[E0764]: mutable references are not allowed in the final value of statics
6045
LL | static CONST_REF: &'static mut i32 = &mut C;
6146
| ^^^^^^
6247

48+
warning: creating a mutable reference to mutable static is discouraged
49+
--> $DIR/E0017.rs:18:52
50+
|
51+
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
52+
| ^^^^^^ mutable reference to mutable static
53+
|
54+
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
55+
= note: this will be a hard error in the 2024 edition
56+
= note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
57+
= note: `#[warn(static_mut_refs)]` on by default
58+
help: use `addr_of_mut!` instead to create a raw pointer
59+
|
60+
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { addr_of_mut!(M) };
61+
| ~~~~~~~~~~~~~~~
62+
6363
error[E0080]: it is undefined behavior to use this value
6464
--> $DIR/E0017.rs:18:1
6565
|

tests/ui/issues/issue-17252.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ note: ...which requires const-evaluating + checking `FOO`...
1010
LL | const FOO: usize = FOO;
1111
| ^^^
1212
= note: ...which again requires simplifying constant for the type system `FOO`, completing the cycle
13-
= note: cycle used when running analysis passes on this crate
13+
note: cycle used when checking that `FOO` is well-formed
14+
--> $DIR/issue-17252.rs:1:1
15+
|
16+
LL | const FOO: usize = FOO;
17+
| ^^^^^^^^^^^^^^^^
1418
= 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
1519

1620
error[E0391]: cycle detected when simplifying constant for the type system `main::BAR`
@@ -25,7 +29,11 @@ note: ...which requires const-evaluating + checking `main::BAR`...
2529
LL | const BAR: usize = BAR;
2630
| ^^^
2731
= note: ...which again requires simplifying constant for the type system `main::BAR`, completing the cycle
28-
= note: cycle used when running analysis passes on this crate
32+
note: cycle used when checking that `main::BAR` is well-formed
33+
--> $DIR/issue-17252.rs:6:9
34+
|
35+
LL | const BAR: usize = BAR;
36+
| ^^^^^^^^^^^^^^^^
2937
= 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
3038

3139
error: aborting due to 2 previous errors

tests/ui/issues/issue-23302-enum-infinite-recursion/issue-23302-3.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ note: ...which requires const-evaluating + checking `B`...
2020
LL | const B: i32 = A;
2121
| ^
2222
= note: ...which again requires simplifying constant for the type system `A`, completing the cycle
23-
= note: cycle used when running analysis passes on this crate
23+
note: cycle used when checking that `A` is well-formed
24+
--> $DIR/issue-23302-3.rs:1:1
25+
|
26+
LL | const A: i32 = B;
27+
| ^^^^^^^^^^^^
2428
= 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
2529

2630
error: aborting due to 1 previous error

tests/ui/issues/issue-24446.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
error[E0277]: `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
2-
--> $DIR/issue-24446.rs:2:17
3-
|
4-
LL | static foo: dyn Fn() -> u32 = || -> u32 {
5-
| ^^^^^^^^^^^^^^^ `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
6-
|
7-
= help: the trait `Sync` is not implemented for `(dyn Fn() -> u32 + 'static)`
8-
= note: shared static variables must have a type that implements `Sync`
9-
10-
error[E0277]: the size for values of type `(dyn Fn() -> u32 + 'static)` cannot be known at compilation time
11-
--> $DIR/issue-24446.rs:2:17
12-
|
13-
LL | static foo: dyn Fn() -> u32 = || -> u32 {
14-
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
15-
|
16-
= help: the trait `Sized` is not implemented for `(dyn Fn() -> u32 + 'static)`
17-
181
error[E0277]: the size for values of type `(dyn Fn() -> u32 + 'static)` cannot be known at compilation time
192
--> $DIR/issue-24446.rs:2:35
203
|
@@ -47,6 +30,23 @@ LL | | };
4730
= note: expected trait object `(dyn Fn() -> u32 + 'static)`
4831
found closure `{closure@$DIR/issue-24446.rs:2:35: 2:44}`
4932

33+
error[E0277]: `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
34+
--> $DIR/issue-24446.rs:2:17
35+
|
36+
LL | static foo: dyn Fn() -> u32 = || -> u32 {
37+
| ^^^^^^^^^^^^^^^ `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
38+
|
39+
= help: the trait `Sync` is not implemented for `(dyn Fn() -> u32 + 'static)`
40+
= note: shared static variables must have a type that implements `Sync`
41+
42+
error[E0277]: the size for values of type `(dyn Fn() -> u32 + 'static)` cannot be known at compilation time
43+
--> $DIR/issue-24446.rs:2:17
44+
|
45+
LL | static foo: dyn Fn() -> u32 = || -> u32 {
46+
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
47+
|
48+
= help: the trait `Sized` is not implemented for `(dyn Fn() -> u32 + 'static)`
49+
5050
error: aborting due to 4 previous errors
5151

5252
Some errors have detailed explanations: E0277, E0308.

0 commit comments

Comments
 (0)