Commit Graph

1271 Commits

Author SHA1 Message Date
Matthias Krüger
9639a7c522
Rollup merge of #142069 - nnethercote:Zmacro-stats, r=petrochenkov
Introduce `-Zmacro-stats`

Introduce `-Zmacro-stats`.

It collects data about macro expansions and prints them in a table after expansion finishes. It's very useful for detecting macro bloat, especially for proc macros.

r? `@petrochenkov`
2025-06-13 05:16:56 +02:00
Matthias Krüger
b12bb2530b
Rollup merge of #134847 - dtolnay:asymmetrical, r=fmease
Implement asymmetrical precedence for closures and jumps

I have been through a series of asymmetrical precedence designs in Syn, and finally have one that I like and is worth backporting into rustc. It is based on just 2 bits of state: `next_operator_can_begin_expr` and `next_operator_can_continue_expr`.

Asymmetrical precedence is the thing that enables `(return 1) + 1` to require parentheses while `1 + return 1` does not, despite `+` always having stronger precedence than `return` [according to the Rust Reference](https://doc.rust-lang.org/1.83.0/reference/expressions.html#expression-precedence). This is facilitated by `next_operator_can_continue_expr`.

Relatedly, it is the thing that enables `(return) - 1` to require parentheses while `return + 1` does not, despite `+` and `-` having exactly the same precedence. This is facilitated by `next_operator_can_begin_expr`.

**Example:**

```rust
macro_rules! repro {
    ($e:expr) => {
        $e - $e;
        $e + $e;
    };
}

fn main() {
    repro!{return}
    repro!{return 1}
}
```

`-Zunpretty=expanded` **Before:**

```console
fn main() {
    (return) - (return);
    (return) + (return);
    (return 1) - (return 1);
    (return 1) + (return 1);
}
```

**After:**

```console
fn main() {
    (return) - return;
    return + return;
    (return 1) - return 1;
    (return 1) + return 1;
}
```
2025-06-13 05:16:54 +02:00
Nicholas Nethercote
fb73893e51 Add some useful Path/PathSegment equality operations.
They will be used in a subsequent commit.
2025-06-12 15:26:06 +10:00
Deadbeef
9b0ad97287 deduplicate the rest of AST walker functions 2025-06-09 21:59:20 +08:00
Guillaume Gomez
2946ce29ed
Rollup merge of #142086 - fee1-dead-contrib:ast-visitor-dedup, r=oli-obk
duduplicate more AST visitor methods

r? oli-obk
2025-06-06 23:53:17 +02:00
Guillaume Gomez
93ca0af08c
Rollup merge of #141603 - nnethercote:reduce-P, r=fee1-dead
Reduce `ast::ptr::P` to a typedef of `Box`

As per the MCP at https://github.com/rust-lang/compiler-team/issues/878.

r? `@fee1-dead`
2025-06-06 23:53:16 +02:00
Deadbeef
8a7262c426 deduplicate more walk_* methods in AST visit 2025-06-06 04:59:26 +00:00
Deadbeef
91b77e080f use helper macro for flat_map vs visit_list, initial dedups 2025-06-05 13:47:21 +00:00
Matthias Krüger
a06160d9a8
Rollup merge of #142007 - nnethercote:visitor-comments, r=chenyukang
Improve some `Visitor` comments.

For AST/HIR/THIR visitors, explain the use of deconstruction.

r? ``@BoxyUwU``
2025-06-04 16:24:12 +02:00
Matthias Krüger
0736a03a78
Rollup merge of #141570 - chenyukang:yukang-fix-eq_unspanned, r=workingjubilee
Fix incorrect eq_unspanned in TokenStream

Fixes rust-lang/rust#141522

r? ``@workingjubilee``

should we remove this function?
since it's used in several places, i'd prefer to keep it.
2025-06-04 16:24:07 +02:00
Nicholas Nethercote
ed300d8ad8 Improve some Visitor comments.
For AST/HIR/THIR visitors, explain the use of deconstruction.
2025-06-04 16:40:00 +10:00
Matthias Krüger
96531418f8
Rollup merge of #141945 - nnethercote:rm-Path-is_ident, r=compiler-errors
Remove `Path::is_ident`.

It checks that a path has a single segment that matches the given symbol, and that there are zero generic arguments. It has a single use.

We also have `impl PartialEq<Symbol> for Path` which does exactly the same thing *except* it doesn't check for zero generic arguments, which seems like an oversight. It has numerous uses.

This commit removes `Path::is_ident`, adds a test for zero generic arguments to `PartialEq<Symbol> for Path`, and changes the single use of `is_ident` to instead use `==`.

r? `@wesleywiser`
2025-06-03 21:53:38 +02:00
bors
a124fb3cb7 Auto merge of #141961 - matthiaskrgr:rollup-r09j2sp, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#141724 (fix(rust-lang/rust#141141): When expanding `PartialEq`, check equality of scalar types first.)
 - rust-lang/rust#141833 (`tests/ui`: A New Order [2/N])
 - rust-lang/rust#141861 (Switch `x86_64-msvc-{1,2}` back to Windows Server 2025 images)
 - rust-lang/rust#141914 (redesign stage 0 std follow-ups)
 - rust-lang/rust#141918 (Deconstruct values in the THIR visitor)
 - rust-lang/rust#141923 (Update books)
 - rust-lang/rust#141931 (Deconstruct values in the THIR visitor)
 - rust-lang/rust#141956 (Remove two trait methods from cg_ssa)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-06-03 16:31:44 +00:00
Matthias Krüger
19437666d9
Rollup merge of #141724 - Sol-Ell:issue-141141-fix, r=nnethercote
fix(#141141): When expanding `PartialEq`, check equality of scalar types first.

Fixes rust-lang/rust#141141.

Now, `cs_eq` function of `partial_eq.rs` compares [scalar types](https://doc.rust-lang.org/rust-by-example/primitives.html#scalar-types) first.

- Add `is_scalar` field to `FieldInfo`.
- Add `is_scalar` method to `TyKind`.
- Pass `FieldInfo` via `CsFold::Combine` and refactor code relying on it.
- Implement `TryFrom<&str>` and `TryFrom<Symbol>` for FloatTy.
- Implement `TryFrom<&str>` and `TryFrom<Symbol>` for IntTy.
- Implement `TryFrom<&str>` and `TryFrom<Symbol>` for UintTy.
2025-06-03 15:00:32 +02:00
Nicholas Nethercote
04391045d0 Remove Path::is_ident.
It checks that a path has a single segment that matches the given
symbol, and that there are zero generic arguments. It has a single use.

We also have `impl PartialEq<Symbol> for Path` which does exactly the
same thing *except* it doesn't check for zero generic arguments, which
seems like an oversight. It has numerous uses.

This commit removes `Path::is_ident`, adds a test for zero generic
arguments to `PartialEq<Symbol> for Path`, and changes the single use of
`is_ident` to instead use `==`.
2025-06-03 15:21:33 +10:00
Ell
a6a1c1b247 Separately check equality of the scalar types and compound types in the order of declaration. 2025-06-02 15:29:34 +03:00
Deadbeef
c33b08552b Add visit_id to ast Visitor
This helps with efforts to deduplicate the `MutVisitor` and the
`Visitor` code. All users of `Visitor`'s methods that have extra
`NodeId` as parameters really just want to visit the id on its
own.

Also includes some methods deduplicated and cleaned up as
a result of this change.
2025-06-01 02:38:24 +00:00
Matthias Krüger
a87bc9d9fe
Rollup merge of #141430 - fee1-dead-contrib:push-nmzoprvtsvww, r=petrochenkov
remove `visit_clobber` and move `DummyAstNode` to `rustc_expand`

`visit_clobber` is not really useful except for one niche purpose
involving generic code. We should just use the replace logic where we
can.
2025-05-30 07:01:29 +02:00
Guillaume Gomez
2b08e4d399
Rollup merge of #141636 - fee1-dead-contrib:push-ntqvvxwuvrvx, r=petrochenkov
avoid some usages of `&mut P<T>` in AST visitors

It's a double indirection, and is also complicating our efforts at rust-lang/rust#127615.

r? `@ghost`
2025-05-29 17:02:59 +02:00
Deadbeef
367a877147 avoid some usages of &mut P<T> in AST visitors 2025-05-29 12:54:55 +08:00
Deadbeef
5e7185583f remove visit_clobber and move DummyAstNode to rustc_expand
`visit_clobber` is not really useful except for one niche purpose
involving generic code. We should just use the replace logic where we
can.
2025-05-29 12:46:26 +08:00
Jacob Pratt
0ac0285c3f
Rollup merge of #141675 - nnethercote:ItemKind-field-order, r=fee1-dead
Reorder `ast::ItemKind::{Struct,Enum,Union}` fields.

So they match the order of the parts in the source code, e.g.:
```
struct Foo<T, U> { t: T, u: U }
       <-><----> <------------>
       /   |       \
   ident generics  variant_data
```

r? `@fee1-dead`
2025-05-29 04:49:43 +02:00
Nicholas Nethercote
4c4a40f6df Reorder ast::ItemKind::{Struct,Enum,Union} fields.
So they match the order of the parts in the source code, e.g.:
```
struct Foo<T, U> { t: T, u: U }
       <-><----> <------------>
       /   |       \
   ident generics  variant_data
```
2025-05-28 15:48:45 +10:00
Matthias Krüger
7807f5f0cb
Rollup merge of #141632 - fee1-dead-contrib:push-txmttkxvwqxl, r=oli-obk
remove `visit_mt` from `ast::mut_visit`

doesn't look like anyone is using it.
2025-05-27 20:57:55 +02:00
Michael Goulet
d7e961a4c9
Rollup merge of #141635 - fee1-dead-contrib:push-lmyymwotrspk, r=oli-obk
further dedup `WalkItemKind` for `mut_visit` and `visit`

also some drive-by fixes.

r? oli-obk
2025-05-27 13:01:44 +02:00
Nicholas Nethercote
89c21f7c1a Remove out-of-date noop_* names.
`mut_visit.rs` has a single function with a `noop_` prefix:
`noop_filter_map_expr`. This commit renames as `walk_filter_map_expr`
which is consistent with other functions in this file.

The commit also removes out-of-date comments that refer to `noop_*`
methods.
2025-05-27 19:16:11 +10:00
Deadbeef
e0d4cf38f4 further dedup WalkItemKind for mut_visit and visit
also some drive-by fixes.
2025-05-27 14:54:02 +08:00
Deadbeef
7fdf35ed1c remove visit_mt from ast::mut_visit
doesn't look like anyone is using it.
2025-05-27 14:24:18 +08:00
Nicholas Nethercote
991c91fdaa Reduce P<T> to a typedef of Box<T>.
Keep the `P` constructor function for now, to minimize immediate churn.

All the `into_inner` calls are removed, which is nice.
2025-05-27 13:29:24 +10:00
Nicholas Nethercote
c42d1fc2c6 Remove unused P stuff. 2025-05-27 02:54:52 +10:00
Nicholas Nethercote
68597509aa Remove support for P<[T]>.
It's no longer used.
2025-05-27 02:54:52 +10:00
Nicholas Nethercote
0f285e346f Remove the one use of P<[T]>.
A `Vec` is fine, the additional word (vector vs. boxed slice) doesn't
matter here.
2025-05-27 02:54:18 +10:00
Nicholas Nethercote
6973fa08a3 Remove P::map.
It's barely used, and the places that use it are better if they don't.
2025-05-27 02:07:15 +10:00
Nicholas Nethercote
a35675d38f Remove 'static bounds on P.
These date back to 2014. I don't think they're needed any more.
2025-05-27 02:07:15 +10:00
yukang
d3347bb32b remove eq_unspanned from TokenStream 2025-05-26 17:02:51 +08:00
yukang
6b5b97a4df Fix incorrect eq_unspanned in TokenStream 2025-05-26 13:47:53 +08:00
Deadbeef
898b6a13f1 further deduplicate ast visitor code 2025-05-23 22:08:12 +08:00
Deadbeef
c5bab6e9aa introduce common macro for MutVisitor and Visitor to dedup code 2025-05-19 21:21:03 +08:00
León Orell Valerian Liehr
4e5b1aa055
Rollup merge of #140746 - dianne:guard-pat-res, r=oli-obk
name resolution for guard patterns

This PR provides an initial implementation of name resolution for guard patterns [(RFC 3637)](https://github.com/rust-lang/rfcs/blob/master/text/3637-guard-patterns.md). This does not change the requirement that the bindings on either side of an or-pattern must be the same [(proposal here)](https://github.com/rust-lang/rfcs/blob/master/text/3637-guard-patterns.md#allowing-mismatching-bindings-when-possible); the code that handles that is separate from what this PR touches, so I'm saving it for a follow-up.

On a technical level, this separates "collecting the bindings in a pattern" (which was already done for or-patterns) from "introducing those bindings into scope". I believe the approach used here can be extended straightforwardly in the future to work with `if let` guard patterns, but I haven't tried it myself since we don't allow those yet.

Tracking issue for guard patterns: #129967

cc ``@Nadrieril``
2025-05-18 18:44:11 +02:00
dianne
f0b8ec1d71 name resolution for guard patterns 2025-05-18 04:21:57 -07:00
Pietro Albini
2ce08ca5d6
update cfg(bootstrap) 2025-05-12 15:33:37 +02:00
Nicholas Nethercote
5ebcbfc1e1 Remove AstDeref.
It's a "utility trait to reduce boilerplate" implemented for `P` and
`AstNodeWrapper`, but removing it gives a net reduction of twenty lines
of code. It's also simpler to just implement
`HasNodeId`/`HasAttrs`/`HasTokens` directly on types instead of via
`AstDeref`.

(I decided to make this change when doing some related refactoring and
the error messages involving `AstDeref` and `HasAttrs` were hard to
understand; removing it helped a lot.)
2025-05-10 08:58:47 +10:00
David Tolnay
6cca4ca82b
Implement asymmetrical precedence for closures and jumps 2025-05-03 23:27:29 -07:00
Nicholas Nethercote
0ea204a5ff Add useful comments on ExprKind::If variants.
Things that aren't obvious and took me a while to work out.
2025-05-02 15:53:39 +10:00
Vadim Petrochenkov
6668d13de2 ast: Remove token visiting from AST visitor
It's no longer necessary after the removal of nonterminal tokens in #124141.
2025-04-30 10:36:03 +03:00
bors
f242d6c26c Auto merge of #127516 - nnethercote:simplify-LazyAttrTokenStream, r=petrochenkov
Simplify `LazyAttrTokenStream`

`LazyAttrTokenStream` is an unpleasant type: `Lrc<Box<dyn ToAttrTokenStream>>`. Why does it look like that?
- There are two `ToAttrTokenStream` impls, one for the lazy case, and one for the case where we already have an `AttrTokenStream`.
- The lazy case (`LazyAttrTokenStreamImpl`) is implemented in `rustc_parse`, but `LazyAttrTokenStream` is defined in `rustc_ast`, which does not depend on `rustc_parse`. The use of the trait lets `rustc_ast` implicitly depend on `rustc_parse`. This explains the `dyn`.
- `LazyAttrTokenStream` must have a `size_of` as small as possible, because it's used in many AST nodes. This explains the `Lrc<Box<_>>`, which keeps it to one word. (It's required `Lrc<dyn _>` would be a fat pointer.)

This PR moves `LazyAttrTokenStreamImpl` (and a few other token stream things) from `rustc_parse` to `rustc_ast`. This lets us replace the `ToAttrTokenStream` trait with a two-variant enum and also remove the `Box`, changing `LazyAttrTokenStream` to `Lrc<LazyAttrTokenStreamInner>`. Plus it does a few cleanups.

r? `@petrochenkov`
2025-04-30 00:09:21 +00:00
Nicholas Nethercote
880e6f716d Use ThinVec to shrink LazyAttrTokenStreamInner. 2025-04-30 07:12:09 +10:00
Nicholas Nethercote
298c56f4ba Simplify LazyAttrTokenStream.
This commit does the following.
- Changes it from `Lrc<Box<dyn ToAttrTokenStream>>` to
  `Lrc<LazyAttrTokenStreamInner>`.
- Reworks `LazyAttrTokenStreamImpl` as `LazyAttrTokenStreamInner`, which
  is a two-variant enum.
- Removes the `ToAttrTokenStream` trait and the two impls of it.

The recursion limit must be increased in some crates otherwise rustdoc
aborts.
2025-04-30 07:10:56 +10:00
Nicholas Nethercote
28236ab703 Move various token stream things from rustc_parse to rustc_ast.
Specifically: `TokenCursor`, `TokenTreeCursor`,
`LazyAttrTokenStreamImpl`, `FlatToken`, `make_attr_token_stream`,
`ParserRange`, `NodeRange`. `ParserReplacement`, and `NodeReplacement`.
These are all related to token streams, rather than actual parsing.

This will facilitate the simplifications in the next commit.
2025-04-29 12:14:27 +10:00
Oli Scherer
b023856f29 Add or-patterns to pattern types 2025-04-28 07:50:18 +00:00