]> git.lizzy.rs Git - rust.git/commitdiff
Change syntax::ast_util::stmt_id to not panic on macros
authorJethro Beekman <jethro@jbeekman.nl>
Mon, 21 Sep 2015 23:10:38 +0000 (16:10 -0700)
committerJethro Beekman <jethro@jbeekman.nl>
Mon, 21 Sep 2015 23:54:11 +0000 (16:54 -0700)
This enables the Debug trait to work on syntax::ast::Stmt

src/libsyntax/ast.rs
src/libsyntax/ast_util.rs

index 5b04fc0e6977b3ece9c9f1d26f31702b70ca088b..cdddaaffc357dfb66208018b58d80505a151de42 100644 (file)
@@ -65,6 +65,7 @@
 
 use std::fmt;
 use std::rc::Rc;
+use std::borrow::Cow;
 use serialize::{Encodable, Decodable, Encoder, Decoder};
 
 // FIXME #6993: in librustc, uses of "ident" should be replaced
@@ -685,7 +686,8 @@ pub enum UnOp {
 impl fmt::Debug for Stmt {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         write!(f, "stmt({}: {})",
-               ast_util::stmt_id(self),
+               ast_util::stmt_id(self)
+                   .map_or(Cow::Borrowed("<macro>"),|id|Cow::Owned(id.to_string())),
                pprust::stmt_to_string(self))
     }
 }
index d024ff117f579d97cbffd8bd11a337b39b92b0b8..a0535408cce1d1c54afe3acc90a1c8ec72bf4e9b 100644 (file)
@@ -28,12 +28,12 @@ pub fn path_name_i(idents: &[Ident]) -> String {
     idents.iter().map(|i| i.to_string()).collect::<Vec<String>>().join("::")
 }
 
-pub fn stmt_id(s: &Stmt) -> NodeId {
+pub fn stmt_id(s: &Stmt) -> Option<NodeId> {
     match s.node {
-      StmtDecl(_, id) => id,
-      StmtExpr(_, id) => id,
-      StmtSemi(_, id) => id,
-      StmtMac(..) => panic!("attempted to analyze unexpanded stmt")
+      StmtDecl(_, id) => Some(id),
+      StmtExpr(_, id) => Some(id),
+      StmtSemi(_, id) => Some(id),
+      StmtMac(..) => None,
     }
 }
 
@@ -385,7 +385,8 @@ fn visit_block(&mut self, block: &Block) {
     }
 
     fn visit_stmt(&mut self, statement: &Stmt) {
-        self.operation.visit_id(ast_util::stmt_id(statement));
+        self.operation
+            .visit_id(ast_util::stmt_id(statement).expect("attempted to visit unexpanded stmt"));
         visit::walk_stmt(self, statement)
     }