Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 3 commits into from

Conversation

spinda
Copy link

@spinda spinda commented Nov 30, 2016

-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
Collaborator

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
Member

eddyb commented Nov 30, 2016

r? @nrc

@rust-highfive rust-highfive assigned nrc and unassigned eddyb Nov 30, 2016
@sanxiyn
Copy link
Member

sanxiyn commented Nov 30, 2016

I'd like to see an example output.

@spinda
Copy link
Author

spinda commented Nov 30, 2016

$ 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
Copy link
Member

nrc commented Nov 30, 2016

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

@spinda
Copy link
Author

spinda commented Dec 1, 2016

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
Author

spinda commented Dec 1, 2016

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
Author

spinda commented Dec 1, 2016

This is working now:

fn main() {
    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::Debug for Stmt {
    fn fmt(&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
Copy link
Member

nrc commented Dec 2, 2016

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
Copy link
Member

eddyb commented Dec 2, 2016

Definitely agree with keeping Debug auto-generated where possible.

@nikomatsakis
Copy link
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
Member

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

@sanxiyn
Copy link
Member

sanxiyn commented Jan 6, 2017

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

@bors
Copy link
Contributor

bors commented Jan 21, 2017

☔ 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
Author

spinda commented Jan 26, 2017

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
Contributor

mrhota commented Feb 23, 2017

@spinda @steveklabnik can someone close this?

@nikomatsakis
Copy link
Contributor

Will do.

Sign up for free to 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