about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-03-26 10:52:11 +0000
committerbors <bors@rust-lang.org>2024-03-26 10:52:11 +0000
commit519d892f9523fe40cc11ec07323ffc2792614742 (patch)
tree5e162acaeec9893a39319a107b43fc9407da5a4a
parentb13a71a2e77f4625d1a2b8a5b9488414686ebca9 (diff)
parent3ec5042cf944cfb242408e45166e7da79eaf0692 (diff)
downloadrust-519d892f9523fe40cc11ec07323ffc2792614742.tar.gz
rust-519d892f9523fe40cc11ec07323ffc2792614742.zip
Auto merge of #121387 - oli-obk:eager_const_failures_regression, r=lcnr
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 https://github.com/rust-lang/rust/pull/121087
-rw-r--r--compiler/rustc_hir_analysis/src/lib.rs10
-rw-r--r--tests/ui/generic-const-items/hkl_where_bounds.rs14
2 files changed, 22 insertions, 2 deletions
diff --git a/compiler/rustc_hir_analysis/src/lib.rs b/compiler/rustc_hir_analysis/src/lib.rs
index 3ec5894700f..b7786ec219c 100644
--- a/compiler/rustc_hir_analysis/src/lib.rs
+++ b/compiler/rustc_hir_analysis/src/lib.rs
@@ -101,8 +101,9 @@ use rustc_errors::ErrorGuaranteed;
 use rustc_hir as hir;
 use rustc_hir::def::DefKind;
 use rustc_middle::middle;
+use rustc_middle::mir::interpret::GlobalId;
 use rustc_middle::query::Providers;
-use rustc_middle::ty::{Ty, TyCtxt};
+use rustc_middle::ty::{self, Ty, TyCtxt};
 use rustc_middle::util;
 use rustc_session::parse::feature_err;
 use rustc_span::{symbol::sym, Span};
@@ -186,7 +187,12 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
         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()),
+            DefKind::Const if tcx.generics_of(item_def_id).params.is_empty() => {
+                let instance = ty::Instance::new(item_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));
+            }
             _ => (),
         }
     });
diff --git a/tests/ui/generic-const-items/hkl_where_bounds.rs b/tests/ui/generic-const-items/hkl_where_bounds.rs
new file mode 100644
index 00000000000..cded518312a
--- /dev/null
+++ b/tests/ui/generic-const-items/hkl_where_bounds.rs
@@ -0,0 +1,14 @@
+//! Test that nonsense bounds prevent consts from being evaluated at all.
+//@ check-pass
+
+#![feature(generic_const_items)]
+#![allow(incomplete_features)]
+trait Trait {
+    const ASSOC: u32;
+}
+
+// rustfmt eats the where bound
+#[rustfmt::skip]
+const ASSOC: u32 = <&'static ()>::ASSOC where for<'a> &'a (): Trait;
+
+fn main() {}