Unnamed repository; edit this file 'description' to name the repository.
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.
Chayim Refael Friedman 13 days ago
parent 6074746 · commit c6786ca
-rw-r--r--crates/hir-ty/src/consteval.rs27
-rw-r--r--crates/hir-ty/src/db.rs2
-rw-r--r--crates/hir-ty/src/diagnostics/expr.rs2
-rw-r--r--crates/hir-ty/src/display.rs8
-rw-r--r--crates/hir-ty/src/infer.rs2
-rw-r--r--crates/hir-ty/src/layout/tests.rs16
-rw-r--r--crates/hir-ty/src/lib.rs2
-rw-r--r--crates/hir-ty/src/lower.rs7
-rw-r--r--crates/hir-ty/src/mir/borrowck.rs2
-rw-r--r--crates/hir-ty/src/mir/eval.rs2
-rw-r--r--crates/hir-ty/src/mir/eval/tests.rs4
-rw-r--r--crates/hir-ty/src/mir/lower.rs4
-rw-r--r--crates/hir-ty/src/opaques.rs7
-rw-r--r--crates/hir-ty/src/specialization.rs9
-rw-r--r--crates/hir/src/lib.rs11
-rw-r--r--crates/hir/src/source_analyzer.rs19
-rw-r--r--crates/rust-analyzer/src/cli/analysis_stats.rs2
17 files changed, 56 insertions, 70 deletions
diff --git a/crates/hir-ty/src/consteval.rs b/crates/hir-ty/src/consteval.rs
index fb52813c52..2c43feeb3b 100644
--- a/crates/hir-ty/src/consteval.rs
+++ b/crates/hir-ty/src/consteval.rs
@@ -5,7 +5,7 @@ mod tests;
use base_db::Crate;
use hir_def::{
- ConstId, EnumVariantId, ExpressionStoreOwnerId, GenericDefId, HasModule, StaticId,
+ ConstId, EnumVariantId, ExpressionStoreOwnerId, HasModule, StaticId,
attrs::AttrFlags,
expr_store::{Body, ExpressionStore, HygieneId, path::Path},
hir::{Expr, ExprId, Literal},
@@ -422,8 +422,11 @@ pub(crate) fn const_eval_discriminant_variant(
let mir_body = db.monomorphized_mir_body(
def.into(),
GenericArgs::empty(interner).store(),
- ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
- .store(),
+ ParamEnvAndCrate {
+ param_env: db.trait_environment(def.generic_def(db)),
+ krate: def.krate(db),
+ }
+ .store(),
)?;
let c = interpret_mir(db, mir_body, false, None)?.0?;
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>(
let body = db.monomorphized_mir_body(
def.into(),
subst,
- ParamEnvAndCrate {
- param_env: db
- .trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(def))),
- krate: def.krate(db),
- }
- .store(),
+ ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
+ .store(),
)?;
let c = interpret_mir(db, body, false, trait_env.as_ref().map(|env| env.as_ref()))?.0?;
Ok(c.store())
@@ -503,7 +502,7 @@ pub(crate) fn anon_const_eval<'db>(
def.into(),
subst,
ParamEnvAndCrate {
- param_env: db.trait_environment(def.loc(db).owner),
+ param_env: db.trait_environment(def.loc(db).owner.generic_def(db)),
krate: def.krate(db),
}
.store(),
@@ -541,12 +540,8 @@ pub(crate) fn const_eval_static<'db>(
let body = db.monomorphized_mir_body(
def.into(),
GenericArgs::empty(interner).store(),
- ParamEnvAndCrate {
- param_env: db
- .trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(def))),
- krate: def.krate(db),
- }
- .store(),
+ ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
+ .store(),
)?;
let c = interpret_mir(db, body, false, None)?.0?;
Ok(c.store())
diff --git a/crates/hir-ty/src/db.rs b/crates/hir-ty/src/db.rs
index c24a5b943d..511ab85610 100644
--- a/crates/hir-ty/src/db.rs
+++ b/crates/hir-ty/src/db.rs
@@ -247,7 +247,7 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
#[salsa::invoke(crate::lower::trait_environment)]
#[salsa::transparent]
- fn trait_environment<'db>(&'db self, def: ExpressionStoreOwnerId) -> ParamEnv<'db>;
+ fn trait_environment<'db>(&'db self, def: GenericDefId) -> ParamEnv<'db>;
#[salsa::invoke(crate::lower::generic_defaults_with_diagnostics)]
#[salsa::transparent]
diff --git a/crates/hir-ty/src/diagnostics/expr.rs b/crates/hir-ty/src/diagnostics/expr.rs
index 760ebd27e0..be4de11ceb 100644
--- a/crates/hir-ty/src/diagnostics/expr.rs
+++ b/crates/hir-ty/src/diagnostics/expr.rs
@@ -83,7 +83,7 @@ impl<'db> BodyValidationDiagnostic<'db> {
let _p = tracing::info_span!("BodyValidationDiagnostic::collect").entered();
let infer = InferenceResult::of(db, owner);
let body = Body::of(db, owner);
- let env = db.trait_environment(owner.into());
+ let env = db.trait_environment(owner.generic_def(db));
let interner = DbInterner::new_with(db, owner.krate(db));
let infcx =
interner.infer_ctxt().build(TypingMode::typeck_for_body(interner, owner.into()));
diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs
index 0acf6e63d7..8edb178cd7 100644
--- a/crates/hir-ty/src/display.rs
+++ b/crates/hir-ty/src/display.rs
@@ -968,9 +968,7 @@ fn render_const_scalar_inner<'db>(
s.fields(f.db),
f,
field_types,
- f.db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(
- def,
- ))),
+ f.db.trait_environment(def.into()),
&layout,
args,
b,
@@ -996,9 +994,7 @@ fn render_const_scalar_inner<'db>(
var_id.fields(f.db),
f,
field_types,
- f.db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(
- def,
- ))),
+ f.db.trait_environment(def.into()),
var_layout,
args,
b,
diff --git a/crates/hir-ty/src/infer.rs b/crates/hir-ty/src/infer.rs
index c063d0211f..7e5fdfdd81 100644
--- a/crates/hir-ty/src/infer.rs
+++ b/crates/hir-ty/src/infer.rs
@@ -1331,7 +1331,7 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
resolver: Resolver<'db>,
allow_using_generic_params: bool,
) -> Self {
- let trait_env = db.trait_environment(store_owner);
+ let trait_env = db.trait_environment(generic_def);
let table = unify::InferenceTable::new(db, trait_env, resolver.krate(), store_owner);
let types = crate::next_solver::default_types(db);
InferenceContext {
diff --git a/crates/hir-ty/src/layout/tests.rs b/crates/hir-ty/src/layout/tests.rs
index b42ac54f13..c12fbeccdb 100644
--- a/crates/hir-ty/src/layout/tests.rs
+++ b/crates/hir-ty/src/layout/tests.rs
@@ -1,7 +1,7 @@
use base_db::target::TargetData;
use either::Either;
use hir_def::{
- DefWithBodyId, ExpressionStoreOwnerId, GenericDefId, HasModule,
+ DefWithBodyId, HasModule,
expr_store::Body,
signatures::{
EnumSignature, FunctionSignature, StructSignature, TypeAliasSignature, UnionSignature,
@@ -92,13 +92,10 @@ fn eval_goal(
),
Either::Right(ty_id) => db.ty(ty_id.into()).instantiate_identity().skip_norm_wip(),
};
- let param_env = db.trait_environment(
- match adt_or_type_alias_id {
- Either::Left(adt) => hir_def::GenericDefId::AdtId(adt),
- Either::Right(ty) => hir_def::GenericDefId::TypeAliasId(ty),
- }
- .into(),
- );
+ let param_env = db.trait_environment(match adt_or_type_alias_id {
+ Either::Left(adt) => hir_def::GenericDefId::AdtId(adt),
+ Either::Right(ty) => hir_def::GenericDefId::TypeAliasId(ty),
+ });
let krate = match adt_or_type_alias_id {
Either::Left(it) => it.krate(&db),
Either::Right(it) => it.krate(&db),
@@ -145,8 +142,7 @@ fn eval_expr(
.0;
let infer = InferenceResult::of(&db, DefWithBodyId::from(function_id));
let goal_ty = infer.type_of_binding[b].clone();
- let param_env =
- db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(function_id)));
+ let param_env = db.trait_environment(function_id.into());
let krate = function_id.krate(&db);
db.layout_of_ty(goal_ty, ParamEnvAndCrate { param_env, krate }.store())
})
diff --git a/crates/hir-ty/src/lib.rs b/crates/hir-ty/src/lib.rs
index 1900a41f7f..f612bdc266 100644
--- a/crates/hir-ty/src/lib.rs
+++ b/crates/hir-ty/src/lib.rs
@@ -385,7 +385,7 @@ pub fn associated_type_shorthand_candidates(
let mut dedup_map = FxHashSet::default();
let param_ty = Ty::new_param(interner, param, type_or_const_param_idx(db, param.into()));
// We use the ParamEnv and not the predicates because the ParamEnv elaborates bounds.
- let param_env = db.trait_environment(ExpressionStoreOwnerId::from(def));
+ let param_env = db.trait_environment(def);
for clause in param_env.clauses {
let ClauseKind::Trait(trait_clause) = clause.kind().skip_binder() else { continue };
if trait_clause.self_ty() != param_ty {
diff --git a/crates/hir-ty/src/lower.rs b/crates/hir-ty/src/lower.rs
index 8beaa481b5..df83b2abb8 100644
--- a/crates/hir-ty/src/lower.rs
+++ b/crates/hir-ty/src/lower.rs
@@ -2240,12 +2240,7 @@ pub(crate) fn param_env_from_predicates<'db>(
ParamEnv { clauses }
}
-pub(crate) fn trait_environment<'db>(
- db: &'db dyn HirDatabase,
- def: ExpressionStoreOwnerId,
-) -> ParamEnv<'db> {
- let def = def.generic_def(db);
-
+pub(crate) fn trait_environment<'db>(db: &'db dyn HirDatabase, def: GenericDefId) -> ParamEnv<'db> {
return ParamEnv { clauses: trait_environment_query(db, def).as_ref() };
#[salsa::tracked(returns(ref))]
diff --git a/crates/hir-ty/src/mir/borrowck.rs b/crates/hir-ty/src/mir/borrowck.rs
index 940bc57259..bc158ac884 100644
--- a/crates/hir-ty/src/mir/borrowck.rs
+++ b/crates/hir-ty/src/mir/borrowck.rs
@@ -141,7 +141,7 @@ pub fn borrowck_query(
let _p = tracing::info_span!("borrowck_query").entered();
let module = def.module(db);
let interner = DbInterner::new_with(db, module.krate(db));
- let env = db.trait_environment(def.expression_store_owner(db));
+ let env = db.trait_environment(def.generic_def(db));
// This calculates opaques defining scope which is a bit costly therefore is put outside `all_mir_bodies()`.
let typing_mode = TypingMode::borrowck(interner, def.into());
let res = all_mir_bodies(
diff --git a/crates/hir-ty/src/mir/eval.rs b/crates/hir-ty/src/mir/eval.rs
index 97aeb7d412..1ee18e09ba 100644
--- a/crates/hir-ty/src/mir/eval.rs
+++ b/crates/hir-ty/src/mir/eval.rs
@@ -685,7 +685,7 @@ impl<'a, 'db: 'a> Evaluator<'a, 'db> {
db,
random_state: oorandom::Rand64::new(0),
param_env: trait_env.unwrap_or_else(|| ParamEnvAndCrate {
- param_env: db.trait_environment(owner.expression_store_owner(db)),
+ param_env: db.trait_environment(owner.generic_def(db)),
krate: crate_id,
}),
crate_id,
diff --git a/crates/hir-ty/src/mir/eval/tests.rs b/crates/hir-ty/src/mir/eval/tests.rs
index 6bf966c3ef..0e94a5b92d 100644
--- a/crates/hir-ty/src/mir/eval/tests.rs
+++ b/crates/hir-ty/src/mir/eval/tests.rs
@@ -1,4 +1,4 @@
-use hir_def::{GenericDefId, HasModule, signatures::FunctionSignature};
+use hir_def::{HasModule, signatures::FunctionSignature};
use hir_expand::EditionedFileId;
use span::Edition;
use syntax::{TextRange, TextSize};
@@ -41,7 +41,7 @@ fn eval_main(db: &TestDB, file_id: EditionedFileId) -> Result<(String, String),
func_id.into(),
GenericArgs::empty(interner).store(),
crate::ParamEnvAndCrate {
- param_env: db.trait_environment(GenericDefId::from(func_id).into()),
+ param_env: db.trait_environment(func_id.into()),
krate: func_id.krate(db),
}
.store(),
diff --git a/crates/hir-ty/src/mir/lower.rs b/crates/hir-ty/src/mir/lower.rs
index 025aff9307..68612c2ce2 100644
--- a/crates/hir-ty/src/mir/lower.rs
+++ b/crates/hir-ty/src/mir/lower.rs
@@ -314,8 +314,8 @@ impl<'a, 'db> MirLowerCtx<'a, 'db> {
closures: vec![],
};
let store_owner = owner.expression_store_owner(db);
- let resolver = store_owner.resolver(db);
- let env = db.trait_environment(store_owner);
+ let resolver = owner.resolver(db);
+ let env = db.trait_environment(owner.generic_def(db));
let interner = DbInterner::new_with(db, resolver.krate());
// FIXME(next-solver): Is `non_body_analysis()` correct here? Don't we want to reveal opaque types defined by this body?
let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis());
diff --git a/crates/hir-ty/src/opaques.rs b/crates/hir-ty/src/opaques.rs
index 79d2fa0c2d..dfd1fd96c6 100644
--- a/crates/hir-ty/src/opaques.rs
+++ b/crates/hir-ty/src/opaques.rs
@@ -1,8 +1,8 @@
//! Handling of opaque types, detection of defining scope and hidden type.
use hir_def::{
- AssocItemId, AssocItemLoc, DefWithBodyId, ExpressionStoreOwnerId, FunctionId, GenericDefId,
- HasModule, ItemContainerId, TypeAliasId, signatures::ImplSignature,
+ AssocItemId, AssocItemLoc, DefWithBodyId, FunctionId, HasModule, ItemContainerId, TypeAliasId,
+ signatures::ImplSignature,
};
use hir_expand::name::Name;
use la_arena::ArenaMap;
@@ -129,8 +129,7 @@ pub(crate) fn tait_hidden_types(
let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis());
let mut ocx = ObligationCtxt::new(&infcx);
let cause = ObligationCause::dummy();
- let param_env =
- db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(type_alias)));
+ let param_env = db.trait_environment(type_alias.into());
let defining_bodies = tait_defining_bodies(db, loc);
diff --git a/crates/hir-ty/src/specialization.rs b/crates/hir-ty/src/specialization.rs
index 467b598447..2d206fe380 100644
--- a/crates/hir-ty/src/specialization.rs
+++ b/crates/hir-ty/src/specialization.rs
@@ -1,9 +1,6 @@
//! Impl specialization related things
-use hir_def::{
- ExpressionStoreOwnerId, GenericDefId, HasModule, ImplId, signatures::ImplSignature,
- unstable_features::UnstableFeatures,
-};
+use hir_def::{HasModule, ImplId, signatures::ImplSignature, unstable_features::UnstableFeatures};
use tracing::debug;
use crate::{
@@ -48,9 +45,7 @@ fn specializes_query(
specializing_impl_def_id: ImplId,
parent_impl_def_id: ImplId,
) -> bool {
- let trait_env = db.trait_environment(ExpressionStoreOwnerId::from(GenericDefId::from(
- specializing_impl_def_id,
- )));
+ let trait_env = db.trait_environment(specializing_impl_def_id.into());
let interner = DbInterner::new_with(db, specializing_impl_def_id.krate(db));
let specializing_impl_signature = ImplSignature::of(db, specializing_impl_def_id);
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 768ffd5697..02800fda98 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -2927,7 +2927,7 @@ impl Function {
id.into(),
GenericArgs::empty(interner).store(),
ParamEnvAndCrate {
- param_env: db.trait_environment(GenericDefId::from(id).into()),
+ param_env: db.trait_environment(id.into()),
krate: id.module(db).krate(db),
}
.store(),
@@ -7456,7 +7456,7 @@ fn param_env_from_resolver<'db>(
ParamEnvAndCrate {
param_env: resolver.generic_def().map_or_else(
|| ParamEnv::empty(DbInterner::new_no_crate(db)),
- |generic_def| db.trait_environment(generic_def.into()),
+ |generic_def| db.trait_environment(generic_def),
),
krate: resolver.krate(),
}
@@ -7466,14 +7466,17 @@ fn param_env_from_has_crate<'db>(
db: &'db dyn HirDatabase,
id: impl hir_def::HasModule + Into<GenericDefId> + Copy,
) -> ParamEnvAndCrate<'db> {
- ParamEnvAndCrate { param_env: db.trait_environment(id.into().into()), krate: id.krate(db) }
+ ParamEnvAndCrate { param_env: db.trait_environment(id.into()), krate: id.krate(db) }
}
fn body_param_env_from_has_crate<'db>(
db: &'db dyn HirDatabase,
id: impl hir_def::HasModule + Into<ExpressionStoreOwnerId> + Copy,
) -> ParamEnvAndCrate<'db> {
- ParamEnvAndCrate { param_env: db.trait_environment(id.into()), krate: id.krate(db) }
+ ParamEnvAndCrate {
+ param_env: db.trait_environment(id.into().generic_def(db)),
+ krate: id.krate(db),
+ }
}
// FIXME: We probably don't want to expose this.
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs
index ba62cc11c3..3afa98365e 100644
--- a/crates/hir/src/source_analyzer.rs
+++ b/crates/hir/src/source_analyzer.rs
@@ -332,10 +332,17 @@ impl<'db> SourceAnalyzer<'db> {
fn trait_environment(&self, db: &'db dyn HirDatabase) -> ParamEnvAndCrate<'db> {
self.param_and(self.body_or_sig.as_ref().map_or_else(
|| ParamEnv::empty(DbInterner::new_no_crate(db)),
- |body_or_sig| match *body_or_sig {
- BodyOrSig::Body { def, .. } => db.trait_environment(def.into()),
- BodyOrSig::VariantFields { def, .. } => db.trait_environment(def.into()),
- BodyOrSig::Sig { def, .. } => db.trait_environment(def.into()),
+ |body_or_sig| {
+ let def = match *body_or_sig {
+ BodyOrSig::Body { def, .. } => def.generic_def(db),
+ BodyOrSig::VariantFields { def, .. } => match def {
+ VariantId::EnumVariantId(def) => def.loc(db).parent.into(),
+ VariantId::StructId(def) => def.into(),
+ VariantId::UnionId(def) => def.into(),
+ },
+ BodyOrSig::Sig { def, .. } => def,
+ };
+ db.trait_environment(def)
},
))
}
@@ -1590,7 +1597,7 @@ impl<'db> SourceAnalyzer<'db> {
func: FunctionId,
substs: GenericArgs<'db>,
) -> (Function, GenericArgs<'db>) {
- let owner = match self.resolver.expression_store_owner() {
+ let owner = match self.resolver.generic_def() {
Some(it) => it,
None => return (func.into(), substs),
};
@@ -1610,7 +1617,7 @@ impl<'db> SourceAnalyzer<'db> {
const_id: ConstId,
subs: GenericArgs<'db>,
) -> (ConstId, GenericArgs<'db>) {
- let owner = match self.resolver.expression_store_owner() {
+ let owner = match self.resolver.generic_def() {
Some(it) => it,
None => return (const_id, subs),
};
diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs
index 61e0b1e611..ec5503fe39 100644
--- a/crates/rust-analyzer/src/cli/analysis_stats.rs
+++ b/crates/rust-analyzer/src/cli/analysis_stats.rs
@@ -415,7 +415,7 @@ impl flags::AnalysisStats {
hir_def::AdtId::from(a),
GenericArgs::empty(interner).store(),
hir_ty::ParamEnvAndCrate {
- param_env: db.trait_environment(GenericDefId::from(a).into()),
+ param_env: db.trait_environment(a.into()),
krate: a.krate(db).into(),
}
.store(),