Skip to content

Commit 299d41e

Browse files
authored
Unrolled build for rust-lang#135147
Rollup merge of rust-lang#135147 - compiler-errors:borrowck-tweaks, r=chenyukang A few borrowck tweaks to improve 2024 edition migration lints See first two commits' changes to test outputs. Test coverage in this area is kinda weak, but I think it affects more cases than this (like the craters that will begin to trigger the `tail_expr_drop_order` tests in rust-lang#134523). Third commit is a drive-by change that removes a deref hack from `UseSpans` which doesn't really improve diagnostics much.
2 parents fd98df8 + 3399029 commit 299d41e

File tree

8 files changed

+69
-37
lines changed

8 files changed

+69
-37
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+34
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_middle::mir::{
1717
};
1818
use rustc_middle::ty::adjustment::PointerCoercion;
1919
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
20+
use rustc_middle::util::CallKind;
2021
use rustc_span::{DesugaringKind, Span, Symbol, kw, sym};
2122
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
2223
use tracing::{debug, instrument};
@@ -635,6 +636,39 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
635636
// Used in a closure.
636637
(LaterUseKind::ClosureCapture, capture_kind_span, Some(path_span))
637638
}
639+
// In the case that the borrowed value (probably a temporary)
640+
// overlaps with the method's receiver, then point at the method.
641+
UseSpans::FnSelfUse {
642+
var_span: span,
643+
kind: CallKind::Normal { desugaring: None, .. },
644+
..
645+
} if span
646+
.overlaps(self.body.local_decls[borrow.assigned_place.local].source_info.span) =>
647+
{
648+
if let TerminatorKind::Call { func, call_source: CallSource::Normal, .. } =
649+
&self.body.basic_blocks[location.block].terminator().kind
650+
{
651+
// Just point to the function, to reduce the chance of overlapping spans.
652+
let function_span = match func {
653+
Operand::Constant(c) => c.span,
654+
Operand::Copy(place) | Operand::Move(place) => {
655+
if let Some(l) = place.as_local() {
656+
let local_decl = &self.body.local_decls[l];
657+
if self.local_names[l].is_none() {
658+
local_decl.source_info.span
659+
} else {
660+
span
661+
}
662+
} else {
663+
span
664+
}
665+
}
666+
};
667+
(LaterUseKind::Call, function_span, None)
668+
} else {
669+
(LaterUseKind::Other, span, None)
670+
}
671+
}
638672
UseSpans::PatUse(span)
639673
| UseSpans::OtherUse(span)
640674
| UseSpans::FnSelfUse { var_span: span, .. } => {

compiler/rustc_borrowck/src/diagnostics/mod.rs

-9
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,6 @@ impl UseSpans<'_> {
661661
UseSpans::ClosureUse { args_span: span, .. }
662662
| UseSpans::PatUse(span)
663663
| UseSpans::OtherUse(span) => span,
664-
UseSpans::FnSelfUse { fn_call_span, kind: CallKind::DerefCoercion { .. }, .. } => {
665-
fn_call_span
666-
}
667664
UseSpans::FnSelfUse { var_span, .. } => var_span,
668665
}
669666
}
@@ -674,9 +671,6 @@ impl UseSpans<'_> {
674671
UseSpans::ClosureUse { path_span: span, .. }
675672
| UseSpans::PatUse(span)
676673
| UseSpans::OtherUse(span) => span,
677-
UseSpans::FnSelfUse { fn_call_span, kind: CallKind::DerefCoercion { .. }, .. } => {
678-
fn_call_span
679-
}
680674
UseSpans::FnSelfUse { var_span, .. } => var_span,
681675
}
682676
}
@@ -687,9 +681,6 @@ impl UseSpans<'_> {
687681
UseSpans::ClosureUse { capture_kind_span: span, .. }
688682
| UseSpans::PatUse(span)
689683
| UseSpans::OtherUse(span) => span,
690-
UseSpans::FnSelfUse { fn_call_span, kind: CallKind::DerefCoercion { .. }, .. } => {
691-
fn_call_span
692-
}
693684
UseSpans::FnSelfUse { var_span, .. } => var_span,
694685
}
695686
}

compiler/rustc_middle/src/util/find_self_call.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,29 @@ pub fn find_self_call<'tcx>(
1717
debug!("find_self_call(local={:?}): terminator={:?}", local, body[block].terminator);
1818
if let Some(Terminator { kind: TerminatorKind::Call { func, args, .. }, .. }) =
1919
&body[block].terminator
20+
&& let Operand::Constant(box ConstOperand { const_, .. }) = func
21+
&& let ty::FnDef(def_id, fn_args) = *const_.ty().kind()
22+
&& let Some(ty::AssocItem { fn_has_self_parameter: true, .. }) =
23+
tcx.opt_associated_item(def_id)
24+
&& let [Spanned { node: Operand::Move(self_place) | Operand::Copy(self_place), .. }, ..] =
25+
**args
2026
{
21-
debug!("find_self_call: func={:?}", func);
22-
if let Operand::Constant(box ConstOperand { const_, .. }) = func {
23-
if let ty::FnDef(def_id, fn_args) = *const_.ty().kind() {
24-
if let Some(ty::AssocItem { fn_has_self_parameter: true, .. }) =
25-
tcx.opt_associated_item(def_id)
26-
{
27-
debug!("find_self_call: args={:?}", fn_args);
28-
if let [
29-
Spanned {
30-
node: Operand::Move(self_place) | Operand::Copy(self_place), ..
31-
},
32-
..,
33-
] = **args
34-
{
35-
if self_place.as_local() == Some(local) {
36-
return Some((def_id, fn_args));
37-
}
38-
}
39-
}
27+
if self_place.as_local() == Some(local) {
28+
return Some((def_id, fn_args));
29+
}
30+
31+
// Handle the case where `self_place` gets reborrowed.
32+
// This happens when the receiver is `&T`.
33+
for stmt in &body[block].statements {
34+
if let StatementKind::Assign(box (place, rvalue)) = &stmt.kind
35+
&& let Some(reborrow_local) = place.as_local()
36+
&& self_place.as_local() == Some(reborrow_local)
37+
&& let Rvalue::Ref(_, _, deref_place) = rvalue
38+
&& let PlaceRef { local: deref_local, projection: [ProjectionElem::Deref] } =
39+
deref_place.as_ref()
40+
&& deref_local == local
41+
{
42+
return Some((def_id, fn_args));
4043
}
4144
}
4245
}

tests/ui/lifetimes/tail-expr-in-nested-expr.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ error[E0716]: temporary value dropped while borrowed
22
--> $DIR/tail-expr-in-nested-expr.rs:4:15
33
|
44
LL | let _ = { String::new().as_str() }.len();
5-
| ^^^^^^^^^^^^^---------
5+
| ^^^^^^^^^^^^^ - --- borrow later used by call
66
| | |
77
| | temporary value is freed at the end of this statement
88
| creates a temporary value which is freed while still in use
9-
| borrow later used here
109
|
1110
= note: consider using a `let` binding to create a longer lived value
1211

tests/ui/lint/lint-const-item-mutation.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ warning: taking a mutable reference to a `const` item
7575
--> $DIR/lint-const-item-mutation.rs:42:5
7676
|
7777
LL | (&mut MY_STRUCT).use_mut();
78-
| ^^^^^^^^^^^^^^^^
78+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
7979
|
8080
= note: each usage of a `const` item creates a new temporary
8181
= note: the mutable reference will refer to this temporary, not the original `const` item
82+
note: mutable reference created due to call to this method
83+
--> $DIR/lint-const-item-mutation.rs:9:5
84+
|
85+
LL | fn use_mut(&mut self) {}
86+
| ^^^^^^^^^^^^^^^^^^^^^
8287
note: `const` item defined here
8388
--> $DIR/lint-const-item-mutation.rs:27:1
8489
|

tests/ui/moves/move-deref-coercion.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0382]: borrow of partially moved value: `val`
44
LL | let _val = val.first;
55
| --------- value partially moved here
66
LL | val.inner;
7-
| ^^^^^^^^^ value borrowed here after partial move
7+
| ^^^ value borrowed here after partial move
88
|
99
= note: partial move occurs because `val.first` has type `NotCopy`, which does not implement the `Copy` trait
1010
= note: borrow occurs due to deref coercion to `NotCopy`
@@ -20,7 +20,7 @@ error[E0382]: borrow of partially moved value: `val`
2020
LL | let _val = val.first;
2121
| --------- value partially moved here
2222
LL | val.inner_method();
23-
| ^^^^^^^^^^^^^^^^^^ value borrowed here after partial move
23+
| ^^^ value borrowed here after partial move
2424
|
2525
= note: partial move occurs because `val.first` has type `NotCopy`, which does not implement the `Copy` trait
2626
= note: borrow occurs due to deref coercion to `NotCopy`

tests/ui/no-capture-arc.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0382]: borrow of moved value: `arc_v`
2-
--> $DIR/no-capture-arc.rs:14:16
2+
--> $DIR/no-capture-arc.rs:14:18
33
|
44
LL | let arc_v = Arc::new(v);
55
| ----- move occurs because `arc_v` has type `Arc<Vec<i32>>`, which does not implement the `Copy` trait
@@ -10,7 +10,7 @@ LL | assert_eq!((*arc_v)[3], 4);
1010
| ----- variable moved due to use in closure
1111
...
1212
LL | assert_eq!((*arc_v)[2], 3);
13-
| ^^^^^^^^ value borrowed here after move
13+
| ^^^^^ value borrowed here after move
1414
|
1515
= note: borrow occurs due to deref coercion to `Vec<i32>`
1616

tests/ui/no-reuse-move-arc.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0382]: borrow of moved value: `arc_v`
2-
--> $DIR/no-reuse-move-arc.rs:12:16
2+
--> $DIR/no-reuse-move-arc.rs:12:18
33
|
44
LL | let arc_v = Arc::new(v);
55
| ----- move occurs because `arc_v` has type `Arc<Vec<i32>>`, which does not implement the `Copy` trait
@@ -10,7 +10,7 @@ LL | assert_eq!((*arc_v)[3], 4);
1010
| ----- variable moved due to use in closure
1111
...
1212
LL | assert_eq!((*arc_v)[2], 3);
13-
| ^^^^^^^^ value borrowed here after move
13+
| ^^^^^ value borrowed here after move
1414
|
1515
= note: borrow occurs due to deref coercion to `Vec<i32>`
1616

0 commit comments

Comments
 (0)