Skip to content

Commit 031dd9c

Browse files
committed
Auto merge of #28577 - jethrogb:topic/ast-stmt-debug, r=pcwalton
This enables the Debug trait to work on syntax::ast::Stmt.
2 parents f5a0158 + 0a2ffa0 commit 031dd9c

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
/// A name is a part of an identifier, representing a string or gensym. It's
@@ -668,7 +669,8 @@ pub type Stmt = Spanned<Stmt_>;
668669
impl fmt::Debug for Stmt {
669670
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
670671
write!(f, "stmt({}: {})",
671-
ast_util::stmt_id(self),
672+
ast_util::stmt_id(self)
673+
.map_or(Cow::Borrowed("<macro>"),|id|Cow::Owned(id.to_string())),
672674
pprust::stmt_to_string(self))
673675
}
674676
}

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

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

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

0 commit comments

Comments
 (0)