Commit Graph

156 Commits

Author SHA1 Message Date
David Tolnay
9e794d7de3
Eliminate offset number from Fits frames
PrintStackElems with pbreak=PrintStackBreak::Fits always carried a
meaningless value offset=0. We can combine the two types PrintStackElem
+ PrintStackBreak into one PrintFrame enum that stores offset only for
Broken frames.
2022-01-19 19:04:34 -08:00
David Tolnay
65dd67096e
Touch up print_string 2022-01-19 19:04:33 -08:00
David Tolnay
d5f15a8c18
Replace all single character variable names 2022-01-19 19:04:32 -08:00
David Tolnay
ea23a1fac7
Combine advance_left matches 2022-01-19 19:04:31 -08:00
David Tolnay
ae75ba692a
Inline print into advance_left 2022-01-19 19:04:30 -08:00
David Tolnay
d2eb46cfec
Simplify advance_left 2022-01-19 19:03:53 -08:00
David Tolnay
351011ec3f
Simplify left_total tracking 2022-01-19 19:02:56 -08:00
David Tolnay
d981c5b354
Eliminate a token clone from advance_left 2022-01-19 19:02:25 -08:00
David Tolnay
d81740ed2a
Grow scan_stack in the conventional direction
The pretty printer algorithm involves 2 VecDeques: a ring-buffer of
tokens and a deque of ring-buffer indices. Confusingly, those two deques
were being grown in opposite directions for no good reason. Ring-buffer
pushes would go on the "back" of the ring-buffer (i.e. higher indices)
while scan_stack pushes would go on the "front" (i.e. lower indices).
This commit flips the scan_stack accesses to grow the scan_stack and
ring-buffer in the same direction, where push does the same
operation as a Vec push i.e. inserting on the high-index end.
2022-01-19 18:32:18 -08:00
David Tolnay
eec6016ec3
Delete unused Display for pretty printer Token 2022-01-19 18:31:36 -08:00
Matthias Krüger
fe93f08051
Rollup merge of #93065 - dtolnay:ringbuffer, r=lcnr
Pretty printer algorithm revamp step 2

This PR follows #92923 as a second chunk of modernizations backported from https://github.com/dtolnay/prettyplease into rustc_ast_pretty.

I've broken this up into atomic commits that hopefully are sensible in isolation. At every commit, the pretty printer is compilable and has runtime behavior that is identical to before and after the PR. None of the refactoring so far changes behavior.

The general theme of this chunk of commits is: the logic in the old pretty printer is doing some very basic things (pushing and popping tokens on a ring buffer) but expressed in a too-low-level way that I found makes it quite complicated/subtle to reason about. There are a number of obvious invariants that are "almost true" -- things like `self.left == self.buf.offset` and `self.right == self.buf.offset + self.buf.data.len()` and `self.right_total == self.left_total + self.buf.data.sum()`. The reason these things are "almost true" is the implementation tends to put updating one side of the invariant unreasonably far apart from updating the other side, leaving the invariant broken while unrelated stuff happens in between. The following code from master is an example of this:

e5e2b0be26/compiler/rustc_ast_pretty/src/pp.rs (L314-L317)

In this code the `advance_right` is reserving an entry into which to write a next token on the right side of the ring buffer, the `check_stack` is doing something totally unrelated to the right boundary of the ring buffer, and the `scan_push` is actually writing the token we previously reserved space for. Much of what this PR is doing is rearranging code to shrink the amount of stuff in between when an invariant is broken to when it is restored, until the whole thing can be factored out into one indivisible method call on the RingBuffer type.

The end state of the PR is that we can entirely eliminate `self.left` (because it's now just equal to `self.buf.offset` always) and `self.right` (because it's equal to `self.buf.offset + self.buf.data.len()` always) and the whole `Token::Eof` state which used to be the value of tokens that have been reserved space for but not yet written.

I found without these changes the pretty printer implementation to be hard to reason about and I wasn't able to confidently introduce improvements like trailing commas in `prettyplease` until after this refactor. The logic here is 43 years old at this point (Graydon translated it as directly as possible from the 1979 pretty printing paper) and while there are advantages to following the paper as closely as possible, in `prettyplease` I decided if we're going to adapt the algorithm to work better for Rust syntax, it was worthwhile making it easier to follow than the original.
2022-01-19 19:19:51 +01:00
Matthias Krüger
420ada6f8a
Rollup merge of #92920 - dtolnay:printtidy, r=cjgillot
Move expr- and item-related pretty printing functions to modules

Currently *compiler/rustc_ast_pretty/src/pprust/state.rs* is 2976 lines on master. The `tidy` limit is 3000, which is blocking #92243.

This PR adds a `mod expr;` and `mod item;` to move logic related to those AST nodes out of the single huge file.
2022-01-19 10:42:18 +01:00
David Tolnay
4d3faae5cd
Eliminate left and right cursors in favor of ring buffer 2022-01-18 20:19:44 -08:00
David Tolnay
cc66a7ff20
Eliminate eof token state 2022-01-18 20:08:52 -08:00
David Tolnay
6e8b06015e
Simplify the buffer push done by scan_break 2022-01-18 19:35:43 -08:00
David Tolnay
fe5c4eab2d
Eliminate a check_stack call on an empty scan stack 2022-01-18 19:23:22 -08:00
David Tolnay
377c9dbabf
Index a single time in check_stack 2022-01-18 19:21:18 -08:00
David Tolnay
a37d272892
Implement check_stack nonrecursively 2022-01-18 19:20:33 -08:00
David Tolnay
0490e43422
Implement check_stream nonrecursively 2022-01-18 19:19:18 -08:00
David Tolnay
947a09a4a8
Replace if + unwrap with if let in check_stack 2022-01-18 19:18:47 -08:00
David Tolnay
80139a0f02
Ensure Printer buf is always indexed using self.left or self.right 2022-01-18 19:18:04 -08:00
David Tolnay
ae28ec5a9c
Inline Printer's scan_pop_bottom method 2022-01-18 19:16:33 -08:00
David Tolnay
2a14275500
Inline Printer's scan_top method 2022-01-18 19:16:32 -08:00
David Tolnay
e20d5abdfb
Inline Printer's scan_pop method 2022-01-18 19:16:32 -08:00
David Tolnay
50d722a691
Simplify ring buffer pushes 2022-01-18 19:07:12 -08:00
David Tolnay
e219b2b5f9
Inline Printer's scan_push method 2022-01-18 19:04:12 -08:00
David Tolnay
fdb95f54e8
Inline Printer's advance_right method 2022-01-18 19:02:49 -08:00
David Tolnay
07a0325137
Move item-related pretty printing functions to module 2022-01-18 12:39:20 -08:00
David Tolnay
b1605643e3
Move expr-related pretty printing functions to module 2022-01-18 12:39:19 -08:00
David Tolnay
fe86dcf0cc
Delete pretty printer tracing 2022-01-18 12:33:42 -08:00
bors
7bc7be860f Auto merge of #87648 - JulianKnodt:const_eq_constrain, r=oli-obk
allow eq constraints on associated constants

Updates #70256

(cc `@varkor,` `@Centril)`
2022-01-18 09:58:39 +00:00
David Tolnay
7b5b3cf82c
Abstract the pretty printer's ringbuffer to be infinitely sized 2022-01-17 12:08:08 -08:00
kadmin
67f56671d0 Use Term in ProjectionPredicate
ProjectionPredicate should be able to handle both associated types and consts so this adds the
first step of that. It mainly just pipes types all the way down, not entirely sure how to handle
consts, but hopefully that'll come with time.
2022-01-17 17:44:56 +00:00
kadmin
fb57b7518d Add term
Instead of having a separate enum variant for types and consts have one but have either a const
or type.
2022-01-17 17:20:57 +00:00
kadmin
0765999622 add eq constraints on associated constants 2022-01-17 17:20:57 +00:00
bors
a34c079752 Auto merge of #92816 - tmiasko:rm-llvm-asm, r=Amanieu
Remove deprecated LLVM-style inline assembly

The `llvm_asm!` was deprecated back in #87590 1.56.0, with intention to remove
it once `asm!` was stabilized, which already happened in #91728 1.59.0. Now it
is time to remove `llvm_asm!` to avoid continued maintenance cost.

Closes #70173.
Closes #92794.
Closes #87612.
Closes #82065.

cc `@rust-lang/wg-inline-asm`

r? `@Amanieu`
2022-01-17 09:40:29 +00:00
Matthias Krüger
216ce7c519
Rollup merge of #92921 - dtolnay:printernew, r=wesleywiser
Rename Printer constructor from mk_printer() to Printer::new()

The original naming is left over from 2011 which was before impl blocks and associated functions existed.

21313d623a/src/comp/pretty/pp.rs
2022-01-17 06:08:16 +01:00
Matthias Krüger
9527533408
Rollup merge of #92487 - dtolnay:traitalias, r=matthewjasper
Fix unclosed boxes in pretty printing of TraitAlias

This was causing trait aliases to not even render at all in stringified / pretty printed output.

```rust
macro_rules! repro {
    ($item:item) => {
        stringify!($item)
    };
}

fn main() {
    println!("{:?}", repro!(pub trait Trait<T> = Sized where T: 'a;));
}
```

Before:&ensp;`""`
After:&ensp;`"pub trait Trait<T> = Sized where T: 'a;"`

The fix is copied from how `head`/`end` for `ItemKind::Use`, `ItemKind::ExternCrate`, and `ItemKind::Mod` are all done in the pretty printer:

dd3ac41495/compiler/rustc_ast_pretty/src/pprust/state.rs (L1178-L1184)
2022-01-16 16:58:10 +01:00
David Tolnay
953da9832d
Rename Printer constructor from mk_printer() to Printer::new() 2022-01-14 21:12:39 -08:00
Tomasz Miąsko
000b36c505 Remove deprecated LLVM-style inline assembly 2022-01-12 18:51:31 +01:00
Eric Huss
81c515bf77
Rollup merge of #92336 - dtolnay:printstateself, r=michaelwoerister
Remove &self from PrintState::to_string

The point of `PrintState::to_string` is to create a `State` and evaluate the caller's closure on it:

e9fbe79292/compiler/rustc_ast_pretty/src/pprust/state.rs (L868-L872)

Making the caller *also* construct and pass in a `State`, which is then ignored, was confusing.
2022-01-07 20:20:56 -08:00
Matthias Krüger
1a8f69826c
Rollup merge of #92417 - dtolnay:printimpl, r=jackh726
Fix spacing and ordering of words in pretty printed Impl

Follow-up to #92238 fixing one of the FIXMEs.

```rust
macro_rules! repro {
    ($item:item) => {
        stringify!($item)
    };
}

fn main() {
    println!("{}", repro!(impl<T> Struct<T> {}));
    println!("{}", repro!(impl<T> const Trait for T {}));
}
```

Before:&ensp;`impl <T> Struct<T> {}`
After:&ensp;`impl<T> Struct<T> {}`

Before:&ensp;`impl const <T> Trait for T {}` 😿
After:&ensp;`impl<T> const Trait for T {}`
2022-01-06 23:15:16 +01:00
Matthias Krüger
df921190f3
Rollup merge of #92418 - dtolnay:emptystructpat, r=michaelwoerister
Fix spacing in pretty printed PatKind::Struct with no fields

Follow-up to #92238 fixing one of the FIXMEs.

```rust
macro_rules! repro {
    ($pat:pat) => {
        stringify!($pat)
    };
}

fn main() {
    println!("{}", repro!(Struct {}));
}
```

Before:&ensp;<code>Struct&nbsp;{&nbsp;&nbsp;}</code>
After:&ensp;<code>Struct&nbsp;{}</code>
2022-01-03 14:44:20 +01:00
David Tolnay
a18f43f25d
Fix unclosed boxes in pretty printing of TraitAlias 2022-01-01 18:02:00 -08:00
Matthias Krüger
efe415878b
Rollup merge of #92420 - dtolnay:patrange, r=Mark-Simulacrum
Fix whitespace in pretty printed PatKind::Range

Follow-up to #92238 fixing one of the FIXMEs.

```rust
macro_rules! repro {
    ($pat:pat) => {
        stringify!($pat)
    };
}

fn main() {
    println!("{}", repro!(0..=1));
}
```

Before:&ensp;`0 ..=1`
After:&ensp;`0..=1`

The canonical spacing applied by rustfmt has no space after the lower expr. Rustc's parser diagnostics also do not put a space there:

df96fb166f/compiler/rustc_parse/src/parser/pat.rs (L754)
2022-01-01 10:48:56 +01:00
Matthias Krüger
682b4cbc4e
Rollup merge of #92412 - dtolnay:tryspace, r=Mark-Simulacrum
Fix double space in pretty printed TryBlock

Follow-up to #92238 fixing one of the FIXMEs.

```rust
macro_rules! repro {
    ($expr:expr) => {
        stringify!($expr)
    };
}

fn main() {
    println!("{}", repro!(try {}));
}
```

Before:&ensp;<code>try&nbsp;&nbsp;{}</code>
After:&ensp;<code>try&nbsp;{}</code>

The `head` helper already appends a space:

2b67c30bfe/compiler/rustc_ast_pretty/src/pprust/state.rs (L654-L664)

so doing `head` followed by `space` resulted in a double space:

2b67c30bfe/compiler/rustc_ast_pretty/src/pprust/state.rs (L2241-L2242)
2022-01-01 10:48:55 +01:00
David Tolnay
bc1a1ff3c8
Fix whitespace in pretty printed PatKind::Range 2021-12-29 13:02:14 -08:00
David Tolnay
8d7cf1a4ca
Fix spacing in pretty printed PatKind::Struct with no fields 2021-12-29 12:45:23 -08:00
David Tolnay
b62163515a
Move equal sign back into head ibox 2021-12-29 12:26:50 -08:00
David Tolnay
b7df49895c
Fix spacing of pretty printed const item without body 2021-12-29 12:26:50 -08:00