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

Better tokentree pretty printer #39079

Closed
wants to merge 4 commits into from
Closed

Conversation

bjorn3
Copy link
Member

@bjorn3 bjorn3 commented Jan 15, 2017

The example at the homepage now print like this when parsed as tokentrees:

fn main()
{
    let program = "+ + * - /";
    let mut accumulator = 0;
    . .. ...;
    a.b;
    .. .a;
    a. ..;
    for token in program.chars()
    {
        match token {
                        '+' => accumulator += 1 , '-' => accumulator -= 1 , '*' => accumulator *= 2 , '/' => accumulator /= 2 , _ => {}

                    }

    }

    println !("The program \"{}\" calculates the value {}" , program , accumulator );
}

@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 @pnkfelix (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.

match *tk {
parse::token::DocComment(..) => {
hardbreak(&mut self.s)
self.print_tts(unsafe{::std::slice::from_raw_parts(tt as *const _, 1)}); // Unsafe block to convert ref to single item slice ref without cloning
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this allowed and is there a other way to do this?

hardbreak(&mut self.s)
/// Forwards to print_tts
pub fn print_tt(&mut self, tt: &ast::TokenTree) -> io::Result<()> {
self.print_tts(unsafe{::std::slice::from_raw_parts(tt as *const _, 1)}); // Unsafe block to convert ref to single item slice ref without cloning
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this allowed and is there a other way to do this?

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 17, 2017

What is going on here while compiling stage1?

   Compiling fmt_macros v0.0.0 (file:///checkout/src/libfmt_macros)
error: no rules expected the token `static`
   --> /checkout/src/liblog/lib.rs:215:5
    |
215 | static LOCAL_LOGGER: RefCell<Option<Box<Logger + Send>>> = {
    | ^^^^^^

@steveklabnik
Copy link
Member

/cc @nrc @rust-lang/compiler

word(&mut self.s, &token_to_string(token))?;
match (token, tts_iter.peek()){
(&parse::token::DocComment(..), _) => hardbreak(&mut self.s)?, // ///abc
// ^^^---
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this comment is not what you wanted...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to fix indentation :(

Copy link
Member

@pnkfelix pnkfelix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ... pretty sure we don't approve PR's with merge commits in them. So you probably need to clean up your revision history; it may suffice to just rebase.

@pnkfelix
Copy link
Member

Also, I'm not yet sure what this is doing, because I don't remember what the token-tree printer was, what it used to look like, whether it was good/bad, et cetera.

I say this mostly as a way of saying "I'm giving feedback on relatively minor points, but you should not interpret that as giving any indication about my feelings on the PR as a whole."

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 17, 2017

It is used in error mesages when for example a proc macro outputs fn a+b(){} which doesnt parse as far as i known. Will rebase soon.

@nikomatsakis
Copy link
Contributor

Pre-existing, but it seems like it'd be good to have some tests of this, no?

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 17, 2017

Will add them when i rebase.

@bjorn3 bjorn3 force-pushed the pp-tokentree branch 2 times, most recently from 5daf971 to 757ee29 Compare January 17, 2017 18:12
@bjorn3
Copy link
Member Author

bjorn3 commented Jan 17, 2017

Messed up a bit with rebasing. will have to do the fix build cycle from before again :(

Copy link
Member Author

@bjorn3 bjorn3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few questions

for token in program.chars()
{
match token {
'+' => accumulator += 1 , '-' => accumulator -= 1 , '*' => accumulator *= 2 , '/' => accumulator /= 2 , _ => {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I do here to fix the tidy error? Can't put a newline because I would have another string then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignored it

}
}
// Unsafe block to convert ref to single item slice ref without cloning
self.print_tts(unsafe{::std::slice::from_raw_parts(tt as *const _, 1)})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this allowed and is there a other way to do this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is worth using unsafe.
I would just clone the token tree (i.e. self.print_tts(&[tt.clone()])) -- it isn't a deep clone, just a copy plus an Rc ref-count update. You could also refactor print_tts to take an Iterator<Item=&TokenTree>.

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 17, 2017

Build fails due to bug in this tt pretty printer.

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 19, 2017

I had a break which I meant to break out of the match not the loop 🤦. This meant skipping a great part of the thread_local macro.

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 19, 2017

Accidentally added a nul byte.

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 19, 2017

Why do the tests fail?

@bjorn3 bjorn3 force-pushed the pp-tokentree branch 2 times, most recently from ad4eb97 to f60ed6f Compare January 20, 2017 17:19
@bjorn3
Copy link
Member Author

bjorn3 commented Jan 20, 2017

Rebased

space(&mut self.s)?;
word(&mut self.s, ")")?;
if let Some(ref tk) = seq.separator {
word(&mut self.s, &token_to_string(tk))?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no way to print a single token? I guess the same is a problem above.

word(&mut self.s, ";")?;
hardbreak(&mut self.s)?;
} else {
word(&mut self.s, &token_to_string(token))?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sad. I guess it's pre-existing, though.

@eddyb
Copy link
Member

eddyb commented Jan 20, 2017

I have no suggestions about the bug other than "comment out the special cases and see if that still fails".
If this is not a very weird bug, it might reproduce with ./x.py test --stage 1 src/test/codegen.

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 22, 2017

@eddyb I can't reproduce the travis error on local mac osx. Only run-fail/assert-macro-explicit.rs fails due to a added space.

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 22, 2017

Are the location of stringify! spaces and newlines (semi)guaranteed. If so this is a breaking change.

@eddyb
Copy link
Member

eddyb commented Jan 22, 2017

@bjorn3 Alright, you can still sort of abuse Travis to test it for you. I'd suggest pushing a single commit that comments out all the special cases but leaves your own code for the default cases.
If that works, you can then bisect which special case breaks. If it doesn't... You can bisect to the original.

@bors
Copy link
Contributor

bors commented Jan 28, 2017

📌 Commit cc4eb99 has been approved by pnkfelix

@bors
Copy link
Contributor

bors commented Jan 28, 2017

⌛ Testing commit cc4eb99 with merge ded091d...

@alexcrichton
Copy link
Member

@bors: retry

  • prioritizing rollup

@bors
Copy link
Contributor

bors commented Jan 28, 2017

⌛ Testing commit cc4eb99 with merge cbf842f...

@bors
Copy link
Contributor

bors commented Jan 28, 2017

💔 Test failed - status-appveyor

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 29, 2017

@pnkfelix test passes.

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 29, 2017

Squashed

+ 3c7226b...0409508 pp-tokentree -> pp-tokentree (forced update) (just in case)

@pnkfelix
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jan 30, 2017

📌 Commit 0409508 has been approved by pnkfelix

@bors
Copy link
Contributor

bors commented Jan 30, 2017

⌛ Testing commit 0409508 with merge a62accf...

@bors
Copy link
Contributor

bors commented Jan 30, 2017

💔 Test failed - status-travis

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 30, 2017

I think i will make a script to update the failing pretty print tests:

test result: FAILED. 2462 passed; 122 failed; 27 ignored; 0 measured

@pnkfelix
Copy link
Member

pnkfelix commented Feb 1, 2017

@bjorn3 travis says tidy checks are failling.

'+' => accumulator += 1 , '-' => accumulator -= 1 , '*' => accumulator *= 2 , '/' => accumulator /= 2 , _ => {}
match token
{
\'+\' => accumulator += 1 , \'-\' => accumulator -= 1 , \'*\' => accumulator *= 2 , \'/\' => accumulator /= 2 , _ => {}
Copy link
Member

@eddyb eddyb Feb 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two interesting things here:

  • { should never start a new line before it, always after
    • it gets trickier sometimes thought (} and ; come to be mind)
    • the whole reason there's a pp.rs is for automating breaks
    • but I'm not sure the algorithm is appropriate for Rust or bug-free
  • , should never have a space before it, always after

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont really understand print::pp to be honest. Documentation would be really appreciated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who does? shrug

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found docs at the Printer struct. Opened #39557 with a few doc improvements.

@bors
Copy link
Contributor

bors commented Mar 1, 2017

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

@pnkfelix
Copy link
Member

@bjorn3 are you going to be able to address the problems on this PR? (From what I can tell, Travis was never satisfied with it...?) Or should I close it to clean up the queue?

@bjorn3
Copy link
Member Author

bjorn3 commented Mar 20, 2017

If I want to retry this in the future I will make a new pr.

@bjorn3 bjorn3 closed this Mar 20, 2017
@bjorn3 bjorn3 deleted the pp-tokentree branch December 17, 2017 11:25
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