Skip to content

Commit b55fbd3

Browse files
committed
Add moved-out-of-ref diagnostic
1 parent 09d1265 commit b55fbd3

File tree

11 files changed

+352
-10
lines changed

11 files changed

+352
-10
lines changed

crates/hir-ty/src/infer/expr.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,18 @@ impl<'a> InferenceContext<'a> {
934934
match fn_x {
935935
FnTrait::FnOnce => (),
936936
FnTrait::FnMut => {
937-
if !matches!(derefed_callee.kind(Interner), TyKind::Ref(Mutability::Mut, _, _)) {
937+
if let TyKind::Ref(Mutability::Mut, _, inner) = derefed_callee.kind(Interner) {
938+
if adjustments
939+
.last()
940+
.map(|x| matches!(x.kind, Adjust::Borrow(_)))
941+
.unwrap_or(true)
942+
{
943+
// prefer reborrow to move
944+
adjustments
945+
.push(Adjustment { kind: Adjust::Deref(None), target: inner.clone() });
946+
adjustments.push(Adjustment::borrow(Mutability::Mut, inner.clone()))
947+
}
948+
} else {
938949
adjustments.push(Adjustment::borrow(Mutability::Mut, derefed_callee.clone()));
939950
}
940951
}

crates/hir-ty/src/mir/borrowck.rs

+115-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
use std::iter;
77

8-
use hir_def::DefWithBodyId;
8+
use hir_def::{DefWithBodyId, HasModule};
99
use la_arena::ArenaMap;
1010
use stdx::never;
1111
use triomphe::Arc;
1212

13-
use crate::{db::HirDatabase, ClosureId};
13+
use crate::{
14+
db::HirDatabase, mir::Operand, utils::ClosureSubst, ClosureId, Interner, Ty, TyExt, TypeFlags,
15+
};
1416

1517
use super::{
1618
BasicBlockId, BorrowKind, LocalId, MirBody, MirLowerError, MirSpan, Place, ProjectionElem,
@@ -24,10 +26,17 @@ pub enum MutabilityReason {
2426
Not,
2527
}
2628

29+
#[derive(Debug, Clone, PartialEq, Eq)]
30+
pub struct MovedOutOfRef {
31+
pub ty: Ty,
32+
pub span: MirSpan,
33+
}
34+
2735
#[derive(Debug, Clone, PartialEq, Eq)]
2836
pub struct BorrowckResult {
2937
pub mir_body: Arc<MirBody>,
3038
pub mutability_of_locals: ArenaMap<LocalId, MutabilityReason>,
39+
pub moved_out_of_ref: Vec<MovedOutOfRef>,
3140
}
3241

3342
fn all_mir_bodies(
@@ -68,12 +77,115 @@ pub fn borrowck_query(
6877
let r = all_mir_bodies(db, def)
6978
.map(|body| {
7079
let body = body?;
71-
Ok(BorrowckResult { mutability_of_locals: mutability_of_locals(&body), mir_body: body })
80+
Ok(BorrowckResult {
81+
mutability_of_locals: mutability_of_locals(&body),
82+
moved_out_of_ref: moved_out_of_ref(db, &body),
83+
mir_body: body,
84+
})
7285
})
7386
.collect::<Result<Vec<_>, MirLowerError>>()?;
7487
Ok(r.into())
7588
}
7689

90+
fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef> {
91+
let mut result = vec![];
92+
let mut for_operand = |op: &Operand, span: MirSpan| match op {
93+
Operand::Copy(p) | Operand::Move(p) => {
94+
let mut ty: Ty = body.locals[p.local].ty.clone();
95+
let mut is_dereference_of_ref = false;
96+
for proj in &p.projection {
97+
if *proj == ProjectionElem::Deref && ty.as_reference().is_some() {
98+
is_dereference_of_ref = true;
99+
}
100+
ty = proj.projected_ty(
101+
ty,
102+
db,
103+
|c, subst, f| {
104+
let (def, _) = db.lookup_intern_closure(c.into());
105+
let infer = db.infer(def);
106+
let (captures, _) = infer.closure_info(&c);
107+
let parent_subst = ClosureSubst(subst).parent_subst();
108+
captures
109+
.get(f)
110+
.expect("broken closure field")
111+
.ty
112+
.clone()
113+
.substitute(Interner, parent_subst)
114+
},
115+
body.owner.module(db.upcast()).krate(),
116+
);
117+
}
118+
if is_dereference_of_ref
119+
&& !ty.clone().is_copy(db, body.owner)
120+
&& !ty.data(Interner).flags.intersects(TypeFlags::HAS_ERROR)
121+
{
122+
result.push(MovedOutOfRef { span, ty });
123+
}
124+
}
125+
Operand::Constant(_) | Operand::Static(_) => (),
126+
};
127+
for (_, block) in body.basic_blocks.iter() {
128+
for statement in &block.statements {
129+
match &statement.kind {
130+
StatementKind::Assign(_, r) => match r {
131+
Rvalue::ShallowInitBoxWithAlloc(_) => (),
132+
Rvalue::ShallowInitBox(o, _)
133+
| Rvalue::UnaryOp(_, o)
134+
| Rvalue::Cast(_, o, _)
135+
| Rvalue::Repeat(o, _)
136+
| Rvalue::Use(o) => for_operand(o, statement.span),
137+
Rvalue::CopyForDeref(_)
138+
| Rvalue::Discriminant(_)
139+
| Rvalue::Len(_)
140+
| Rvalue::Ref(_, _) => (),
141+
Rvalue::CheckedBinaryOp(_, o1, o2) => {
142+
for_operand(o1, statement.span);
143+
for_operand(o2, statement.span);
144+
}
145+
Rvalue::Aggregate(_, ops) => {
146+
for op in ops {
147+
for_operand(op, statement.span);
148+
}
149+
}
150+
},
151+
StatementKind::Deinit(_)
152+
| StatementKind::StorageLive(_)
153+
| StatementKind::StorageDead(_)
154+
| StatementKind::Nop => (),
155+
}
156+
}
157+
match &block.terminator {
158+
Some(terminator) => match &terminator.kind {
159+
TerminatorKind::SwitchInt { discr, .. } => for_operand(discr, terminator.span),
160+
TerminatorKind::FalseEdge { .. }
161+
| TerminatorKind::FalseUnwind { .. }
162+
| TerminatorKind::Goto { .. }
163+
| TerminatorKind::Resume
164+
| TerminatorKind::GeneratorDrop
165+
| TerminatorKind::Abort
166+
| TerminatorKind::Return
167+
| TerminatorKind::Unreachable
168+
| TerminatorKind::Drop { .. } => (),
169+
TerminatorKind::DropAndReplace { value, .. } => {
170+
for_operand(value, terminator.span);
171+
}
172+
TerminatorKind::Call { func, args, .. } => {
173+
for_operand(func, terminator.span);
174+
args.iter().for_each(|x| for_operand(x, terminator.span));
175+
}
176+
TerminatorKind::Assert { cond, .. } => {
177+
for_operand(cond, terminator.span);
178+
}
179+
TerminatorKind::Yield { value, .. } => {
180+
for_operand(value, terminator.span);
181+
}
182+
},
183+
None => (),
184+
}
185+
}
186+
result
187+
}
188+
77189
fn is_place_direct(lvalue: &Place) -> bool {
78190
!lvalue.projection.iter().any(|x| *x == ProjectionElem::Deref)
79191
}

crates/hir/src/diagnostics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ diagnostics![
4646
MissingFields,
4747
MissingMatchArms,
4848
MissingUnsafe,
49+
MovedOutOfRef,
4950
NeedMut,
5051
NoSuchField,
5152
PrivateAssocItem,
@@ -252,3 +253,9 @@ pub struct NeedMut {
252253
pub struct UnusedMut {
253254
pub local: Local,
254255
}
256+
257+
#[derive(Debug)]
258+
pub struct MovedOutOfRef {
259+
pub ty: Type,
260+
pub span: InFile<SyntaxNodePtr>,
261+
}

crates/hir/src/lib.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ pub use crate::{
9090
AnyDiagnostic, BreakOutsideOfLoop, ExpectedFunction, InactiveCode, IncoherentImpl,
9191
IncorrectCase, InvalidDeriveTarget, MacroDefError, MacroError, MacroExpansionParseError,
9292
MalformedDerive, MismatchedArgCount, MissingFields, MissingMatchArms, MissingUnsafe,
93-
NeedMut, NoSuchField, PrivateAssocItem, PrivateField, ReplaceFilterMapNextWithFindMap,
94-
TypeMismatch, UndeclaredLabel, UnimplementedBuiltinMacro, UnreachableLabel,
95-
UnresolvedExternCrate, UnresolvedField, UnresolvedImport, UnresolvedMacroCall,
96-
UnresolvedMethodCall, UnresolvedModule, UnresolvedProcMacro, UnusedMut,
93+
MovedOutOfRef, NeedMut, NoSuchField, PrivateAssocItem, PrivateField,
94+
ReplaceFilterMapNextWithFindMap, TypeMismatch, UndeclaredLabel, UnimplementedBuiltinMacro,
95+
UnreachableLabel, UnresolvedExternCrate, UnresolvedField, UnresolvedImport,
96+
UnresolvedMacroCall, UnresolvedMethodCall, UnresolvedModule, UnresolvedProcMacro,
97+
UnusedMut,
9798
},
9899
has_source::HasSource,
99100
semantics::{PathResolution, Semantics, SemanticsScope, TypeInfo, VisibleTraits},
@@ -1575,6 +1576,26 @@ impl DefWithBody {
15751576
if let Ok(borrowck_results) = db.borrowck(self.into()) {
15761577
for borrowck_result in borrowck_results.iter() {
15771578
let mir_body = &borrowck_result.mir_body;
1579+
for moof in &borrowck_result.moved_out_of_ref {
1580+
let span: InFile<SyntaxNodePtr> = match moof.span {
1581+
mir::MirSpan::ExprId(e) => match source_map.expr_syntax(e) {
1582+
Ok(s) => s.map(|x| x.into()),
1583+
Err(_) => continue,
1584+
},
1585+
mir::MirSpan::PatId(p) => match source_map.pat_syntax(p) {
1586+
Ok(s) => s.map(|x| match x {
1587+
Either::Left(e) => e.into(),
1588+
Either::Right(e) => e.into(),
1589+
}),
1590+
Err(_) => continue,
1591+
},
1592+
mir::MirSpan::Unknown => continue,
1593+
};
1594+
acc.push(
1595+
MovedOutOfRef { ty: Type::new_for_crate(krate, moof.ty.clone()), span }
1596+
.into(),
1597+
)
1598+
}
15781599
let mol = &borrowck_result.mutability_of_locals;
15791600
for (binding_id, _) in hir_body.bindings.iter() {
15801601
let Some(&local) = mir_body.binding_locals.get(binding_id) else {

crates/ide-diagnostics/src/handlers/missing_match_arms.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@ fn main() {
10271027

10281028
check_diagnostics(
10291029
r#"
1030+
//- minicore: copy
10301031
fn main() {
10311032
match &false {
10321033
&true => {}
@@ -1041,6 +1042,7 @@ fn main() {
10411042
cov_mark::check_count!(validate_match_bailed_out, 1);
10421043
check_diagnostics(
10431044
r#"
1045+
//- minicore: copy
10441046
fn main() {
10451047
match (&false,) {
10461048
//^^^^^^^^^ error: missing match arm: `(&false,)` not covered

crates/ide-diagnostics/src/handlers/missing_unsafe.rs

+5
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ fn main() {
142142
fn missing_unsafe_diagnostic_with_static_mut() {
143143
check_diagnostics(
144144
r#"
145+
//- minicore: copy
146+
145147
struct Ty {
146148
a: u8,
147149
}
@@ -256,6 +258,7 @@ fn main() {
256258
fn add_unsafe_block_when_accessing_mutable_static() {
257259
check_fix(
258260
r#"
261+
//- minicore: copy
259262
struct Ty {
260263
a: u8,
261264
}
@@ -374,6 +377,7 @@ fn main() {
374377
fn unsafe_expr_as_right_hand_side_of_assignment() {
375378
check_fix(
376379
r#"
380+
//- minicore: copy
377381
static mut STATIC_MUT: u8 = 0;
378382
379383
fn main() {
@@ -396,6 +400,7 @@ fn main() {
396400
fn unsafe_expr_in_binary_plus() {
397401
check_fix(
398402
r#"
403+
//- minicore: copy
399404
static mut STATIC_MUT: u8 = 0;
400405
401406
fn main() {

0 commit comments

Comments
 (0)