Skip to content

Commit c6786ca

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 6074746 commit c6786ca

17 files changed

Lines changed: 56 additions & 70 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},
@@ -422,8 +422,11 @@ pub(crate) fn const_eval_discriminant_variant(
422422
let mir_body = db.monomorphized_mir_body(
423423
def.into(),
424424
GenericArgs::empty(interner).store(),
425-
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
426-
.store(),
425+
ParamEnvAndCrate {
426+
param_env: db.trait_environment(def.generic_def(db)),
427+
krate: def.krate(db),
428+
}
429+
.store(),
427430
)?;
428431
let c = interpret_mir(db, mir_body, false, None)?.0?;
429432
let c = if is_signed { allocation_as_isize(c) } else { allocation_as_usize(c) as i128 };
@@ -459,12 +462,8 @@ pub(crate) fn const_eval<'db>(
459462
let body = db.monomorphized_mir_body(
460463
def.into(),
461464
subst,
462-
ParamEnvAndCrate {
463-
param_env: db
464-
.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(def))),
465-
krate: def.krate(db),
466-
}
467-
.store(),
465+
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
466+
.store(),
468467
)?;
469468
let c = interpret_mir(db, body, false, trait_env.as_ref().map(|env| env.as_ref()))?.0?;
470469
Ok(c.store())
@@ -503,7 +502,7 @@ pub(crate) fn anon_const_eval<'db>(
503502
def.into(),
504503
subst,
505504
ParamEnvAndCrate {
506-
param_env: db.trait_environment(def.loc(db).owner),
505+
param_env: db.trait_environment(def.loc(db).owner.generic_def(db)),
507506
krate: def.krate(db),
508507
}
509508
.store(),
@@ -541,12 +540,8 @@ pub(crate) fn const_eval_static<'db>(
541540
let body = db.monomorphized_mir_body(
542541
def.into(),
543542
GenericArgs::empty(interner).store(),
544-
ParamEnvAndCrate {
545-
param_env: db
546-
.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(def))),
547-
krate: def.krate(db),
548-
}
549-
.store(),
543+
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
544+
.store(),
550545
)?;
551546
let c = interpret_mir(db, body, false, None)?.0?;
552547
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: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -996,9 +994,7 @@ fn render_const_scalar_inner<'db>(
996994
var_id.fields(f.db),
997995
f,
998996
field_types,
999-
f.db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(
1000-
def,
1001-
))),
997+
f.db.trait_environment(def.into()),
1002998
var_layout,
1003999
args,
10041000
b,

crates/hir-ty/src/infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
13311331
resolver: Resolver<'db>,
13321332
allow_using_generic_params: bool,
13331333
) -> Self {
1334-
let trait_env = db.trait_environment(store_owner);
1334+
let trait_env = db.trait_environment(generic_def);
13351335
let table = unify::InferenceTable::new(db, trait_env, resolver.krate(), store_owner);
13361336
let types = crate::next_solver::default_types(db);
13371337
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
@@ -385,7 +385,7 @@ pub fn associated_type_shorthand_candidates(
385385
let mut dedup_map = FxHashSet::default();
386386
let param_ty = Ty::new_param(interner, param, type_or_const_param_idx(db, param.into()));
387387
// We use the ParamEnv and not the predicates because the ParamEnv elaborates bounds.
388-
let param_env = db.trait_environment(ExpressionStoreOwnerId::from(def));
388+
let param_env = db.trait_environment(def);
389389
for clause in param_env.clauses {
390390
let ClauseKind::Trait(trait_clause) = clause.kind().skip_binder() else { continue };
391391
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
@@ -2240,12 +2240,7 @@ pub(crate) fn param_env_from_predicates<'db>(
22402240
ParamEnv { clauses }
22412241
}
22422242

2243-
pub(crate) fn trait_environment<'db>(
2244-
db: &'db dyn HirDatabase,
2245-
def: ExpressionStoreOwnerId,
2246-
) -> ParamEnv<'db> {
2247-
let def = def.generic_def(db);
2248-
2243+
pub(crate) fn trait_environment<'db>(db: &'db dyn HirDatabase, def: GenericDefId) -> ParamEnv<'db> {
22492244
return ParamEnv { clauses: trait_environment_query(db, def).as_ref() };
22502245

22512246
#[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)