Skip to content

Commit ab7b1cc

Browse files
fee1-deadMark-Simulacrum
authored andcommitted
Fix ICE of for-loop mut borrowck where no suggestions are available
1 parent 28e5e93 commit ab7b1cc

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -642,15 +642,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
642642
.starts_with(&original_method_ident.name.to_string())
643643
})
644644
.map(|ident| format!("{}()", ident))
645+
.peekable()
645646
});
646647

647-
if let Some(suggestions) = opt_suggestions {
648-
err.span_suggestions(
649-
path_segment.ident.span,
650-
&format!("use mutable method"),
651-
suggestions,
652-
Applicability::MaybeIncorrect,
653-
);
648+
if let Some(mut suggestions) = opt_suggestions {
649+
if suggestions.peek().is_some() {
650+
err.span_suggestions(
651+
path_segment.ident.span,
652+
&format!("use mutable method"),
653+
suggestions,
654+
Applicability::MaybeIncorrect,
655+
);
656+
}
654657
}
655658
}
656659
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// rust-lang/rust#83309: The compiler tries to suggest potential
2+
// methods that return `&mut` items. However, when it doesn't
3+
// find such methods, it still tries to add suggestions
4+
// which then fails an assertion later because there was
5+
// no suggestions to make.
6+
7+
8+
fn main() {
9+
for v in Query.iter_mut() {
10+
//~^ NOTE this iterator yields `&` references
11+
*v -= 1;
12+
//~^ ERROR cannot assign to `*v` which is behind a `&` reference
13+
//~| NOTE `v` is a `&` reference, so the data it refers to cannot be written
14+
}
15+
}
16+
17+
pub struct Query;
18+
pub struct QueryIter<'a>(&'a i32);
19+
20+
impl Query {
21+
pub fn iter_mut<'a>(&'a mut self) -> QueryIter<'a> {
22+
todo!();
23+
}
24+
}
25+
26+
impl<'a> Iterator for QueryIter<'a> {
27+
type Item = &'a i32;
28+
29+
fn next(&mut self) -> Option<Self::Item> {
30+
todo!();
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0594]: cannot assign to `*v` which is behind a `&` reference
2+
--> $DIR/issue-83309-ice-immut-in-for-loop.rs:11:9
3+
|
4+
LL | for v in Query.iter_mut() {
5+
| ---------------- this iterator yields `&` references
6+
LL |
7+
LL | *v -= 1;
8+
| ^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0594`.

0 commit comments

Comments
 (0)