Skip to content

Commit

Permalink
Auto merge of rust-lang#121387 - oli-obk:eager_const_failures_regress…
Browse files Browse the repository at this point in the history
…ion, 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 rust-lang#121087
  • Loading branch information
bors committed Feb 21, 2024
2 parents 0987e41 + 56d7af2 commit 5dd0e76
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 145 deletions.
10 changes: 9 additions & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rustc_hir::lang_items::LangItem;
use rustc_hir::ItemKind;
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
use rustc_middle::mir::interpret::GlobalId;
use rustc_middle::query::Providers;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::trait_def::TraitSpecializationKind;
Expand Down Expand Up @@ -291,9 +292,16 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
check_item_fn(tcx, def_id, item.ident, item.span, sig.decl)
}
hir::ItemKind::Static(ty, ..) => {
tcx.ensure().eval_static_initializer(def_id);
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
}
hir::ItemKind::Const(ty, ..) => {
hir::ItemKind::Const(ty, ast_generics, _) => {
if ast_generics.params.is_empty() {
let instance = ty::Instance::new(def_id.into(), ty::GenericArgs::empty());
let cid = GlobalId { instance, promoted: None };
let param_env = ty::ParamEnv::reveal_all();
tcx.ensure().eval_to_const_value_raw(param_env.and(cid));
}
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
}
hir::ItemKind::Struct(_, ast_generics) => {
Expand Down
11 changes: 0 additions & 11 deletions compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,6 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
collect::test_opaque_hidden_types(tcx)?;
}

// Make sure we evaluate all static and (non-associated) const items, even if unused.
// If any of these fail to evaluate, we do not want this crate to pass compilation.
tcx.hir().par_body_owners(|item_def_id| {
let def_kind = tcx.def_kind(item_def_id);
match def_kind {
DefKind::Static(_) => tcx.ensure().eval_static_initializer(item_def_id),
DefKind::Const => tcx.ensure().const_eval_poly(item_def_id.into()),
_ => (),
}
});

// Freeze definitions as we don't add new ones at this point. This improves performance by
// allowing lock-free access to them.
tcx.untracked().definitions.freeze();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-c
LL | const BAR: u32 = IMPL_REF_BAR;
| ^^^^^^^^^^^^
= note: ...which again requires simplifying constant for the type system `IMPL_REF_BAR`, completing the cycle
= note: cycle used when running analysis passes on this crate
note: cycle used when checking that `IMPL_REF_BAR` is well-formed
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
|
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^
= 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

error: aborting due to 1 previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-c
LL | const BAR: u32 = TRAIT_REF_BAR;
| ^^^^^^^^^^^^^
= note: ...which again requires simplifying constant for the type system `TRAIT_REF_BAR`, completing the cycle
= note: cycle used when running analysis passes on this crate
note: cycle used when checking that `TRAIT_REF_BAR` is well-formed
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
|
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^
= 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

error: aborting due to 1 previous error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the trait bound `String: Copy` is not satisfied
--> $DIR/generic-associated-types-bad.rs:16:10
--> $DIR/generic-associated-types-bad.rs:16:27
|
LL | const _: Ty::Pr<String> = String::new();
| ^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
|
note: required by a bound in `Ty::Pr`
--> $DIR/generic-associated-types-bad.rs:10:16
Expand All @@ -11,10 +11,10 @@ LL | type Pr<T: Copy> = T;
| ^^^^ required by this bound in `Ty::Pr`

error[E0277]: the trait bound `String: Copy` is not satisfied
--> $DIR/generic-associated-types-bad.rs:16:27
--> $DIR/generic-associated-types-bad.rs:16:10
|
LL | const _: Ty::Pr<String> = String::new();
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
| ^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
|
note: required by a bound in `Ty::Pr`
--> $DIR/generic-associated-types-bad.rs:10:16
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/consts/const-array-oob.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
error[E0080]: evaluation of constant value failed
--> $DIR/const-array-oob.rs:5:19
|
LL | const BLUB: [u32; FOO[4]] = [5, 6];
| ^^^^^^ index out of bounds: the length is 3 but the index is 4

error[E0080]: evaluation of constant value failed
--> $DIR/const-array-oob.rs:2:20
|
LL | const BAR: usize = FOO[5];
| ^^^^^^ index out of bounds: the length is 3 but the index is 5

error[E0080]: evaluation of constant value failed
--> $DIR/const-array-oob.rs:5:19
|
LL | const BLUB: [u32; FOO[4]] = [5, 6];
| ^^^^^^ index out of bounds: the length is 3 but the index is 4

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.
4 changes: 3 additions & 1 deletion tests/ui/consts/const-eval/const-eval-query-stack.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ LL | const X: i32 = 1 / 0;
query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `X`
#1 [eval_to_const_value_raw] simplifying constant for the type system `X`
#2 [analysis] running analysis passes on this crate
#2 [check_well_formed] checking that `X` is well-formed
#3 [check_mod_type_wf] checking that types are well-formed in top-level module
#4 [analysis] running analysis passes on this crate
end of query stack
40 changes: 20 additions & 20 deletions tests/ui/consts/const-unsized.stderr
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
--> $DIR/const-unsized.rs:3:16
--> $DIR/const-unsized.rs:3:35
|
LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
= note: constant expressions must have a statically known size

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

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

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

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

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

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

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

error[E0161]: cannot move a value of type `str`
--> $DIR/const-unsized.rs:20:48
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/consts/const_cmp_type_id.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
error[E0131]: `main` function is not allowed to have generic parameters
--> $DIR/const_cmp_type_id.rs:7:14
|
LL | const fn main() {
| ^ `main` cannot have generic parameters

error[E0080]: evaluation of constant value failed
--> $DIR/const_cmp_type_id.rs:10:22
|
LL | const _A: bool = TypeId::of::<u8>() < TypeId::of::<u16>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling non-const function `<TypeId as PartialOrd>::lt`

error[E0131]: `main` function is not allowed to have generic parameters
--> $DIR/const_cmp_type_id.rs:7:14
|
LL | const fn main() {
| ^ `main` cannot have generic parameters

error[E0308]: mismatched types
--> $DIR/const_cmp_type_id.rs:8:13
|
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/consts/recursive-zst-static.default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ note: ...which requires evaluating initializer of static `B`...
LL | static B: () = A;
| ^
= note: ...which again requires evaluating initializer of static `A`, completing the cycle
= note: cycle used when running analysis passes on this crate
note: cycle used when checking that `A` is well-formed
--> $DIR/recursive-zst-static.rs:13:1
|
LL | static A: () = B;
| ^^^^^^^^^^^^
= 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

error: aborting due to 2 previous errors
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/consts/recursive-zst-static.unleash.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ note: ...which requires evaluating initializer of static `B`...
LL | static B: () = A;
| ^
= note: ...which again requires evaluating initializer of static `A`, completing the cycle
= note: cycle used when running analysis passes on this crate
note: cycle used when checking that `A` is well-formed
--> $DIR/recursive-zst-static.rs:13:1
|
LL | static A: () = B;
| ^^^^^^^^^^^^
= 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

error: aborting due to 2 previous errors
Expand Down
30 changes: 15 additions & 15 deletions tests/ui/error-codes/E0017.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
warning: creating a mutable reference to mutable static is discouraged
--> $DIR/E0017.rs:18:52
|
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
| ^^^^^^ mutable reference to mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: this will be a hard error in the 2024 edition
= 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
= note: `#[warn(static_mut_refs)]` on by default
help: use `addr_of_mut!` instead to create a raw pointer
|
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { addr_of_mut!(M) };
| ~~~~~~~~~~~~~~~

warning: taking a mutable reference to a `const` item
--> $DIR/E0017.rs:10:30
|
Expand Down Expand Up @@ -60,6 +45,21 @@ error[E0764]: mutable references are not allowed in the final value of statics
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^

warning: creating a mutable reference to mutable static is discouraged
--> $DIR/E0017.rs:18:52
|
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
| ^^^^^^ mutable reference to mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: this will be a hard error in the 2024 edition
= 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
= note: `#[warn(static_mut_refs)]` on by default
help: use `addr_of_mut!` instead to create a raw pointer
|
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { addr_of_mut!(M) };
| ~~~~~~~~~~~~~~~

error[E0080]: it is undefined behavior to use this value
--> $DIR/E0017.rs:18:1
|
Expand Down
12 changes: 10 additions & 2 deletions tests/ui/issues/issue-17252.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ note: ...which requires const-evaluating + checking `FOO`...
LL | const FOO: usize = FOO;
| ^^^
= note: ...which again requires simplifying constant for the type system `FOO`, completing the cycle
= note: cycle used when running analysis passes on this crate
note: cycle used when checking that `FOO` is well-formed
--> $DIR/issue-17252.rs:1:1
|
LL | const FOO: usize = FOO;
| ^^^^^^^^^^^^^^^^
= 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

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

error: aborting due to 2 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ note: ...which requires const-evaluating + checking `B`...
LL | const B: i32 = A;
| ^
= note: ...which again requires simplifying constant for the type system `A`, completing the cycle
= note: cycle used when running analysis passes on this crate
note: cycle used when checking that `A` is well-formed
--> $DIR/issue-23302-3.rs:1:1
|
LL | const A: i32 = B;
| ^^^^^^^^^^^^
= 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

error: aborting due to 1 previous error
Expand Down
34 changes: 17 additions & 17 deletions tests/ui/issues/issue-24446.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
error[E0277]: `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
--> $DIR/issue-24446.rs:2:17
|
LL | static foo: dyn Fn() -> u32 = || -> u32 {
| ^^^^^^^^^^^^^^^ `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
|
= help: the trait `Sync` is not implemented for `(dyn Fn() -> u32 + 'static)`
= note: shared static variables must have a type that implements `Sync`

error[E0277]: the size for values of type `(dyn Fn() -> u32 + 'static)` cannot be known at compilation time
--> $DIR/issue-24446.rs:2:17
|
LL | static foo: dyn Fn() -> u32 = || -> u32 {
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Fn() -> u32 + 'static)`

error[E0277]: the size for values of type `(dyn Fn() -> u32 + 'static)` cannot be known at compilation time
--> $DIR/issue-24446.rs:2:35
|
Expand Down Expand Up @@ -47,6 +30,23 @@ LL | | };
= note: expected trait object `(dyn Fn() -> u32 + 'static)`
found closure `{closure@$DIR/issue-24446.rs:2:35: 2:44}`

error[E0277]: `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
--> $DIR/issue-24446.rs:2:17
|
LL | static foo: dyn Fn() -> u32 = || -> u32 {
| ^^^^^^^^^^^^^^^ `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
|
= help: the trait `Sync` is not implemented for `(dyn Fn() -> u32 + 'static)`
= note: shared static variables must have a type that implements `Sync`

error[E0277]: the size for values of type `(dyn Fn() -> u32 + 'static)` cannot be known at compilation time
--> $DIR/issue-24446.rs:2:17
|
LL | static foo: dyn Fn() -> u32 = || -> u32 {
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Fn() -> u32 + 'static)`

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0277, E0308.
Expand Down
Loading

0 comments on commit 5dd0e76

Please sign in to comment.