Skip to content

Commit bd1a1e4

Browse files
committed
Don't mark for loop head span with desugaring
1 parent 313e71a commit bd1a1e4

File tree

5 files changed

+48
-53
lines changed

5 files changed

+48
-53
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,15 +1331,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
13311331
body: &Block,
13321332
opt_label: Option<Label>,
13331333
) -> hir::Expr<'hir> {
1334-
let orig_head_span = head.span;
13351334
// expand <head>
1336-
let mut head = self.lower_expr_mut(head);
1337-
let desugared_span = self.mark_span_with_reason(
1338-
DesugaringKind::ForLoop(ForLoopLoc::Head),
1339-
orig_head_span,
1340-
None,
1341-
);
1342-
head.span = self.lower_span(desugared_span);
1335+
let head = self.lower_expr_mut(head);
1336+
let desugared_span =
1337+
self.mark_span_with_reason(DesugaringKind::ForLoop(ForLoopLoc::Head), head.span, None);
13431338

13441339
let iter = Ident::with_dummy_span(sym::iter);
13451340

@@ -1428,7 +1423,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14281423
loop_block,
14291424
self.lower_label(opt_label),
14301425
hir::LoopSource::ForLoop,
1431-
self.lower_span(e.span.with_hi(orig_head_span.hi())),
1426+
self.lower_span(e.span.with_hi(head.span.hi())),
14321427
);
14331428
let loop_expr = self.arena.alloc(hir::Expr {
14341429
hir_id: self.lower_node_id(e.id),
@@ -1441,7 +1436,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14411436

14421437
let into_iter_span = self.mark_span_with_reason(
14431438
DesugaringKind::ForLoop(ForLoopLoc::IntoIter),
1444-
orig_head_span,
1439+
head.span,
14451440
None,
14461441
);
14471442

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_middle::mir::{
1111
};
1212
use rustc_middle::ty::{self, suggest_constraining_type_param, Ty};
1313
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
14-
use rustc_span::source_map::DesugaringKind;
1514
use rustc_span::symbol::sym;
1615
use rustc_span::{BytePos, MultiSpan, Span, DUMMY_SP};
1716
use rustc_trait_selection::infer::InferCtxtExt;
@@ -247,6 +246,36 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
247246
place_name, partially_str, loop_message
248247
),
249248
);
249+
let sess = self.infcx.tcx.sess;
250+
let ty = used_place.ty(self.body, self.infcx.tcx).ty;
251+
// If we have a `&mut` ref, we need to reborrow.
252+
if let ty::Ref(_, _, hir::Mutability::Mut) = ty.kind() {
253+
// If we are in a loop this will be suggested later.
254+
if !is_loop_move {
255+
err.span_suggestion_verbose(
256+
move_span.shrink_to_lo(),
257+
&format!(
258+
"consider creating a fresh reborrow of {} here",
259+
self.describe_place(moved_place.as_ref())
260+
.map(|n| format!("`{}`", n))
261+
.unwrap_or_else(
262+
|| "the mutable reference".to_string()
263+
),
264+
),
265+
"&mut *".to_string(),
266+
Applicability::MachineApplicable,
267+
);
268+
}
269+
} else if let Ok(snippet) =
270+
sess.source_map().span_to_snippet(move_span)
271+
{
272+
err.span_suggestion(
273+
move_span,
274+
"consider borrowing to avoid moving into the for loop",
275+
format!("&{}", snippet),
276+
Applicability::MaybeIncorrect,
277+
);
278+
}
250279
} else {
251280
err.span_label(
252281
fn_call_span,
@@ -315,35 +344,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
315344
in_pattern = true;
316345
}
317346
}
318-
319-
if let Some(DesugaringKind::ForLoop(_)) = move_span.desugaring_kind() {
320-
let sess = self.infcx.tcx.sess;
321-
let ty = used_place.ty(self.body, self.infcx.tcx).ty;
322-
// If we have a `&mut` ref, we need to reborrow.
323-
if let ty::Ref(_, _, hir::Mutability::Mut) = ty.kind() {
324-
// If we are in a loop this will be suggested later.
325-
if !is_loop_move {
326-
err.span_suggestion_verbose(
327-
move_span.shrink_to_lo(),
328-
&format!(
329-
"consider creating a fresh reborrow of {} here",
330-
self.describe_place(moved_place.as_ref())
331-
.map(|n| format!("`{}`", n))
332-
.unwrap_or_else(|| "the mutable reference".to_string()),
333-
),
334-
"&mut *".to_string(),
335-
Applicability::MachineApplicable,
336-
);
337-
}
338-
} else if let Ok(snippet) = sess.source_map().span_to_snippet(move_span) {
339-
err.span_suggestion(
340-
move_span,
341-
"consider borrowing to avoid moving into the for loop",
342-
format!("&{}", snippet),
343-
Applicability::MaybeIncorrect,
344-
);
345-
}
346-
}
347347
}
348348

349349
use_spans.var_span_label_path_only(

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ use rustc_middle::ty;
55
use rustc_mir_dataflow::move_paths::{
66
IllegalMoveOrigin, IllegalMoveOriginKind, LookupResult, MoveError, MovePathIndex,
77
};
8-
use rustc_span::source_map::DesugaringKind;
98
use rustc_span::{sym, Span, DUMMY_SP};
109
use rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions;
1110

12-
use crate::diagnostics::UseSpans;
11+
use crate::diagnostics::{FnSelfUseKind, UseSpans};
1312
use crate::prefixes::PrefixSet;
1413
use crate::MirBorrowckCtxt;
1514

@@ -400,19 +399,21 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
400399
| ty::Opaque(def_id, _) => def_id,
401400
_ => return err,
402401
};
403-
let is_option = self.infcx.tcx.is_diagnostic_item(sym::Option, def_id);
404-
let is_result = self.infcx.tcx.is_diagnostic_item(sym::Result, def_id);
405-
if (is_option || is_result) && use_spans.map_or(true, |v| !v.for_closure()) {
402+
let diag_name = self.infcx.tcx.get_diagnostic_name(def_id);
403+
if matches!(diag_name, Some(sym::Option | sym::Result))
404+
&& use_spans.map_or(true, |v| !v.for_closure())
405+
{
406406
err.span_suggestion_verbose(
407407
span.shrink_to_hi(),
408-
&format!(
409-
"consider borrowing the `{}`'s content",
410-
if is_option { "Option" } else { "Result" }
411-
),
408+
&format!("consider borrowing the `{}`'s content", diag_name.unwrap()),
412409
".as_ref()".to_string(),
413410
Applicability::MaybeIncorrect,
414411
);
415-
} else if matches!(span.desugaring_kind(), Some(DesugaringKind::ForLoop(_))) {
412+
} else if let Some(UseSpans::FnSelfUse {
413+
kind: FnSelfUseKind::Normal { implicit_into_iter: true, .. },
414+
..
415+
}) = use_spans
416+
{
416417
let suggest = match self.infcx.tcx.get_diagnostic_item(sym::IntoIterator) {
417418
Some(def_id) => self.infcx.tcx.infer_ctxt().enter(|infcx| {
418419
type_known_to_meet_bound_modulo_regions(

compiler/rustc_lint/src/array_into_iter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
134134
Applicability::MachineApplicable,
135135
);
136136
if self.for_expr_span == expr.span {
137-
let expr_span = expr.span.ctxt().outer_expn_data().call_site;
138137
diag.span_suggestion(
139-
receiver_arg.span.shrink_to_hi().to(expr_span.shrink_to_hi()),
138+
receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
140139
"or remove `.into_iter()` to iterate by value",
141140
String::new(),
142141
Applicability::MaybeIncorrect,

src/test/incremental/hashes/for_loops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn change_iteration_variable_pattern() {
8383
#[cfg(not(any(cfail1,cfail4)))]
8484
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, optimized_mir, typeck")]
8585
#[rustc_clean(cfg="cfail3")]
86-
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, optimized_mir, typeck, promoted_mir")]
86+
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, optimized_mir, typeck")]
8787
#[rustc_clean(cfg="cfail6")]
8888
pub fn change_iteration_variable_pattern() {
8989
let mut _x = 0;

0 commit comments

Comments
 (0)