Unnamed repository; edit this file 'description' to name the repository.
migrate `HirDatabase::borrowck`
Ada Alakbarova 4 weeks ago
parent b6cec38 · commit a2a9b4b
-rw-r--r--crates/hir-ty/src/db.rs7
-rw-r--r--crates/hir-ty/src/mir.rs2
-rw-r--r--crates/hir-ty/src/mir/borrowck.rs106
-rw-r--r--crates/hir-ty/src/mir/lower/tests.rs4
-rw-r--r--crates/hir/src/lib.rs2
-rw-r--r--crates/hir/src/term_search/tactics.rs2
6 files changed, 63 insertions, 60 deletions
diff --git a/crates/hir-ty/src/db.rs b/crates/hir-ty/src/db.rs
index 028694cd94..c74ac51b61 100644
--- a/crates/hir-ty/src/db.rs
+++ b/crates/hir-ty/src/db.rs
@@ -27,7 +27,7 @@ use crate::{
dyn_compatibility::DynCompatibilityViolation,
layout::{Layout, LayoutError},
lower::{GenericDefaults, TrackedStructToken, TypeAliasBounds},
- mir::{BorrowckResult, MirBody, MirLowerError},
+ mir::{MirBody, MirLowerError},
next_solver::{
Allocation, Clause, EarlyBinder, GenericArgs, ParamEnv, PolyFnSig, StoredClauses,
StoredEarlyBinder, StoredGenericArgs, StoredPolyFnSig, StoredTraitRef, StoredTy, TraitRef,
@@ -73,11 +73,6 @@ pub trait HirDatabase: SourceDatabase + std::fmt::Debug {
.map_err(|err| err.clone())
}
- #[salsa::transparent]
- fn borrowck(&self, def: InferBodyId) -> Result<&[BorrowckResult], MirLowerError> {
- crate::mir::borrowck_query(self, def).map_err(|err| err.clone())
- }
-
#[salsa::invoke(crate::consteval::const_eval)]
#[salsa::transparent]
fn const_eval<'db>(
diff --git a/crates/hir-ty/src/mir.rs b/crates/hir-ty/src/mir.rs
index d004e3b5da..2fa3f5e797 100644
--- a/crates/hir-ty/src/mir.rs
+++ b/crates/hir-ty/src/mir.rs
@@ -37,7 +37,7 @@ mod lower;
mod monomorphization;
mod pretty;
-pub use borrowck::{BorrowckResult, MutabilityReason, borrowck_query};
+pub use borrowck::{BorrowckResult, MutabilityReason};
pub use eval::{
Evaluator, MirEvalError, VTableMap, interpret_mir, pad16, render_const_using_debug_impl,
};
diff --git a/crates/hir-ty/src/mir/borrowck.rs b/crates/hir-ty/src/mir/borrowck.rs
index 31a75d6ab0..a9271675a0 100644
--- a/crates/hir-ty/src/mir/borrowck.rs
+++ b/crates/hir-ty/src/mir/borrowck.rs
@@ -133,58 +133,66 @@ fn all_mir_bodies<'db>(
}
}
-#[salsa_macros::tracked(returns(as_deref), lru = 2024)]
-pub fn borrowck_query(
- db: &dyn HirDatabase,
- def: InferBodyId,
-) -> Result<Box<[BorrowckResult]>, MirLowerError> {
- 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.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());
- all_mir_bodies(
- db,
- def,
- |body, owner| {
- // FIXME(next-solver): Opaques.
- let infcx = interner.infer_ctxt().build(typing_mode);
- BorrowckResult {
- owner,
- mutability_of_locals: mutability_of_locals(&infcx, env, body),
- moved_out_of_ref: moved_out_of_ref(&infcx, env, body),
- partially_moved: partially_moved(&infcx, env, body),
- borrow_regions: borrow_regions(db, body),
- }
- },
- |(parent, parent_mir_body), (child, child_mir_body)| {
- for (upvar, child_locals) in &child_mir_body.upvar_locals {
- let Some(&parent_local) = parent_mir_body.binding_locals.get(*upvar) else {
- continue;
- };
- for (child_local, capture_place) in child_locals {
- if !capture_place
- .projections
- .iter()
- .any(|proj| matches!(proj.kind, HirProjectionKind::Deref))
- {
- let parent_mol = &mut parent.mutability_of_locals[parent_local];
- match (&*parent_mol, &child.mutability_of_locals[*child_local]) {
- (MutabilityReason::Mut { .. }, _) => {}
- (_, MutabilityReason::Mut { .. }) => {
- // FIXME: Fix the child spans.
- *parent_mol = MutabilityReason::Mut { spans: Vec::new() }
+impl InferBodyId {
+ pub fn borrowck(self, db: &dyn HirDatabase) -> Result<&[BorrowckResult], MirLowerError> {
+ return borrowck_query(db, self).map_err(|e| e.clone());
+
+ #[salsa::tracked(returns(as_deref), lru = 2024)]
+ fn borrowck_query(
+ db: &dyn HirDatabase,
+ def: InferBodyId,
+ ) -> Result<Box<[BorrowckResult]>, MirLowerError> {
+ let _p = tracing::info_span!("InferBodyId::borrowck").entered();
+ let module = def.module(db);
+ let interner = DbInterner::new_with(db, module.krate(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());
+ all_mir_bodies(
+ db,
+ def,
+ |body, owner| {
+ // FIXME(next-solver): Opaques.
+ let infcx = interner.infer_ctxt().build(typing_mode);
+ BorrowckResult {
+ owner,
+ mutability_of_locals: mutability_of_locals(&infcx, env, body),
+ moved_out_of_ref: moved_out_of_ref(&infcx, env, body),
+ partially_moved: partially_moved(&infcx, env, body),
+ borrow_regions: borrow_regions(db, body),
+ }
+ },
+ |(parent, parent_mir_body), (child, child_mir_body)| {
+ for (upvar, child_locals) in &child_mir_body.upvar_locals {
+ let Some(&parent_local) = parent_mir_body.binding_locals.get(*upvar) else {
+ continue;
+ };
+ for (child_local, capture_place) in child_locals {
+ if !capture_place
+ .projections
+ .iter()
+ .any(|proj| matches!(proj.kind, HirProjectionKind::Deref))
+ {
+ let parent_mol = &mut parent.mutability_of_locals[parent_local];
+ match (&*parent_mol, &child.mutability_of_locals[*child_local]) {
+ (MutabilityReason::Mut { .. }, _) => {}
+ (_, MutabilityReason::Mut { .. }) => {
+ // FIXME: Fix the child spans.
+ *parent_mol = MutabilityReason::Mut { spans: Vec::new() }
+ }
+ (MutabilityReason::Not, _) => {}
+ (_, MutabilityReason::Not) => {
+ *parent_mol = MutabilityReason::Not
+ }
+ (MutabilityReason::Unused, MutabilityReason::Unused) => {}
+ }
}
- (MutabilityReason::Not, _) => {}
- (_, MutabilityReason::Not) => *parent_mol = MutabilityReason::Not,
- (MutabilityReason::Unused, MutabilityReason::Unused) => {}
}
}
- }
- }
- },
- )
+ },
+ )
+ }
+ }
}
fn moved_out_of_ref<'db>(
diff --git a/crates/hir-ty/src/mir/lower/tests.rs b/crates/hir-ty/src/mir/lower/tests.rs
index cd49549388..d42072afa4 100644
--- a/crates/hir-ty/src/mir/lower/tests.rs
+++ b/crates/hir-ty/src/mir/lower/tests.rs
@@ -1,7 +1,7 @@
use hir_def::DefWithBodyId;
use test_fixture::WithFixture;
-use crate::{db::HirDatabase, setup_tracing, test_db::TestDB};
+use crate::{InferBodyId, db::HirDatabase, setup_tracing, test_db::TestDB};
fn lower_mir(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
let _tracing = setup_tracing();
@@ -78,7 +78,7 @@ fn check_borrowck(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
}
for body in bodies {
- let _ = db.borrowck(body.into());
+ let _ = InferBodyId::from(body).borrowck(&db);
}
})
}
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 4beb7e9033..1ba73c180c 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -2100,7 +2100,7 @@ impl DefWithBody {
}
}
- if let Ok(borrowck_results) = db.borrowck(id.into()) {
+ if let Ok(borrowck_results) = InferBodyId::from(id).borrowck(db) {
for borrowck_result in borrowck_results {
let mir_body = borrowck_result.mir_body(db);
for moof in &borrowck_result.moved_out_of_ref {
diff --git a/crates/hir/src/term_search/tactics.rs b/crates/hir/src/term_search/tactics.rs
index 2b7f7da3bf..8e107afc9e 100644
--- a/crates/hir/src/term_search/tactics.rs
+++ b/crates/hir/src/term_search/tactics.rs
@@ -48,7 +48,7 @@ pub(super) fn trivial<'a, 'lt, 'db, DB: HirDatabase>(
ScopeDef::GenericParam(GenericParam::ConstParam(it)) => Some(Expr::ConstParam(*it)),
ScopeDef::Local(it) => {
if ctx.config.enable_borrowcheck {
- let borrowck = db.borrowck(it.parent_infer).ok()?;
+ let borrowck = it.parent_infer.borrowck(db).ok()?;
let invalid = borrowck.iter().any(|b| {
let mir_body = b.mir_body(ctx.sema.db);