Improve span when temporary receiver is dropped in edition 2024
This commit is contained in:
parent
b3b368a183
commit
3560a2b399
2 changed files with 35 additions and 2 deletions
|
@ -17,6 +17,7 @@ use rustc_middle::mir::{
|
||||||
};
|
};
|
||||||
use rustc_middle::ty::adjustment::PointerCoercion;
|
use rustc_middle::ty::adjustment::PointerCoercion;
|
||||||
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
|
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
|
||||||
|
use rustc_middle::util::CallKind;
|
||||||
use rustc_span::{DesugaringKind, Span, Symbol, kw, sym};
|
use rustc_span::{DesugaringKind, Span, Symbol, kw, sym};
|
||||||
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
|
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
|
||||||
use tracing::{debug, instrument};
|
use tracing::{debug, instrument};
|
||||||
|
@ -635,6 +636,39 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
|
||||||
// Used in a closure.
|
// Used in a closure.
|
||||||
(LaterUseKind::ClosureCapture, capture_kind_span, Some(path_span))
|
(LaterUseKind::ClosureCapture, capture_kind_span, Some(path_span))
|
||||||
}
|
}
|
||||||
|
// In the case that the borrowed value (probably a temporary)
|
||||||
|
// overlaps with the method's receiver, then point at the method.
|
||||||
|
UseSpans::FnSelfUse {
|
||||||
|
var_span: span,
|
||||||
|
kind: CallKind::Normal { desugaring: None, .. },
|
||||||
|
..
|
||||||
|
} if span
|
||||||
|
.overlaps(self.body.local_decls[borrow.assigned_place.local].source_info.span) =>
|
||||||
|
{
|
||||||
|
if let TerminatorKind::Call { func, call_source: CallSource::Normal, .. } =
|
||||||
|
&self.body.basic_blocks[location.block].terminator().kind
|
||||||
|
{
|
||||||
|
// Just point to the function, to reduce the chance of overlapping spans.
|
||||||
|
let function_span = match func {
|
||||||
|
Operand::Constant(c) => c.span,
|
||||||
|
Operand::Copy(place) | Operand::Move(place) => {
|
||||||
|
if let Some(l) = place.as_local() {
|
||||||
|
let local_decl = &self.body.local_decls[l];
|
||||||
|
if self.local_names[l].is_none() {
|
||||||
|
local_decl.source_info.span
|
||||||
|
} else {
|
||||||
|
span
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
span
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
(LaterUseKind::Call, function_span, None)
|
||||||
|
} else {
|
||||||
|
(LaterUseKind::Other, span, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
UseSpans::PatUse(span)
|
UseSpans::PatUse(span)
|
||||||
| UseSpans::OtherUse(span)
|
| UseSpans::OtherUse(span)
|
||||||
| UseSpans::FnSelfUse { var_span: span, .. } => {
|
| UseSpans::FnSelfUse { var_span: span, .. } => {
|
||||||
|
|
|
@ -2,11 +2,10 @@ error[E0716]: temporary value dropped while borrowed
|
||||||
--> $DIR/tail-expr-in-nested-expr.rs:4:15
|
--> $DIR/tail-expr-in-nested-expr.rs:4:15
|
||||||
|
|
|
|
||||||
LL | let _ = { String::new().as_str() }.len();
|
LL | let _ = { String::new().as_str() }.len();
|
||||||
| ^^^^^^^^^^^^^---------
|
| ^^^^^^^^^^^^^ - --- borrow later used by call
|
||||||
| | |
|
| | |
|
||||||
| | temporary value is freed at the end of this statement
|
| | temporary value is freed at the end of this statement
|
||||||
| creates a temporary value which is freed while still in use
|
| creates a temporary value which is freed while still in use
|
||||||
| borrow later used here
|
|
||||||
|
|
|
|
||||||
= note: consider using a `let` binding to create a longer lived value
|
= note: consider using a `let` binding to create a longer lived value
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue