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

Minor refactorings & implement --pretty expanded,hygiene to see syntax context & gensym information #16419

Merged
merged 3 commits into from
Aug 30, 2014

Conversation

huonw
Copy link
Member

@huonw huonw commented Aug 11, 2014

Different Identifiers and Names can have identical textual representations, but different internal representations, due to the macro hygiene machinery (syntax contexts and gensyms). This provides a way to see these internals by compiling with --pretty expanded,hygiene.

This is useful for debugging & hacking on macros (e.g. diagnosing #15750 likely would've been faster with this functionality).

E.g.

#![feature(macro_rules)]
// minimal junk
#![no_std]

macro_rules! foo {
    ($x: ident) => { y + $x }
}

fn bar() {
    foo!(x)
}
#![feature(macro_rules)]
// minimal junk
#![no_std]


fn bar /* 61#0 */() { y /* 60#2 */ + x /* 58#3 */ }

@huonw
Copy link
Member Author

huonw commented Aug 11, 2014

This isn't as informative as it could be (#16420), but the gensym information is useful and the syntax context stuff is still useful as a basic positive test (i.e. if two identifiers with the same name also have the same syntax context, then they're the same).

@pnkfelix
Copy link
Member

(I will review)

@pnkfelix
Copy link
Member

@huonw so while I regard this as a net-win (and thus I r-plused it), I idly wonder: What is more important for a person using this -- that the names correspond to the names that would be printed via things like stringify!, with the hygiene information only rendered in a comment? Or would it be better if we tried to maintain an invariant that if the input code compiled successfully, then the output of pretty printing will also compile successfully, which would imply that we should in fact render the hygiene information using a semantically-significant format, such as e.g. re-rendering your example

fn bar_61_0 /* 61#0 */() { y /* 60#2 */ + x /* 58#3 */ }

as this instead:

#![feature(non_ascii_idents)]

fn barʃ61ʃ0() { yʃ60ʃ2 + xʃ58ʃ3 }

(where I have chosen to put a non-ascii character as the marker for where the meta-data starts to reduce the chance that it will end up actually colliding with an actual variable from the source text -- another, more robust option would be to dynamically choose between _, __, ___, etc until we find one that will not yield collisions with the identifiers from the original text.)

@huonw
Copy link
Member Author

huonw commented Aug 14, 2014

Ah, that's an interesting point. I hadn't thought of it, and it does seem like it could be better than just comments.

(On that note, if we want pretty printing to always compile & not change semantics, then --pretty expanded would need to do something like this too.)

@pnkfelix
Copy link
Member

Nonetheless we can adopt this PR for now and put my suggestion down as a back burner task

huonw added 2 commits August 29, 2014 18:05
There's a lot of it, and it's a fairly well-defined/separate chunk of
code, so it might as well be separate.
The type in the `impl` is now in the module with the trait.
@huonw huonw force-pushed the pretty-expanded-hygiene branch from ec2680b to 7d7d907 Compare August 29, 2014 08:15
@huonw
Copy link
Member Author

huonw commented Aug 29, 2014

re-r? @pnkfelix

I have just changed the test to avoid checking the exact numbers, since it makes the test very sensitive to compiler internals. I'm not sure if the new implementation of the test is reasonable.

@huonw huonw force-pushed the pretty-expanded-hygiene branch from 7d7d907 to 1e45d84 Compare August 29, 2014 08:40
@huonw huonw force-pushed the pretty-expanded-hygiene branch from 1e45d84 to d79047e Compare August 29, 2014 12:21
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Aug 29, 2014
`--pretty expanded,hygiene` is helpful with debugging macro issues,
since two identifiers/names can be textually the same, but different
internally (resulting in weird "undefined variable" errors).
@huonw huonw force-pushed the pretty-expanded-hygiene branch from d79047e to 32e4371 Compare August 30, 2014 06:50
bors added a commit that referenced this pull request Aug 30, 2014
Different Identifiers and Names can have identical textual representations, but different internal representations, due to the macro hygiene machinery (syntax contexts and gensyms). This provides a way to see these internals by compiling with `--pretty expanded,hygiene`.

This is useful for debugging & hacking on macros (e.g. diagnosing #15750 likely would've been faster with this functionality).

E.g. 

```rust
#![feature(macro_rules)]
// minimal junk
#![no_std]

macro_rules! foo {
    ($x: ident) => { y + $x }
}

fn bar() {
    foo!(x)
}
```
```rust
#![feature(macro_rules)]
// minimal junk
#![no_std]


fn bar /* 61#0 */() { y /* 60#2 */ + x /* 58#3 */ }
```
@bors bors closed this Aug 30, 2014
@bors bors merged commit 32e4371 into rust-lang:master Aug 30, 2014
Centril added a commit to Centril/rust that referenced this pull request Jan 15, 2019
Add missing unpretty option help message

There are some missing help messages that is printed `ructc -Zunpretty help` and receiving invalid option.

related with rust-lang#16419, rust-lang#45721, rust-lang#21085, rust-lang#31916
Centril added a commit to Centril/rust that referenced this pull request Jan 19, 2019
Add missing unpretty option help message

There are some missing help messages that is printed `ructc -Zunpretty help` and receiving invalid option.

related with rust-lang#16419, rust-lang#45721, rust-lang#21085, rust-lang#31916
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.

3 participants