Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-ty/src/mir/borrowck.rs')
-rw-r--r--crates/hir-ty/src/mir/borrowck.rs163
1 files changed, 81 insertions, 82 deletions
diff --git a/crates/hir-ty/src/mir/borrowck.rs b/crates/hir-ty/src/mir/borrowck.rs
index 01892657bc..dece61a57d 100644
--- a/crates/hir-ty/src/mir/borrowck.rs
+++ b/crates/hir-ty/src/mir/borrowck.rs
@@ -8,16 +8,17 @@ use std::iter;
use hir_def::{DefWithBodyId, HasModule};
use la_arena::ArenaMap;
use rustc_hash::FxHashMap;
+use rustc_type_ir::inherent::GenericArgs as _;
use stdx::never;
use triomphe::Arc;
use crate::{
- TraitEnvironment,
+ InferenceResult,
db::{HirDatabase, InternedClosure, InternedClosureId},
display::DisplayTarget,
mir::OperandKind,
next_solver::{
- DbInterner, GenericArgs, Ty, TypingMode,
+ DbInterner, GenericArgs, ParamEnv, StoredTy, Ty, TypingMode,
infer::{DbInternerInferExt, InferCtxt},
},
};
@@ -36,44 +37,44 @@ pub enum MutabilityReason {
}
#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct MovedOutOfRef<'db> {
- pub ty: Ty<'db>,
+pub struct MovedOutOfRef {
+ pub ty: StoredTy,
pub span: MirSpan,
}
#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct PartiallyMoved<'db> {
- pub ty: Ty<'db>,
+pub struct PartiallyMoved {
+ pub ty: StoredTy,
pub span: MirSpan,
- pub local: LocalId<'db>,
+ pub local: LocalId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct BorrowRegion<'db> {
- pub local: LocalId<'db>,
+pub struct BorrowRegion {
+ pub local: LocalId,
pub kind: BorrowKind,
pub places: Vec<MirSpan>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct BorrowckResult<'db> {
- pub mir_body: Arc<MirBody<'db>>,
- pub mutability_of_locals: ArenaMap<LocalId<'db>, MutabilityReason>,
- pub moved_out_of_ref: Vec<MovedOutOfRef<'db>>,
- pub partially_moved: Vec<PartiallyMoved<'db>>,
- pub borrow_regions: Vec<BorrowRegion<'db>>,
+pub struct BorrowckResult {
+ pub mir_body: Arc<MirBody>,
+ pub mutability_of_locals: ArenaMap<LocalId, MutabilityReason>,
+ pub moved_out_of_ref: Vec<MovedOutOfRef>,
+ pub partially_moved: Vec<PartiallyMoved>,
+ pub borrow_regions: Vec<BorrowRegion>,
}
-fn all_mir_bodies<'db>(
- db: &'db dyn HirDatabase,
+fn all_mir_bodies(
+ db: &dyn HirDatabase,
def: DefWithBodyId,
- mut cb: impl FnMut(Arc<MirBody<'db>>),
-) -> Result<(), MirLowerError<'db>> {
- fn for_closure<'db>(
- db: &'db dyn HirDatabase,
+ mut cb: impl FnMut(Arc<MirBody>),
+) -> Result<(), MirLowerError> {
+ fn for_closure(
+ db: &dyn HirDatabase,
c: InternedClosureId,
- cb: &mut impl FnMut(Arc<MirBody<'db>>),
- ) -> Result<(), MirLowerError<'db>> {
+ cb: &mut impl FnMut(Arc<MirBody>),
+ ) -> Result<(), MirLowerError> {
match db.mir_body_for_closure(c) {
Ok(body) => {
cb(body.clone());
@@ -91,13 +92,13 @@ fn all_mir_bodies<'db>(
}
}
-pub fn borrowck_query<'db>(
- db: &'db dyn HirDatabase,
+pub fn borrowck_query(
+ db: &dyn HirDatabase,
def: DefWithBodyId,
-) -> Result<Arc<[BorrowckResult<'db>]>, MirLowerError<'db>> {
+) -> Result<Arc<[BorrowckResult]>, MirLowerError> {
let _p = tracing::info_span!("borrowck_query").entered();
let module = def.module(db);
- let interner = DbInterner::new_with(db, Some(module.krate()), module.containing_block());
+ let interner = DbInterner::new_with(db, module.krate(db));
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()`.
@@ -106,9 +107,9 @@ pub fn borrowck_query<'db>(
// FIXME(next-solver): Opaques.
let infcx = interner.infer_ctxt().build(typing_mode);
res.push(BorrowckResult {
- mutability_of_locals: mutability_of_locals(&infcx, &body),
- moved_out_of_ref: moved_out_of_ref(&infcx, &env, &body),
- partially_moved: partially_moved(&infcx, &env, &body),
+ 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),
mir_body: body,
});
@@ -121,24 +122,24 @@ fn make_fetch_closure_field<'db>(
) -> impl FnOnce(InternedClosureId, GenericArgs<'db>, usize) -> Ty<'db> + use<'db> {
|c: InternedClosureId, subst: GenericArgs<'db>, f: usize| {
let InternedClosure(def, _) = db.lookup_intern_closure(c);
- let infer = db.infer(def);
+ let infer = InferenceResult::for_body(db, def);
let (captures, _) = infer.closure_info(c);
- let parent_subst = subst.split_closure_args_untupled().parent_args;
- let interner = DbInterner::new_with(db, None, None);
- captures.get(f).expect("broken closure field").ty.instantiate(interner, parent_subst)
+ let parent_subst = subst.as_closure().parent_args();
+ let interner = DbInterner::new_no_crate(db);
+ captures.get(f).expect("broken closure field").ty.get().instantiate(interner, parent_subst)
}
}
fn moved_out_of_ref<'db>(
infcx: &InferCtxt<'db>,
- env: &TraitEnvironment<'db>,
- body: &MirBody<'db>,
-) -> Vec<MovedOutOfRef<'db>> {
+ env: ParamEnv<'db>,
+ body: &MirBody,
+) -> Vec<MovedOutOfRef> {
let db = infcx.interner.db;
let mut result = vec![];
- let mut for_operand = |op: &Operand<'db>, span: MirSpan| match op.kind {
+ let mut for_operand = |op: &Operand, span: MirSpan| match op.kind {
OperandKind::Copy(p) | OperandKind::Move(p) => {
- let mut ty: Ty<'db> = body.locals[p.local].ty;
+ let mut ty: Ty<'db> = body.locals[p.local].ty.as_ref();
let mut is_dereference_of_ref = false;
for proj in p.projection.lookup(&body.projection_store) {
if *proj == ProjectionElem::Deref && ty.as_reference().is_some() {
@@ -146,16 +147,17 @@ fn moved_out_of_ref<'db>(
}
ty = proj.projected_ty(
infcx,
+ env,
ty,
make_fetch_closure_field(db),
- body.owner.module(db).krate(),
+ body.owner.module(db).krate(db),
);
}
if is_dereference_of_ref
- && !infcx.type_is_copy_modulo_regions(env.env, ty)
+ && !infcx.type_is_copy_modulo_regions(env, ty)
&& !ty.references_non_lt_error()
{
- result.push(MovedOutOfRef { span: op.span.unwrap_or(span), ty });
+ result.push(MovedOutOfRef { span: op.span.unwrap_or(span), ty: ty.store() });
}
}
OperandKind::Constant { .. } | OperandKind::Static(_) => (),
@@ -231,24 +233,25 @@ fn moved_out_of_ref<'db>(
fn partially_moved<'db>(
infcx: &InferCtxt<'db>,
- env: &TraitEnvironment<'db>,
- body: &MirBody<'db>,
-) -> Vec<PartiallyMoved<'db>> {
+ env: ParamEnv<'db>,
+ body: &MirBody,
+) -> Vec<PartiallyMoved> {
let db = infcx.interner.db;
let mut result = vec![];
- let mut for_operand = |op: &Operand<'db>, span: MirSpan| match op.kind {
+ let mut for_operand = |op: &Operand, span: MirSpan| match op.kind {
OperandKind::Copy(p) | OperandKind::Move(p) => {
- let mut ty: Ty<'db> = body.locals[p.local].ty;
+ let mut ty: Ty<'db> = body.locals[p.local].ty.as_ref();
for proj in p.projection.lookup(&body.projection_store) {
ty = proj.projected_ty(
infcx,
+ env,
ty,
make_fetch_closure_field(db),
- body.owner.module(db).krate(),
+ body.owner.module(db).krate(db),
);
}
- if !infcx.type_is_copy_modulo_regions(env.env, ty) && !ty.references_non_lt_error() {
- result.push(PartiallyMoved { span, ty, local: p.local });
+ if !infcx.type_is_copy_modulo_regions(env, ty) && !ty.references_non_lt_error() {
+ result.push(PartiallyMoved { span, ty: ty.store(), local: p.local });
}
}
OperandKind::Constant { .. } | OperandKind::Static(_) => (),
@@ -322,7 +325,7 @@ fn partially_moved<'db>(
result
}
-fn borrow_regions<'db>(db: &'db dyn HirDatabase, body: &MirBody<'db>) -> Vec<BorrowRegion<'db>> {
+fn borrow_regions(db: &dyn HirDatabase, body: &MirBody) -> Vec<BorrowRegion> {
let mut borrows = FxHashMap::default();
for (_, block) in body.basic_blocks.iter() {
db.unwind_if_revision_cancelled();
@@ -330,7 +333,7 @@ fn borrow_regions<'db>(db: &'db dyn HirDatabase, body: &MirBody<'db>) -> Vec<Bor
if let StatementKind::Assign(_, Rvalue::Ref(kind, p)) = &statement.kind {
borrows
.entry(p.local)
- .and_modify(|it: &mut BorrowRegion<'db>| {
+ .and_modify(|it: &mut BorrowRegion| {
it.places.push(statement.span);
})
.or_insert_with(|| BorrowRegion {
@@ -374,12 +377,13 @@ enum ProjectionCase {
fn place_case<'db>(
infcx: &InferCtxt<'db>,
- body: &MirBody<'db>,
- lvalue: &Place<'db>,
+ env: ParamEnv<'db>,
+ body: &MirBody,
+ lvalue: &Place,
) -> ProjectionCase {
let db = infcx.interner.db;
let mut is_part_of = false;
- let mut ty = body.locals[lvalue.local].ty;
+ let mut ty = body.locals[lvalue.local].ty.as_ref();
for proj in lvalue.projection.lookup(&body.projection_store).iter() {
match proj {
ProjectionElem::Deref if ty.as_adt().is_none() => return ProjectionCase::Indirect, // It's indirect in case of reference and raw
@@ -395,9 +399,10 @@ fn place_case<'db>(
}
ty = proj.projected_ty(
infcx,
+ env,
ty,
make_fetch_closure_field(db),
- body.owner.module(db).krate(),
+ body.owner.module(db).krate(db),
);
}
if is_part_of { ProjectionCase::DirectPart } else { ProjectionCase::Direct }
@@ -406,18 +411,18 @@ fn place_case<'db>(
/// Returns a map from basic blocks to the set of locals that might be ever initialized before
/// the start of the block. Only `StorageDead` can remove something from this map, and we ignore
/// `Uninit` and `drop` and similar after initialization.
-fn ever_initialized_map<'db>(
- db: &'db dyn HirDatabase,
- body: &MirBody<'db>,
-) -> ArenaMap<BasicBlockId<'db>, ArenaMap<LocalId<'db>, bool>> {
- let mut result: ArenaMap<BasicBlockId<'db>, ArenaMap<LocalId<'db>, bool>> =
+fn ever_initialized_map(
+ db: &dyn HirDatabase,
+ body: &MirBody,
+) -> ArenaMap<BasicBlockId, ArenaMap<LocalId, bool>> {
+ let mut result: ArenaMap<BasicBlockId, ArenaMap<LocalId, bool>> =
body.basic_blocks.iter().map(|it| (it.0, ArenaMap::default())).collect();
- fn dfs<'db>(
- db: &'db dyn HirDatabase,
- body: &MirBody<'db>,
- l: LocalId<'db>,
- stack: &mut Vec<BasicBlockId<'db>>,
- result: &mut ArenaMap<BasicBlockId<'db>, ArenaMap<LocalId<'db>, bool>>,
+ fn dfs(
+ db: &dyn HirDatabase,
+ body: &MirBody,
+ l: LocalId,
+ stack: &mut Vec<BasicBlockId>,
+ result: &mut ArenaMap<BasicBlockId, ArenaMap<LocalId, bool>>,
) {
while let Some(b) = stack.pop() {
let mut is_ever_initialized = result[b][l]; // It must be filled, as we use it as mark for dfs
@@ -505,11 +510,7 @@ fn ever_initialized_map<'db>(
result
}
-fn push_mut_span<'db>(
- local: LocalId<'db>,
- span: MirSpan,
- result: &mut ArenaMap<LocalId<'db>, MutabilityReason>,
-) {
+fn push_mut_span(local: LocalId, span: MirSpan, result: &mut ArenaMap<LocalId, MutabilityReason>) {
match &mut result[local] {
MutabilityReason::Mut { spans } => spans.push(span),
it @ (MutabilityReason::Not | MutabilityReason::Unused) => {
@@ -518,16 +519,13 @@ fn push_mut_span<'db>(
};
}
-fn record_usage<'db>(local: LocalId<'db>, result: &mut ArenaMap<LocalId<'db>, MutabilityReason>) {
+fn record_usage(local: LocalId, result: &mut ArenaMap<LocalId, MutabilityReason>) {
if let it @ MutabilityReason::Unused = &mut result[local] {
*it = MutabilityReason::Not;
};
}
-fn record_usage_for_operand<'db>(
- arg: &Operand<'db>,
- result: &mut ArenaMap<LocalId<'db>, MutabilityReason>,
-) {
+fn record_usage_for_operand(arg: &Operand, result: &mut ArenaMap<LocalId, MutabilityReason>) {
if let OperandKind::Copy(p) | OperandKind::Move(p) = arg.kind {
record_usage(p.local, result);
}
@@ -535,10 +533,11 @@ fn record_usage_for_operand<'db>(
fn mutability_of_locals<'db>(
infcx: &InferCtxt<'db>,
- body: &MirBody<'db>,
-) -> ArenaMap<LocalId<'db>, MutabilityReason> {
+ env: ParamEnv<'db>,
+ body: &MirBody,
+) -> ArenaMap<LocalId, MutabilityReason> {
let db = infcx.interner.db;
- let mut result: ArenaMap<LocalId<'db>, MutabilityReason> =
+ let mut result: ArenaMap<LocalId, MutabilityReason> =
body.locals.iter().map(|it| (it.0, MutabilityReason::Unused)).collect();
let ever_init_maps = ever_initialized_map(db, body);
@@ -547,7 +546,7 @@ fn mutability_of_locals<'db>(
for statement in &block.statements {
match &statement.kind {
StatementKind::Assign(place, value) => {
- match place_case(infcx, body, place) {
+ match place_case(infcx, env, body, place) {
ProjectionCase::Direct => {
if ever_init_map.get(place.local).copied().unwrap_or_default() {
push_mut_span(place.local, statement.span, &mut result);
@@ -596,7 +595,7 @@ fn mutability_of_locals<'db>(
},
p,
) = value
- && place_case(infcx, body, p) != ProjectionCase::Indirect
+ && place_case(infcx, env, body, p) != ProjectionCase::Indirect
{
push_mut_span(p.local, statement.span, &mut result);
}