-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Change for-loop desugar to not borrow the iterator during the loop #42265
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -891,6 +891,7 @@ impl<'a> LoweringContext<'a> { | |
| init: l.init.as_ref().map(|e| P(self.lower_expr(e))), | ||
| span: l.span, | ||
| attrs: l.attrs.clone(), | ||
| source: hir::LocalSource::Normal, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -2167,10 +2168,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) { | ||
| // ::std::option::Option::Some(val) => val, | ||
| // ::std::option::Option::None => break | ||
| // } | ||
| // }; | ||
| // SemiExpr(<body>); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is wrong, the statement used is a |
||
| // } | ||
| // } | ||
| // }; | ||
|
|
@@ -2182,15 +2184,13 @@ impl<'a> LoweringContext<'a> { | |
|
|
||
| let iter = self.str_to_ident("iter"); | ||
|
|
||
| // `::std::option::Option::Some(<pat>) => <body>` | ||
| // `::std::option::Option::Some(val) => val` | ||
| let pat_arm = { | ||
| let body_block = self.with_loop_scope(e.id, | ||
| |this| this.lower_block(body, false)); | ||
| let body_expr = P(self.expr_block(body_block, ThinVec::new())); | ||
| let pat = self.lower_pat(pat); | ||
| let some_pat = self.pat_some(e.span, pat); | ||
|
|
||
| self.arm(hir_vec![some_pat], body_expr) | ||
| let val_ident = self.str_to_ident("val"); | ||
| let val_pat = self.pat_ident(e.span, val_ident); | ||
| let val_expr = P(self.expr_ident(e.span, val_ident, val_pat.id)); | ||
| let some_pat = self.pat_some(e.span, val_pat); | ||
| self.arm(hir_vec![some_pat], val_expr) | ||
| }; | ||
|
|
||
| // `::std::option::Option::None => break` | ||
|
|
@@ -2221,8 +2221,20 @@ impl<'a> LoweringContext<'a> { | |
| ThinVec::new())) | ||
| }; | ||
|
|
||
| let pat = self.lower_pat(pat); | ||
| let pat_let = self.stmt_let_pat(e.span, | ||
| match_expr, | ||
| pat, | ||
| hir::LocalSource::ForLoopDesugar); | ||
|
|
||
| let body_block = self.with_loop_scope(e.id, | ||
| |this| this.lower_block(body, false)); | ||
| let body_expr = P(self.expr_block(body_block, ThinVec::new())); | ||
| let body_stmt = respan(e.span, hir::StmtExpr(body_expr, self.next_id())); | ||
|
|
||
| let loop_block = P(self.block_all(e.span, hir_vec![pat_let, body_stmt], None)); | ||
|
|
||
| // `[opt_ident]: loop { ... }` | ||
| let loop_block = P(self.block_expr(match_expr)); | ||
| let loop_expr = hir::ExprLoop(loop_block, self.lower_opt_sp_ident(opt_ident), | ||
| hir::LoopSource::ForLoop); | ||
| let loop_expr = P(hir::Expr { | ||
|
|
@@ -2585,24 +2597,34 @@ impl<'a> LoweringContext<'a> { | |
| } | ||
| } | ||
|
|
||
| fn stmt_let(&mut self, sp: Span, mutbl: bool, ident: Name, ex: P<hir::Expr>) | ||
| -> (hir::Stmt, NodeId) { | ||
| let pat = if mutbl { | ||
| self.pat_ident_binding_mode(sp, ident, hir::BindByValue(hir::MutMutable)) | ||
| } else { | ||
| self.pat_ident(sp, ident) | ||
| }; | ||
| let pat_id = pat.id; | ||
| fn stmt_let_pat(&mut self, | ||
| sp: Span, | ||
| ex: P<hir::Expr>, | ||
| pat: P<hir::Pat>, | ||
| source: hir::LocalSource) | ||
| -> hir::Stmt { | ||
| let local = P(hir::Local { | ||
| pat: pat, | ||
| ty: None, | ||
| init: Some(ex), | ||
| id: self.next_id(), | ||
| span: sp, | ||
| attrs: ThinVec::new(), | ||
| source, | ||
| }); | ||
| let decl = respan(sp, hir::DeclLocal(local)); | ||
| (respan(sp, hir::StmtDecl(P(decl), self.next_id())), pat_id) | ||
| respan(sp, hir::StmtDecl(P(decl), self.next_id())) | ||
| } | ||
|
|
||
| fn stmt_let(&mut self, sp: Span, mutbl: bool, ident: Name, ex: P<hir::Expr>) | ||
| -> (hir::Stmt, NodeId) { | ||
| let pat = if mutbl { | ||
| self.pat_ident_binding_mode(sp, ident, hir::BindByValue(hir::MutMutable)) | ||
| } else { | ||
| self.pat_ident(sp, ident) | ||
| }; | ||
| let pat_id = pat.id; | ||
| (self.stmt_let_pat(sp, ex, pat, hir::LocalSource::Normal), pat_id) | ||
| } | ||
|
|
||
| fn block_expr(&mut self, expr: P<hir::Expr>) -> hir::Block { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -452,12 +452,14 @@ enum Method { GET, POST } | |
|
|
||
|
|
||
| E0297: r##" | ||
| #### Note: this error code is no longer emitted by the compiler. | ||
|
||
| Patterns used to bind names must be irrefutable. That is, they must guarantee | ||
| that a name will be extracted in all cases. Instead of pattern matching the | ||
| loop variable, consider using a `match` or `if let` inside the loop body. For | ||
| instance: | ||
| ```compile_fail,E0297 | ||
| ```compile_fail,E0005 | ||
| let xs : Vec<Option<i32>> = vec![Some(1), None]; | ||
| // This fails because `None` is not covered. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
| // file at the top-level directory of this distribution and at | ||
| // http://rust-lang.org/COPYRIGHT. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
| // option. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| fn main() { | ||
| for x in 0..3 { | ||
| x //~ ERROR mismatched types | ||
| //~| NOTE expected () | ||
| //~| NOTE expected type `()` | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
| // file at the top-level directory of this distribution and at | ||
| // http://rust-lang.org/COPYRIGHT. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
| // option. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| fn main() { | ||
| // Check that the tail statement in the body unifies with something | ||
| for _ in 0..3 { | ||
| unsafe { std::mem::uninitialized() } | ||
| } | ||
|
|
||
| // 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.
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?