Skip to content

Commit 0a2ffa0

Browse files
author
Jethro Beekman
committed
Change syntax::ast_util::stmt_id to not panic on macros
This enables the Debug trait to work on syntax::ast::Stmt
1 parent 7e8d19b commit 0a2ffa0

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/libsyntax/ast.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use ptr::P;
6565

6666
use std::fmt;
6767
use std::rc::Rc;
68+
use std::borrow::Cow;
6869
use serialize::{Encodable, Decodable, Encoder, Decoder};
6970

7071
// FIXME #6993: in librustc, uses of "ident" should be replaced
@@ -685,7 +686,8 @@ pub type Stmt = Spanned<Stmt_>;
685686
impl fmt::Debug for Stmt {
686687
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
687688
write!(f, "stmt({}: {})",
688-
ast_util::stmt_id(self),
689+
ast_util::stmt_id(self)
690+
.map_or(Cow::Borrowed("<macro>"),|id|Cow::Owned(id.to_string())),
689691
pprust::stmt_to_string(self))
690692
}
691693
}

src/libsyntax/ast_util.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ pub fn path_name_i(idents: &[Ident]) -> String {
2828
idents.iter().map(|i| i.to_string()).collect::<Vec<String>>().join("::")
2929
}
3030

31-
pub fn stmt_id(s: &Stmt) -> NodeId {
31+
pub fn stmt_id(s: &Stmt) -> Option<NodeId> {
3232
match s.node {
33-
StmtDecl(_, id) => id,
34-
StmtExpr(_, id) => id,
35-
StmtSemi(_, id) => id,
36-
StmtMac(..) => panic!("attempted to analyze unexpanded stmt")
33+
StmtDecl(_, id) => Some(id),
34+
StmtExpr(_, id) => Some(id),
35+
StmtSemi(_, id) => Some(id),
36+
StmtMac(..) => None,
3737
}
3838
}
3939

@@ -385,7 +385,8 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
385385
}
386386

387387
fn visit_stmt(&mut self, statement: &Stmt) {
388-
self.operation.visit_id(ast_util::stmt_id(statement));
388+
self.operation
389+
.visit_id(ast_util::stmt_id(statement).expect("attempted to visit unexpanded stmt"));
389390
visit::walk_stmt(self, statement)
390391
}
391392

0 commit comments

Comments
 (0)