Skip to content
Merged
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
2 changes: 1 addition & 1 deletion crates/hir-ty/src/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn autoderef<'db>(
env: Arc<TraitEnvironment<'db>>,
ty: Canonical<'db, Ty<'db>>,
) -> impl Iterator<Item = Ty<'db>> + use<'db> {
let interner = DbInterner::new_with(db, env.krate, env.block);
let interner = DbInterner::new_with(db, env.krate);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
let (ty, _) = infcx.instantiate_canonical(&ty);
let autoderef = Autoderef::new(&infcx, &env, ty);
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 @@ -79,7 +79,7 @@ impl BodyValidationDiagnostic {
let infer = InferenceResult::for_body(db, owner);
let body = db.body(owner);
let env = db.trait_environment_for_body(owner);
let interner = DbInterner::new_with(db, env.krate, env.block);
let interner = DbInterner::new_with(db, env.krate);
let infcx =
interner.infer_ctxt().build(TypingMode::typeck_for_body(interner, owner.into()));
let mut validator = ExprValidator {
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 @@ -309,7 +309,7 @@ pub trait HirDisplay<'db> {
allow_opaque: bool,
) -> Result<String, DisplaySourceCodeError> {
let mut result = String::new();
let interner = DbInterner::new_with(db, module_id.krate(), module_id.containing_block());
let interner = DbInterner::new_with(db, module_id.krate());
match self.hir_fmt(&mut HirFormatter {
db,
interner,
Expand Down Expand Up @@ -544,11 +544,7 @@ pub enum ClosureStyle {
impl<'db, T: HirDisplay<'db>> HirDisplayWrapper<'_, 'db, T> {
pub fn write_to<F: HirWrite>(&self, f: &mut F) -> Result<(), HirDisplayError> {
let krate = self.display_target.krate;
let block = match self.display_kind {
DisplayKind::SourceCode { target_module_id, .. } => target_module_id.containing_block(),
DisplayKind::Diagnostics | DisplayKind::Test => None,
};
let interner = DbInterner::new_with(self.db, krate, block);
let interner = DbInterner::new_with(self.db, krate);
self.t.hir_fmt(&mut HirFormatter {
db: self.db,
interner,
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/dyn_compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn dyn_compatibility_of_trait_query(

pub fn generics_require_sized_self(db: &dyn HirDatabase, def: GenericDefId) -> bool {
let krate = def.module(db).krate();
let interner = DbInterner::new_with(db, krate, None);
let interner = DbInterner::new_with(db, krate);
let Some(sized) = interner.lang_items().Sized else {
return false;
};
Expand Down Expand Up @@ -402,7 +402,7 @@ fn receiver_is_dispatchable<'db>(
let sig = sig.instantiate_identity();

let module = trait_.module(db);
let interner = DbInterner::new_with(db, module.krate(), module.containing_block());
let interner = DbInterner::new_with(db, module.krate());
let self_param_id = TypeParamId::from_unchecked(TypeOrConstParamId {
parent: trait_.into(),
local_id: LocalTypeOrConstParamId::from_raw(la_arena::RawIdx::from_u32(0)),
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/infer/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,7 @@ fn coerce<'db>(
env: Arc<TraitEnvironment<'db>>,
tys: &Canonical<'db, (Ty<'db>, Ty<'db>)>,
) -> Result<(Vec<Adjustment<'db>>, Ty<'db>), TypeError<DbInterner<'db>>> {
let interner = DbInterner::new_with(db, env.krate, env.block);
let interner = DbInterner::new_with(db, env.krate);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
let ((ty1_with_vars, ty2_with_vars), vars) = infcx.instantiate_canonical(tys);

Expand Down
8 changes: 3 additions & 5 deletions crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1455,11 +1455,10 @@ impl<'db> InferenceContext<'_, 'db> {
) -> Ty<'db> {
let coerce_ty = expected.coercion_target_type(&mut self.table);
let g = self.resolver.update_to_inner_scope(self.db, self.owner, expr);
let prev_state = block_id.map(|block_id| {
let prev_env = block_id.map(|block_id| {
let prev_env = self.table.trait_env.clone();
TraitEnvironment::with_block(&mut self.table.trait_env, block_id);
let prev_block = self.table.infer_ctxt.interner.block.replace(block_id);
(prev_env, prev_block)
prev_env
});

let (break_ty, ty) =
Expand Down Expand Up @@ -1567,9 +1566,8 @@ impl<'db> InferenceContext<'_, 'db> {
}
});
self.resolver.reset_to_guard(g);
if let Some((prev_env, prev_block)) = prev_state {
if let Some(prev_env) = prev_env {
self.table.trait_env = prev_env;
self.table.infer_ctxt.interner.block = prev_block;
}

break_ty.unwrap_or(ty)
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn could_unify_impl<'db>(
tys: &Canonical<'db, (Ty<'db>, Ty<'db>)>,
select: for<'a> fn(&mut ObligationCtxt<'a, 'db>) -> Vec<NextSolverError<'db>>,
) -> bool {
let interner = DbInterner::new_with(db, env.krate, env.block);
let interner = DbInterner::new_with(db, env.krate);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
let cause = ObligationCause::dummy();
let at = infcx.at(&cause, env.env);
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<'db> InferenceTable<'db> {
trait_env: Arc<TraitEnvironment<'db>>,
owner: Option<DefWithBodyId>,
) -> Self {
let interner = DbInterner::new_with(db, trait_env.krate, trait_env.block);
let interner = DbInterner::new_with(db, trait_env.krate);
let typing_mode = match owner {
Some(owner) => TypingMode::typeck_for_body(interner, owner.into()),
// IDE things wants to reveal opaque types.
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub fn layout_of_ty_query<'db>(
trait_env: Arc<TraitEnvironment<'db>>,
) -> Result<Arc<Layout>, LayoutError> {
let krate = trait_env.krate;
let interner = DbInterner::new_with(db, krate, trait_env.block);
let interner = DbInterner::new_with(db, krate);
let Ok(target) = db.target_data_layout(krate) else {
return Err(LayoutError::TargetLayoutNotAvailable);
};
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
) -> Self {
let impl_trait_mode = ImplTraitLoweringState::new(ImplTraitLoweringMode::Disallowed);
let in_binders = DebruijnIndex::ZERO;
let interner = DbInterner::new_with(db, resolver.krate(), None);
let interner = DbInterner::new_with(db, resolver.krate());
Self {
db,
// Can provide no block since we don't use it for trait solving.
Expand Down Expand Up @@ -1756,7 +1756,7 @@ pub(crate) fn trait_environment_query<'db>(
def: GenericDefId,
) -> Arc<TraitEnvironment<'db>> {
let module = def.module(db);
let interner = DbInterner::new_with(db, module.krate(), module.containing_block());
let interner = DbInterner::new_with(db, module.krate());
let predicates = GenericPredicates::query_all(db, def);
let traits_in_scope = predicates
.iter_identity_copied()
Expand Down Expand Up @@ -2289,7 +2289,7 @@ pub(crate) fn associated_type_by_name_including_super_traits<'db>(
name: &Name,
) -> Option<(TraitRef<'db>, TypeAliasId)> {
let module = trait_ref.def_id.0.module(db);
let interner = DbInterner::new_with(db, module.krate(), module.containing_block());
let interner = DbInterner::new_with(db, module.krate());
rustc_type_ir::elaborate::supertraits(interner, Binder::dummy(trait_ref)).find_map(|t| {
let trait_id = t.as_ref().skip_binder().def_id.0;
let assoc_type = trait_id.trait_items(db).associated_type_by_name(name)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ pub(crate) fn lookup_impl_method_query<'db>(
func: FunctionId,
fn_subst: GenericArgs<'db>,
) -> (FunctionId, GenericArgs<'db>) {
let interner = DbInterner::new_with(db, env.krate, env.block);
let interner = DbInterner::new_with(db, env.krate);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);

let ItemContainerId::TraitId(trait_id) = func.loc(db).container else {
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 @@ -97,7 +97,7 @@ pub fn borrowck_query<'db>(
) -> Result<Arc<[BorrowckResult<'db>]>, MirLowerError<'db>> {
let _p = tracing::info_span!("borrowck_query").entered();
let module = def.module(db);
let interner = DbInterner::new_with(db, module.krate(), module.containing_block());
let interner = DbInterner::new_with(db, module.krate());
let env = db.trait_environment_for_body(def);
let mut res = vec![];
// This calculates opaques defining scope which is a bit costly therefore is put outside `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 @@ -641,7 +641,7 @@ impl<'db> Evaluator<'db> {
Err(e) => return Err(MirEvalError::TargetDataLayoutNotAvailable(e)),
};
let cached_ptr_size = target_data_layout.pointer_size().bytes_usize();
let interner = DbInterner::new_with(db, crate_id, module.containing_block());
let interner = DbInterner::new_with(db, crate_id);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
let lang_items = interner.lang_items();
Ok(Evaluator {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl<'a, 'db> MirLowerCtx<'a, 'db> {
};
let resolver = owner.resolver(db);
let env = db.trait_environment_for_body(owner);
let interner = DbInterner::new_with(db, env.krate, env.block);
let interner = DbInterner::new_with(db, env.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
2 changes: 1 addition & 1 deletion crates/hir-ty/src/mir/monomorphization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'db> Filler<'db> {
env: Arc<TraitEnvironment<'db>>,
subst: GenericArgs<'db>,
) -> Self {
let interner = DbInterner::new_with(db, env.krate, env.block);
let interner = DbInterner::new_with(db, env.krate);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
Self { infcx, trait_env: env, subst }
}
Expand Down
20 changes: 6 additions & 14 deletions crates/hir-ty/src/next_solver/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub use tls_db::{attach_db, attach_db_allow_change, with_attached_db};

use base_db::Crate;
use hir_def::{
AdtId, BlockId, CallableDefId, DefWithBodyId, EnumVariantId, HasModule, ItemContainerId,
StructId, UnionId, VariantId,
AdtId, CallableDefId, DefWithBodyId, EnumVariantId, HasModule, ItemContainerId, StructId,
UnionId, VariantId,
attrs::AttrFlags,
lang_item::LangItems,
signatures::{FieldData, FnFlags, ImplFlags, StructFlags, TraitFlags},
Expand Down Expand Up @@ -271,8 +271,7 @@ pub use crate::_interned_vec_db as interned_vec_db;
#[derive(Debug, Copy, Clone)]
pub struct DbInterner<'db> {
pub(crate) db: &'db dyn HirDatabase,
pub(crate) krate: Option<Crate>,
pub(crate) block: Option<BlockId>,
krate: Option<Crate>,
lang_items: Option<&'db LangItems>,
}

Expand All @@ -286,29 +285,22 @@ impl<'db> DbInterner<'db> {
crate::with_attached_db(|db| DbInterner {
db: unsafe { std::mem::transmute::<&dyn HirDatabase, &'db dyn HirDatabase>(db) },
krate: None,
block: None,
lang_items: None,
})
}

/// Creates a new interner without an active crate. Good only for interning things, not for trait solving etc..
/// As a rule of thumb, when you create an `InferCtxt`, you need to provide the crate (and the block).
///
/// Elaboration is a special kind: it needs lang items (for `Sized`), therefore it needs `new_with()`, but you
/// can not specify the block.
/// Elaboration is a special kind: it needs lang items (for `Sized`), therefore it needs `new_with()`.
pub fn new_no_crate(db: &'db dyn HirDatabase) -> Self {
DbInterner { db, krate: None, block: None, lang_items: None }
DbInterner { db, krate: None, lang_items: None }
}

pub fn new_with(
db: &'db dyn HirDatabase,
krate: Crate,
block: Option<BlockId>,
) -> DbInterner<'db> {
pub fn new_with(db: &'db dyn HirDatabase, krate: Crate) -> DbInterner<'db> {
DbInterner {
db,
krate: Some(krate),
block,
// As an approximation, when we call `new_with` we're trait solving, therefore we need the lang items.
// This is also convenient since here we have a starting crate but not in `new_no_crate`.
lang_items: Some(hir_def::lang_item::lang_items(db, krate)),
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/opaques.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub(crate) fn tait_hidden_types<'db>(

let loc = type_alias.loc(db);
let module = loc.module(db);
let interner = DbInterner::new_with(db, module.krate(), module.containing_block());
let interner = DbInterner::new_with(db, module.krate());
let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis());
let mut ocx = ObligationCtxt::new(&infcx);
let cause = ObligationCause::dummy();
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn specializes_query(
parent_impl_def_id: ImplId,
) -> bool {
let trait_env = db.trait_environment(specializing_impl_def_id.into());
let interner = DbInterner::new_with(db, trait_env.krate, trait_env.block);
let interner = DbInterner::new_with(db, trait_env.krate);

let specializing_impl_signature = db.impl_signature(specializing_impl_def_id);
let parent_impl_signature = db.impl_signature(parent_impl_def_id);
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ fn implements_trait_unique_impl<'db>(
trait_: TraitId,
create_args: &mut dyn FnMut(&InferCtxt<'db>) -> GenericArgs<'db>,
) -> bool {
let interner = DbInterner::new_with(db, env.krate, env.block);
let interner = DbInterner::new_with(db, env.krate);
// FIXME(next-solver): I believe this should be `PostAnalysis`.
let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis());

Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ fn resolve_impl_trait_item<'db>(
// 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, environment.block);
let interner = DbInterner::new_with(db, environment.krate);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
let unstable_features =
MethodResolutionUnstableFeatures::from_def_map(resolver.top_level_def_map());
Expand Down
8 changes: 4 additions & 4 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ impl Module {
}
self.legacy_macros(db).into_iter().for_each(|m| emit_macro_def_diagnostics(db, acc, m));

let interner = DbInterner::new_with(db, self.id.krate(), self.id.containing_block());
let interner = DbInterner::new_with(db, self.id.krate());
let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis());

let mut impl_assoc_items_scratch = vec![];
Expand Down Expand Up @@ -5130,7 +5130,7 @@ impl<'db> Type<'db> {
args: &[Type<'db>],
alias: TypeAlias,
) -> Option<Type<'db>> {
let interner = DbInterner::new_with(db, self.env.krate, self.env.block);
let interner = DbInterner::new_with(db, self.env.krate);
let args = generic_args_from_tys(
interner,
alias.id.into(),
Expand Down Expand Up @@ -5473,7 +5473,7 @@ impl<'db> Type<'db> {
f: impl FnOnce(&MethodResolutionContext<'_, 'db>) -> R,
) -> R {
let module = resolver.module();
let interner = DbInterner::new_with(db, module.krate(), module.containing_block());
let interner = DbInterner::new_with(db, module.krate());
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
let unstable_features =
MethodResolutionUnstableFeatures::from_def_map(resolver.top_level_def_map());
Expand Down Expand Up @@ -5812,7 +5812,7 @@ impl<'db> Type<'db> {
}

pub fn drop_glue(&self, db: &'db dyn HirDatabase) -> DropGlue {
let interner = DbInterner::new_with(db, self.env.krate, self.env.block);
let interner = DbInterner::new_with(db, self.env.krate);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
hir_ty::drop::has_drop_glue(&infcx, self.ty, self.env.clone())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ impl<'db> SourceAnalyzer<'db> {

let trait_env = container.env;

let interner = DbInterner::new_with(db, trait_env.krate, trait_env.block);
let interner = DbInterner::new_with(db, trait_env.krate);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);

let mut container = Either::Right(container.ty);
Expand Down Expand Up @@ -1427,7 +1427,7 @@ impl<'db> SourceAnalyzer<'db> {
None => return (const_id, subs),
};
let env = db.trait_environment_for_body(owner);
let interner = DbInterner::new_with(db, env.krate, env.block);
let interner = DbInterner::new_with(db, env.krate);
let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
method_resolution::lookup_impl_const(&infcx, env, const_id, subs)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn existing_from_impl(
let variant = sema.to_def(variant)?;
let krate = variant.module(db).krate();
let from_trait = FamousDefs(sema, krate).core_convert_From()?;
let interner = DbInterner::new_with(db, krate.base(), None);
let interner = DbInterner::new_with(db, krate.base());
use hir::next_solver::infer::DbInternerInferExt;
let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn from_impl_exists(
let strukt = sema.to_def(strukt)?;
let krate = strukt.krate(db);
let from_trait = FamousDefs(sema, krate).core_convert_From()?;
let interner = DbInterner::new_with(db, krate.base(), None);
let interner = DbInterner::new_with(db, krate.base());
use hir::next_solver::infer::DbInternerInferExt;
let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis());

Expand Down