Stream: t-compiler/help

Topic: Dumping/pretty-printing AST/HIR?


osa1 (Nov 26 2019 at 18:54, on Zulip):

Is there a way to dump/pretty-print (showing rustc types/constructors used) parsed AST or HIR passed to the type checker? I'm looking at some front-end issues and it'd be really helpful if I knew how a program is represented in AST or HIR (ideally both). I checked rustc book but couldn't see anything relevant.

osa1 (Nov 27 2019 at 14:57, on Zulip):

Anyone?

oli (Nov 27 2019 at 15:03, on Zulip):

I think we're removing some of that dumping logic

oli (Nov 27 2019 at 15:03, on Zulip):

not sure if the hir dumps ever showed types

osa1 (Nov 27 2019 at 15:03, on Zulip):

Interesting. Is it not a useful feature for devs? It'd be helpful to me if I could see how my program is represented.

oli (Nov 27 2019 at 15:04, on Zulip):

You'll want -Zunpretty=hir,typed I think

osa1 (Nov 27 2019 at 15:08, on Zulip):

That doesn't show the AST, also is there a way to get a similar output but for type checker input?

osa1 (Nov 28 2019 at 14:53, on Zulip):

If anyone else needs this, the AST types implement Debug so I was able to do what I need with a small modification in the parser entry point:

diff --git a/src/librustc_parse/parser/module.rs b/src/librustc_parse/parser/module.rs
index 59d7c2b4239..62df04f859a 100644
--- a/src/librustc_parse/parser/module.rs
+++ b/src/librustc_parse/parser/module.rs
@@ -14,6 +14,8 @@ use errors::PResult;

 use std::path::{self, Path, PathBuf};

+use log::debug;
+
 /// Information about the path to a module.
 pub(super) struct ModulePath {
     name: String,
@@ -35,6 +37,9 @@ impl<'a> Parser<'a> {
             module: self.parse_mod_items(&token::Eof, lo)?,
             span: lo.to(self.token.span),
         });
+
+        debug!("Parsed crate: {:?}", krate.as_ref().unwrap());
+
         krate
     }

I run with RUSTC_LOG=rustc_parse::parser::module rustc --edition 2018 -Zno-codegen foo.rs

osa1 (Nov 28 2019 at 20:14, on Zulip):

For HIR there's -Zunpretty=hir-tree

Last update: Dec 12 2019 at 00:50UTC