Skip to content

Commit da1d099

Browse files
committed
Auto merge of #112945 - compiler-errors:tighten-span-of-adjustment-error, r=oli-obk
(re-)tighten sourceinfo span of adjustments in MIR Diagnostics rely on the spans of MIR statements being (approximately) correct in order to give suggestions relative to that span (i.e. `shrink_to_hi` and `shrink_to_lo`). I discovered that we're *intentionally* lowering THIR exprs with their parent expr's span if they come from adjustments that are due to a parent expression. While I understand why that may be desirable to demonstrate the relationship of an adjustment and the expression that requires it, it leads to 1. very verbose borrowck output 2. incorrect spans for suggestions Some diagnostics get around that by giving suggestions relative to other spans we've collected during MIR lowering, such as the span of the method's identifier (e.g. `name` in `.name()`), but this doesn't work too well when things come from desugaring. I assume it also has lead to numerous tweaks and complications to diagnostics code down the road, which this PR doesn't necessarily aim to fix but may open the gates to fixing later... The last three commits are simplifications due to the fact that we can assume that the move span actually points to what is being moved (and a test). This regressed in #89110, which was debated somewhat in #90286. cc `@Aaron1011` who originally made this change. r? diagnostics Fixes #113547 Fixes #111016
2 parents 136dab6 + a74db1a commit da1d099

File tree

172 files changed

+426
-452
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+426
-452
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11211121
err.eager_subdiagnostic(
11221122
&self.infcx.tcx.sess.parse_sess.span_diagnostic,
11231123
CaptureReasonSuggest::FreshReborrow {
1124-
span: fn_call_span.shrink_to_lo(),
1124+
span: move_span.shrink_to_hi(),
11251125
});
11261126
}
11271127
if let Some(clone_trait) = tcx.lang_items().clone_trait()
@@ -1135,10 +1135,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11351135
&& self.infcx.predicate_must_hold_modulo_regions(&o)
11361136
{
11371137
err.span_suggestion_verbose(
1138-
fn_call_span.shrink_to_lo(),
1138+
move_span.shrink_to_hi(),
11391139
"you can `clone` the value and consume it, but this might not be \
11401140
your desired behavior",
1141-
"clone().".to_string(),
1141+
".clone()".to_string(),
11421142
Applicability::MaybeIncorrect,
11431143
);
11441144
}

compiler/rustc_borrowck/src/session_diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ pub(crate) enum CaptureReasonSuggest<'tcx> {
398398
#[suggestion(
399399
borrowck_suggest_create_freash_reborrow,
400400
applicability = "maybe-incorrect",
401-
code = "as_mut().",
401+
code = ".as_mut()",
402402
style = "verbose"
403403
)]
404404
FreshReborrow {

compiler/rustc_mir_build/src/thir/cx/expr.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,14 @@ impl<'tcx> Cx<'tcx> {
4141

4242
let mut expr = self.make_mirror_unadjusted(hir_expr);
4343

44-
let adjustment_span = match self.adjustment_span {
45-
Some((hir_id, span)) if hir_id == hir_expr.hir_id => Some(span),
46-
_ => None,
47-
};
48-
4944
trace!(?expr.ty);
5045

5146
// Now apply adjustments, if any.
5247
if self.apply_adjustments {
5348
for adjustment in self.typeck_results.expr_adjustments(hir_expr) {
5449
trace!(?expr, ?adjustment);
5550
let span = expr.span;
56-
expr = self.apply_adjustment(
57-
hir_expr,
58-
expr,
59-
adjustment,
60-
adjustment_span.unwrap_or(span),
61-
);
51+
expr = self.apply_adjustment(hir_expr, expr, adjustment, span);
6252
}
6353
}
6454

@@ -274,7 +264,6 @@ impl<'tcx> Cx<'tcx> {
274264
fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
275265
let tcx = self.tcx;
276266
let expr_ty = self.typeck_results().expr_ty(expr);
277-
let expr_span = expr.span;
278267
let temp_lifetime =
279268
self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id);
280269

@@ -283,17 +272,11 @@ impl<'tcx> Cx<'tcx> {
283272
hir::ExprKind::MethodCall(segment, receiver, ref args, fn_span) => {
284273
// Rewrite a.b(c) into UFCS form like Trait::b(a, c)
285274
let expr = self.method_callee(expr, segment.ident.span, None);
286-
// When we apply adjustments to the receiver, use the span of
287-
// the overall method call for better diagnostics. args[0]
288-
// is guaranteed to exist, since a method call always has a receiver.
289-
let old_adjustment_span =
290-
self.adjustment_span.replace((receiver.hir_id, expr_span));
291275
info!("Using method span: {:?}", expr.span);
292276
let args = std::iter::once(receiver)
293277
.chain(args.iter())
294278
.map(|expr| self.mirror_expr(expr))
295279
.collect();
296-
self.adjustment_span = old_adjustment_span;
297280
ExprKind::Call {
298281
ty: expr.ty,
299282
fun: self.thir.exprs.push(expr),

compiler/rustc_mir_build/src/thir/cx/mod.rs

-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_hir::Node;
1616
use rustc_middle::middle::region;
1717
use rustc_middle::thir::*;
1818
use rustc_middle::ty::{self, RvalueScopes, Ty, TyCtxt};
19-
use rustc_span::Span;
2019

2120
pub(crate) fn thir_body(
2221
tcx: TyCtxt<'_>,
@@ -62,14 +61,6 @@ struct Cx<'tcx> {
6261
typeck_results: &'tcx ty::TypeckResults<'tcx>,
6362
rvalue_scopes: &'tcx RvalueScopes,
6463

65-
/// When applying adjustments to the expression
66-
/// with the given `HirId`, use the given `Span`,
67-
/// instead of the usual span. This is used to
68-
/// assign the span of an overall method call
69-
/// (e.g. `my_val.foo()`) to the adjustment expressions
70-
/// for the receiver.
71-
adjustment_span: Option<(HirId, Span)>,
72-
7364
/// False to indicate that adjustments should not be applied. Only used for `custom_mir`
7465
apply_adjustments: bool,
7566

@@ -110,7 +101,6 @@ impl<'tcx> Cx<'tcx> {
110101
typeck_results,
111102
rvalue_scopes: &typeck_results.rvalue_scopes,
112103
body_owner: def.to_def_id(),
113-
adjustment_span: None,
114104
apply_adjustments: hir
115105
.attrs(hir_id)
116106
.iter()

src/tools/miri/tests/fail/box-cell-alias.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: Undefined Behavior: trying to retag from <TAG> for SharedReadWrite permis
22
--> $DIR/box-cell-alias.rs:LL:CC
33
|
44
LL | unsafe { (*ptr).set(20) };
5-
| ^^^^^^^^^^^^^^
5+
| ^^^^^^
66
| |
77
| trying to retag from <TAG> for SharedReadWrite permission at ALLOC[0x0], but that tag does not exist in the borrow stack for this location
88
| this error occurs as part of retag at ALLOC[0x0..0x1]

src/tools/miri/tests/fail/stacked_borrows/illegal_read7.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: Undefined Behavior: trying to retag from <TAG> for SharedReadWrite permis
22
--> $DIR/illegal_read7.rs:LL:CC
33
|
44
LL | let _val = *x.get_mut();
5-
| ^^^^^^^^^^^
5+
| ^
66
| |
77
| trying to retag from <TAG> for SharedReadWrite permission at ALLOC[0x0], but that tag does not exist in the borrow stack for this location
88
| this error occurs as part of two-phase retag at ALLOC[0x0..0x4]

src/tools/miri/tests/fail/stacked_borrows/interior_mut1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: Undefined Behavior: trying to retag from <TAG> for SharedReadWrite permis
22
--> $DIR/interior_mut1.rs:LL:CC
33
|
44
LL | let _val = *inner_shr.get();
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^
66
| |
77
| trying to retag from <TAG> for SharedReadWrite permission at ALLOC[0x0], but that tag does not exist in the borrow stack for this location
88
| this error occurs as part of retag at ALLOC[0x0..0x4]

src/tools/miri/tests/fail/stacked_borrows/interior_mut2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: Undefined Behavior: trying to retag from <TAG> for SharedReadWrite permis
22
--> $DIR/interior_mut2.rs:LL:CC
33
|
44
LL | let _val = *inner_shr.get();
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^
66
| |
77
| trying to retag from <TAG> for SharedReadWrite permission at ALLOC[0x0], but that tag does not exist in the borrow stack for this location
88
| this error occurs as part of retag at ALLOC[0x0..0x4]

src/tools/miri/tests/fail/stacked_borrows/shared_rw_borrows_are_weak1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: Undefined Behavior: trying to retag from <TAG> for SharedReadWrite permis
22
--> $DIR/shared_rw_borrows_are_weak1.rs:LL:CC
33
|
44
LL | y.get_mut();
5-
| ^^^^^^^^^^^
5+
| ^
66
| |
77
| trying to retag from <TAG> for SharedReadWrite permission at ALLOC[0x0], but that tag does not exist in the borrow stack for this location
88
| this error occurs as part of two-phase retag at ALLOC[0x0..0x4]

src/tools/miri/tests/fail/tree_borrows/fnentry_invalidation.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ help: the accessed tag <TAG> later transitioned to Frozen due to a reborrow (act
2121
--> $DIR/fnentry_invalidation.rs:LL:CC
2222
|
2323
LL | x.do_bad();
24-
| ^^^^^^^^^^
24+
| ^
2525
= help: this transition corresponds to a loss of write permissions
2626
= note: BACKTRACE (of the first span):
2727
= note: inside `main` at $DIR/fnentry_invalidation.rs:LL:CC

src/tools/miri/tests/fail/tree_borrows/write-during-2phase.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,8 @@ LL | fn add(&mut self, n: u64) -> u64 {
99
help: the accessed tag <TAG> was created here, in the initial state Reserved
1010
--> $DIR/write-during-2phase.rs:LL:CC
1111
|
12-
LL | let _res = f.add(unsafe {
13-
| ________________^
14-
LL | | let n = f.0;
15-
LL | | // This is the access at fault, but it's not immediately apparent because
16-
LL | | // the reference that got invalidated is not under a Protector.
17-
LL | | *inner = 42;
18-
LL | | n
19-
LL | | });
20-
| |______^
12+
LL | let _res = f.add(unsafe {
13+
| ^
2114
help: the accessed tag <TAG> later transitioned to Disabled due to a foreign write access at offsets [0x0..0x8]
2215
--> $DIR/write-during-2phase.rs:LL:CC
2316
|

tests/ui/array-slice-vec/vec-mut-iter-borrow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | for x in &mut xs {
77
| first mutable borrow occurs here
88
| first borrow later used here
99
LL | xs.push(1)
10-
| ^^^^^^^^^^ second mutable borrow occurs here
10+
| ^^ second mutable borrow occurs here
1111

1212
error: aborting due to previous error
1313

tests/ui/async-await/clone-suggestion.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ note: `into_future` takes ownership of the receiver `self`, which moves `f`
1313
help: you can `clone` the value and consume it, but this might not be your desired behavior
1414
|
1515
LL | f.clone().await;
16-
| ++++++++
16+
| ++++++++
1717

1818
error: aborting due to previous error
1919

tests/ui/async-await/issue-61452.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
22
--> $DIR/issue-61452.rs:4:5
33
|
44
LL | x.take();
5-
| ^^^^^^^^ cannot borrow as mutable
5+
| ^ cannot borrow as mutable
66
|
77
help: consider changing this to be mutable
88
|

tests/ui/async-await/issues/issue-61187.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow `data` as mutable, as it is not declared as mutable
22
--> $DIR/issue-61187.rs:6:5
33
|
44
LL | data.reverse();
5-
| ^^^^^^^^^^^^^^ cannot borrow as mutable
5+
| ^^^^ cannot borrow as mutable
66
|
77
help: consider changing this to be mutable
88
|

tests/ui/binop/binop-move-semantics.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ LL | x
2727
| - value moved here
2828
LL | +
2929
LL | x.clone();
30-
| ^^^^^^^^^ value borrowed here after move
30+
| ^ value borrowed here after move
3131
|
3232
help: consider cloning the value if the performance cost is acceptable
3333
|

tests/ui/borrowck/borrow-tuple-fields.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | let y = x;
99
| ^ move out of `x` occurs here
1010
LL |
1111
LL | r.use_ref();
12-
| ----------- borrow later used here
12+
| - borrow later used here
1313

1414
error[E0502]: cannot borrow `x.0` as mutable because it is also borrowed as immutable
1515
--> $DIR/borrow-tuple-fields.rs:18:13
@@ -19,7 +19,7 @@ LL | let a = &x.0;
1919
LL | let b = &mut x.0;
2020
| ^^^^^^^^ mutable borrow occurs here
2121
LL | a.use_ref();
22-
| ----------- immutable borrow later used here
22+
| - immutable borrow later used here
2323

2424
error[E0499]: cannot borrow `x.0` as mutable more than once at a time
2525
--> $DIR/borrow-tuple-fields.rs:23:13
@@ -29,7 +29,7 @@ LL | let a = &mut x.0;
2929
LL | let b = &mut x.0;
3030
| ^^^^^^^^ second mutable borrow occurs here
3131
LL | a.use_ref();
32-
| ----------- first borrow later used here
32+
| - first borrow later used here
3333

3434
error[E0505]: cannot move out of `x` because it is borrowed
3535
--> $DIR/borrow-tuple-fields.rs:28:13
@@ -41,7 +41,7 @@ LL | let r = &x.0;
4141
LL | let y = x;
4242
| ^ move out of `x` occurs here
4343
LL | r.use_ref();
44-
| ----------- borrow later used here
44+
| - borrow later used here
4545

4646
error[E0502]: cannot borrow `x.0` as mutable because it is also borrowed as immutable
4747
--> $DIR/borrow-tuple-fields.rs:33:13
@@ -51,7 +51,7 @@ LL | let a = &x.0;
5151
LL | let b = &mut x.0;
5252
| ^^^^^^^^ mutable borrow occurs here
5353
LL | a.use_ref();
54-
| ----------- immutable borrow later used here
54+
| - immutable borrow later used here
5555

5656
error[E0499]: cannot borrow `x.0` as mutable more than once at a time
5757
--> $DIR/borrow-tuple-fields.rs:38:13
@@ -61,7 +61,7 @@ LL | let a = &mut x.0;
6161
LL | let b = &mut x.0;
6262
| ^^^^^^^^ second mutable borrow occurs here
6363
LL | a.use_mut();
64-
| ----------- first borrow later used here
64+
| - first borrow later used here
6565

6666
error: aborting due to 6 previous errors
6767

tests/ui/borrowck/borrowck-argument.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow `arg` as mutable, as it is not declared as mutable
22
--> $DIR/borrowck-argument.rs:10:5
33
|
44
LL | arg.mutate();
5-
| ^^^^^^^^^^^^ cannot borrow as mutable
5+
| ^^^ cannot borrow as mutable
66
|
77
help: consider changing this to be mutable
88
|
@@ -13,7 +13,7 @@ error[E0596]: cannot borrow `arg` as mutable, as it is not declared as mutable
1313
--> $DIR/borrowck-argument.rs:15:9
1414
|
1515
LL | arg.mutate();
16-
| ^^^^^^^^^^^^ cannot borrow as mutable
16+
| ^^^ cannot borrow as mutable
1717
|
1818
help: consider changing this to be mutable
1919
|
@@ -24,7 +24,7 @@ error[E0596]: cannot borrow `arg` as mutable, as it is not declared as mutable
2424
--> $DIR/borrowck-argument.rs:21:9
2525
|
2626
LL | arg.mutate();
27-
| ^^^^^^^^^^^^ cannot borrow as mutable
27+
| ^^^ cannot borrow as mutable
2828
|
2929
help: consider changing this to be mutable
3030
|
@@ -35,7 +35,7 @@ error[E0596]: cannot borrow `arg` as mutable, as it is not declared as mutable
3535
--> $DIR/borrowck-argument.rs:32:17
3636
|
3737
LL | (|arg: S| { arg.mutate() })(s);
38-
| ^^^^^^^^^^^^ cannot borrow as mutable
38+
| ^^^ cannot borrow as mutable
3939
|
4040
help: consider changing this to be mutable
4141
|

tests/ui/borrowck/borrowck-auto-mut-ref-to-immut-var.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
22
--> $DIR/borrowck-auto-mut-ref-to-immut-var.rs:15:5
33
|
44
LL | x.printme();
5-
| ^^^^^^^^^^^ cannot borrow as mutable
5+
| ^ cannot borrow as mutable
66
|
77
help: consider changing this to be mutable
88
|

tests/ui/borrowck/borrowck-borrow-immut-deref-of-box-as-mut.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0596]: cannot borrow `*a` as mutable, as `a` is not declared as mutable
22
--> $DIR/borrowck-borrow-immut-deref-of-box-as-mut.rs:12:5
33
|
44
LL | a.foo();
5-
| ^^^^^^^ cannot borrow as mutable
5+
| ^ cannot borrow as mutable
66
|
77
help: consider changing this to be mutable
88
|

tests/ui/borrowck/borrowck-borrow-mut-object-twice.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ error[E0499]: cannot borrow `*x` as mutable more than once at a time
22
--> $DIR/borrowck-borrow-mut-object-twice.rs:13:5
33
|
44
LL | let y = x.f1();
5-
| ------ first mutable borrow occurs here
5+
| - first mutable borrow occurs here
66
LL | x.f2();
7-
| ^^^^^^ second mutable borrow occurs here
7+
| ^ second mutable borrow occurs here
88
LL | y.use_ref();
9-
| ----------- first borrow later used here
9+
| - first borrow later used here
1010

1111
error: aborting due to previous error
1212

0 commit comments

Comments
 (0)