Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 62 additions & 11 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4022,23 +4022,74 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
if let Some(decl) = local_decl
&& decl.can_be_made_mutable()
{
let is_for_loop = matches!(
decl.local_info(),
LocalInfo::User(BindingForm::Var(VarBindingForm {
opt_match_place: Some((_, match_span)),
..
})) if matches!(match_span.desugaring_kind(), Some(DesugaringKind::ForLoop))
);
let message = if is_for_loop
let mut is_for_loop = false;
let mut is_ref_pattern = false;
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
opt_match_place: Some((_, match_span)),
..
})) = *decl.local_info()
{
if matches!(match_span.desugaring_kind(), Some(DesugaringKind::ForLoop)) {
is_for_loop = true;

if let Some(body) = self.infcx.tcx.hir_maybe_body_owned_by(self.mir_def_id()) {
struct RefPatternFinder<'tcx> {
tcx: TyCtxt<'tcx>,
binding_span: Span,
is_ref_pattern: bool,
}

impl<'tcx> Visitor<'tcx> for RefPatternFinder<'tcx> {
type NestedFilter = OnlyBodies;

fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.tcx
}

fn visit_pat(&mut self, pat: &'tcx hir::Pat<'tcx>) {
if !self.is_ref_pattern
&& let hir::PatKind::Binding(_, _, ident, _) = pat.kind
&& ident.span == self.binding_span
{
self.is_ref_pattern =
self.tcx.hir_parent_iter(pat.hir_id).any(|(_, node)| {
matches!(
node,
hir::Node::Pat(hir::Pat {
kind: hir::PatKind::Ref(..),
..
})
)
});
}
hir::intravisit::walk_pat(self, pat);
}
}

let mut finder = RefPatternFinder {
tcx: self.infcx.tcx,
binding_span: decl.source_info.span,
is_ref_pattern: false,
};

finder.visit_body(body);
is_ref_pattern = finder.is_ref_pattern;
}
}
}

let (span, message) = if is_for_loop
&& is_ref_pattern
&& let Ok(binding_name) =
self.infcx.tcx.sess.source_map().span_to_snippet(decl.source_info.span)
{
format!("(mut {}) ", binding_name)
(decl.source_info.span, format!("(mut {})", binding_name))
} else {
"mut ".to_string()
(decl.source_info.span.shrink_to_lo(), "mut ".to_string())
};

err.span_suggestion_verbose(
decl.source_info.span.shrink_to_lo(),
span,
"consider making this binding mutable",
message,
Applicability::MachineApplicable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ LL | num *= 2;
|
help: consider making this binding mutable
|
LL | for &(mut num) num in nums {
| +++++++++
LL - for &num in nums {
LL + for &(mut num) in nums {
|

error: aborting due to 1 previous error

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/borrowck/borrowck_for_loop_pattern_assignment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! regression test for <https://github.com/rust-lang/rust/issues/155030>

fn main() {
let nums: [u32; 3] = [1, 2, 3];
for num in nums {
num *= 2; //~ ERROR cannot assign twice to immutable variable `num`
println!("{num}");
}
}
16 changes: 16 additions & 0 deletions tests/ui/borrowck/borrowck_for_loop_pattern_assignment.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0384]: cannot assign twice to immutable variable `num`
--> $DIR/borrowck_for_loop_pattern_assignment.rs:6:9
|
LL | for num in nums {
| --- first assignment to `num`
LL | num *= 2;
| ^^^^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
LL | for mut num in nums {
| +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0384`.
Loading