Skip to content

Add -Zast and -Zast-noexpand to pretty-print AST (without the JSON conversion) - #38083

Closed
spinda wants to merge 3 commits into
rust-lang:masterfrom
spinda:dump-ast
Closed

Add -Zast and -Zast-noexpand to pretty-print AST (without the JSON conversion)#38083
spinda wants to merge 3 commits into
rust-lang:masterfrom
spinda:dump-ast

Conversation

@spinda

Copy link
Copy Markdown
Contributor

-Zast behaves like -Zast-json but uses {:#?} pretty-printing instead of converting to JSON.

-Zast-noexpand is the pretty-printing counterpart to -Zast-json-noexpand.

Fixes#37873.

@rust-highfive

Copy link
Copy Markdown
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @eddyb (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@eddyb

Copy link
Copy Markdown
Contributor

r? @nrc

@rust-highfiverust-highfive assigned nrc and unassigned eddybNov 30, 2016
@sanxiyn

Copy link
Copy Markdown
Contributor

I'd like to see an example output.

@spinda

Copy link
Copy Markdown
ContributorAuthor
$ cat test.rs
fn main() {}
$ rustc -Zast test.rs
Crate {
module: Mod {
inner: Span { lo: BytePos(0), hi: BytePos(12), expn_id: ExpnId(4294967295) },
items: [
Item {
ident: #0,
attrs: [
Attribute {
id: AttrId(
1
),
style: Outer,
value: MetaItem {
name: prelude_import(85),
node: Word,
span: Span { lo: BytePos(0), hi: BytePos(0), expn_id: ExpnId(0) }
},
is_sugared_doc: false,
span: Span { lo: BytePos(0), hi: BytePos(0), expn_id: ExpnId(0) }
}
],
id: NodeId(
2
),
node: Use(
Spanned {
node: ViewPathGlob(
path(std::prelude::v1)
),
span: Span { lo: BytePos(0), hi: BytePos(0), expn_id: ExpnId(4294967295) }
}
),
vis: Inherited,
span: Span { lo: BytePos(0), hi: BytePos(0), expn_id: ExpnId(0) }
},
Item {
ident: std#0,
attrs: [
Attribute {
id: AttrId(
0
),
style: Outer,
value: MetaItem {
name: macro_use(83),
node: Word,
span: Span { lo: BytePos(0), hi: BytePos(0), expn_id: ExpnId(4294967295) }
},
is_sugared_doc: false,
span: Span { lo: BytePos(0), hi: BytePos(0), expn_id: ExpnId(4294967295) }
}
],
id: NodeId(
3
),
node: ExternCrate(
Some(
std(82)
)
),
vis: Inherited,
span: Span { lo: BytePos(0), hi: BytePos(0), expn_id: ExpnId(4294967295) }
},
Item {
ident: main#0,
attrs: [],
id: NodeId(
4
),
node: Fn(
FnDecl {
inputs: [],
output: Default(
Span { lo: BytePos(10), hi: BytePos(10), expn_id: ExpnId(4294967295) }
),
variadic: false
},
Normal,
Spanned {
node: NotConst,
span: Span { lo: BytePos(0), hi: BytePos(2), expn_id: ExpnId(4294967295) }
},
Rust,
Generics {
lifetimes: [],
ty_params: [],
where_clause: WhereClause {
id: NodeId(
5
),
predicates: []
},
span: Span { lo: BytePos(0), hi: BytePos(0), expn_id: ExpnId(4294967295) }
},
Block {
stmts: [],
id: NodeId(
6
),
rules: Default,
span: Span { lo: BytePos(10), hi: BytePos(12), expn_id: ExpnId(4294967295) }
}
),
vis: Inherited,
span: Span { lo: BytePos(0), hi: BytePos(12), expn_id: ExpnId(4294967295) }
}
]
},
attrs: [],
span: Span { lo: BytePos(0), hi: BytePos(11), expn_id: ExpnId(4294967295) },
exported_macros: []
}

@nrc

nrc commented Nov 30, 2016

Copy link
Copy Markdown
Member

Could this be a pretty printing option rather than a -Z option?

@spinda

Copy link
Copy Markdown
ContributorAuthor

Is that the right place to put it? The --pretty options seem focused on producing valid Rust source. I placed ast and ast-noexpand under -Z because that's where ast-json and ast-json-noexpand already are. But I can move them under --pretty if that's best.

@spinda

Copy link
Copy Markdown
ContributorAuthor

Ah, I see now that there's already --unpretty=mir, --unpretty=hir, and similar. I'll see if I can move this to --unpretty.

@spinda

Copy link
Copy Markdown
ContributorAuthor

This is working now:

fnmain(){println!("Hello, World!");go(there);}
$ rustc -Zunstable-options --unpretty=ast test.rs 11:39:24
Crate {
module: Mod {
inner: Span { lo: BytePos(0), hi: BytePos(59), expn_id: ExpnId(4294967295) },
items: [
Item {
ident: main#0,
attrs: [],
id: NodeId(
4294967295
),
node: Fn(
FnDecl {
inputs: [],
output: Default(
Span { lo: BytePos(10), hi: BytePos(10), expn_id: ExpnId(4294967295) }
),
variadic: false
},
Normal,
Spanned {
node: NotConst,
span: Span { lo: BytePos(0), hi: BytePos(2), expn_id: ExpnId(4294967295) }
},
Rust,
Generics {
lifetimes: [],
ty_params: [],
where_clause: WhereClause {
id: NodeId(
4294967295
),
predicates: []
},
span: Span { lo: BytePos(0), hi: BytePos(0), expn_id: ExpnId(4294967295) }
},
Block {
stmts: [
stmt(4294967295: println!("Hello, World!");),
stmt(4294967295: go(there);)
],
id: NodeId(
4294967295
),
rules: Default,
span: Span { lo: BytePos(10), hi: BytePos(59), expn_id: ExpnId(4294967295) }
}
),
vis: Inherited,
span: Span { lo: BytePos(0), hi: BytePos(59), expn_id: ExpnId(4294967295) }
}
]
},
attrs: [],
span: Span { lo: BytePos(0), hi: BytePos(58), expn_id: ExpnId(4294967295) },
exported_macros: []
}

However, the printing of statements isn't very useful for exploring the AST:

 stmts: [
stmt(4294967295: println!("Hello, World!");),
stmt(4294967295: go(there);)
],

This is because Stmt, Expr, and similar have explicit Debug instances that use the Rust pretty-printer:

impl fmt::DebugforStmt{fnfmt(&self,f:&mut fmt::Formatter) -> fmt::Result{write!(f,"stmt({}: {})",self.id.to_string(), pprust::stmt_to_string(self))}}

Is there a good way around this?

@nrc

nrc commented Dec 2, 2016

Copy link
Copy Markdown
Member

I don't think there is a good way around this. You could try creating your own trait and use specialisation so that by default is calls the debug implementation, but you override this for statements and expressions. I'm not sure if that will work though, it might fall foul of the specialisation/coherence rules.

You could also just change the debug impls for Stmt and Expr, I'm not sure how the rest of @rust-lang/compiler feel, but I'd prefer Debug to contain what you want and maybe implement Display for the current stuff.

@eddyb

eddyb commented Dec 2, 2016

Copy link
Copy Markdown
Contributor

Definitely agree with keeping Debug auto-generated where possible.

@nikomatsakis

Copy link
Copy Markdown
Contributor

Hmm. I...agree with auto-generating Debug, though I do find it really useful to be able to search the RUST_LOG output for specific expressions. I do this all the time when tracking down type-checking or region-inference failures. Probably the solution is to change a lot of those debug logs to {} not {:?} though.

@steveklabnik

Copy link
Copy Markdown
Contributor

Ping! It's been a month since there's been any activity on this PR; can we move it forward?

@sanxiyn

Copy link
Copy Markdown
Contributor

I think the main consideration here is whether --unpretty ast is useful without changing Debug impl of Stmt.

@bors

bors commented Jan 21, 2017

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (presumably #39199) made this pull request unmergeable. Please resolve the merge conflicts.

-Zast behaves like -Zast-json but uses {:#?} pretty-printing instead of
converting to JSON.
-Zast-noexpand is the pretty-printing counterpart to -Zast-json-noexpand.
@spinda

Copy link
Copy Markdown
ContributorAuthor

I don't think I'm familiar enough with the rustc codebase to go through and audit all the uses of {:?} vs {}. I'd be okay with closing this.

@mrhota

Copy link
Copy Markdown
Contributor

@spinda@steveklabnik can someone close this?

@nikomatsakis

Copy link
Copy Markdown
Contributor

Will do.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@spinda@rust-highfive@eddyb@sanxiyn@nrc@nikomatsakis@steveklabnik@bors@mrhota