Skip to content

Commit f73e9bf

Browse files
Make trait_environment() take GenericDefId and not ExpressionStoreOwnedId
A `ParamEnv` is fundamentally associated with generic arguments and not with a body. It feels wrong to attach it to `ExpressionStoreOwnedId`. There is one exception: a defaulted trait method with RPITIT has a different ParamEnv inside its body than for its callers, since inside the body the RPITIT is interpreted as an RPIT equating the internal assoc type. We don't yet handle RPITIT, but even when we'll do I don't feel it's justifies treating *any* body as different.
1 parent 7c3fc86 commit f73e9bf

17 files changed

Lines changed: 59 additions & 73 deletions

File tree

crates/hir-ty/src/consteval.rs

Lines changed: 11 additions & 16 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-
ConstId, EnumVariantId, ExpressionStoreOwnerId, GenericDefId, HasModule, StaticId,
8+
ConstId, EnumVariantId, ExpressionStoreOwnerId, HasModule, StaticId,
99
attrs::AttrFlags,
1010
expr_store::{Body, ExpressionStore, HygieneId, path::Path},
1111
hir::{Expr, ExprId, Literal},
@@ -418,8 +418,11 @@ pub(crate) fn const_eval_discriminant_variant(
418418
let mir_body = db.monomorphized_mir_body(
419419
def.into(),
420420
GenericArgs::empty(interner).store(),
421-
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
422-
.store(),
421+
ParamEnvAndCrate {
422+
param_env: db.trait_environment(def.generic_def(db)),
423+
krate: def.krate(db),
424+
}
425+
.store(),
423426
)?;
424427
let c = interpret_mir(db, mir_body, false, None)?.0?;
425428
let c = if is_signed { allocation_as_isize(c) } else { allocation_as_usize(c) as i128 };
@@ -455,12 +458,8 @@ pub(crate) fn const_eval<'db>(
455458
let body = db.monomorphized_mir_body(
456459
def.into(),
457460
subst,
458-
ParamEnvAndCrate {
459-
param_env: db
460-
.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(def))),
461-
krate: def.krate(db),
462-
}
463-
.store(),
461+
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
462+
.store(),
464463
)?;
465464
let c = interpret_mir(db, body, false, trait_env.as_ref().map(|env| env.as_ref()))?.0?;
466465
Ok(c.store())
@@ -499,7 +498,7 @@ pub(crate) fn anon_const_eval<'db>(
499498
def.into(),
500499
subst,
501500
ParamEnvAndCrate {
502-
param_env: db.trait_environment(def.loc(db).owner),
501+
param_env: db.trait_environment(def.loc(db).owner.generic_def(db)),
503502
krate: def.krate(db),
504503
}
505504
.store(),
@@ -537,12 +536,8 @@ pub(crate) fn const_eval_static<'db>(
537536
let body = db.monomorphized_mir_body(
538537
def.into(),
539538
GenericArgs::empty(interner).store(),
540-
ParamEnvAndCrate {
541-
param_env: db
542-
.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(def))),
543-
krate: def.krate(db),
544-
}
545-
.store(),
539+
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
540+
.store(),
546541
)?;
547542
let c = interpret_mir(db, body, false, None)?.0?;
548543
Ok(c.store())

crates/hir-ty/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
247247

248248
#[salsa::invoke(crate::lower::trait_environment)]
249249
#[salsa::transparent]
250-
fn trait_environment<'db>(&'db self, def: ExpressionStoreOwnerId) -> ParamEnv<'db>;
250+
fn trait_environment<'db>(&'db self, def: GenericDefId) -> ParamEnv<'db>;
251251

252252
#[salsa::invoke(crate::lower::generic_defaults_with_diagnostics)]
253253
#[salsa::transparent]

crates/hir-ty/src/diagnostics/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'db> BodyValidationDiagnostic<'db> {
8383
let _p = tracing::info_span!("BodyValidationDiagnostic::collect").entered();
8484
let infer = InferenceResult::of(db, owner);
8585
let body = Body::of(db, owner);
86-
let env = db.trait_environment(owner.into());
86+
let env = db.trait_environment(owner.generic_def(db));
8787
let interner = DbInterner::new_with(db, owner.krate(db));
8888
let infcx =
8989
interner.infer_ctxt().build(TypingMode::typeck_for_body(interner, owner.into()));

crates/hir-ty/src/display.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::{
1010
use base_db::{Crate, FxIndexMap};
1111
use either::Either;
1212
use hir_def::{
13-
ExpressionStoreOwnerId, FindPathConfig, GenericDefId, GenericParamId, HasModule,
14-
ItemContainerId, LocalFieldId, Lookup, ModuleDefId, ModuleId, TraitId, TypeAliasId,
13+
FindPathConfig, GenericDefId, GenericParamId, HasModule, ItemContainerId, LocalFieldId, Lookup,
14+
ModuleDefId, ModuleId, TraitId, TypeAliasId,
1515
expr_store::{ExpressionStore, path::Path},
1616
find_path::{self, PrefixKind},
1717
hir::{
@@ -968,9 +968,7 @@ fn render_const_scalar_inner<'db>(
968968
s.fields(f.db),
969969
f,
970970
field_types,
971-
f.db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(
972-
def,
973-
))),
971+
f.db.trait_environment(def.into()),
974972
&layout,
975973
args,
976974
b,
@@ -1002,9 +1000,7 @@ fn render_const_scalar_inner<'db>(
10021000
var_id.fields(f.db),
10031001
f,
10041002
field_types,
1005-
f.db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(
1006-
def,
1007-
))),
1003+
f.db.trait_environment(def.into()),
10081004
var_layout,
10091005
args,
10101006
b,

crates/hir-ty/src/infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
12891289
resolver: Resolver<'db>,
12901290
allow_using_generic_params: bool,
12911291
) -> Self {
1292-
let trait_env = db.trait_environment(store_owner);
1292+
let trait_env = db.trait_environment(generic_def);
12931293
let table = unify::InferenceTable::new(db, trait_env, resolver.krate(), store_owner);
12941294
let types = crate::next_solver::default_types(db);
12951295
InferenceContext {

crates/hir-ty/src/layout/tests.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use base_db::target::TargetData;
22
use either::Either;
33
use hir_def::{
4-
DefWithBodyId, ExpressionStoreOwnerId, GenericDefId, HasModule,
4+
DefWithBodyId, HasModule,
55
expr_store::Body,
66
signatures::{
77
EnumSignature, FunctionSignature, StructSignature, TypeAliasSignature, UnionSignature,
@@ -92,13 +92,10 @@ fn eval_goal(
9292
),
9393
Either::Right(ty_id) => db.ty(ty_id.into()).instantiate_identity().skip_norm_wip(),
9494
};
95-
let param_env = db.trait_environment(
96-
match adt_or_type_alias_id {
97-
Either::Left(adt) => hir_def::GenericDefId::AdtId(adt),
98-
Either::Right(ty) => hir_def::GenericDefId::TypeAliasId(ty),
99-
}
100-
.into(),
101-
);
95+
let param_env = db.trait_environment(match adt_or_type_alias_id {
96+
Either::Left(adt) => hir_def::GenericDefId::AdtId(adt),
97+
Either::Right(ty) => hir_def::GenericDefId::TypeAliasId(ty),
98+
});
10299
let krate = match adt_or_type_alias_id {
103100
Either::Left(it) => it.krate(&db),
104101
Either::Right(it) => it.krate(&db),
@@ -145,8 +142,7 @@ fn eval_expr(
145142
.0;
146143
let infer = InferenceResult::of(&db, DefWithBodyId::from(function_id));
147144
let goal_ty = infer.type_of_binding[b].clone();
148-
let param_env =
149-
db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(function_id)));
145+
let param_env = db.trait_environment(function_id.into());
150146
let krate = function_id.krate(&db);
151147
db.layout_of_ty(goal_ty, ParamEnvAndCrate { param_env, krate }.store())
152148
})

crates/hir-ty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ pub fn associated_type_shorthand_candidates(
384384
let mut dedup_map = FxHashSet::default();
385385
let param_ty = Ty::new_param(interner, param, type_or_const_param_idx(db, param.into()));
386386
// We use the ParamEnv and not the predicates because the ParamEnv elaborates bounds.
387-
let param_env = db.trait_environment(ExpressionStoreOwnerId::from(def));
387+
let param_env = db.trait_environment(def);
388388
for clause in param_env.clauses {
389389
let ClauseKind::Trait(trait_clause) = clause.kind().skip_binder() else { continue };
390390
if trait_clause.self_ty() != param_ty {

crates/hir-ty/src/lower.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,12 +2197,7 @@ pub(crate) fn param_env_from_predicates<'db>(
21972197
ParamEnv { clauses }
21982198
}
21992199

2200-
pub(crate) fn trait_environment<'db>(
2201-
db: &'db dyn HirDatabase,
2202-
def: ExpressionStoreOwnerId,
2203-
) -> ParamEnv<'db> {
2204-
let def = def.generic_def(db);
2205-
2200+
pub(crate) fn trait_environment<'db>(db: &'db dyn HirDatabase, def: GenericDefId) -> ParamEnv<'db> {
22062201
return ParamEnv { clauses: trait_environment_query(db, def).as_ref() };
22072202

22082203
#[salsa::tracked(returns(ref))]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub fn borrowck_query(
141141
let _p = tracing::info_span!("borrowck_query").entered();
142142
let module = def.module(db);
143143
let interner = DbInterner::new_with(db, module.krate(db));
144-
let env = db.trait_environment(def.expression_store_owner(db));
144+
let env = db.trait_environment(def.generic_def(db));
145145
// This calculates opaques defining scope which is a bit costly therefore is put outside `all_mir_bodies()`.
146146
let typing_mode = TypingMode::borrowck(interner, def.into());
147147
let res = all_mir_bodies(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ impl<'a, 'db: 'a> Evaluator<'a, 'db> {
685685
db,
686686
random_state: oorandom::Rand64::new(0),
687687
param_env: trait_env.unwrap_or_else(|| ParamEnvAndCrate {
688-
param_env: db.trait_environment(owner.expression_store_owner(db)),
688+
param_env: db.trait_environment(owner.generic_def(db)),
689689
krate: crate_id,
690690
}),
691691
crate_id,

0 commit comments

Comments
 (0)