about summary refs log tree commit diff
path: root/tests/ui/macros/stringify.rs
AgeCommit message (Collapse)AuthorLines
2023-12-11Rollup merge of #118726 - dtolnay:matchguardlet, r=compiler-errorsMatthias Krüger-1/+20
Do not parenthesize exterior struct lit inside match guards Before this PR, the AST pretty-printer injects parentheses around expressions any time parens _could_ be needed depending on what else is in the code that surrounds that expression. But the pretty-printer did not pass around enough context to understand whether parentheses really _are_ needed on any particular expression. As a consequence, there are false positives where unneeded parentheses are being inserted. Example: ```rust #![feature(if_let_guard)] macro_rules! pp { ($e:expr) => { stringify!($e) }; } fn main() { println!("{}", pp!(match () { () if let _ = Struct {} => {} })); } ``` **Before:** ```console match () { () if let _ = (Struct {}) => {} } ``` **After:** ```console match () { () if let _ = Struct {} => {} } ``` This PR introduces a bit of state that is passed across various expression printing methods to help understand accurately whether particular situations require parentheses injected by the pretty printer, and it fixes one such false positive involving match guards as shown above. There are other parenthesization false positive cases not fixed by this PR. I intend to address these in follow-up PRs. For example here is one: the expression `{ let _ = match x {} + 1; }` is pretty-printed as `{ let _ = (match x {}) + 1; }` despite there being no reason for parentheses to appear there.
2023-12-11Add a few cases with wonky formatting to `stringify.rs` test.Nicholas Nethercote-0/+4
Because the spacing-based pretty-printing partially preserves that.
2023-12-11Add spacing information to delimiters.Nicholas Nethercote-15/+15
This is an extension of the previous commit. It means the output of something like this: ``` stringify!(let a: Vec<u32> = vec![];) ``` goes from this: ``` let a: Vec<u32> = vec![] ; ``` With this PR, it now produces this string: ``` let a: Vec<u32> = vec![]; ```
2023-12-11Improve `print_tts` by changing `tokenstream::Spacing`.Nicholas Nethercote-192/+144
`tokenstream::Spacing` appears on all `TokenTree::Token` instances, both punct and non-punct. Its current usage: - `Joint` means "can join with the next token *and* that token is a punct". - `Alone` means "cannot join with the next token *or* can join with the next token but that token is not a punct". The fact that `Alone` is used for two different cases is awkward. This commit augments `tokenstream::Spacing` with a new variant `JointHidden`, resulting in: - `Joint` means "can join with the next token *and* that token is a punct". - `JointHidden` means "can join with the next token *and* that token is a not a punct". - `Alone` means "cannot join with the next token". This *drastically* improves the output of `print_tts`. For example, this: ``` stringify!(let a: Vec<u32> = vec![];) ``` currently produces this string: ``` let a : Vec < u32 > = vec! [] ; ``` With this PR, it now produces this string: ``` let a: Vec<u32> = vec![] ; ``` (The space after the `]` is because `TokenTree::Delimited` currently doesn't have spacing information. The subsequent commit fixes this.) The new `print_tts` doesn't replicate original code perfectly. E.g. multiple space characters will be condensed into a single space character. But it's much improved. `print_tts` still produces the old, uglier output for code produced by proc macros. Because we have to translate the generated code from `proc_macro::Spacing` to the more expressive `token::Spacing`, which results in too much `proc_macro::Along` usage and no `proc_macro::JointHidden` usage. So `space_between` still exists and is used by `print_tts` in conjunction with the `Spacing` field. This change will also help with the removal of `Token::Interpolated`. Currently interpolated tokens are pretty-printed nicely via AST pretty printing. `Token::Interpolated` removal will mean they get printed with `print_tts`. Without this change, that would result in much uglier output for code produced by decl macro expansions. With this change, AST pretty printing and `print_tts` produce similar results. The commit also tweaks the comments on `proc_macro::Spacing`. In particular, it refers to "compound tokens" rather than "multi-char operators" because lifetimes aren't operators.
2023-12-08Do not parenthesize exterior struct lit inside match guardsDavid Tolnay-1/+1
2023-12-08Add if_let_guard and let_chains pretty printer testsDavid Tolnay-1/+20
2023-11-29Add `never_patterns` feature gateNadrieril-0/+5
2023-11-08More tests for token stream pretty-printing with adjacent punctuation.Nicholas Nethercote-0/+29
We currently do the wrong thing on a lot of these. The next commit will fix things.
2023-11-08Clarify `space_between`.Nicholas Nethercote-0/+2
To avoid `!matches!(...)`, which is hard to think about. Instead every case now uses direct pattern matching and returns true or false. Also add a couple of cases to the `stringify.rs` test that currently print badly.
2023-10-24Augment `stringify.rs` test some more.Nicholas Nethercote-40/+38
By making some case more complex, adding some new cases, tweaking formatting, and removing unnecessary `rustfmt` attributes.
2023-10-24Augment `stringify.rs` test.Nicholas Nethercote-2/+47
By adding tests (or placeholders, or comments) for missing AST variants.
2023-10-24Redo `stringify.rs` test.Nicholas Nethercote-539/+412
Currently it only tests AST pretty-printing. This commit changes it to run every example through both AST pretty-printing and TokenStream pretty-printing. This makes it clear where there two pretty-printing approaches produce different results.
2023-10-20s/generator/coroutine/Oli Scherer-1/+1
2023-10-03Gate against auto traits pre-expansionMichael Goulet-0/+1
2023-05-01Rip it outNilstrieb-2/+1
My type ascription Oh rip it out Ah If you think we live too much then You can sacrifice diagnostics Don't mix your garbage Into my syntax So many weird hacks keep diagnostics alive Yet I don't even step outside So many bad diagnostics keep tyasc alive Yet tyasc doesn't even bother to survive!
2023-03-12Remove uses of `box_syntax` in rustc and toolsclubby789-4/+0
2023-02-07Expand const-if-const trait bounds correctlyMichael Goulet-3/+3
2023-01-11Move /src/test to /testsAlbert Larsan-0/+889