2675 Commits

Author SHA1 Message Date
Oli Scherer
08c391ca09 Temporarily allow const impl and impl const at the same time to migrate 2025-11-18 09:20:21 +00:00
Oli Scherer
a16ff533b9 Refactor impl block frontmatter checking in the parser 2025-11-18 09:20:21 +00:00
bors
2636cb4c13 Auto merge of #148818 - Zalathar:rollup-4vujcg0, r=Zalathar
Rollup of 13 pull requests

Successful merges:

 - rust-lang/rust#148694 (std: support `RwLock` and thread parking on TEEOS)
 - rust-lang/rust#148712 (Port `cfg_select!` to the new attribute parsing system)
 - rust-lang/rust#148760 (rustc_target: hide TargetOptions::vendor)
 - rust-lang/rust#148771 (IAT: Reinstate early bailout)
 - rust-lang/rust#148775 (Fix a typo in the documentation for the strict_shr function)
 - rust-lang/rust#148779 (Implement DynSend and DynSync for std::panic::Location.)
 - rust-lang/rust#148781 ([rustdoc] Remove unneeded `allow(rustc::potential_query_instability)`)
 - rust-lang/rust#148783 (add test for assoc type norm wf check)
 - rust-lang/rust#148785 (Replace `master` branch references with `main`)
 - rust-lang/rust#148791 (fix "is_closure_like" doc comment)
 - rust-lang/rust#148792 (Prefer to use file.stable_id over file.name from source map)
 - rust-lang/rust#148805 (rustc-dev-guide subtree update)
 - rust-lang/rust#148807 (Document (and test) a problem with `Clone`/`Copy` deriving.)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-11-11 13:30:50 +00:00
Stuart Cook
e26b42333d
Rollup merge of #148712 - JonathanBrouwer:cfg_select, r=jdonszelmann
Port `cfg_select!` to the new attribute parsing system

Best reviewed commit by commit, since it involves some moving around of code

r? `````@jdonszelmann`````
2025-11-11 21:11:48 +11:00
Stuart Cook
3538bc18fe
Rollup merge of #143619 - beetrees:varargs-named, r=jdonszelmann
`c_variadic`: Add future-incompatibility warning for `...` arguments without a pattern outside of `extern` blocks

This PR makes `...` arguments without a pattern in non-foreign functions (such as the argument in `unsafe extern "C" fn f(...) {}`) a future-compatibility warning; making this error would be consistent with how `unsafe extern "C" fn f(u32) {}` is handled. Allowing `...` arguments without a pattern in non-foreign functions is a source of confusion for programmers coming from C, where the `...` parameter is never named and instead calling `va_start` is required; disallowing `...` arguments without a pattern also improves the overall consistency of the Rust language by matching the treatment of other arguments without patterns. `...` arguments without a pattern in `extern` blocks (such as `unsafe extern "C" { fn f(...); }`) continue to compile without warnings after this PR, as they are already stable and heavily used (and don't cause the mentioned confusion as they are just being used in function declarations).

As all the syntax gating for `c_variadic` has been done post-expansion, this is technically a breaking change. In particular, code like this has compiled on stable since Rust 1.35.0:
```rust
#[cfg(any())] // Equivalent to the more recent #[cfg(false)]
unsafe extern "C" fn bar(_: u32, ...) {}
```
Since this is more or less a stability hole and a Crater run shows only the `binrw` crate is using this, I think it would be ok to break this. This will require a lang FCP.

The idea of rejecting `...` pre-expansion was first raised here https://github.com/rust-lang/rust/pull/143546#issuecomment-3043142052.

Tracking issue: rust-lang/rust#44930
cc `@folkertdev` `@workingjubilee`
r? `@joshtriplett`
2025-11-11 21:09:33 +11:00
beetrees
02e1f4421d
c_variadic: Add future-incompatibility warning for ... arguments without a pattern outside of extern blocks 2025-11-10 14:33:56 +01:00
Frank King
5ef48ed448 Implement &pin patterns and ref pin bindings 2025-11-10 09:57:08 +08:00
Jonathan Brouwer
a5814a5c61
Move parse_cfg_select to rustc_builtin_macros
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-11-09 16:08:00 +01:00
Stuart Cook
784e91df60
Rollup merge of #148680 - ShE3py:array-colon, r=JonathanBrouwer
Recover `[T: N]` as `[T; N]`

`;` is similar and (keyboard-wise) next to `:`, so a verbose suggestion may help to see the difference.

Parent PR: rust-lang/rust#143905

---
`@rustbot` label +A-parser +A-array +A-diagnostics +A-suggestion-diagnostics +D-papercut
2025-11-09 13:22:34 +11:00
Stuart Cook
2fe69a9d7e
Rollup merge of #148673 - fmease:del-dyn_star-remnant, r=JonathanBrouwer
Remove a remnant of `dyn*` from the parser

Follow-up to https://github.com/rust-lang/rust/pull/146664 and https://github.com/rust-lang/rust/pull/143036.

`is_explicit_dyn_type` still checked for `TokenKind::Star` which made no sense now that `dyn*` is no more.

Removing it doesn't represent a functional change and merely affects diagnostics. That's because the check only dictated whether to interpret `dyn` as the start of a trait object type in Rust 2015 (where this identifier is only a *contextual* keyword). However, we would still fail at the `*` later on as it doesn't start a bound.

While at it, I also took the time to clean up in the vicinity.
2025-11-09 13:22:33 +11:00
Stuart Cook
0a7401148e
Rollup merge of #146305 - Kivooeo:a-lot-of-references-in-self, r=JonathanBrouwer
Add correct suggestion for multi-references for self type in method

Currently the suggestion for this code

```rust
fn main() {}

struct A {
    field: i32,
}

impl A {
    fn f(&&self) {}
}
```

looks like this, which is incorrect and missleading

```rust
   Compiling playground v0.0.1 (/playground)
error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
 --> src/main.rs:8:16
  |
8 |     fn f(&&self) {}
  |                ^ expected one of 9 possible tokens
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: explicitly ignore the parameter name
  |
8 |     fn f(_: &&self) {}
  |          ++
```

So this fixes it and make more correct suggestions

```rust
error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
 --> /home/gh-Kivooeo/test_/src/main.rs:8:16
  |
8 |     fn f(&&self) {}
  |                ^ expected one of 9 possible tokens
  |
help: `self` should be `self`, `&self` or `&mut self`, please remove extra references
  |
8 -     fn f(&&self) {}
8 +     fn f(&self) {}
```

Implementation is pretty self-documenting, but if you have suggestions on how to improve this (according to current test, which may be not fully covering all cases, this is works very well) or have some funny edge cases to show, I would appreciate it

r? compiler
2025-11-09 13:22:23 +11:00
bors
72b21e1a64 Auto merge of #139558 - camelid:mgca-const-items, r=oli-obk,BoxyUwU
mgca: Add ConstArg representation for const items

tracking issue: rust-lang/rust#132980
fixes rust-lang/rust#131046
fixes rust-lang/rust#134641

As part of implementing `min_generic_const_args`, we need to distinguish const items that can be used in the type system, such as in associated const equality projections, from const items containing arbitrary const code, which must be kept out of the type system. Specifically, all "type consts" must be either concrete (no generics) or generic with a trivial expression like `N` or a path to another type const item.

To syntactically distinguish these cases, we require, for now at least, that users annotate all type consts with the `#[type_const]` attribute. Then, we validate that the const's right-hand side is indeed eligible to be a type const and represent it differently in the HIR.

We accomplish this representation using a new `ConstItemRhs` enum in the HIR, and a similar but simpler enum in the AST. When `#[type_const]` is **not** applied to a const (e.g. on stable), we represent const item right-hand sides (rhs's) as HIR bodies, like before. However, when the attribute is applied, we instead lower to a `hir::ConstArg`. This syntactically distinguishes between trivial const args (paths) and arbitrary expressions, which are represented using `AnonConst`s. Then in `generics_of`, we can take advantage of the existing machinery to bar the `AnonConst` rhs's from using parent generics.
2025-11-08 22:31:33 +00:00
Noah Lev
66267da3e9 Use "rhs" terminology instead of "body" 2025-11-08 13:50:48 -05:00
Kivooeo
7298174cd5 add parser check for multi-reference self 2025-11-08 13:39:40 +00:00
León Orell Valerian Liehr
474501b672
Remove a remnant of dyn* from the parser 2025-11-08 06:48:20 +01:00
Lieselotte
d1052e476b
Recover [T: N] as [T; N] 2025-11-08 04:51:33 +01:00
Scott Schafer
4748d92a95
feat: Always use annotate-snippets for Unicode output 2025-11-05 09:01:07 -07:00
Guillaume Gomez
8482454209
Rollup merge of #145903 - Kivooeo:c-style-pointer, r=davidtwco
Give correct suggestion for a typo in raw pointers

This adds a check for when user wrote `const* T` instead of `*const T`, I just saw how a C-person made this typo and compiler suggests this

```rust
 --> src/main.rs:2:17
  |
2 |     let p: const* u8 = 0 as _;
  |                 ^
  |
help: add `mut` or `const` here
  |
2 |     let p: const*mut  u8 = 0 as _;
  |                  +++
2 |     let p: const*const  u8 = 0 as _;
  |                  +++++
```

which is very incorrect

also fixes https://github.com/rust-lang/rust/issues/136602

r? compiler
2025-11-03 17:20:31 +01:00
Noah Lev
0515aa5a3e mgca: Add ConstArg representation for const items 2025-11-01 14:59:10 -04:00
Matthias Krüger
dc9060688a
Rollup merge of #139751 - frank-king:feature/pin-project, r=Nadrieril,traviscross
Implement pin-project in pattern matching for `&pin mut|const T`

This PR implements part of rust-lang/rust#130494. It supports pin-project in pattern matching for `&pin mut|const T`.

~Pin-projection by field access (i.e. `&pin mut|const place.field`) is not fully supported yet since pinned-borrow is not ready (rust-lang/rust#135731).~

CC ``````@traviscross``````
2025-11-01 08:25:44 +01:00
Matthias Krüger
149ad71e05
Rollup merge of #144291 - oli-obk:const_trait_alias, r=fee1-dead
Constify trait aliases

Allow `const trait Foo = Bar + [const] Baz;` trait alias declarations. Their rules are the same as with super traits of const traits. So `[const] Baz` or `const Baz` is only required for `[const] Foo` or `const Foo` bounds respectively.

tracking issue rust-lang/rust#41517 (part of the general trait alias feature gate, but I can split it out into a separate const trait alias feature gate. I just assumed that const traits would stabilize before trait aliases, and we'd want to stabilize trait aliases together with const trait aliases at the same time)

r? ``@compiler-errors`` ``@fee1-dead``
2025-10-31 02:39:14 +01:00
Oli Scherer
5f6772c2a7 Constify trait aliases 2025-10-30 08:05:37 +00:00
Frank King
26f35ae269 Implement pattern matching for &pin mut|const T 2025-10-30 07:56:16 +08:00
Matthias Krüger
4602127362
Rollup merge of #144444 - dawidl022:contracts/variable-scoping-rebased, r=jackh726
Contract variable declarations

This change adds contract variables that can be declared in the `requires` clause and can be referenced both in `requires` and `ensures`, subject to usual borrow checking rules. This allows any setup common to both the `requires` and `ensures` clauses to only be done once.

In particular, one future use case would be for [Fulminate](https://dl.acm.org/doi/10.1145/3704879)-like ownership assertions in contracts, that are essentially side-effects, and executing them twice would alter the semantics of the contract.

As of this change, `requires` can now be an arbitrary sequence of statements, with the final expression being of type `bool`. They are executed in sequence as expected, before checking if the final `bool` expression holds.

This PR depends on rust-lang/rust#144438 (which has now been merged).

Contracts tracking issue: https://github.com/rust-lang/rust/issues/128044

**Other changes introduced**:
- Contract macros now wrap the content in braces to produce blocks, meaning there's no need to wrap the content in `{}` when using multiple statements. The change is backwards compatible, in that wrapping the content in `{}` still works as before. The macros also now treat `requires` and `ensures` uniformally, meaning the `requires` closure is built inside the parser, as opposed to in the macro.

**Known limiatations**:
- Contracts with variable declarations are subject to the regular borrow checking rules, and the way contracts are currently lowered limits the usefulness of contract variable declarations. Consider the below example:

  ```rust
  #[requires(let init_x = *x; true)]
  #[ensures(move |_| *x == 2 * init_x)]
  fn double_in_place(x: &mut i32) {
      *x *= 2;
  }
  ```

  We have used the new variable declarations feature to remember the initial value pointed to by `x`, however, moving `x` into the `ensures` does not pass the borrow checker, meaning the above function contract is illegal. Ideally, something like the above should be expressable in contracts.
2025-10-29 08:07:49 +01:00
Oli Scherer
3ca752f979 Trait aliases are rare large ast nodes, box them 2025-10-28 11:11:56 +00:00
Kivooeo
ea96b79056 add parser check for pointer typo 2025-10-25 19:20:39 +00:00
Camille Gillot
15c91bf308 Retire ast::TyAliasWhereClauses. 2025-10-23 00:40:01 +00:00
bors
dc1feabef2 Auto merge of #147207 - Muscraft:anstyle-anstream, r=davidtwco
refactor: Move to anstream + anstyle for styling

`rustc` uses [`termcolor`](https://crates.io/crates/termcolor) for styling and writing, while `annotate-snippets` uses [`anstyle`](https://crates.io/crates/anstyle) for styling and currently writes directly to a `String`. When rendering directly to a terminal, there isn't/shouldn't be any differences. Still, there are differences in the escape sequences, which leads to slightly different output in JSON and SVG tests. As part of my work to have `rustc` use `annotate-snippets`, and to reduce the test differences between the two, I switched `rustc` to use `anstlye` and [`anstream`](https://crates.io/crates/anstream) for styling and writing.

The first commit migrates to `anstyle` and `anstream` and notably does not change the output. This is because it includes extra formatting to ensure that `anstyle` + `anstream` match the current output exactly. Most of this code is unnecessary, as it adds redundant resets or uses 256-color (8-bit) when it could be using 4-bit color. The subsequent commits remove this extra formatting while maintaining the correct output when rendered.

[Zulip discussion](https://rust-lang.zulipchat.com/#narrow/channel/147480-t-compiler.2Fdiagnostics/topic/annotate-snippets.20hurdles)
2025-10-22 16:22:51 +00:00
Oli Scherer
ad4bd083f3 Add not-null pointer patterns to pattern types 2025-10-21 11:22:51 +00:00
Scott Schafer
926d4535cd
refactor: Move to anstream + anstyle for styling 2025-10-20 12:13:25 -06:00
Deadbeef
b145213cee Parse const unsafe trait properly
Previously, this was greedily stolen by the `fn` parsing logic.
2025-10-18 23:39:30 +00:00
Dawid Lachowicz
aeae085dc3
Add contract variable declarations
Contract variables can be declared in the `requires` clause and
can be referenced both in `requires` and `ensures`, subject to usual
borrow checking rules.

This allows any setup common to both the `requires` and `ensures`
clauses to only be done once.
2025-10-18 15:00:34 +01:00
Matthias Krüger
2b582ea0ba
Rollup merge of #147532 - JonathanBrouwer:cfg_attr2, r=jdonszelmann
Port `#[cfg_attr]` to the new attribute parsing infrastructure

This work in progress, not ready for review.
PR mostly for ci/perf runs
2025-10-18 15:09:03 +02:00
bors
f5242367f4 Auto merge of #146221 - camsteffen:ast-boxes, r=cjgillot
Remove boxes from ast list elements

Less indirection should be better perf.
2025-10-16 02:31:44 +00:00
Jonathan Brouwer
e0c190f681
Move parse_cfg_attr to rustc_attr_parsing
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-10-15 22:18:18 +02:00
bors
828c2a9afc Auto merge of #147360 - chenyukang:yukang-fix-assoc-eq-missing-term, r=nnethercote
Remove extra space for missing associated type term suggestion

r? `@nnethercote`
2025-10-06 01:24:16 +00:00
bors
981353ca16 Auto merge of #147345 - Kivooeo:tidy-flt-fix, r=Kobzol
Tidy: revert `flt` to `ftl`

As was explained here https://github.com/rust-lang/rust/pull/147191#issuecomment-3353801210, this reverting this change because `flt` is incorrect format

Also maybe there is existed PR for that? I didn't found one

Follow up https://github.com/rust-lang/rust/pull/147191

cc `@GuillaumeGomez`
2025-10-05 15:55:18 +00:00
yukang
34ad919858 Remove extra space for missing associated type term 2025-10-05 11:11:47 +08:00
Cameron Steffen
0ea65e0bcd Break out some parsing cold functions
This recovers some instruction count regressions after changing most Pat
parsing functions return a non-boxed Pat. It seems to be beneficial to
break out a #[cold] parsing recovery function when the function includes
more parsing, presumably because this requires more stack space and/or
mem copies when LLVM optimizes the wrong path.
2025-10-04 13:21:37 -05:00
Kivooeo
67bc030833 change flt back to ftl 2025-10-04 18:18:58 +00:00
Cameron Steffen
c44500b4a1 Remove boxes from ast Pat lists 2025-10-04 12:39:58 -05:00
Matthias Krüger
2e06dcdbeb
Rollup merge of #147245 - karolzwolak:only-replace-intended-bar-not-all-in-pattern, r=lcnr
only replace the intended comma in pattern suggestions

Only suggest to replace the intended comma, not all bars in the pattern.
Fixes rust-lang/rust#143330.
This continues rust-lang/rust#143331, the credit for making the fix goes to `@A4-Tacks.` I just blessed tests and added a regression test.
2025-10-03 21:10:31 +02:00
Karol Zwolak
d1d7b9472a bring back plural 'alternatives' in suggestion message 2025-10-02 20:24:34 +02:00
Matthias Krüger
e041b2c078
Rollup merge of #147004 - estebank:ascription-in-pat, r=fee1-dead
Tweak handling of "struct like start" where a struct isn't supported

This improves the case where someone tries to write a `match` expr where the patterns have type ascription syntax. Makes them less verbose, by giving up on the first encounter in the block, and makes them more accurate by only treating them as a struct literal if successfully parsed as such.

Before, encountering something like `match a { b:` would confuse the parser and think everything after `match` *must* be a struct, and if it wasn't it would generate a cascade of unnecessary diagnostics.
2025-10-02 10:27:50 +02:00
A4-Tacks
4735c2184c Fix diagnostics str::replace comma to bar 2025-10-01 22:45:16 +02:00
Yotam Ofek
68a7c25078 Use Iterator::eq and (dogfood) eq_by in compiler and library 2025-09-29 08:08:05 +03:00
Esteban Küber
bb48c162b6 Simplify logic slightly 2025-09-27 00:42:11 +00:00
Esteban Küber
43057698c1 Tweak handling of "struct like start" where a struct isn't supported
This improves the case where someone tries to write a `match` expr where the patterns have type ascription syntax. Makes them less verbose, by giving up on the first encounter in the block, and makes them more accurate by only treating them as a struct literal if successfuly parsed as such.
2025-09-24 21:31:23 +00:00
Esteban Küber
e9270e3cba Detect top-level ... in argument type
When writing something like the expression `|_: ...| {}`, we now detect the `...` during parsing explicitly instead of relying on the detection in `parse_ty_common` so that we don't talk about "nested `...` are not supported".

```
error: unexpected `...`
  --> $DIR/no-closure.rs:6:35
   |
LL | const F: extern "C" fn(...) = |_: ...| {};
   |                                   ^^^
   |
   = note: only `extern "C"` and `extern "C-unwind"` functions may have a C variable argument list
```
2025-09-16 11:38:08 -07:00
Esteban Küber
8306a2f02e Reword note 2025-09-16 11:24:51 -07:00