11use rustc_ast:: ptr:: P ;
22use 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} ;
64use rustc_errors:: PResult ;
75use rustc_expand:: base:: { self , DummyResult , ExpandResult , ExtCtxt , MacroExpanderResult } ;
86use 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}
0 commit comments