1
1
use rustc_ast:: ptr:: P ;
2
2
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} ;
6
4
use rustc_errors:: PResult ;
7
5
use rustc_expand:: base:: { self , DummyResult , ExpandResult , ExtCtxt , MacroExpanderResult } ;
8
6
use rustc_span:: Span ;
@@ -27,52 +25,29 @@ fn parse_closure<'a>(
27
25
span : Span ,
28
26
stream : TokenStream ,
29
27
) -> 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) ;
32
29
33
30
let coroutine_kind = Some ( CoroutineKind :: Gen {
34
31
span,
35
32
closure_id : DUMMY_NODE_ID ,
36
33
return_impl_trait_id : DUMMY_NODE_ID ,
37
34
} ) ;
38
35
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!`" ) ;
50
41
}
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 ( ) )
51
51
}
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 ( ) ?;
75
52
}
76
- let span = lo. to ( parser. token . span ) ;
77
- Ok ( cx. expr ( span, ExprKind :: Closure ( Box :: new ( closure) ) ) )
78
53
}
0 commit comments