Commit Graph

289 Commits

Author SHA1 Message Date
Yuki Okushi
da57b3a832
Rollup merge of #96628 - joshtriplett:stabilize-then-some, r=m-ou-se
Stabilize `bool::then_some`

FCP completed in https://github.com/rust-lang/rust/issues/80967
2022-05-05 10:20:35 +09:00
bors
4c60a0ea5b Auto merge of #96546 - nnethercote:overhaul-MacArgs, r=petrochenkov
Overhaul `MacArgs`

Motivation:
- Clarify some code that I found hard to understand.
- Eliminate one use of three places where `TokenKind::Interpolated` values are created.

r? `@petrochenkov`
2022-05-04 21:16:28 +00:00
Nicholas Nethercote
99f5945f85 Overhaul MacArgs::Eq.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.

This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.

Things to note:
- Error messages have improved. Messages like this:
  ```
  unexpected token: `"bug" + "found"`
  ```
  now say "unexpected expression", which makes more sense. Although
  arbitrary expressions can exist within tokens thanks to
  `TokenKind::Interpolated`, that's not obvious to anyone who doesn't
  know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
  the value expression.
2022-05-05 07:06:12 +10:00
Josh Triplett
0fc5c524f5 Stabilize bool::then_some 2022-05-04 13:22:08 +02:00
Nicholas Nethercote
5022d764cf Show invisible delimeters (within comments) when pretty printing. 2022-05-04 10:06:06 +10:00
Elliot Roberts
7907385999 fix most compiler/ doctests 2022-05-02 17:40:30 -07:00
Scott McMurray
e094ee5f10 Add do yeet expressions to allow experimentation in nightly
Using an obviously-placeholder syntax.  An RFC would still be needed before this could have any chance at stabilization, and it might be removed at any point.

But I'd really like to have it in nightly at least to ensure it works well with try_trait_v2, especially as we refactor the traits.
2022-04-30 17:40:27 -07:00
Nicholas Nethercote
5a05b614bf Tweak print_attr_item.
This commit rearranges the `match`. The new code avoids testing for
`MacArgs::Eq` twice, at the cost of repeating the `self.print_path()`
call. I think this is worthwhile because it puts the `match` in a more
standard and readable form.
2022-04-29 15:00:32 +10:00
Vadim Petrochenkov
2733ec1be3 rustc_ast: Harmonize delimiter naming with proc_macro::Delimiter 2022-04-28 10:04:29 +03:00
Nicholas Nethercote
6b367a0532 Avoid producing NoDelim values in MacArgs::delim(). 2022-04-27 08:15:12 +10:00
Dylan DPC
22d554657d
Rollup merge of #94985 - dtolnay:constattr, r=pnkfelix
Parse inner attributes on inline const block

According to https://github.com/rust-lang/rust/pull/84414#issuecomment-826150936, inner attributes are intended to be supported *"in all containers for statements (or some subset of statements)"*.

This PR adds inner attribute parsing and pretty-printing for inline const blocks (https://github.com/rust-lang/rust/issues/76001), which contain statements just like an unsafe block or a loop body.

```rust
let _ = const {
    #![allow(...)]

    let x = ();
    x
};
```
2022-04-16 19:42:00 +02:00
Amanieu d'Antras
dc345d8bff Reimplement lowering of sym operands for asm! so that it also works with global_asm! 2022-04-14 15:32:03 +01:00
Nicholas Nethercote
364b908d57 Remove Nonterminal::NtTT.
It's only needed for macro expansion, not as a general element in the
AST. This commit removes it, adds `NtOrTt` for the parser and macro
expansion cases, and renames the variants in `NamedMatch` to better
match the new type.
2022-03-28 10:03:02 +11:00
David Tolnay
f427698c03
Parse inner attributes on inline const block 2022-03-15 17:56:59 -07:00
Jack Huey
c20b4f5584 Change syntax for TyAlias where clauses 2022-03-05 13:13:45 -05:00
David Tolnay
d1b9e4a6f2
Pretty print ItemKind::Use in rustfmt style 2022-02-07 21:51:05 -08:00
David Tolnay
40fcbbafa0
Change struct expr pretty printing to match rustfmt style 2022-02-03 10:59:35 -08:00
David Tolnay
63406ac771
Support offsetting the most recent break 2022-02-03 10:59:34 -08:00
David Tolnay
8bdf08fbed
Change pp indent to signed to allow negative indents 2022-02-03 10:59:09 -08:00
David Tolnay
0b7e1baa58
Add trailing comma support 2022-02-03 10:56:58 -08:00
Yuki Okushi
761705ebe5
Rollup merge of #93515 - dtolnay:convenience, r=davidtwco
Factor convenience functions out of main printer implementation

The pretty printer in rustc_ast_pretty has a section of methods commented "Convenience functions to talk to the printer". This PR pulls those out to a separate module. This leaves pp.rs with only the minimal API that is core to the pretty printing algorithm.

I found this separation to be helpful in https://github.com/dtolnay/prettyplease because it makes clear when changes are adding some fundamental new capability to the pretty printer algorithm vs just making it more convenient to call some already existing functionality.
2022-02-03 22:20:25 +09:00
David Tolnay
2d7ffbbc40
Factor convenience functions out of main printer implementation 2022-01-31 11:23:47 -08:00
David Tolnay
6db97b35d8
Allow any line to have at least 60 chars 2022-01-31 10:56:57 -08:00
David Tolnay
67259e74a4
Extract constant MARGIN out of Printer struct 2022-01-31 10:56:40 -08:00
David Tolnay
125c729e05
Restore a visual alignment mode for block comments 2022-01-30 19:50:54 -08:00
David Tolnay
8ac05b9766
Fix some double indents on exprs containing blocks
The `print_expr` method already places an `ibox(INDENT_UNIT)` around
every expr that gets printed. Some exprs were then using `self.head`
inside of that, which does its own `cbox(INDENT_UNIT)`, resulting in two
levels of indentation:

    while true {
            stuff;
        }

This commit fixes those cases to produce the expected single level of
indentation within every expression containing a block.

    while true {
        stuff;
    }
2022-01-30 19:45:00 -08:00
David Tolnay
cb93e9c0ec
Compute indent never relative to current column
Previously the pretty printer would compute indentation always relative
to whatever column a block begins at, like this:

    fn demo(arg1: usize,
            arg2: usize);

This is never the thing to do in the dominant contemporary Rust style.
Rustfmt's default and the style used by the vast majority of Rust
codebases is block indentation:

    fn demo(
        arg1: usize,
        arg2: usize,
    );

where every indentation level is a multiple of 4 spaces and each level
is indented relative to the indentation of the previous line, not the
position that the block starts in.
2022-01-30 19:44:59 -08:00
Eric Huss
ba013373d8
Rollup merge of #92908 - dtolnay:rustdoc, r=GuillaumeGomez
Render more readable macro matcher tokens in rustdoc

Follow-up to #92334.

This PR lifts some of the token rendering logic from https://github.com/dtolnay/prettyplease into rustdoc so that even the matchers for which a source code snippet is not available (because they are macro-generated, or any other reason) follow some baseline good assumptions about where the tokens in the macro matcher are appropriate to space.

The below screenshots show an example of the difference using one of the gnarliest macros I could find. Some things to notice:

- In the **before**, notice how a couple places break in between `$(....)`↵`*`, which is just about the worst possible place that it could break.

- In the **before**, the lines that wrapped are weirdly indented by 1 space of indentation relative to column 0. In the **after**, we use the typical way of block indenting in Rust syntax which is put the open/close delimiters on their own line and indent their contents by 4 spaces relative to the previous line (so 8 spaces relative to column 0, because the matcher itself is indented by 4 relative to the `macro_rules` header).

- In the **after**, macro_rules metavariables like `$tokens:tt` are kept together, which is how just about everybody writing Rust today writes them.

## Before

![Screenshot from 2022-01-14 13-05-53](https://user-images.githubusercontent.com/1940490/149585105-1f182b78-751f-421f-a234-9dbc04fa3bbd.png)

## After

![Screenshot from 2022-01-14 13-06-04](https://user-images.githubusercontent.com/1940490/149585118-d4b52ea7-3e67-4b6e-a12b-31dfb8172f86.png)

r? `@camelid`
2022-01-30 08:37:47 -08:00
lcnr
2684dfe583 try apply rustc_pass_by_value to Span 2022-01-27 11:29:41 +01:00
David Tolnay
21c1571e79
Deduplicate branches of print_break implementation 2022-01-19 19:04:36 -08:00
David Tolnay
51eeb82d9d
Inline print_newline function 2022-01-19 19:04:35 -08:00
David Tolnay
224536f4fe
Inline indent function 2022-01-19 19:04:35 -08:00
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
David Tolnay
0904614751
Render more readable macro matchers in rustdoc 2022-01-18 11:13:29 -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
David Tolnay
a24e238bdf
Fix spacing and ordering of words in pretty printed Impl 2021-12-29 12:10:24 -08:00
David Tolnay
2f25a4ab30
Fix double space in pretty printed TryBlock 2021-12-29 11:13:33 -08:00
Matthias Krüger
949769cf3b
Rollup merge of #92372 - dtolnay:fntype, r=jackh726
Print space after formal generic params in fn type

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

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

fn main() {
    println!("{}", repro!(for<'a> fn(&'a u8)));
}
```

Before:&ensp;`for<'a>fn(&'a u8)`
After:&ensp;`for<'a> fn(&'a u8)`

The pretty printer's `print_formal_generic_params` already prints formal generic params correctly with a space, we just need to call it when printing BareFn types instead of reimplementing the printing incorrectly without a space.

83b15bfe1c/compiler/rustc_ast_pretty/src/pprust/state.rs (L1394-L1400)
2021-12-29 10:17:13 +01:00
Matthias Krüger
5583010347
Rollup merge of #92371 - dtolnay:attrblock, r=oli-obk
Remove pretty printer space inside block with only outer attrs

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

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

fn main() {
    println!("{}", repro!(#[attr] {}));
}
```

Before:&ensp;`#[attr] { }`
After:&ensp;`#[attr] {}`
2021-12-29 10:17:12 +01:00
David Tolnay
ad29c177f4
Print space after formal generic params in fn type 2021-12-28 12:56:55 -08:00
David Tolnay
cbccc4a597
Remove pretty printer space inside block with only outer attrs 2021-12-28 12:42:50 -08:00
Jack Huey
4391a11537 Parse and suggest moving where clauses after equals for type aliases 2021-12-28 14:20:15 -05:00
David Tolnay
e9fbe79292
Remove &self from PrintState::to_string 2021-12-27 16:20:50 -08:00
Nicholas Nethercote
056d48a2c9 Remove unnecessary sigils around Symbol::as_str() calls. 2021-12-15 17:32:14 +11:00
Matthias Krüger
40988591ec
Rollup merge of #91625 - est31:remove_indexes, r=oli-obk
Remove redundant [..]s
2021-12-10 22:40:36 +01:00
est31
15de4cbc4b Remove redundant [..]s 2021-12-09 00:01:29 +01:00
David Tolnay
f0f7b8d44a
Pretty print break and continue without redundant space 2021-12-08 14:35:20 -08:00
David Tolnay
a79b702956
Remove unneeded access to pretty printer's s field in favor of deref 2021-12-07 09:14:46 -08:00
Matthias Krüger
b2dcfddb24
Rollup merge of #91562 - dtolnay:asyncspace, r=Mark-Simulacrum
Pretty print async block without redundant space

**Repro:**

```rust
macro_rules! m {
    ($e:expr) => { stringify!($e) };
}
fn main() {
    println!("{:?}", m!(async {}));
}
```

**Before:** <code>"async&nbsp;&nbsp;{}"</code>
**After:** `"async {}"`

<br>

In this function:

65c55bf931/compiler/rustc_ast_pretty/src/pprust/state.rs (L2049-L2051)

the `print_capture_clause` and `word_nbsp`/`word_space` calls already put a space after the `async` and `move` keywords being printed. The extra `self.s.space()` call removed by this PR resulted in the redundant double space.

65c55bf931/compiler/rustc_ast_pretty/src/pprust/state.rs (L2640-L2645)

65c55bf931/compiler/rustc_ast_pretty/src/helpers.rs (L34-L37)

65c55bf931/compiler/rustc_ast_pretty/src/helpers.rs (L5-L8)
2021-12-07 11:05:06 +01:00
David Tolnay
596e33ac32
Delete duplicated helpers from HIR printer 2021-12-05 12:45:51 -08:00
David Tolnay
33c29a3ad3
Pretty print async block without redundant space 2021-12-05 11:32:00 -08:00
Matthias Krüger
a8f8f746fc
Rollup merge of #91437 - dtolnay:emptybrace, r=nagisa
Pretty print empty blocks as {}

**Example:**

```rust
macro_rules! p {
    ($e:expr) => {
        println!("{}", stringify!($e));
    };
    ($i:item) => {
        println!("{}", stringify!($i));
    };
}

fn main() {
    p!(if true {});
    p!(struct S {});
}
```

**Before:**

```console
if true { }
struct S {
}
```

**After:**

```console
if true {}
struct S {}
```

This affects [`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html), as well as ecosystem uses of stringify such as in [`anyhow::ensure!`](https://docs.rs/anyhow/1/anyhow/macro.ensure.html). Printing a `{ }` in today's heavily rustfmt'd world comes out looking jarring/sloppy.
2021-12-05 15:04:20 +01:00
Amanieu d'Antras
940b2eabad Add initial AST and MIR support for unwinding from inline assembly 2021-12-03 23:51:46 +01:00
David Tolnay
b516a8c5cb
Pretty print empty blocks as {} 2021-12-01 13:50:13 -08:00
Vadim Petrochenkov
141c6cc78e expand: Turn ast::Crate into a first class expansion target
And stop creating a fake `mod` item for the crate root when expanding a crate.
2021-11-28 15:48:55 +08:00
asquared31415
b233d3b5da Add support for specifying multiple clobber_abi in asm!
Allow multiple clobber_abi in asm

Update docs
Fix aarch64 test
Combine abis
Emit duplicate ABI error, empty ABI list error
multiple clobber_abi
2021-11-10 01:06:03 -05:00
bors
46b8e7488e Auto merge of #90668 - matthiaskrgr:clippy_nov7, r=jyn514
more clippy fixes
2021-11-07 20:04:54 +00:00
Matthias Krüger
5c454551da more clippy fixes 2021-11-07 16:59:05 +01:00
Vadim Petrochenkov
2834f57c45 ast: Fix naming conventions in AST structures
TraitKind -> Trait
TyAliasKind -> TyAlias
ImplKind -> Impl
FnKind -> Fn

All `*Kind`s in AST are supposed to be enums.

Tuple structs are converted to braced structs for the types above, and fields are reordered in syntactic order.

Also, mutable AST visitor now correctly visit spans in defaultness, unsafety, impl polarity and constness.
2021-11-07 21:38:17 +08:00
r00ster91
3c1d55422a Some "parenthesis" and "parentheses" fixes 2021-10-17 12:04:01 +02:00
Mark Rousskov
c746be2219 Migrate to 2021 2021-09-20 22:21:42 -04:00
Felix S. Klock II
91feb76d13 Revert "Implement Anonymous{Struct, Union} in the AST"
This reverts commit 059b68dd67.

Note that this was manually adjusted to retain some of the refactoring
introduced by commit 059b68dd67, so that it could
likewise retain the correction introduced in commit
5b4bc05fa5
2021-09-09 09:14:17 -04:00
Felix S. Klock II
5560f6d90a Revert "Fix ast expanded printing for anonymous types"
This reverts commit 5b4bc05fa5.
2021-09-09 09:14:16 -04:00
Cameron Steffen
89d2600d01 Add let-else to AST 2021-08-30 20:17:45 -05:00
bors
ae0b03bc6b Auto merge of #88262 - klensy:pprust-cow, r=nagisa
Cow'ify some pprust methods

Reduce number of potential needless de/allocations by using `Cow<'static, str>` instead of explicit `String` type.
2021-08-29 17:46:29 +00:00
inquisitivecrystal
8c62fa0575 Treat macros as HIR items 2021-08-28 00:16:34 -07:00
klensy
c565339c37 Convert some functions to return Cow<'static,str> instead of String to reduce potential reallocations 2021-08-25 00:24:44 +03:00
Caio
6aa9937a76 Introduce hir::ExprKind::Let - Take 2 2021-08-15 16:18:26 -03:00
Amanieu d'Antras
3fd463a5ca Add support for clobber_abi to asm! 2021-08-12 12:43:11 +01:00
bors
b53a93db2d Auto merge of #87535 - lf-:authors, r=Mark-Simulacrum
rfc3052 followup: Remove authors field from Cargo manifests

Since RFC 3052 soft deprecated the authors field, hiding it from
crates.io, docs.rs, and making Cargo not add it by default, and it is
not generally up to date/useful information for contributors, we may as well
remove it from crates in this repo.
2021-08-02 05:49:17 +00:00
Jade
3cf820e17d rfc3052: Remove authors field from Cargo manifests
Since RFC 3052 soft deprecated the authors field anyway, hiding it from
crates.io, docs.rs, and making Cargo not add it by default, and it is
not generally up to date/useful information, we should remove it from
crates in this repo.
2021-07-29 14:56:05 -07:00
Matthias Krüger
1c129f7b97 use vec![] macro to create Vector with first item inside instead of pushing to an empty vec![]
slightly reduces code bloat
2021-07-25 12:19:33 +02:00
Guillaume Gomez
a2654fb64c Rework SESSION_GLOBALS API to prevent overwriting it 2021-07-08 16:16:28 +02:00
Noah Lev
7ffec7028a rustc_ast_pretty: Don't print space after $
For example, this code:

    $arg:expr

used to be pretty-printed as:

    $ arg : expr

but is now pretty-printed as:

    $arg : expr
2021-07-03 16:35:18 -07:00
Yuki Okushi
de93434cf0
Rollup merge of #86358 - klensy:pp-loop, r=Mark-Simulacrum
fix pretty print for `loop`
2021-06-29 00:26:55 +09:00
bors
e6b4c252ea Auto merge of #86599 - Amanieu:asm_raw, r=nagisa
Add a "raw" option for asm! which ignores format string specifiers

This is useful when including raw assembly snippets using `include_str!`.
2021-06-25 20:44:28 +00:00
Amanieu d'Antras
d0443bb7c2 Add a "raw" option for asm! which ignores format string specifiers 2021-06-24 23:42:15 +01:00
klensy
ac72773215 fix pretty print for loop in mir and hir 2021-06-23 11:49:08 +03:00
bors
6a758ea7e4 Auto merge of #85193 - pnkfelix:readd-support-for-inner-attrs-within-match, r=nikomatsakis
Re-add support for parsing (and pretty-printing) inner-attributes in match body

Re-add support for parsing (and pretty-printing) inner-attributes within body of a `match`.

In other words, we can do `match EXPR { #![inner_attr] ARM_1 ARM_2 ... }` again.

I believe this unbreaks the only four crates that crater flagged as broken by PR #83312.

(I am putting this up so that the lang-team can check it out and decide whether it changes their mind about what to do regarding PR #83312.)
2021-06-22 21:17:12 +00:00
Yuki Okushi
4f8e0ebcc5
Use AttrVec for Arm, FieldDef, and Variant 2021-06-17 08:04:54 +09:00
Ryan Levick
6936349233 Add support for using qualified paths with structs in expression and pattern
position.
2021-06-10 13:18:41 +02:00
Pietro Albini
9e22b844dd remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
jedel1043
5b4bc05fa5 Fix ast expanded printing for anonymous types 2021-05-24 00:03:59 -05:00
jedel1043
059b68dd67 Implement Anonymous{Struct, Union} in the AST
Add unnamed_fields feature gate and gate unnamed fields on parsing
2021-05-16 09:49:16 -05:00
Aaron Hill
357c013ff5
Remove some unncessary spaces from pretty-printed tokenstream output
In addition to making the output look nicer for all crates, this also
aligns the pretty-printing output with what the `rental` crate expects.
This will allow us to eventually disable a backwards-compat hack in a
follow-up PR.
2021-05-15 12:05:03 -04:00
Amanieu d'Antras
5918ee4317 Add support for const operands and options to global_asm!
On x86, the default syntax is also switched to Intel to match asm!
2021-05-13 22:31:57 +01:00
Felix S. Klock II
75d6293128 Re-add support for parsing (and pretty-printing) inner-attributes within body of a match.
In other words, we can do `match EXPR { #![inner_attr] ARM_1 ARM_2 ... }` again.

I believe this unbreaks the only four crates that crater flagged as broken by PR 83312.

(I am putting this up so that the lang-team can check it out and decide whether
it changes their mind about what to do regarding PR 83312.)
2021-05-11 15:18:09 -04:00
Vadim Petrochenkov
1443c7646d parser: Remove support for inner attributes on non-block expressions 2021-05-03 13:33:53 +03:00
Amanieu d'Antras
32be124e30 Use AnonConst for asm! constants 2021-04-06 12:35:41 +01:00
Joshua Nelson
441dc3640a Remove (lots of) dead code
Found with https://github.com/est31/warnalyzer.

Dubious changes:
- Is anyone else using rustc_apfloat? I feel weird completely deleting
  x87 support.
- Maybe some of the dead code in rustc_data_structures, in case someone
  wants to use it in the future?
- Don't change rustc_serialize

  I plan to scrap most of the json module in the near future (see
  https://github.com/rust-lang/compiler-team/issues/418) and fixing the
  tests needed more work than I expected.

TODO: check if any of the comments on the deleted code should be kept.
2021-03-27 22:16:33 -04:00
kadmin
7116bb5c33 Update with comments 2021-03-23 17:16:20 +00:00
kadmin
ea2af70466 Update with comments
A bunch of nits fixed, and a new test for pretty printing the AST.
2021-03-23 17:16:20 +00:00
varkor
8ef81388e2 Some refactoring 2021-03-23 17:16:20 +00:00
kadmin
e4e5db4e42 Add has_default to GenericParamDefKind::Const
This currently creates a field which is always false on GenericParamDefKind for future use when
consts are permitted to have defaults

Update const_generics:default locations

Previously just ignored them, now actually do something about them.

Fix using type check instead of value

Add parsing

This adds all the necessary changes to lower const-generics defaults from parsing.

Change P<Expr> to AnonConst

This matches the arguments passed to instantiations of const generics, and makes it specific to
just anonymous constants.

Attempt to fix lowering bugs
2021-03-23 17:16:20 +00:00
mark
db5629adcb stabilize or_patterns 2021-03-19 19:45:32 -05:00
Vadim Petrochenkov
d1522b39dd ast: Reduce size of ExprKind by boxing fields of ExprKind::Struct 2021-03-16 11:41:24 +03:00
Vadim Petrochenkov
b25d3ba781 ast/hir: Rename field-related structures
StructField -> FieldDef ("field definition")
Field -> ExprField ("expression field", not "field expression")
FieldPat -> PatField ("pattern field", not "field pattern")

Also rename visiting and other methods working on them.
2021-03-16 11:41:24 +03:00
Dylan DPC
30f39fee9d
Rollup merge of #82238 - petrochenkov:nocratemod, r=Aaron1011
ast: Keep expansion status for out-of-line module items

I.e. whether a module `mod foo;` is already loaded from a file or not.
This is a pre-requisite to correctly treating inner attributes on such modules (https://github.com/rust-lang/rust/issues/81661).

With this change AST structures for `mod` items diverge even more for AST structure for the crate root, which previously used `ast::Mod`.
Therefore this PR removes `ast::Mod` from `ast::Crate` in the first commit, these two things are sufficiently different from each other, at least at syntactic level.
Customization points for visiting a "`mod` item or crate root" were also removed from AST visitors (`fn visit_mod`).
`ast::Mod` itself was refactored away in the second commit in favor of `ItemKind::Mod(Unsafe, ModKind)`.
2021-02-19 02:49:08 +01:00
Dylan DPC
66211f6657
Rollup merge of #82066 - matthewjasper:trait-ref-fix, r=jackh726
Ensure valid TraitRefs are created for GATs

This fixes `ProjectionTy::trait_ref` to use the correct substs. Places that need all of the substs have been updated to not use `trait_ref`.

r? ````@jackh726````
2021-02-18 16:57:34 +01:00
Vadim Petrochenkov
4a88165124 ast: Keep expansion status for out-of-line module items
Also remove `ast::Mod` which is mostly redundant now
2021-02-18 13:07:49 +03:00
Vadim Petrochenkov
eb65f15c78 ast: Stop using Mod in Crate
Crate root is sufficiently different from `mod` items, at least at syntactic level.

Also remove customization point for "`mod` item or crate root" from AST visitors.
2021-02-18 13:07:49 +03:00
Matthias Krüger
4390a61b64 avoid full-slicing slices
If we already have a slice, there is no need to get another full-range slice from that, just use the original.
clippy::redundant_slicing
2021-02-16 00:31:11 +01:00