Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit da1d099

Browse files
committed
Auto merge of rust-lang#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 rust-lang#89110, which was debated somewhat in rust-lang#90286. cc `@Aaron1011` who originally made this change. r? diagnostics Fixes rust-lang#113547 Fixes rust-lang#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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 18 deletions
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

Lines changed: 0 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)