2453 Commits

Author SHA1 Message Date
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
3ca752f979 Trait aliases are rare large ast nodes, box them 2025-10-28 11:11:56 +00:00
Stuart Cook
0bd2437cac Rollup merge of #147478 - Jamesbarford:fix/tuple-private-fields-constructor, r=davidtwco
More intuitive error when using self to instantiate tuple struct with private field

Fixes https://github.com/rust-lang/rust/issues/147343
2025-10-27 22:13:21 +11:00
bors
b1b464d6f6 Auto merge of #147914 - petrochenkov:oosmc-used, r=fmease
resolve: When suppressing `out_of_scope_macro_calls` suppress `unused_imports` as well

Fixes the example from this comment - https://github.com/rust-lang/rust/issues/147823#issuecomment-3421770900.
Fixes https://github.com/rust-lang/rust/issues/148143.
2025-10-26 23:44:52 +00:00
James Barford-Evans
7af72e79b9 More intuitive error when using self to instantiate tuple struct with private field 2025-10-25 22:46:15 +01:00
Camille GILLOT
b67453fccd Stop passing resolver disambiguator state to AST lowering. 2025-10-24 02:41:52 +00:00
Oli Scherer
ad4bd083f3 Add not-null pointer patterns to pattern types 2025-10-21 11:22:51 +00:00
Vadim Petrochenkov
078fe7ca29 resolve: When suppressing out_of_scope_macro_calls suppress unused_imports as well 2025-10-20 19:14:46 +03:00
LorrensP-2158466
8492b24424 use module_child index as disambiguator for external items 2025-10-17 18:04:28 +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
Matthias Krüger
c607de5508 Rollup merge of #147676 - jdonszelmann:span-is-doc-comment, r=GuillaumeGomez
Return spans out of `is_doc_comment` to reduce reliance on `.span()` on attributes

r? `@GuillaumeGomez`
2025-10-15 23:41:03 +02:00
bors
28d0a4a205 Auto merge of #143548 - Diggsey:db-limit-extern-crate-usage, r=oli-obk
Restrict sysroot crate imports to those defined in this repo.

It's common to import dependencies from the sysroot via `extern crate` rather than use an explicit cargo dependency, when it's necessary to use the same dependency version as used by rustc itself. However, this is dangerous for crates.io crates, since rustc may not pull in the dependency on some targets, or may pull in multiple versions. In both cases, the `extern crate` fails to resolve.

To address this, re-export all such dependencies from the appropriate `rustc_*` crates, and use this alias from crates which would otherwise need to use `extern crate`.

See https://github.com/rust-lang/rust/pull/143492 for an example of the kind of issue that can occur.
2025-10-15 13:32:03 +00:00
Diggory Blake
c6a952159f Restrict sysroot crate imports to those defined in this repo.
It's common to import dependencies from the sysroot via `extern crate`
rather than use an explicit cargo dependency, when it's necessary to use
the same dependency version as used by rustc itself. However, this is
dangerous for crates.io crates, since rustc may not pull in the
dependency on some targets, or may pull in multiple versions. In both
cases, the `extern crate` fails to resolve.

To address this, re-export all such dependencies from the appropriate
`rustc_*` crates, and use this alias from crates which would otherwise
need to use `extern crate`.
2025-10-15 13:17:25 +01:00
Matthias Krüger
33b1e92d4f Rollup merge of #147539 - petrochenkov:cmresolve, r=eholk
resolve: Use primitives for conditional mutability more consistently

No bare `(Ref)Cell`s remain in `rustc_resolve`, only `Cm(Ref)Cell`s checking that nothing is modified during speculative resolution, and `Cache(Ref)Cell` aliases for cells that need to be migrated to mutexes/atomics.

cc `@LorrensP-2158466`
2025-10-15 07:09:55 +02:00
yukang
c00b4ba5ef Fix ICE caused by associated_item_def_ids on wrong type in resolve diag 2025-10-14 22:39:10 +08:00
Jana Dönszelmann
3941b42993 return spans out of is_doc_comment to reduce reliance on .span() on attrs 2025-10-14 15:36:09 +02:00
Vadim Petrochenkov
204508d928 resolve: Use primitives for conditional mutability more consistently 2025-10-09 23:10:25 +03:00
Vadim Petrochenkov
64d9d42364 resolver: Move local_modules from ResolverArenas to Resolver
Thus avoiding a `RefCell`.
2025-10-09 20:53:32 +03:00
Stuart Cook
9ace0de26b Rollup merge of #147489 - chenyukang:yukang-prefer-repeat-n, r=Kivooeo,oli-obk
Prefer to use repeat_n over repeat().take()

More from https://github.com/rust-lang/rust/pull/147464, but batch processed with `ast-grep` to find and replace.

second commit add notes for library: affaf532f9

r? ``@RalfJung``
2025-10-09 18:43:26 +11:00
yukang
1654cce210 prefer to use repeat_n over repeat and take 2025-10-09 01:24:55 +08:00
Jane Losare-Lusby
6920a8e8fc Convert impossible cases in macro resolution into assertions 2025-10-07 14:48:28 -07:00
yukang
95445f9b96 Trivial code cleanup in resolve 2025-10-05 17:27:45 +08:00
Cameron Steffen
c44500b4a1 Remove boxes from ast Pat lists 2025-10-04 12:39:58 -05:00
Esteban Küber
8f7d61b9ef Detect unconstructable re-exported tuple structs
When a tuple-struct is re-exported that has inaccessible fields at the `use` scope, the type's constructor cannot be accessed through that re-export. We now account for this case and extend the resulting resolution error. We also check if the constructor would be accessible directly, not through the re-export, and if so, we suggest using the full path instead.

```
error[E0423]: cannot initialize a tuple struct which contains private fields
  --> $DIR/ctor-not-accessible-due-to-inaccessible-field-in-reexport.rs:12:33
   |
LL |             let crate::Foo(x) = crate::Foo(42);
   |                                 ^^^^^^^^^^
   |
note: the type is accessed through this re-export, but the type's constructor is not visible in this import's scope due to private fields
  --> $DIR/ctor-not-accessible-due-to-inaccessible-field-in-reexport.rs:3:9
   |
LL | pub use my_mod::Foo;
   |         ^^^^^^^^^^^
help: the type can be constructed directly, because its fields are available from the current scope
   |
LL |             let crate::Foo(x) = crate::my_mod::Foo(42);
   |                                 ~~~~~~~~~~~~~~~~~~
```

Fix #133343.
2025-09-28 21:33:34 +00:00
Matthias Krüger
f104ae4252 Rollup merge of #145113 - petrochenkov:lessfinalize, r=lcnr
resolve: Do not finalize shadowed bindings

I.e. do not mark them as used, or non-speculatively loaded, or similar.
Previously they were sometimes finalized during early resolution, causing issues like https://github.com/rust-lang/rust/pull/144793#issuecomment-3168108005.
2025-09-26 18:11:08 +02:00
Matthias Krüger
82e9e54ef3 Rollup merge of #146283 - LorrensP-2158466:resolve-cm-cell, r=petrochenkov
Resolve: (Ref)Cell wrappers to deny mutation during spec resolution.

Introduces wrappers around `Cell` and `RefCell` that only allow mutation when we are not in speculative resolution. This is preparatory work for rust-lang/rust#145108.

It would allow us to make `ImportData` and `ModuleData` sync and send safe.

r? ``@petrochenkov``
2025-09-26 06:36:30 +02:00
Vadim Petrochenkov
f89660e4aa resolve: Do not finalize shadowed bindings
I.e. do not mark them as used, or non-speculative loaded, or similar.
Previously they were sometimes finalized during early resolution, causing issues like https://github.com/rust-lang/rust/pull/144793#issuecomment-3168108005.
2025-09-25 20:36:14 +03:00
LorrensP-2158466
0374df1b50 Introduce and use CmCell during import resolution. 2025-09-25 18:38:36 +02:00
León Orell Valerian Liehr
27a180a31a Move more early buffered lints to dyn lint diagnostics (3/N) 2025-09-14 12:38:12 +02:00
León Orell Valerian Liehr
31c0d96cb6 Move more early buffered lints to dyn lint diagnostics (2/N) 2025-09-14 12:38:11 +02:00
León Orell Valerian Liehr
2e816736ef Move more early buffered lints to dyn lint diagnostics (1/N) 2025-09-14 12:38:11 +02:00
Matthias Krüger
cb8b5fa4e2 Rollup merge of #146254 - yotamofek:pr/itertools-all-equal-value, r=cjgillot
Use `Itertools::all_equal_value()` where applicable

Just a small cleanup.
We already have `itertools` as a dep in these crates, so might as well use another of its features.
Makes the code simpler IMHO :)
2025-09-07 08:18:59 +02:00
Yotam Ofek
f279ae1b05 Use Itertools::all_equal_value() where applicable 2025-09-05 18:43:43 +00:00
Matthias Krüger
81042523c6 remove couple of clones 2025-09-05 15:38:01 +02:00
bors
ad85bc524b Auto merge of #144737 - petrochenkov:extprelcache, r=davidtwco
resolve: Avoid finalizing extern prelude entries more than once
2025-09-05 05:50:24 +00:00
Stuart Cook
3a6ae1167f Rollup merge of #145827 - estebank:issue-51976, r=jackh726
On unused binding or binding not present in all patterns, suggest potential typo of unit struct/variant or const

When encountering an or-pattern with a binding not available in all patterns, look for consts and unit struct/variants that have similar names as the binding to detect typos.

```
error[E0408]: variable `Ban` is not bound in all patterns
  --> $DIR/binding-typo.rs:22:9
   |
LL |         (Foo, _) | (Ban, Foo) => {}
   |         ^^^^^^^^    --- variable not in all patterns
   |         |
   |         pattern doesn't bind `Ban`
   |
help: you might have meant to use the similarly named unit variant `Bar`
   |
LL -         (Foo, _) | (Ban, Foo) => {}
LL +         (Foo, _) | (Bar, Foo) => {}
   |
```

For items that are not in the immedate scope, suggest the full path for them:

```
error[E0408]: variable `Non` is not bound in all patterns
  --> $DIR/binding-typo-2.rs:51:16
   |
LL |         (Non | Some(_))=> {}
   |          ---   ^^^^^^^ pattern doesn't bind `Non`
   |          |
   |          variable not in all patterns
   |
help: you might have meant to use the similarly named unit variant `None`
   |
LL -         (Non | Some(_))=> {}
LL +         (core::option::Option::None | Some(_))=> {}
   |
```

When encountering a typo in a pattern that gets interpreted as an unused binding, look for unit struct/variant of the same type as the binding:

```
error: unused variable: `Non`
  --> $DIR/binding-typo-2.rs:36:9
   |
LL |         Non => {}
   |         ^^^
   |
help: if this is intentional, prefix it with an underscore
   |
LL |         _Non => {}
   |         +
help: you might have meant to pattern match on the similarly named variant `None`
   |
LL -         Non => {}
LL +         std::prelude::v1::None => {}
   |
```

 Suggest constant on unused binding in a pattern

```
error: unused variable: `Batery`
  --> $DIR/binding-typo-2.rs:110:9
   |
LL |         Batery => {}
   |         ^^^^^^
   |
help: if this is intentional, prefix it with an underscore
   |
LL |         _Batery => {}
   |         +
help: you might have meant to pattern match on the similarly named constant `Battery`
   |
LL |         Battery => {}
   |            +
```

Fix rust-lang/rust#51976.
2025-09-04 10:01:54 +10:00
Stuart Cook
8746c2302c Rollup merge of #145961 - petrochenkov:extprelregr, r=nnethercote
resolve: Avoid a regression from splitting prelude into two scopes

Fixes https://github.com/rust-lang/rust/issues/145575.
2025-09-03 23:08:08 +10:00
Guillaume Gomez
af315b0725 Rollup merge of #145783 - Erk-:et-cetera-span, r=compiler-errors
add span to struct pattern rest (..)

Struct pattern rest (`..`) did not retain span information compared to normal fields. This patch adds span information for it.

The motivation of this patch comes from when I implemented this PR for Clippy: https://github.com/rust-lang/rust-clippy/pull/15000#discussion_r2134145163

It is possible to get the span of the Et cetera in a bit roundabout way, but I thought this would be nicer.
2025-09-02 17:08:52 +02:00
Vadim Petrochenkov
235dfbab9c resolve: Avoid finalizing extern prelude entries more than once 2025-09-02 16:22:16 +03:00
Esteban Küber
86085b4f65 Avoid unnecessary suggestion in or-pattern 2025-08-30 17:44:23 +00:00
Esteban Küber
cb9cd8f830 On binding not present in all patterns, look at consts and unit structs/variants for suggestions
When encountering an or-pattern with a binding not available in all patterns, look for consts and unit struct/variants that have similar names as the binding to detect typos.

```
error[E0408]: variable `Ban` is not bound in all patterns
  --> $DIR/binding-typo.rs:22:9
   |
LL |         (Foo, _) | (Ban, Foo) => {}
   |         ^^^^^^^^    --- variable not in all patterns
   |         |
   |         pattern doesn't bind `Ban`
   |
help: you might have meant to use the similarly named unit variant `Bar`
   |
LL -         (Foo, _) | (Ban, Foo) => {}
LL +         (Foo, _) | (Bar, Foo) => {}
   |
```

For items that are not in the immedate scope, suggest the full path for them:

```
error[E0408]: variable `Non` is not bound in all patterns
  --> $DIR/binding-typo-2.rs:51:16
   |
LL |         (Non | Some(_))=> {}
   |          ---   ^^^^^^^ pattern doesn't bind `Non`
   |          |
   |          variable not in all patterns
   |
help: you might have meant to use the similarly named unit variant `None`
   |
LL -         (Non | Some(_))=> {}
LL +         (core::option::Option::None | Some(_))=> {}
   |
```
2025-08-30 02:54:46 +00:00
Vadim Petrochenkov
f0dbfadaa2 resolve: Merge ExternPreludeEntry::only_item into flag_binding 2025-08-29 18:42:04 +03:00
Vadim Petrochenkov
7aec84d485 resolve: Merge ExternPreludeEntry::introduced_by_item into item_binding 2025-08-29 17:23:36 +03:00
Vadim Petrochenkov
8930d3a750 resolve: Avoid a regression from splitting prelude into two scopes 2025-08-29 16:58:51 +03:00
Guillaume Gomez
ab0ee84eac Add new doc(attribute = "...") attribute 2025-08-28 15:56:29 +02:00
binarycat
f16d1fcb55 use size_hint in attrs_to_doc_fragments 2025-08-25 10:26:22 -05:00
Esteban Küber
8dbdb1760b On binding not present in all patterns, suggest potential typo
```
error[E0408]: variable `Ban` is not bound in all patterns
 --> f12.rs:9:9
  |
9 |         (Foo,Bar)|(Ban,Foo) => {}
  |         ^^^^^^^^^  --- variable not in all patterns
  |         |
  |         pattern doesn't bind `Ban`
  |
help: you might have meant to use the similarly named previously used binding `Bar`
  |
9 -         (Foo,Bar)|(Ban,Foo) => {}
9 +         (Foo,Bar)|(Bar,Foo) => {}
  |
```
2025-08-25 15:16:25 +00:00
Valdemar Erk
75d8687f2b add span to struct pattern rest (..) 2025-08-25 09:55:50 +02:00
Jacob Pratt
d3c9908a8a Rollup merge of #145747 - joshtriplett:builtin-diag-dyn, r=jdonszelmann
Refactor lint buffering to avoid requiring a giant enum

Lint buffering currently relies on a giant enum `BuiltinLintDiag` containing all the lints that might potentially get buffered. In addition to being an unwieldy enum in a central crate, this also makes `rustc_lint_defs` a build bottleneck: it depends on various types from various crates (with a steady pressure to add more), and many crates depend on it.

Having all of these variants in a separate crate also prevents detecting when a variant becomes unused, which we can do with a dedicated type defined and used in the same crate.

Refactor this to use a dyn trait, to allow using `LintDiagnostic` types directly.

Because the existing `BuiltinLintDiag` requires some additional types in order to decorate some variants, which are only available later in `rustc_lint`, use an enum `DecorateDiagCompat` to handle both the `dyn LintDiagnostic` case and the `BuiltinLintDiag` case.

---

With the infrastructure in place, use it to migrate three of the enum variants to use `LintDiagnostic` directly, as a proof of concept and to demonstrate that the net result is a reduction in code size and a removal of a boilerplate-heavy layer of indirection.

Also remove an unused `BuiltinLintDiag` variant.
2025-08-22 22:00:59 -04:00
Jacob Pratt
8066e76c7d Rollup merge of #145597 - petrochenkov:nolateset, r=b-naber
resolve: Remove `ScopeSet::Late`

It's better to decouple the late/early stage from scope set, because in https://github.com/rust-lang/rust/pull/144131#discussion_r2283200511 we'll need the stage for `ScopeSet::Module` as well.

See individual commits for the refactoring details.
r? ``@b-naber``
2025-08-22 22:00:51 -04:00