Skip to content

Commit

Permalink
Auto merge of #28577 - jethrogb:topic/ast-stmt-debug, r=pcwalton
Browse files Browse the repository at this point in the history
This enables the Debug trait to work on syntax::ast::Stmt.
  • Loading branch information
bors committed Oct 1, 2015
2 parents f5a0158 + 0a2ffa0 commit 031dd9c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use ptr::P;

use std::fmt;
use std::rc::Rc;
use std::borrow::Cow;
use serialize::{Encodable, Decodable, Encoder, Decoder};

/// A name is a part of an identifier, representing a string or gensym. It's
Expand Down Expand Up @@ -668,7 +669,8 @@ pub type Stmt = Spanned<Stmt_>;
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))
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/libsyntax/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -384,7 +384,8 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
}

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)
}

Expand Down

0 comments on commit 031dd9c

Please sign in to comment.