Skip to content

Commit 7ba3c65

Browse files
committed
Use const_eval_static query for statics
1 parent 66b6a1e commit 7ba3c65

File tree

8 files changed

+91
-67
lines changed

8 files changed

+91
-67
lines changed

crates/hir-ty/src/consteval.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod tests;
55

66
use base_db::Crate;
77
use hir_def::{
8-
EnumVariantId, GeneralConstId, HasModule, StaticId,
8+
ConstId, EnumVariantId, StaticId,
99
expr_store::Body,
1010
hir::{Expr, ExprId},
1111
type_ref::LiteralConstRef,
@@ -139,16 +139,18 @@ pub fn try_const_usize<'db>(db: &'db dyn HirDatabase, c: Const<'db>) -> Option<u
139139
ConstKind::Infer(_) => None,
140140
ConstKind::Bound(_, _) => None,
141141
ConstKind::Placeholder(_) => None,
142-
ConstKind::Unevaluated(unevaluated_const) => {
143-
let c = match unevaluated_const.def {
144-
SolverDefId::ConstId(id) => GeneralConstId::ConstId(id),
145-
SolverDefId::StaticId(id) => GeneralConstId::StaticId(id),
146-
_ => unreachable!(),
147-
};
148-
let subst = unevaluated_const.args;
149-
let ec = db.const_eval(c, subst, None).ok()?;
150-
try_const_usize(db, ec)
151-
}
142+
ConstKind::Unevaluated(unevaluated_const) => match unevaluated_const.def {
143+
SolverDefId::ConstId(id) => {
144+
let subst = unevaluated_const.args;
145+
let ec = db.const_eval(id, subst, None).ok()?;
146+
try_const_usize(db, ec)
147+
}
148+
SolverDefId::StaticId(id) => {
149+
let ec = db.const_eval_static(id).ok()?;
150+
try_const_usize(db, ec)
151+
}
152+
_ => unreachable!(),
153+
},
152154
ConstKind::Value(val) => Some(u128::from_le_bytes(pad16(&val.value.inner().memory, false))),
153155
ConstKind::Error(_) => None,
154156
ConstKind::Expr(_) => None,
@@ -161,16 +163,18 @@ pub fn try_const_isize<'db>(db: &'db dyn HirDatabase, c: &Const<'db>) -> Option<
161163
ConstKind::Infer(_) => None,
162164
ConstKind::Bound(_, _) => None,
163165
ConstKind::Placeholder(_) => None,
164-
ConstKind::Unevaluated(unevaluated_const) => {
165-
let c = match unevaluated_const.def {
166-
SolverDefId::ConstId(id) => GeneralConstId::ConstId(id),
167-
SolverDefId::StaticId(id) => GeneralConstId::StaticId(id),
168-
_ => unreachable!(),
169-
};
170-
let subst = unevaluated_const.args;
171-
let ec = db.const_eval(c, subst, None).ok()?;
172-
try_const_isize(db, &ec)
173-
}
166+
ConstKind::Unevaluated(unevaluated_const) => match unevaluated_const.def {
167+
SolverDefId::ConstId(id) => {
168+
let subst = unevaluated_const.args;
169+
let ec = db.const_eval(id, subst, None).ok()?;
170+
try_const_isize(db, &ec)
171+
}
172+
SolverDefId::StaticId(id) => {
173+
let ec = db.const_eval_static(id).ok()?;
174+
try_const_isize(db, &ec)
175+
}
176+
_ => unreachable!(),
177+
},
174178
ConstKind::Value(val) => Some(i128::from_le_bytes(pad16(&val.value.inner().memory, true))),
175179
ConstKind::Error(_) => None,
176180
ConstKind::Expr(_) => None,
@@ -254,7 +258,7 @@ pub(crate) fn eval_to_const<'db>(expr: ExprId, ctx: &mut InferenceContext<'_, 'd
254258

255259
pub(crate) fn const_eval_cycle_result<'db>(
256260
_: &'db dyn HirDatabase,
257-
_: GeneralConstId,
261+
_: ConstId,
258262
_: GenericArgs<'db>,
259263
_: Option<Arc<TraitEnvironment<'db>>>,
260264
) -> Result<Const<'db>, ConstEvalError<'db>> {
@@ -277,19 +281,11 @@ pub(crate) fn const_eval_discriminant_cycle_result<'db>(
277281

278282
pub(crate) fn const_eval_query<'db>(
279283
db: &'db dyn HirDatabase,
280-
def: GeneralConstId,
284+
def: ConstId,
281285
subst: GenericArgs<'db>,
282286
trait_env: Option<Arc<TraitEnvironment<'db>>>,
283287
) -> Result<Const<'db>, ConstEvalError<'db>> {
284-
let body = match def {
285-
GeneralConstId::ConstId(c) => {
286-
db.monomorphized_mir_body(c.into(), subst, db.trait_environment(c.into()))?
287-
}
288-
GeneralConstId::StaticId(s) => {
289-
let krate = s.module(db).krate();
290-
db.monomorphized_mir_body(s.into(), subst, TraitEnvironment::empty(krate))?
291-
}
292-
};
288+
let body = db.monomorphized_mir_body(def.into(), subst, db.trait_environment(def.into()))?;
293289
let c = interpret_mir(db, body, false, trait_env)?.0?;
294290
Ok(c)
295291
}

crates/hir-ty/src/db.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use base_db::{Crate, target::TargetLoadError};
55
use hir_def::{
6-
AdtId, CallableDefId, ConstParamId, DefWithBodyId, EnumVariantId, FunctionId, GeneralConstId,
6+
AdtId, CallableDefId, ConstId, ConstParamId, DefWithBodyId, EnumVariantId, FunctionId,
77
GenericDefId, ImplId, LifetimeParamId, LocalFieldId, StaticId, TraitId, TypeAliasId,
88
TypeOrConstParamId, VariantId, db::DefDatabase, hir::ExprId, layout::TargetDataLayout,
99
};
@@ -29,6 +29,8 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
2929

3030
// region:mir
3131

32+
// FXME: Collapse `mir_body_for_closure` into `mir_body`
33+
// and `monomorphized_mir_body_for_closure` into `monomorphized_mir_body`
3234
#[salsa::invoke(crate::mir::mir_body_query)]
3335
#[salsa::cycle(cycle_result = crate::mir::mir_body_cycle_result)]
3436
fn mir_body<'db>(
@@ -70,7 +72,7 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
7072
#[salsa::cycle(cycle_result = crate::consteval::const_eval_cycle_result)]
7173
fn const_eval<'db>(
7274
&'db self,
73-
def: GeneralConstId,
75+
def: ConstId,
7476
subst: GenericArgs<'db>,
7577
trait_env: Option<Arc<TraitEnvironment<'db>>>,
7678
) -> Result<Const<'db>, ConstEvalError<'db>>;
@@ -232,13 +234,6 @@ fn hir_database_is_dyn_compatible() {
232234
fn _assert_dyn_compatible(_: &dyn HirDatabase) {}
233235
}
234236

235-
#[salsa_macros::interned(no_lifetime, debug, revisions = usize::MAX)]
236-
#[derive(PartialOrd, Ord)]
237-
pub struct InternedTypeOrConstParamId {
238-
/// This stores the param and its index.
239-
pub loc: (TypeOrConstParamId, u32),
240-
}
241-
242237
#[salsa_macros::interned(no_lifetime, debug, revisions = usize::MAX)]
243238
#[derive(PartialOrd, Ord)]
244239
pub struct InternedLifetimeParamId {

crates/hir-ty/src/mir/eval.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,24 +1917,32 @@ impl<'db> Evaluator<'db> {
19171917
let value = match konst.kind() {
19181918
ConstKind::Value(value) => value,
19191919
ConstKind::Unevaluated(UnevaluatedConst { def: const_id, args: subst }) => 'b: {
1920-
let mut const_id = match const_id {
1920+
let mut id = match const_id {
19211921
SolverDefId::ConstId(it) => GeneralConstId::from(it),
19221922
SolverDefId::StaticId(it) => it.into(),
19231923
_ => unreachable!("unevaluated consts should be consts or statics"),
19241924
};
19251925
let mut subst = subst;
1926-
if let hir_def::GeneralConstId::ConstId(c) = const_id {
1926+
if let hir_def::GeneralConstId::ConstId(c) = id {
19271927
let (c, s) = lookup_impl_const(&self.infcx, self.trait_env.clone(), c, subst);
1928-
const_id = hir_def::GeneralConstId::ConstId(c);
1928+
id = hir_def::GeneralConstId::ConstId(c);
19291929
subst = s;
19301930
}
1931-
result_owner = self
1932-
.db
1933-
.const_eval(const_id, subst, Some(self.trait_env.clone()))
1934-
.map_err(|e| {
1935-
let name = const_id.name(self.db);
1936-
MirEvalError::ConstEvalError(name, Box::new(e))
1937-
})?;
1931+
result_owner = match id {
1932+
GeneralConstId::ConstId(const_id) => self
1933+
.db
1934+
.const_eval(const_id, subst, Some(self.trait_env.clone()))
1935+
.map_err(|e| {
1936+
let name = id.name(self.db);
1937+
MirEvalError::ConstEvalError(name, Box::new(e))
1938+
})?,
1939+
GeneralConstId::StaticId(static_id) => {
1940+
self.db.const_eval_static(static_id).map_err(|e| {
1941+
let name = id.name(self.db);
1942+
MirEvalError::ConstEvalError(name, Box::new(e))
1943+
})?
1944+
}
1945+
};
19381946
if let ConstKind::Value(value) = result_owner.kind() {
19391947
break 'b value;
19401948
}

crates/hir-ty/src/mir/lower.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,10 +1532,20 @@ impl<'a, 'db> MirLowerCtx<'a, 'db> {
15321532
UnevaluatedConst { def: const_id.into(), args: subst },
15331533
)
15341534
} else {
1535-
let name = const_id.name(self.db);
1536-
self.db
1537-
.const_eval(const_id, subst, None)
1538-
.map_err(|e| MirLowerError::ConstEvalError(name.into(), Box::new(e)))?
1535+
match const_id {
1536+
id @ GeneralConstId::ConstId(const_id) => {
1537+
self.db.const_eval(const_id, subst, None).map_err(|e| {
1538+
let name = id.name(self.db);
1539+
MirLowerError::ConstEvalError(name.into(), Box::new(e))
1540+
})?
1541+
}
1542+
GeneralConstId::StaticId(static_id) => {
1543+
self.db.const_eval_static(static_id).map_err(|e| {
1544+
let name = const_id.name(self.db);
1545+
MirLowerError::ConstEvalError(name.into(), Box::new(e))
1546+
})?
1547+
}
1548+
}
15391549
};
15401550
let ty = self
15411551
.db

crates/hir-ty/src/next_solver/generic_arg.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::{
1717
generics::Generics,
1818
};
1919

20-
#[derive(Copy, Clone, PartialEq, Eq, Hash, TypeVisitable, TypeFoldable)]
20+
#[derive(Copy, Clone, PartialEq, Eq, Hash, TypeVisitable, TypeFoldable, salsa::Supertype)]
2121
pub enum GenericArg<'db> {
2222
Ty(Ty<'db>),
2323
Lifetime(Region<'db>),
@@ -196,6 +196,11 @@ impl<'db> GenericArgs<'db> {
196196
{
197197
let defs = interner.generics_of(def_id);
198198
let count = defs.count();
199+
200+
if count == 0 {
201+
return Default::default();
202+
}
203+
199204
let mut args = SmallVec::with_capacity(count);
200205
Self::fill_item(&mut args, interner, defs, &mut mk_kind);
201206
interner.mk_args(&args)

crates/hir-ty/src/next_solver/interner.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ macro_rules! _interned_vec_db {
214214
}
215215

216216
impl<'db> $name<'db> {
217+
pub fn empty(interner: DbInterner<'db>) -> Self {
218+
$name::new_(interner.db(), smallvec::SmallVec::new())
219+
}
220+
217221
pub fn new_from_iter(
218222
interner: DbInterner<'db>,
219223
data: impl IntoIterator<Item = $ty<'db>>,

crates/hir-ty/src/next_solver/solver.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Defining `SolverContext` for next-trait-solver.
22
3-
use hir_def::{AssocItemId, GeneralConstId};
3+
use hir_def::AssocItemId;
44
use rustc_next_trait_solver::delegate::SolverDelegate;
55
use rustc_type_ir::{
66
AliasTyKind, GenericArgKind, InferCtxtLike, Interner, PredicatePolarity, TypeFlags,
@@ -233,14 +233,18 @@ impl<'db> SolverDelegate for SolverContext<'db> {
233233
_param_env: ParamEnv<'db>,
234234
uv: rustc_type_ir::UnevaluatedConst<Self::Interner>,
235235
) -> Option<<Self::Interner as rustc_type_ir::Interner>::Const> {
236-
let c = match uv.def {
237-
SolverDefId::ConstId(c) => GeneralConstId::ConstId(c),
238-
SolverDefId::StaticId(c) => GeneralConstId::StaticId(c),
236+
match uv.def {
237+
SolverDefId::ConstId(c) => {
238+
let subst = uv.args;
239+
let ec = self.cx().db.const_eval(c, subst, None).ok()?;
240+
Some(ec)
241+
}
242+
SolverDefId::StaticId(c) => {
243+
let ec = self.cx().db.const_eval_static(c).ok()?;
244+
Some(ec)
245+
}
239246
_ => unreachable!(),
240-
};
241-
let subst = uv.args;
242-
let ec = self.cx().db.const_eval(c, subst, None).ok()?;
243-
Some(ec)
247+
}
244248
}
245249

246250
fn compute_goal_fast_path(

crates/hir/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,10 +2885,12 @@ impl Static {
28852885

28862886
/// Evaluate the static initializer.
28872887
pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst<'_>, ConstEvalError<'_>> {
2888-
let interner = DbInterner::new_with(db, None, None);
28892888
let ty = db.value_ty(self.id.into()).unwrap().instantiate_identity();
2890-
db.const_eval(self.id.into(), GenericArgs::new_from_iter(interner, []), None)
2891-
.map(|it| EvaluatedConst { const_: it, def: self.id.into(), ty })
2889+
db.const_eval_static(self.id).map(|it| EvaluatedConst {
2890+
const_: it,
2891+
def: self.id.into(),
2892+
ty,
2893+
})
28922894
}
28932895
}
28942896

0 commit comments

Comments
 (0)