Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions crates/hir-ty/src/consteval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/diagnostics/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
8 changes: 2 additions & 6 deletions crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,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 {
Expand Down
16 changes: 6 additions & 10 deletions crates/hir-ty/src/layout/tests.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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())
})
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/mir/borrowck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/mir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/mir/eval/tests.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
7 changes: 3 additions & 4 deletions crates/hir-ty/src/opaques.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 2 additions & 7 deletions crates/hir-ty/src/specialization.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-ty/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn implements_trait_unique<'db>(
env: ParamEnvAndCrate<'db>,
trait_: TraitId,
) -> bool {
implements_trait_unique_impl(db, env, trait_, &mut |infcx| {
implements_trait_unique_with_infcx(db, env, trait_, &mut |infcx| {
infcx.fill_rest_fresh_args(Span::Dummy, trait_.into(), [ty.into()])
})
}
Expand All @@ -133,10 +133,10 @@ pub fn implements_trait_unique_with_args<'db>(
trait_: TraitId,
args: GenericArgs<'db>,
) -> bool {
implements_trait_unique_impl(db, env, trait_, &mut |_| args)
implements_trait_unique_with_infcx(db, env, trait_, &mut |_| args)
}

fn implements_trait_unique_impl<'db>(
pub fn implements_trait_unique_with_infcx<'db>(
db: &'db dyn HirDatabase,
env: ParamEnvAndCrate<'db>,
trait_: TraitId,
Expand Down
15 changes: 8 additions & 7 deletions crates/hir/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use stdx::never;

use crate::{
Adt, AsAssocItem, AssocItem, BuiltinType, Const, ConstParam, DocLinkDef, Enum, EnumVariant,
ExternCrateDecl, Field, Function, GenericParam, HasCrate, Impl, LangItem, LifetimeParam, Macro,
Module, ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, Variant,
ExternCrateDecl, Field, Function, GenericParam, Impl, LangItem, LifetimeParam, Macro, Module,
ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, Variant,
};

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -487,27 +487,28 @@ fn resolve_impl_trait_item<'db>(
ns: Option<Namespace>,
) -> Option<DocLinkDef> {
let krate = ty.krate(db);
let environment = crate::param_env_from_resolver(db, &resolver);
let param_env = ty.param_env(db);
let traits_in_scope = resolver.traits_in_scope(db);

// `ty.iterate_path_candidates()` require a scope, which is not available when resolving
// attributes here. Use path resolution directly instead.
//
// FIXME: resolve type aliases (which are not yielded by iterate_path_candidates)
let interner = DbInterner::new_with(db, environment.krate);
let interner = DbInterner::new_with(db, param_env.krate);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
let features = resolver.top_level_def_map().features();
let ctx = MethodResolutionContext {
infcx: &infcx,
resolver: &resolver,
param_env: environment.param_env,
param_env: param_env.param_env,
traits_in_scope: &traits_in_scope,
edition: krate.edition(db),
edition: krate.data(db).edition,
features,
call_span: hir_ty::Span::Dummy,
receiver_span: hir_ty::Span::Dummy,
};
let resolution = ctx.probe_for_name(method_resolution::Mode::Path, name.clone(), ty.ty);
let resolution =
ctx.probe_for_name(method_resolution::Mode::Path, name.clone(), ty.ty.skip_binder());
let resolution = match resolution {
Ok(resolution) => resolution.item,
Err(MethodError::PrivateMatch(resolution)) => resolution.item,
Expand Down
Loading
Loading