Skip to content

Commit

Permalink
Auto merge of #89110 - Aaron1011:adjustment-span, r=estebank
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Sep 30, 2021
2 parents 24a789b + 4d66986 commit 4aa7879
Show file tree
Hide file tree
Showing 170 changed files with 453 additions and 583 deletions.
19 changes: 17 additions & 2 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -150,14 +158,21 @@ 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 {
// Here comes the interesting stuff:
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),
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_mir_build/src/thir/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
}
Expand All @@ -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,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// MIR for `BAR::promoted[0]` after SimplifyCfg-elaborate-drops

promoted[0] in BAR: &[&i32; 1] = {
let mut _0: &[&i32; 1]; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
let mut _0: &[&i32; 1]; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
let mut _1: [&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
let mut _2: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
let mut _3: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
Expand All @@ -16,8 +16,8 @@ promoted[0] in BAR: &[&i32; 1] = {
// + literal: Const { ty: &i32, val: Value(Scalar(alloc1)) }
_2 = &(*_3); // scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
_1 = [move _2]; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
_0 = &_1; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
return; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
_0 = &_1; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
return; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

static mut BAR: *const &i32 = {
let mut _0: *const &i32; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:9:17: 9:28
let mut _1: &[&i32]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
let mut _2: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
let mut _1: &[&i32]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
let mut _2: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
let _3: [&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
let mut _4: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
let _5: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
+ let mut _6: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
+ let mut _6: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44

bb0: {
StorageLive(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
StorageLive(_2); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
StorageLive(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
StorageLive(_2); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
- StorageLive(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
- StorageLive(_4); // scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
- StorageLive(_5); // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
- _5 = const {alloc1: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
+ _6 = const BAR::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
+ _6 = const BAR::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
// ty::Const
- // + ty: &i32
- // + val: Value(Scalar(alloc1))
Expand All @@ -28,11 +28,11 @@
- // + literal: Const { ty: &i32, val: Value(Scalar(alloc1)) }
- _4 = &(*_5); // scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
- _3 = [move _4]; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
- _2 = &_3; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
+ // + span: $DIR/const-promotion-extern-static.rs:9:31: 9:35
- _2 = &_3; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
+ // + span: $DIR/const-promotion-extern-static.rs:9:31: 9:44
+ // + literal: Const { ty: &[&i32; 1], val: Unevaluated(Unevaluated { def: WithOptConstParam { did: DefId(0:6 ~ const_promotion_extern_static[55e6]::BAR), const_param_did: None }, substs_: Some([]), promoted: Some(promoted[0]) }) }
+ _2 = &(*_6); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
_1 = move _2 as &[&i32] (Pointer(Unsize)); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
+ _2 = &(*_6); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
_1 = move _2 as &[&i32] (Pointer(Unsize)); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
- StorageDead(_4); // scope 0 at $DIR/const-promotion-extern-static.rs:9:34: 9:35
StorageDead(_2); // scope 0 at $DIR/const-promotion-extern-static.rs:9:34: 9:35
_0 = core::slice::<impl [&i32]>::as_ptr(move _1) -> [return: bb1, unwind: bb2]; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// MIR for `FOO::promoted[0]` after SimplifyCfg-elaborate-drops

promoted[0] in FOO: &[&i32; 1] = {
let mut _0: &[&i32; 1]; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
let mut _0: &[&i32; 1]; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
let mut _1: [&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
let mut _2: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:32: 13:45
let mut _3: *const i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
Expand All @@ -16,8 +16,8 @@ promoted[0] in FOO: &[&i32; 1] = {
// + literal: Const { ty: *const i32, val: Value(Scalar(alloc3)) }
_2 = &(*_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:41: 13:43
_1 = [move _2]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
_0 = &_1; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
return; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
_0 = &_1; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
return; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

static mut FOO: *const &i32 = {
let mut _0: *const &i32; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:13:17: 13:28
let mut _1: &[&i32]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
let mut _2: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
let mut _1: &[&i32]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
let mut _2: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
let _3: [&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
let mut _4: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:32: 13:45
let _5: *const i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
+ let mut _6: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
+ let mut _6: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
scope 1 {
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
StorageLive(_2); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
StorageLive(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
StorageLive(_2); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
- StorageLive(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
- StorageLive(_4); // scope 0 at $DIR/const-promotion-extern-static.rs:13:32: 13:45
- StorageLive(_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
- _5 = const {alloc3: *const i32}; // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
+ _6 = const FOO::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
+ _6 = const FOO::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
// ty::Const
- // + ty: *const i32
- // + val: Value(Scalar(alloc3))
Expand All @@ -30,11 +30,11 @@
- // + literal: Const { ty: *const i32, val: Value(Scalar(alloc3)) }
- _4 = &(*_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:41: 13:43
- _3 = [move _4]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
- _2 = &_3; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
+ // + span: $DIR/const-promotion-extern-static.rs:13:31: 13:46
- _2 = &_3; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
+ // + span: $DIR/const-promotion-extern-static.rs:13:31: 13:55
+ // + literal: Const { ty: &[&i32; 1], val: Unevaluated(Unevaluated { def: WithOptConstParam { did: DefId(0:7 ~ const_promotion_extern_static[55e6]::FOO), const_param_did: None }, substs_: Some([]), promoted: Some(promoted[0]) }) }
+ _2 = &(*_6); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
_1 = move _2 as &[&i32] (Pointer(Unsize)); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
+ _2 = &(*_6); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
_1 = move _2 as &[&i32] (Pointer(Unsize)); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
- StorageDead(_4); // scope 0 at $DIR/const-promotion-extern-static.rs:13:45: 13:46
StorageDead(_2); // scope 0 at $DIR/const-promotion-extern-static.rs:13:45: 13:46
_0 = core::slice::<impl [&i32]>::as_ptr(move _1) -> [return: bb1, unwind: bb2]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
debug s => _1; // in scope 0 at $DIR/deduplicate_blocks.rs:2:36: 2:37
let mut _0: bool; // return place in scope 0 at $DIR/deduplicate_blocks.rs:2:48: 2:52
let mut _2: &[u8]; // in scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:23
let mut _3: &str; // in scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:12
let mut _3: &str; // in scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:23
let mut _4: usize; // in scope 0 at $DIR/deduplicate_blocks.rs:5:9: 5:31
let mut _5: bool; // in scope 0 at $DIR/deduplicate_blocks.rs:5:9: 5:31
let mut _6: usize; // in scope 0 at $DIR/deduplicate_blocks.rs:4:9: 4:37
Expand All @@ -19,8 +19,8 @@

bb0: {
StorageLive(_2); // scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:23
StorageLive(_3); // scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:12
_3 = _1; // scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:12
StorageLive(_3); // scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:23
_3 = _1; // scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:23
StorageLive(_8); // scope 2 at $DIR/deduplicate_blocks.rs:3:11: 3:23
_8 = _3; // scope 2 at $DIR/deduplicate_blocks.rs:3:11: 3:23
- _2 = transmute::<&str, &[u8]>(move _8) -> bb14; // scope 2 at $DIR/deduplicate_blocks.rs:3:11: 3:23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
debug upper => _3; // in scope 0 at $DIR/funky_arms.rs:11:69: 11:74
let mut _0: std::result::Result<(), std::fmt::Error>; // return place in scope 0 at $DIR/funky_arms.rs:11:85: 11:91
let _4: bool; // in scope 0 at $DIR/funky_arms.rs:15:9: 15:19
let mut _5: &std::fmt::Formatter; // in scope 0 at $DIR/funky_arms.rs:15:22: 15:25
let mut _5: &std::fmt::Formatter; // in scope 0 at $DIR/funky_arms.rs:15:22: 15:37
let mut _7: std::option::Option<usize>; // in scope 0 at $DIR/funky_arms.rs:24:30: 24:45
let mut _8: &std::fmt::Formatter; // in scope 0 at $DIR/funky_arms.rs:24:30: 24:33
let mut _8: &std::fmt::Formatter; // in scope 0 at $DIR/funky_arms.rs:24:30: 24:45
let mut _9: isize; // in scope 0 at $DIR/funky_arms.rs:24:12: 24:27
let mut _11: &mut std::fmt::Formatter; // in scope 0 at $DIR/funky_arms.rs:26:43: 26:46
let mut _12: &T; // in scope 0 at $DIR/funky_arms.rs:26:48: 26:51
Expand All @@ -36,8 +36,8 @@

bb0: {
StorageLive(_4); // scope 0 at $DIR/funky_arms.rs:15:9: 15:19
StorageLive(_5); // scope 0 at $DIR/funky_arms.rs:15:22: 15:25
_5 = &(*_1); // scope 0 at $DIR/funky_arms.rs:15:22: 15:25
StorageLive(_5); // scope 0 at $DIR/funky_arms.rs:15:22: 15:37
_5 = &(*_1); // scope 0 at $DIR/funky_arms.rs:15:22: 15:37
_4 = Formatter::sign_plus(move _5) -> bb1; // scope 0 at $DIR/funky_arms.rs:15:22: 15:37
// mir::Constant
// + span: $DIR/funky_arms.rs:15:26: 15:35
Expand All @@ -62,8 +62,8 @@

bb4: {
StorageLive(_7); // scope 2 at $DIR/funky_arms.rs:24:30: 24:45
StorageLive(_8); // scope 2 at $DIR/funky_arms.rs:24:30: 24:33
_8 = &(*_1); // scope 2 at $DIR/funky_arms.rs:24:30: 24:33
StorageLive(_8); // scope 2 at $DIR/funky_arms.rs:24:30: 24:45
_8 = &(*_1); // scope 2 at $DIR/funky_arms.rs:24:30: 24:45
_7 = Formatter::precision(move _8) -> bb5; // scope 2 at $DIR/funky_arms.rs:24:30: 24:45
// mir::Constant
// + span: $DIR/funky_arms.rs:24:34: 24:43
Expand Down
Loading

0 comments on commit 4aa7879

Please sign in to comment.