Skip to content

Commit a858e4b

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 e26f9ee commit a858e4b

17 files changed

Lines changed: 58 additions & 74 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},
@@ -426,8 +426,11 @@ pub(crate) fn const_eval_discriminant_variant(
426426
let mir_body = db.monomorphized_mir_body(
427427
def.into(),
428428
GenericArgs::empty(interner).store(),
429-
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
430-
.store(),
429+
ParamEnvAndCrate {
430+
param_env: db.trait_environment(def.generic_def(db)),
431+
krate: def.krate(db),
432+
}
433+
.store(),
431434
)?;
432435
let c = interpret_mir(db, mir_body, false, None)?.0?;
433436
let c = if is_signed { allocation_as_isize(c) } else { allocation_as_usize(c) as i128 };
@@ -463,12 +466,8 @@ pub(crate) fn const_eval<'db>(
463466
let body = db.monomorphized_mir_body(
464467
def.into(),
465468
subst,
466-
ParamEnvAndCrate {
467-
param_env: db
468-
.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(def))),
469-
krate: def.krate(db),
470-
}
471-
.store(),
469+
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
470+
.store(),
472471
)?;
473472
let c = interpret_mir(db, body, false, trait_env.as_ref().map(|env| env.as_ref()))?.0?;
474473
Ok(c.store())
@@ -507,7 +506,7 @@ pub(crate) fn anon_const_eval<'db>(
507506
def.into(),
508507
subst,
509508
ParamEnvAndCrate {
510-
param_env: db.trait_environment(def.loc(db).owner),
509+
param_env: db.trait_environment(def.loc(db).owner.generic_def(db)),
511510
krate: def.krate(db),
512511
}
513512
.store(),
@@ -545,12 +544,8 @@ pub(crate) fn const_eval_static<'db>(
545544
let body = db.monomorphized_mir_body(
546545
def.into(),
547546
GenericArgs::empty(interner).store(),
548-
ParamEnvAndCrate {
549-
param_env: db
550-
.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(def))),
551-
krate: def.krate(db),
552-
}
553-
.store(),
547+
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
548+
.store(),
554549
)?;
555550
let c = interpret_mir(db, body, false, None)?.0?;
556551
Ok(c.store())

crates/hir-ty/src/db.rs

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

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

253253
#[salsa::invoke(crate::lower::generic_defaults_with_diagnostics)]
254254
#[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
@@ -1281,7 +1281,7 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
12811281
resolver: Resolver<'db>,
12821282
allow_using_generic_params: bool,
12831283
) -> Self {
1284-
let trait_env = db.trait_environment(store_owner);
1284+
let trait_env = db.trait_environment(generic_def);
12851285
let table = unify::InferenceTable::new(db, trait_env, resolver.krate(), store_owner);
12861286
let types = crate::next_solver::default_types(db);
12871287
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
@@ -383,7 +383,7 @@ pub fn associated_type_shorthand_candidates(
383383
let mut dedup_map = FxHashSet::default();
384384
let param_ty = Ty::new_param(interner, param, type_or_const_param_idx(db, param.into()));
385385
// We use the ParamEnv and not the predicates because the ParamEnv elaborates bounds.
386-
let param_env = db.trait_environment(ExpressionStoreOwnerId::from(def));
386+
let param_env = db.trait_environment(def);
387387
for clause in param_env.clauses {
388388
let ClauseKind::Trait(trait_clause) = clause.kind().skip_binder() else { continue };
389389
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
@@ -2102,12 +2102,7 @@ pub(crate) fn param_env_from_predicates<'db>(
21022102
ParamEnv { clauses }
21032103
}
21042104

2105-
pub(crate) fn trait_environment<'db>(
2106-
db: &'db dyn HirDatabase,
2107-
def: ExpressionStoreOwnerId,
2108-
) -> ParamEnv<'db> {
2109-
let def = def.generic_def(db);
2110-
2105+
pub(crate) fn trait_environment<'db>(db: &'db dyn HirDatabase, def: GenericDefId) -> ParamEnv<'db> {
21112106
return ParamEnv { clauses: trait_environment_query(db, def).as_ref() };
21122107

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