Skip to content

Commit 3c2fd1a

Browse files
committed
Print syntax contexts and marks when printing hygiene information
1 parent b4ef99f commit 3c2fd1a

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

src/librustc_driver/pretty.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
326326
}
327327
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
328328
match node {
329+
pprust::AnnNode::Crate(_) |
329330
pprust::AnnNode::Ident(_) |
330331
pprust::AnnNode::Name(_) => {},
331332

@@ -431,14 +432,18 @@ impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
431432
match node {
432433
pprust::AnnNode::Ident(&ast::Ident { name, span }) => {
433434
s.s.space();
434-
// FIXME #16420: this doesn't display the connections
435-
// between syntax contexts
436435
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
437436
}
438437
pprust::AnnNode::Name(&name) => {
439438
s.s.space();
440439
s.synth_comment(name.as_u32().to_string())
441440
}
441+
pprust::AnnNode::Crate(_) => {
442+
s.s.hardbreak();
443+
let verbose = self.sess.verbose();
444+
s.synth_comment(syntax_pos::hygiene::debug_hygiene_data(verbose));
445+
s.s.hardbreak_if_not_bol();
446+
}
442447
_ => {}
443448
}
444449
}

src/libsyntax/print/pprust.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub enum AnnNode<'a> {
3535
SubItem(ast::NodeId),
3636
Expr(&'a ast::Expr),
3737
Pat(&'a ast::Pat),
38+
Crate(&'a ast::Crate),
3839
}
3940

4041
pub trait PpAnn {
@@ -140,6 +141,7 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
140141

141142
s.print_mod(&krate.module, &krate.attrs);
142143
s.print_remaining_comments();
144+
s.ann.post(&mut s, AnnNode::Crate(krate));
143145
s.s.eof()
144146
}
145147

src/libsyntax_pos/hygiene.rs

+32
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,38 @@ pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symb
343343
}))
344344
}
345345

346+
pub fn debug_hygiene_data(verbose: bool) -> String {
347+
HygieneData::with(|data| {
348+
if verbose {
349+
format!("{:#?}", data)
350+
} else {
351+
let mut s = String::from("");
352+
s.push_str("Expansions:");
353+
data.expn_data.iter().enumerate().for_each(|(id, expn_info)| {
354+
let expn_info = expn_info.as_ref().expect("no expansion data for an expansion ID");
355+
s.push_str(&format!(
356+
"\n{}: parent: {:?}, call_site_ctxt: {:?}, kind: {:?}",
357+
id,
358+
expn_info.parent,
359+
expn_info.call_site.ctxt(),
360+
expn_info.kind,
361+
));
362+
});
363+
s.push_str("\n\nSyntaxContexts:");
364+
data.syntax_context_data.iter().enumerate().for_each(|(id, ctxt)| {
365+
s.push_str(&format!(
366+
"\n#{}: parent: {:?}, outer_mark: ({:?}, {:?})",
367+
id,
368+
ctxt.parent,
369+
ctxt.outer_expn,
370+
ctxt.outer_transparency,
371+
));
372+
});
373+
s
374+
}
375+
})
376+
}
377+
346378
impl SyntaxContext {
347379
#[inline]
348380
pub const fn root() -> Self {

src/test/ui/hygiene/unpretty-debug.stdout

+10
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ macro_rules! foo /* 0#0 */ { ($ x : ident) => { y + $ x } }
1313
fn bar /* 0#0 */() { let x /* 0#0 */ = 1; y /* 0#1 */ + x /* 0#0 */ }
1414

1515
fn y /* 0#0 */() { }
16+
17+
/*
18+
Expansions:
19+
0: parent: ExpnId(0), call_site_ctxt: #0, kind: Root
20+
1: parent: ExpnId(0), call_site_ctxt: #0, kind: Macro(Bang, foo)
21+
22+
SyntaxContexts:
23+
#0: parent: #0, outer_mark: (ExpnId(0), Opaque)
24+
#1: parent: #0, outer_mark: (ExpnId(1), SemiTransparent)
25+
*/

0 commit comments

Comments
 (0)