Skip to content

Commit

Permalink
Rollup merge of rust-lang#64486 - matthewjasper:hygiene-debugging, r=…
Browse files Browse the repository at this point in the history
…petrochenkov

Print out more information for `-Zunpretty=expanded,hygiene`

I've found this helpful when trying to understand how hygiene works.

Closes rust-lang#16420
  • Loading branch information
tmandry committed Sep 18, 2019
2 parents 5283791 + 3c2fd1a commit c7b5567
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/librustc_driver/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
}
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
match node {
pprust::AnnNode::Crate(_) |
pprust::AnnNode::Ident(_) |
pprust::AnnNode::Name(_) => {},

Expand Down Expand Up @@ -431,14 +432,18 @@ impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
match node {
pprust::AnnNode::Ident(&ast::Ident { name, span }) => {
s.s.space();
// FIXME #16420: this doesn't display the connections
// between syntax contexts
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
}
pprust::AnnNode::Name(&name) => {
s.s.space();
s.synth_comment(name.as_u32().to_string())
}
pprust::AnnNode::Crate(_) => {
s.s.hardbreak();
let verbose = self.sess.verbose();
s.synth_comment(syntax_pos::hygiene::debug_hygiene_data(verbose));
s.s.hardbreak_if_not_bol();
}
_ => {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2387,7 +2387,7 @@ pub enum ItemKind {
),
/// A macro invocation.
///
/// E.g., `macro_rules! foo { .. }` or `foo!(..)`.
/// E.g., `foo!(..)`.
Mac(Mac),

/// A macro definition.
Expand Down
10 changes: 8 additions & 2 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum AnnNode<'a> {
SubItem(ast::NodeId),
Expr(&'a ast::Expr),
Pat(&'a ast::Pat),
Crate(&'a ast::Crate),
}

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

s.print_mod(&krate.module, &krate.attrs);
s.print_remaining_comments();
s.ann.post(&mut s, AnnNode::Crate(krate));
s.s.eof()
}

Expand Down Expand Up @@ -1369,8 +1371,12 @@ impl<'a> State<'a> {
}
}
ast::ItemKind::MacroDef(ref macro_def) => {
let (kw, has_bang) =
if macro_def.legacy { ("macro_rules", true) } else { ("macro", false) };
let (kw, has_bang) = if macro_def.legacy {
("macro_rules", true)
} else {
self.print_visibility(&item.vis);
("macro", false)
};
self.print_mac_common(
Some(MacHeader::Keyword(kw)),
has_bang,
Expand Down
32 changes: 32 additions & 0 deletions src/libsyntax_pos/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,38 @@ pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symb
}))
}

pub fn debug_hygiene_data(verbose: bool) -> String {
HygieneData::with(|data| {
if verbose {
format!("{:#?}", data)
} else {
let mut s = String::from("");
s.push_str("Expansions:");
data.expn_data.iter().enumerate().for_each(|(id, expn_info)| {
let expn_info = expn_info.as_ref().expect("no expansion data for an expansion ID");
s.push_str(&format!(
"\n{}: parent: {:?}, call_site_ctxt: {:?}, kind: {:?}",
id,
expn_info.parent,
expn_info.call_site.ctxt(),
expn_info.kind,
));
});
s.push_str("\n\nSyntaxContexts:");
data.syntax_context_data.iter().enumerate().for_each(|(id, ctxt)| {
s.push_str(&format!(
"\n#{}: parent: {:?}, outer_mark: ({:?}, {:?})",
id,
ctxt.parent,
ctxt.outer_expn,
ctxt.outer_transparency,
));
});
s
}
})
}

impl SyntaxContext {
#[inline]
pub const fn root() -> Self {
Expand Down
2 changes: 1 addition & 1 deletion src/test/pretty/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

#![feature(decl_macro)]

macro mac { ($ arg : expr) => { $ arg + $ arg } }
pub(crate) macro mac { ($ arg : expr) => { $ arg + $ arg } }

fn main() { }
10 changes: 10 additions & 0 deletions src/test/ui/hygiene/unpretty-debug.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ macro_rules! foo /* 0#0 */ { ($ x : ident) => { y + $ x } }
fn bar /* 0#0 */() { let x /* 0#0 */ = 1; y /* 0#1 */ + x /* 0#0 */ }

fn y /* 0#0 */() { }

/*
Expansions:
0: parent: ExpnId(0), call_site_ctxt: #0, kind: Root
1: parent: ExpnId(0), call_site_ctxt: #0, kind: Macro(Bang, foo)

SyntaxContexts:
#0: parent: #0, outer_mark: (ExpnId(0), Opaque)
#1: parent: #0, outer_mark: (ExpnId(1), SemiTransparent)
*/

0 comments on commit c7b5567

Please sign in to comment.