Skip to content

Commit eb7b6e0

Browse files
committed
Remove support for expr-only yield!
1 parent 51c75e7 commit eb7b6e0

File tree

5 files changed

+30
-53
lines changed

5 files changed

+30
-53
lines changed
+16-41
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use rustc_ast::ptr::P;
22
use rustc_ast::tokenstream::TokenStream;
3-
use rustc_ast::{
4-
CaptureBy, ClosureBinder, Const, CoroutineKind, DUMMY_NODE_ID, Expr, ExprKind, ast, token,
5-
};
3+
use rustc_ast::{CoroutineKind, DUMMY_NODE_ID, Expr, ast, token};
64
use rustc_errors::PResult;
75
use rustc_expand::base::{self, DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
86
use rustc_span::Span;
@@ -27,52 +25,29 @@ fn parse_closure<'a>(
2725
span: Span,
2826
stream: TokenStream,
2927
) -> PResult<'a, P<Expr>> {
30-
let mut parser = cx.new_parser_from_tts(stream);
31-
let mut closure_parser = parser.clone();
28+
let mut closure_parser = cx.new_parser_from_tts(stream);
3229

3330
let coroutine_kind = Some(CoroutineKind::Gen {
3431
span,
3532
closure_id: DUMMY_NODE_ID,
3633
return_impl_trait_id: DUMMY_NODE_ID,
3734
});
3835

39-
match closure_parser.parse_expr() {
40-
Ok(mut closure) => {
41-
if let ast::ExprKind::Closure(c) = &mut closure.kind {
42-
if let Some(kind) = c.coroutine_kind {
43-
cx.dcx().span_err(kind.span(), "only plain closures allowed in `iter!`");
44-
}
45-
c.coroutine_kind = coroutine_kind;
46-
if closure_parser.token != token::Eof {
47-
closure_parser.unexpected()?;
48-
}
49-
return Ok(closure);
36+
let mut closure = closure_parser.parse_expr()?;
37+
match &mut closure.kind {
38+
ast::ExprKind::Closure(c) => {
39+
if let Some(kind) = c.coroutine_kind {
40+
cx.dcx().span_err(kind.span(), "only plain closures allowed in `iter!`");
5041
}
42+
c.coroutine_kind = coroutine_kind;
43+
if closure_parser.token != token::Eof {
44+
closure_parser.unexpected()?;
45+
}
46+
Ok(closure)
47+
}
48+
_ => {
49+
cx.dcx().span_err(closure.span, "`iter!` body must be a closure");
50+
Err(closure_parser.unexpected().unwrap_err())
5151
}
52-
Err(diag) => diag.cancel(),
53-
}
54-
55-
let lo = parser.token.span.shrink_to_lo();
56-
let block = parser.parse_block_tail(
57-
lo,
58-
ast::BlockCheckMode::Default,
59-
rustc_parse::parser::AttemptLocalParseRecovery::No,
60-
)?;
61-
let fn_decl = cx.fn_decl(Default::default(), ast::FnRetTy::Default(span));
62-
let closure = ast::Closure {
63-
binder: ClosureBinder::NotPresent,
64-
capture_clause: CaptureBy::Ref,
65-
constness: Const::No,
66-
coroutine_kind,
67-
movability: ast::Movability::Movable,
68-
fn_decl,
69-
body: cx.expr_block(block),
70-
fn_decl_span: span,
71-
fn_arg_span: span,
72-
};
73-
if parser.token != token::Eof {
74-
parser.unexpected()?;
7552
}
76-
let span = lo.to(parser.token.span);
77-
Ok(cx.expr(span, ExprKind::Closure(Box::new(closure))))
7853
}

tests/ui/iterators/generator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use std::iter::iter;
66

77
fn main() {
8-
let i = iter! {
8+
let i = iter! { || {
99
yield 0;
1010
for x in 5..10 {
1111
yield x * 2;
1212
}
13-
};
13+
} };
1414
let mut i = i();
1515
assert_eq!(i.next(), Some(0));
1616
assert_eq!(i.next(), Some(10));

tests/ui/iterators/generator_capture_fail.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use std::iter::iter;
55
fn main() {
66
let i = {
77
let s = String::new();
8-
iter! {
9-
yield s.len(); //~ ERROR `s` does not live long enough
8+
iter! { || { //~ ERROR `s` does not live long enough
9+
yield s.len();
1010
for x in 5..10 {
1111
yield x * 2;
1212
}
13-
}
13+
} }
1414
};
1515
let mut i = i();
1616
assert_eq!(i.next(), Some(0));

tests/ui/iterators/generator_capture_fail.stderr

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
error[E0597]: `s` does not live long enough
2-
--> $DIR/generator_capture_fail.rs:9:13
2+
--> $DIR/generator_capture_fail.rs:8:17
33
|
44
LL | let i = {
55
| - borrow later stored here
6-
...
7-
LL | / yield s.len();
6+
LL | let s = String::new();
7+
LL | iter! { || {
8+
| _________________^
9+
LL | | yield s.len();
810
LL | | for x in 5..10 {
911
LL | | yield x * 2;
1012
LL | | }
11-
| |_____________^ borrowed value does not live long enough
12-
LL | }
13+
LL | | } }
14+
| |_________^ borrowed value does not live long enough
1315
LL | };
1416
| - `s` dropped here while still borrowed
1517

tests/ui/iterators/generator_returned_from_fn.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
use std::iter::iter;
44

55
fn plain() -> impl Fn() -> impl Iterator<Item = u32> {
6-
iter! {
6+
iter! { || {
77
yield 0;
88
for x in 5..10 {
99
yield x * 2;
1010
}
11-
}
11+
} }
1212
}
1313

1414
fn arg() -> impl Fn(u32) -> impl Iterator<Item = u32> {

0 commit comments

Comments
 (0)