Auto merge of #89110 - Aaron1011:adjustment-span, r=estebank
Use larger span for adjustment THIR expressions Currently, we use a relatively 'small' span for THIR expressions generated by an 'adjustment' (e.g. an autoderef, autoborrow, unsizing). As a result, if a borrow generated by an adustment ends up causing a borrowcheck error, for example: ```rust let mut my_var = String::new(); let my_ref = &my_var my_var.push('a'); my_ref; ``` then the span for the mutable borrow may end up referring to only the base expression (e.g. `my_var`), rather than the method call which triggered the mutable borrow (e.g. `my_var.push('a')`) Due to a quirk of the MIR borrowck implementation, this doesn't always get exposed in migration mode, but it does in many cases. This commit makes THIR building consistently use 'larger' spans for adjustment expressions. These spans are recoded when we first create the adjustment during typecheck. For example, an autoref adjustment triggered by a method call will record the span of the entire method call. The intent of this change it make it clearer to users when it's the specific way in which a variable is used (for example, in a method call) that produdes a borrowcheck error. For example, an error message claiming that a 'mutable borrow occurs here' might be confusing if it just points at a usage of a variable (e.g. `my_var`), when no `&mut` is in sight. Pointing at the entire expression should help to emphasize that the method call itself is responsible for the mutable borrow. In several cases, this makes the `#![feature(nll)]` diagnostic output match up exactly with the default (migration mode) output. As a result, several `.nll.stderr` files end up getting removed entirely.
This commit is contained in:
commit
4aa7879b55
170 changed files with 453 additions and 583 deletions
|
@ -39,10 +39,17 @@ impl<'tcx> Cx<'tcx> {
|
|||
|
||||
let mut expr = self.make_mirror_unadjusted(hir_expr);
|
||||
|
||||
let adjustment_span = match self.adjustment_span {
|
||||
Some((hir_id, span)) if hir_id == hir_expr.hir_id => Some(span),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// Now apply adjustments, if any.
|
||||
for adjustment in self.typeck_results.expr_adjustments(hir_expr) {
|
||||
debug!("make_mirror: expr={:?} applying adjustment={:?}", expr, adjustment);
|
||||
expr = self.apply_adjustment(hir_expr, expr, adjustment);
|
||||
let span = expr.span;
|
||||
expr =
|
||||
self.apply_adjustment(hir_expr, expr, adjustment, adjustment_span.unwrap_or(span));
|
||||
}
|
||||
|
||||
// Next, wrap this up in the expr's scope.
|
||||
|
@ -82,8 +89,9 @@ impl<'tcx> Cx<'tcx> {
|
|||
hir_expr: &'tcx hir::Expr<'tcx>,
|
||||
mut expr: Expr<'tcx>,
|
||||
adjustment: &Adjustment<'tcx>,
|
||||
mut span: Span,
|
||||
) -> Expr<'tcx> {
|
||||
let Expr { temp_lifetime, mut span, .. } = expr;
|
||||
let Expr { temp_lifetime, .. } = expr;
|
||||
|
||||
// Adjust the span from the block, to the last expression of the
|
||||
// block. This is a better span when returning a mutable reference
|
||||
|
@ -150,6 +158,7 @@ impl<'tcx> Cx<'tcx> {
|
|||
|
||||
fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
|
||||
let expr_ty = self.typeck_results().expr_ty(expr);
|
||||
let expr_span = expr.span;
|
||||
let temp_lifetime = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
|
||||
|
||||
let kind = match expr.kind {
|
||||
|
@ -157,7 +166,13 @@ impl<'tcx> Cx<'tcx> {
|
|||
hir::ExprKind::MethodCall(_, method_span, ref args, fn_span) => {
|
||||
// Rewrite a.b(c) into UFCS form like Trait::b(a, c)
|
||||
let expr = self.method_callee(expr, method_span, None);
|
||||
// When we apply adjustments to the receiver, use the span of
|
||||
// the overall method call for better diagnostics. args[0]
|
||||
// is guaranteed to exist, since a method call always has a receiver.
|
||||
let old_adjustment_span = self.adjustment_span.replace((args[0].hir_id, expr_span));
|
||||
tracing::info!("Using method span: {:?}", expr.span);
|
||||
let args = self.mirror_exprs(args);
|
||||
self.adjustment_span = old_adjustment_span;
|
||||
ExprKind::Call {
|
||||
ty: expr.ty,
|
||||
fun: self.thir.exprs.push(expr),
|
||||
|
|
|
@ -9,6 +9,7 @@ use rustc_ast as ast;
|
|||
use rustc_data_structures::steal::Steal;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::{DefId, LocalDefId};
|
||||
use rustc_hir::HirId;
|
||||
use rustc_hir::Node;
|
||||
use rustc_middle::middle::region;
|
||||
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
|
||||
|
@ -46,6 +47,14 @@ struct Cx<'tcx> {
|
|||
crate region_scope_tree: &'tcx region::ScopeTree,
|
||||
crate typeck_results: &'tcx ty::TypeckResults<'tcx>,
|
||||
|
||||
/// When applying adjustments to the expression
|
||||
/// with the given `HirId`, use the given `Span`,
|
||||
/// instead of the usual span. This is used to
|
||||
/// assign the span of an overall method call
|
||||
/// (e.g. `my_val.foo()`) to the adjustment expressions
|
||||
/// for the receiver.
|
||||
adjustment_span: Option<(HirId, Span)>,
|
||||
|
||||
/// The `DefId` of the owner of this body.
|
||||
body_owner: DefId,
|
||||
}
|
||||
|
@ -60,6 +69,7 @@ impl<'tcx> Cx<'tcx> {
|
|||
region_scope_tree: tcx.region_scope_tree(def.did),
|
||||
typeck_results,
|
||||
body_owner: def.did.to_def_id(),
|
||||
adjustment_span: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue