21
21
//! nested within a uniquely determined `FnLike`), and users can ask
22
22
//! for the `Code` associated with a particular NodeId.
23
23
24
- pub use self :: Code :: * ;
25
-
26
24
use hir as ast;
27
25
use hir:: map:: { self , Node } ;
28
- use hir:: { Block , FnDecl } ;
26
+ use hir:: { Expr , FnDecl } ;
29
27
use hir:: intravisit:: FnKind ;
30
28
use syntax:: abi;
31
29
use syntax:: ast:: { Attribute , Name , NodeId } ;
@@ -50,7 +48,7 @@ pub trait MaybeFnLike { fn is_fn_like(&self) -> bool; }
50
48
/// Components shared by fn-like things (fn items, methods, closures).
51
49
pub struct FnParts < ' a > {
52
50
pub decl : & ' a FnDecl ,
53
- pub body : & ' a Block ,
51
+ pub body : & ' a Expr ,
54
52
pub kind : FnKind < ' a > ,
55
53
pub span : Span ,
56
54
pub id : NodeId ,
@@ -77,29 +75,32 @@ impl MaybeFnLike for ast::Expr {
77
75
}
78
76
}
79
77
80
- /// Carries either an FnLikeNode or a Block , as these are the two
78
+ /// Carries either an FnLikeNode or a Expr , as these are the two
81
79
/// constructs that correspond to "code" (as in, something from which
82
80
/// we can construct a control-flow graph).
83
81
#[ derive( Copy , Clone ) ]
84
82
pub enum Code < ' a > {
85
- FnLikeCode ( FnLikeNode < ' a > ) ,
86
- BlockCode ( & ' a Block ) ,
83
+ FnLike ( FnLikeNode < ' a > ) ,
84
+ Expr ( & ' a Expr ) ,
87
85
}
88
86
89
87
impl < ' a > Code < ' a > {
90
88
pub fn id ( & self ) -> NodeId {
91
89
match * self {
92
- FnLikeCode ( node) => node. id ( ) ,
93
- BlockCode ( block) => block. id ,
90
+ Code :: FnLike ( node) => node. id ( ) ,
91
+ Code :: Expr ( block) => block. id ,
94
92
}
95
93
}
96
94
97
- /// Attempts to construct a Code from presumed FnLike or Block node input.
98
- pub fn from_node ( node : Node ) -> Option < Code > {
99
- if let map:: NodeBlock ( block) = node {
100
- Some ( BlockCode ( block) )
101
- } else {
102
- FnLikeNode :: from_node ( node) . map ( |fn_like| FnLikeCode ( fn_like) )
95
+ /// Attempts to construct a Code from presumed FnLike or Expr node input.
96
+ pub fn from_node ( map : & map:: Map < ' a > , id : NodeId ) -> Option < Code < ' a > > {
97
+ match map. get ( id) {
98
+ map:: NodeBlock ( _) => {
99
+ // Use the parent, hopefully an expression node.
100
+ Code :: from_node ( map, map. get_parent_node ( id) )
101
+ }
102
+ map:: NodeExpr ( expr) => Some ( Code :: Expr ( expr) ) ,
103
+ node => FnLikeNode :: from_node ( node) . map ( Code :: FnLike )
103
104
}
104
105
}
105
106
}
@@ -114,7 +115,7 @@ struct ItemFnParts<'a> {
114
115
abi : abi:: Abi ,
115
116
vis : & ' a ast:: Visibility ,
116
117
generics : & ' a ast:: Generics ,
117
- body : & ' a Block ,
118
+ body : & ' a Expr ,
118
119
id : NodeId ,
119
120
span : Span ,
120
121
attrs : & ' a [ Attribute ] ,
@@ -124,14 +125,14 @@ struct ItemFnParts<'a> {
124
125
/// for use when implementing FnLikeNode operations.
125
126
struct ClosureParts < ' a > {
126
127
decl : & ' a FnDecl ,
127
- body : & ' a Block ,
128
+ body : & ' a Expr ,
128
129
id : NodeId ,
129
130
span : Span ,
130
131
attrs : & ' a [ Attribute ] ,
131
132
}
132
133
133
134
impl < ' a > ClosureParts < ' a > {
134
- fn new ( d : & ' a FnDecl , b : & ' a Block , id : NodeId , s : Span , attrs : & ' a [ Attribute ] ) -> Self {
135
+ fn new ( d : & ' a FnDecl , b : & ' a Expr , id : NodeId , s : Span , attrs : & ' a [ Attribute ] ) -> Self {
135
136
ClosureParts {
136
137
decl : d,
137
138
body : b,
@@ -171,9 +172,9 @@ impl<'a> FnLikeNode<'a> {
171
172
}
172
173
}
173
174
174
- pub fn body ( self ) -> & ' a Block {
175
+ pub fn body ( self ) -> & ' a Expr {
175
176
self . handle ( |i : ItemFnParts < ' a > | & * i. body ,
176
- |_, _, _: & ' a ast:: MethodSig , _, body : & ' a ast:: Block , _, _| body,
177
+ |_, _, _: & ' a ast:: MethodSig , _, body : & ' a ast:: Expr , _, _| body,
177
178
|c : ClosureParts < ' a > | c. body )
178
179
}
179
180
@@ -214,7 +215,7 @@ impl<'a> FnLikeNode<'a> {
214
215
Name ,
215
216
& ' a ast:: MethodSig ,
216
217
Option < & ' a ast:: Visibility > ,
217
- & ' a ast:: Block ,
218
+ & ' a ast:: Expr ,
218
219
Span ,
219
220
& ' a [ Attribute ] )
220
221
-> A ,
0 commit comments