-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #70264 - tirr-c:issue-69789-mut-suggestion, r=estebank
Fix invalid suggestion on `&mut` iterators yielding `&` references Fixes #69789. rustc suggested an invalid code when `&` reference from `&mut` iterator is mutated. The compiler knew we're mutating a value behind `&` reference, but as the assignment RHS is from desugaring, it could only see the iterator expression from source and inserted `mut` there. r? @estebank
- Loading branch information
Showing
3 changed files
with
82 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/test/ui/borrowck/issue-69789-iterator-mut-suggestion.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Regression test for #69789: rustc generated an invalid suggestion | ||
// when `&` reference from `&mut` iterator is mutated. | ||
|
||
fn main() { | ||
for item in &mut std::iter::empty::<&'static ()>() { | ||
//~^ NOTE this iterator yields `&` references | ||
*item = (); | ||
//~^ ERROR cannot assign | ||
//~| NOTE cannot be written | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/borrowck/issue-69789-iterator-mut-suggestion.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0594]: cannot assign to `*item` which is behind a `&` reference | ||
--> $DIR/issue-69789-iterator-mut-suggestion.rs:7:9 | ||
| | ||
LL | for item in &mut std::iter::empty::<&'static ()>() { | ||
| -------------------------------------- this iterator yields `&` references | ||
LL | | ||
LL | *item = (); | ||
| ^^^^^^^^^^ `item` is a `&` reference, so the data it refers to cannot be written | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0594`. |