deal with non-Drop
types and add a test case for projections
This commit is contained in:
parent
20d1bb1867
commit
60508f546a
@ -239,12 +239,23 @@ fn is_ok_to_suggest<'tcx>(cx: &LateContext<'tcx>, lhs: &Expr<'tcx>, call: &CallC
|
|||||||
///
|
///
|
||||||
/// This cannot be written `s2.clone_into(&mut s)` because it has conflicting borrows.
|
/// This cannot be written `s2.clone_into(&mut s)` because it has conflicting borrows.
|
||||||
fn clone_source_borrows_from_dest(cx: &LateContext<'_>, lhs: &Expr<'_>, call_span: Span) -> bool {
|
fn clone_source_borrows_from_dest(cx: &LateContext<'_>, lhs: &Expr<'_>, call_span: Span) -> bool {
|
||||||
|
/// If this basic block only exists to drop a local as part of an assignment, returns its
|
||||||
|
/// successor. Otherwise returns the basic block that was passed in.
|
||||||
|
fn skip_drop_block(mir: &mir::Body<'_>, bb: mir::BasicBlock) -> mir::BasicBlock {
|
||||||
|
if let mir::TerminatorKind::Drop { target, .. } = mir.basic_blocks[bb].terminator().kind {
|
||||||
|
target
|
||||||
|
} else {
|
||||||
|
bb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let Some(mir) = enclosing_mir(cx.tcx, lhs.hir_id) else {
|
let Some(mir) = enclosing_mir(cx.tcx, lhs.hir_id) else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
let PossibleBorrowerMap { map: borrow_map, .. } = PossibleBorrowerMap::new(cx, mir);
|
let PossibleBorrowerMap { map: borrow_map, .. } = PossibleBorrowerMap::new(cx, mir);
|
||||||
|
|
||||||
// The operation `dest = src.to_owned()` in MIR is split up across 3 blocks.
|
// The operation `dest = src.to_owned()` in MIR is split up across 3 blocks *if* the type has `Drop`
|
||||||
|
// code. For types that don't, the second basic block is simply skipped.
|
||||||
// For the doc example above that would be roughly:
|
// For the doc example above that would be roughly:
|
||||||
//
|
//
|
||||||
// bb0:
|
// bb0:
|
||||||
@ -260,26 +271,28 @@ fn clone_source_borrows_from_dest(cx: &LateContext<'_>, lhs: &Expr<'_>, call_spa
|
|||||||
let terminator = bb.terminator();
|
let terminator = bb.terminator();
|
||||||
|
|
||||||
// Look for the to_owned/clone call.
|
// Look for the to_owned/clone call.
|
||||||
if terminator.source_info.span == call_span
|
if terminator.source_info.span != call_span {
|
||||||
&& let mir::TerminatorKind::Call {
|
continue;
|
||||||
args,
|
}
|
||||||
target: Some(drop_bb),
|
|
||||||
..
|
if let mir::TerminatorKind::Call { ref args, target: Some(assign_bb), .. } = terminator.kind
|
||||||
} = &terminator.kind
|
|
||||||
&& let [source] = &**args
|
&& let [source] = &**args
|
||||||
&& let mir::Operand::Move(source) = &source.node
|
&& let mir::Operand::Move(source) = &source.node
|
||||||
// Block 2 only has the `drop()` terminator from to the assignment
|
&& let assign_bb = skip_drop_block(mir, assign_bb)
|
||||||
&& let drop_bb = &mir.basic_blocks[*drop_bb]
|
// Skip any storage statements as they are just noise
|
||||||
&& let mir::TerminatorKind::Drop { target: assign_bb, .. } = drop_bb.terminator().kind
|
&& let Some(assignment) = mir.basic_blocks[assign_bb].statements
|
||||||
// Block 3 has the final assignment to the original local
|
.iter()
|
||||||
&& let assign_bb = &mir.basic_blocks[assign_bb]
|
.find(|stmt| {
|
||||||
&& let [assignment, ..] = &*assign_bb.statements
|
!matches!(stmt.kind, mir::StatementKind::StorageDead(_) | mir::StatementKind::StorageLive(_))
|
||||||
|
})
|
||||||
&& let mir::StatementKind::Assign(box (borrowed, _)) = &assignment.kind
|
&& let mir::StatementKind::Assign(box (borrowed, _)) = &assignment.kind
|
||||||
&& let Some(borrowers) = borrow_map.get(&borrowed.local)
|
&& let Some(borrowers) = borrow_map.get(&borrowed.local)
|
||||||
&& borrowers.contains(source.local)
|
&& borrowers.contains(source.local)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
@ -290,6 +290,33 @@ mod borrowck_conflicts {
|
|||||||
s = s2.to_owned();
|
s = s2.to_owned();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue12444_nodrop_projections() {
|
||||||
|
struct NoDrop;
|
||||||
|
|
||||||
|
impl Clone for NoDrop {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
fn clone_from(&mut self, other: &Self) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut s = NoDrop;
|
||||||
|
let s2 = &s;
|
||||||
|
s = s2.clone();
|
||||||
|
|
||||||
|
let mut s = (NoDrop, NoDrop);
|
||||||
|
let s2 = &s.0;
|
||||||
|
s.0 = s2.clone();
|
||||||
|
|
||||||
|
// This *could* emit a warning, but PossibleBorrowerMap only works with locals so it
|
||||||
|
// considers `s` fully borrowed
|
||||||
|
let mut s = (NoDrop, NoDrop);
|
||||||
|
let s2 = &s.1;
|
||||||
|
s.0 = s2.clone();
|
||||||
|
}
|
||||||
|
|
||||||
fn issue12460(mut name: String) {
|
fn issue12460(mut name: String) {
|
||||||
if let Some(stripped_name) = name.strip_prefix("baz-") {
|
if let Some(stripped_name) = name.strip_prefix("baz-") {
|
||||||
name = stripped_name.to_owned();
|
name = stripped_name.to_owned();
|
||||||
|
@ -290,6 +290,33 @@ mod borrowck_conflicts {
|
|||||||
s = s2.to_owned();
|
s = s2.to_owned();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue12444_nodrop_projections() {
|
||||||
|
struct NoDrop;
|
||||||
|
|
||||||
|
impl Clone for NoDrop {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
fn clone_from(&mut self, other: &Self) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut s = NoDrop;
|
||||||
|
let s2 = &s;
|
||||||
|
s = s2.clone();
|
||||||
|
|
||||||
|
let mut s = (NoDrop, NoDrop);
|
||||||
|
let s2 = &s.0;
|
||||||
|
s.0 = s2.clone();
|
||||||
|
|
||||||
|
// This *could* emit a warning, but PossibleBorrowerMap only works with locals so it
|
||||||
|
// considers `s` fully borrowed
|
||||||
|
let mut s = (NoDrop, NoDrop);
|
||||||
|
let s2 = &s.1;
|
||||||
|
s.0 = s2.clone();
|
||||||
|
}
|
||||||
|
|
||||||
fn issue12460(mut name: String) {
|
fn issue12460(mut name: String) {
|
||||||
if let Some(stripped_name) = name.strip_prefix("baz-") {
|
if let Some(stripped_name) = name.strip_prefix("baz-") {
|
||||||
name = stripped_name.to_owned();
|
name = stripped_name.to_owned();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user