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

Lines changed: 9 additions & 1 deletion
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

Lines changed: 0 additions & 11 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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
Lines changed: 6 additions & 6 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 20 additions & 20 deletions
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 5 additions & 1 deletion
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

0 commit comments

Comments
 (0)