-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change for-loop desugar to not borrow the iterator during the loop #42265
Conversation
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
@@ -2146,10 +2147,11 @@ impl<'a> LoweringContext<'a> { | |||
// let result = match ::std::iter::IntoIterator::into_iter(<head>) { | |||
// mut iter => { | |||
// [opt_ident]: loop { | |||
// match ::std::iter::Iterator::next(&mut iter) { | |||
// ::std::option::Option::Some(<pat>) => <body>, | |||
// let <pat> = match ::std::iter::Iterator::next(&mut iter) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes semantics, although I can't think of any situation in which this would fail.
FWIW when I asked about while let Some(x) = iter.next()
- that would correspond to the old desugaring, not your new one, wouldn't it?
@@ -452,12 +452,14 @@ enum Method { GET, POST } | |||
|
|||
|
|||
E0297: r##" | |||
#### Note: this error code is no longer emitted by the compiler. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rust-lang/compiler This is an annoying (lack of a?) convention - is there any consensus?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be what we have done in the past. I see some instances of ##
and some with ####
.
Personally, I long to get rid of --explain XXX
and just have a "long error mode" in which the long explanations are emitted 'inline'. That would basically make this point irrelevant.
Started crater run. |
Crater report is pretty long, but by looking at non-root regressions I found something. |
|
||
fn main() { | ||
for i in 0..3 { | ||
unsafe { std::mem::uninitialized() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this makes sense? I'd rather have a compile-fail test checking there's a type mismatch when there's a body that's not ()
.
src/librustc/hir/lowering.rs
Outdated
@@ -2172,7 +2172,7 @@ impl<'a> LoweringContext<'a> { | |||
// ::std::option::Option::Some(val) => val, | |||
// ::std::option::Option::None => break | |||
// }; | |||
// <body> | |||
// <body>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not the same, ;
corresponds to StmtSemi
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, there aren't really syntax for StmtExpr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certain expressions (loops? not sure. maybe if
too) don't require ;
after them.
|
||
// Check that the tail statement in the body can be unit | ||
for _ in 0..3 { | ||
() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not that interesting, since it doesn't directly check for the property I care about, i.e. that non-()
bodies are denied.
I am not an expert on what's going on here but if we're changing the sugar, I would like to see https://doc.rust-lang.org/std/iter/#for-loops-and-intoiterator updated |
for _ in 0..3 { | ||
() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline, here and in the other test.
Started a second crater run. |
Second crater report is clean (modulo network errors)! |
I believe the documentation still needs updating (#42265 (comment)) and @eddyb wanted a few changes made (#42265 (comment)). |
Those have both been addressed. |
Ah, okay. Thanks! |
@bors r+ |
📌 Commit cfdbff7 has been approved by |
Change for-loop desugar to not borrow the iterator during the loop This is enables the use of suspend points inside for-loops in movable generators. This is illegal in the current desugaring as `iter` is borrowed across the body.
☀️ Test successful - status-appveyor, status-travis |
// ::std::option::Option::None => break | ||
// } | ||
// }; | ||
// SemiExpr(<body>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is wrong, the statement used is a StmtExpr
.
This is enables the use of suspend points inside for-loops in movable generators. This is illegal in the current desugaring as
iter
is borrowed across the body.