Skip to content

Commit 3ec5042

Browse files
committed
Avoid computing generic params or a param env for free const items
1 parent 200e3f7 commit 3ec5042

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

compiler/rustc_hir_analysis/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ use rustc_errors::ErrorGuaranteed;
102102
use rustc_hir as hir;
103103
use rustc_hir::def::DefKind;
104104
use rustc_middle::middle;
105+
use rustc_middle::mir::interpret::GlobalId;
105106
use rustc_middle::query::Providers;
106-
use rustc_middle::ty::{Ty, TyCtxt};
107+
use rustc_middle::ty::{self, Ty, TyCtxt};
107108
use rustc_middle::util;
108109
use rustc_session::parse::feature_err;
109110
use rustc_span::{symbol::sym, Span};
@@ -187,7 +188,12 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
187188
let def_kind = tcx.def_kind(item_def_id);
188189
match def_kind {
189190
DefKind::Static { .. } => tcx.ensure().eval_static_initializer(item_def_id),
190-
DefKind::Const => tcx.ensure().const_eval_poly(item_def_id.into()),
191+
DefKind::Const if tcx.generics_of(item_def_id).params.is_empty() => {
192+
let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty());
193+
let cid = GlobalId { instance, promoted: None };
194+
let param_env = ty::ParamEnv::reveal_all();
195+
tcx.ensure().eval_to_const_value_raw(param_env.and(cid));
196+
}
191197
_ => (),
192198
}
193199
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Test that nonsense bounds prevent consts from being evaluated at all.
2+
//@ check-pass
3+
4+
#![feature(generic_const_items)]
5+
#![allow(incomplete_features)]
6+
trait Trait {
7+
const ASSOC: u32;
8+
}
9+
10+
// rustfmt eats the where bound
11+
#[rustfmt::skip]
12+
const ASSOC: u32 = <&'static ()>::ASSOC where for<'a> &'a (): Trait;
13+
14+
fn main() {}

0 commit comments

Comments
 (0)