Skip to content

Commit f29cf63

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 0cfcec3 commit f29cf63

18 files changed

Lines changed: 74 additions & 103 deletions

File tree

crates/hir-def/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -778,12 +778,12 @@ impl From<EnumVariantId> for DefWithBodyId {
778778
}
779779

780780
impl DefWithBodyId {
781-
pub fn as_generic_def_id(self, db: &dyn DefDatabase) -> Option<GenericDefId> {
781+
pub fn generic_def(self, db: &dyn DefDatabase) -> GenericDefId {
782782
match self {
783-
DefWithBodyId::FunctionId(f) => Some(f.into()),
784-
DefWithBodyId::StaticId(s) => Some(s.into()),
785-
DefWithBodyId::ConstId(c) => Some(c.into()),
786-
DefWithBodyId::VariantId(c) => Some(c.lookup(db).parent.into()),
783+
DefWithBodyId::FunctionId(f) => f.into(),
784+
DefWithBodyId::StaticId(s) => s.into(),
785+
DefWithBodyId::ConstId(c) => c.into(),
786+
DefWithBodyId::VariantId(c) => c.lookup(db).parent.into(),
787787
}
788788
}
789789
}

crates/hir-ty/src/consteval.rs

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

66
use base_db::Crate;
77
use hir_def::{
8-
ConstId, EnumVariantId, ExpressionStoreOwnerId, GeneralConstId, GenericDefId, HasModule,
9-
StaticId,
8+
ConstId, EnumVariantId, GeneralConstId, HasModule, StaticId,
109
attrs::AttrFlags,
1110
expr_store::{Body, ExpressionStore},
1211
hir::{Expr, ExprId, Literal},
@@ -259,8 +258,11 @@ pub(crate) fn const_eval_discriminant_variant(
259258
let mir_body = db.monomorphized_mir_body(
260259
def,
261260
GenericArgs::empty(interner).store(),
262-
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
263-
.store(),
261+
ParamEnvAndCrate {
262+
param_env: db.trait_environment(def.generic_def(db)),
263+
krate: def.krate(db),
264+
}
265+
.store(),
264266
)?;
265267
let c = interpret_mir(db, mir_body, false, None)?.0?;
266268
let c = if is_signed { allocation_as_isize(c) } else { allocation_as_usize(c) as i128 };
@@ -339,12 +341,8 @@ pub(crate) fn const_eval<'db>(
339341
let body = db.monomorphized_mir_body(
340342
def.into(),
341343
subst,
342-
ParamEnvAndCrate {
343-
param_env: db
344-
.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(def))),
345-
krate: def.krate(db),
346-
}
347-
.store(),
344+
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
345+
.store(),
348346
)?;
349347
let c = interpret_mir(db, body, false, trait_env.as_ref().map(|env| env.as_ref()))?.0?;
350348
Ok(c.store())
@@ -379,12 +377,8 @@ pub(crate) fn const_eval_static<'db>(
379377
let body = db.monomorphized_mir_body(
380378
def.into(),
381379
GenericArgs::empty(interner).store(),
382-
ParamEnvAndCrate {
383-
param_env: db
384-
.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(def))),
385-
krate: def.krate(db),
386-
}
387-
.store(),
380+
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
381+
.store(),
388382
)?;
389383
let c = interpret_mir(db, body, false, None)?.0?;
390384
Ok(c.store())

crates/hir-ty/src/db.rs

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

198198
#[salsa::invoke(crate::lower::trait_environment)]
199199
#[salsa::transparent]
200-
fn trait_environment<'db>(&'db self, def: ExpressionStoreOwnerId) -> ParamEnv<'db>;
200+
fn trait_environment<'db>(&'db self, def: GenericDefId) -> ParamEnv<'db>;
201201

202202
#[salsa::invoke(crate::lower::generic_defaults_with_diagnostics_query)]
203203
#[salsa::cycle(cycle_result = crate::lower::generic_defaults_with_diagnostics_cycle_result)]

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 BodyValidationDiagnostic {
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,
13+
FindPathConfig, GenericDefId, GenericParamId, HasModule, ItemContainerId, LocalFieldId, Lookup,
14+
ModuleDefId, ModuleId, TraitId,
1515
expr_store::{ExpressionStore, path::Path},
1616
find_path::{self, PrefixKind},
1717
hir::{
@@ -953,9 +953,7 @@ fn render_const_scalar_inner<'db>(
953953
s.fields(f.db),
954954
f,
955955
field_types,
956-
f.db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(
957-
def,
958-
))),
956+
f.db.trait_environment(def.into()),
959957
&layout,
960958
args,
961959
b,
@@ -987,9 +985,7 @@ fn render_const_scalar_inner<'db>(
987985
var_id.fields(f.db),
988986
f,
989987
field_types,
990-
f.db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(
991-
def,
992-
))),
988+
f.db.trait_environment(def.into()),
993989
var_layout,
994990
args,
995991
b,

crates/hir-ty/src/infer.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,17 +1298,8 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
12981298
store: &'body ExpressionStore,
12991299
resolver: Resolver<'db>,
13001300
) -> Self {
1301-
let trait_env = match owner {
1302-
ExpressionStoreOwnerId::Signature(generic_def_id) => {
1303-
db.trait_environment(ExpressionStoreOwnerId::from(generic_def_id))
1304-
}
1305-
ExpressionStoreOwnerId::Body(def_with_body_id) => {
1306-
db.trait_environment(ExpressionStoreOwnerId::Body(def_with_body_id))
1307-
}
1308-
ExpressionStoreOwnerId::VariantFields(variant_id) => {
1309-
db.trait_environment(ExpressionStoreOwnerId::VariantFields(variant_id))
1310-
}
1311-
};
1301+
let generic_def = owner.generic_def(db);
1302+
let trait_env = db.trait_environment(generic_def);
13121303
let table = unify::InferenceTable::new(db, trait_env, resolver.krate(), owner);
13131304
let types = crate::next_solver::default_types(db);
13141305
InferenceContext {
@@ -1325,7 +1316,7 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
13251316
return_coercion: None,
13261317
db,
13271318
owner,
1328-
generic_def: owner.generic_def(db),
1319+
generic_def,
13291320
store,
13301321
traits_in_scope: resolver.traits_in_scope(db),
13311322
resolver,

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(),
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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ mod tests;
6262
use std::{hash::Hash, ops::ControlFlow};
6363

6464
use hir_def::{
65-
CallableDefId, ExpressionStoreOwnerId, GenericDefId, LifetimeParamId, TypeAliasId,
66-
TypeOrConstParamId, TypeParamId,
65+
CallableDefId, GenericDefId, LifetimeParamId, TypeAliasId, TypeOrConstParamId, TypeParamId,
6766
hir::{BindingId, ExprId, ExprOrPatId, PatId},
6867
resolver::TypeNs,
6968
type_ref::{Rawness, TypeRefId},
@@ -511,7 +510,7 @@ pub fn associated_type_shorthand_candidates(
511510
let mut dedup_map = FxHashSet::default();
512511
let param_ty = Ty::new_param(interner, param, type_or_const_param_idx(db, param.into()));
513512
// We use the ParamEnv and not the predicates because the ParamEnv elaborates bounds.
514-
let param_env = db.trait_environment(ExpressionStoreOwnerId::from(def));
513+
let param_env = db.trait_environment(def);
515514
for clause in param_env.clauses {
516515
let ClauseKind::Trait(trait_clause) = clause.kind().skip_binder() else { continue };
517516
if trait_clause.self_ty() != param_ty {

crates/hir-ty/src/lower.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use std::{cell::OnceCell, iter, mem};
1212

1313
use either::Either;
1414
use hir_def::{
15-
AdtId, AssocItemId, CallableDefId, ConstId, ConstParamId, EnumId, EnumVariantId,
16-
ExpressionStoreOwnerId, FunctionId, GeneralConstId, GenericDefId, GenericParamId, HasModule,
17-
ImplId, ItemContainerId, LifetimeParamId, LocalFieldId, Lookup, StaticId, StructId, TraitId,
18-
TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId, VariantId,
15+
AdtId, AssocItemId, CallableDefId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId,
16+
GeneralConstId, GenericDefId, GenericParamId, HasModule, ImplId, ItemContainerId,
17+
LifetimeParamId, LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId,
18+
TypeOrConstParamId, TypeParamId, UnionId, VariantId,
1919
builtin_type::BuiltinType,
2020
expr_store::{ExpressionStore, HygieneId, path::Path},
2121
hir::generics::{
@@ -2148,12 +2148,7 @@ pub(crate) fn param_env_from_predicates<'db>(
21482148
ParamEnv { clauses }
21492149
}
21502150

2151-
pub(crate) fn trait_environment<'db>(
2152-
db: &'db dyn HirDatabase,
2153-
def: ExpressionStoreOwnerId,
2154-
) -> ParamEnv<'db> {
2155-
let def = def.generic_def(db);
2156-
2151+
pub(crate) fn trait_environment<'db>(db: &'db dyn HirDatabase, def: GenericDefId) -> ParamEnv<'db> {
21572152
return ParamEnv { clauses: trait_environment_query(db, def).as_ref() };
21582153

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use std::iter;
77

88
use either::Either;
9-
use hir_def::{DefWithBodyId, ExpressionStoreOwnerId, HasModule};
9+
use hir_def::{DefWithBodyId, HasModule};
1010
use la_arena::ArenaMap;
1111
use rustc_hash::FxHashMap;
1212
use stdx::never;
@@ -140,7 +140,7 @@ pub fn borrowck_query(
140140
let _p = tracing::info_span!("borrowck_query").entered();
141141
let module = def.module(db);
142142
let interner = DbInterner::new_with(db, module.krate(db));
143-
let env = db.trait_environment(ExpressionStoreOwnerId::from(def));
143+
let env = db.trait_environment(def.generic_def(db));
144144
// This calculates opaques defining scope which is a bit costly therefore is put outside `all_mir_bodies()`.
145145
let typing_mode = TypingMode::borrowck(interner, def.into());
146146
let res = all_mir_bodies(

0 commit comments

Comments
 (0)