2121//! nested within a uniquely determined `FnLike`), and users can ask
2222//! for the `Code` associated with a particular NodeId.
2323
24- pub use self :: Code :: * ;
25-
2624use hir as ast;
2725use hir:: map:: { self , Node } ;
28- use hir:: { Block , FnDecl } ;
26+ use hir:: { Expr , FnDecl } ;
2927use hir:: intravisit:: FnKind ;
3028use syntax:: abi;
3129use syntax:: ast:: { Attribute , Name , NodeId } ;
@@ -50,7 +48,7 @@ pub trait MaybeFnLike { fn is_fn_like(&self) -> bool; }
5048/// Components shared by fn-like things (fn items, methods, closures).
5149pub struct FnParts < ' a > {
5250 pub decl : & ' a FnDecl ,
53- pub body : & ' a Block ,
51+ pub body : & ' a Expr ,
5452 pub kind : FnKind < ' a > ,
5553 pub span : Span ,
5654 pub id : NodeId ,
@@ -77,29 +75,32 @@ impl MaybeFnLike for ast::Expr {
7775 }
7876}
7977
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
8179/// constructs that correspond to "code" (as in, something from which
8280/// we can construct a control-flow graph).
8381#[ derive( Copy , Clone ) ]
8482pub enum Code < ' a > {
85- FnLikeCode ( FnLikeNode < ' a > ) ,
86- BlockCode ( & ' a Block ) ,
83+ FnLike ( FnLikeNode < ' a > ) ,
84+ Expr ( & ' a Expr ) ,
8785}
8886
8987impl < ' a > Code < ' a > {
9088 pub fn id ( & self ) -> NodeId {
9189 match * self {
92- FnLikeCode ( node) => node. id ( ) ,
93- BlockCode ( block) => block. id ,
90+ Code :: FnLike ( node) => node. id ( ) ,
91+ Code :: Expr ( block) => block. id ,
9492 }
9593 }
9694
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 )
103104 }
104105 }
105106}
@@ -114,7 +115,7 @@ struct ItemFnParts<'a> {
114115 abi : abi:: Abi ,
115116 vis : & ' a ast:: Visibility ,
116117 generics : & ' a ast:: Generics ,
117- body : & ' a Block ,
118+ body : & ' a Expr ,
118119 id : NodeId ,
119120 span : Span ,
120121 attrs : & ' a [ Attribute ] ,
@@ -124,14 +125,14 @@ struct ItemFnParts<'a> {
124125/// for use when implementing FnLikeNode operations.
125126struct ClosureParts < ' a > {
126127 decl : & ' a FnDecl ,
127- body : & ' a Block ,
128+ body : & ' a Expr ,
128129 id : NodeId ,
129130 span : Span ,
130131 attrs : & ' a [ Attribute ] ,
131132}
132133
133134impl < ' 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 {
135136 ClosureParts {
136137 decl : d,
137138 body : b,
@@ -171,9 +172,9 @@ impl<'a> FnLikeNode<'a> {
171172 }
172173 }
173174
174- pub fn body ( self ) -> & ' a Block {
175+ pub fn body ( self ) -> & ' a Expr {
175176 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,
177178 |c : ClosureParts < ' a > | c. body )
178179 }
179180
@@ -214,7 +215,7 @@ impl<'a> FnLikeNode<'a> {
214215 Name ,
215216 & ' a ast:: MethodSig ,
216217 Option < & ' a ast:: Visibility > ,
217- & ' a ast:: Block ,
218+ & ' a ast:: Expr ,
218219 Span ,
219220 & ' a [ Attribute ] )
220221 -> A ,
0 commit comments