From 0a2ffa083589e113ad45e64712259c17c391779a Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Mon, 21 Sep 2015 16:10:38 -0700 Subject: [PATCH] Change syntax::ast_util::stmt_id to not panic on macros This enables the Debug trait to work on syntax::ast::Stmt --- src/libsyntax/ast.rs | 4 +++- src/libsyntax/ast_util.rs | 13 +++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 5b04fc0e697..cdddaaffc35 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -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(""),|id|Cow::Owned(id.to_string())), pprust::stmt_to_string(self)) } } diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index d024ff117f5..a0535408cce 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -28,12 +28,12 @@ pub fn path_name_i(idents: &[Ident]) -> String { idents.iter().map(|i| i.to_string()).collect::>().join("::") } -pub fn stmt_id(s: &Stmt) -> NodeId { +pub fn stmt_id(s: &Stmt) -> Option { 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) } -- 2.44.0