Commit Graph

2800 Commits

Author SHA1 Message Date
Jonathan Brouwer 85ddb8e414 Rollup merge of #154313 - mu001999-contrib:fix/154295, r=petrochenkov
Init `self_decl` with a correct visibility

Fixes rust-lang/rust#154295

r? petrochenkov
2026-03-25 19:52:52 +01:00
Jonathan Brouwer 6e3c17424d Rollup merge of #153702 - SpriteOvO:guard-matcher, r=davidtwco
Add macro matcher for `guard` fragment specifier

Tracking issue #153104

This PR implements a new `guard` macro matcher to match `if-let` guards (specifically [`MatchArmGuard`](https://github.com/rust-lang/reference/blob/50a1075e879be75aeec436252c84eef0fad489f4/src/expressions/match-expr.md#match-guards)). In the upcoming PR, we can use this new matcher in the `matches!` and `assert_matches!` macros to support their use with `if-let` guards. (see #152313)

The original `Expr` used to represent a guard has been wrapped in a new `Guard` type, allowing us to carry the span information of the leading `if` keyword. However, it might be even better to include the `if` keyword in the `Guard` type as well? I've left a FIXME comment in the code.
2026-03-25 19:52:50 +01:00
mu001999 01795c3eab Init self_decl with a correct vis 2026-03-25 21:41:33 +08:00
Guillaume Gomez 5d7f89417c Remove more BuiltinLintDiag in rustc_resolve 2026-03-24 11:47:03 +01:00
Jonathan Brouwer d7beee5f18 Rollup merge of #154224 - GuillaumeGomez:migrate-diag, r=JonathanBrouwer
Remove more `BuiltinLintDiag` variants

Part of https://github.com/rust-lang/rust/issues/153099.

r? @JonathanBrouwer
2026-03-23 20:18:35 +01:00
Jonathan Brouwer 924b91147b Rollup merge of #153912 - mu001999-contrib:fix-153842, r=petrochenkov
Avoid prematurely choosing a glob import

Fixes rust-lang/rust#153842

Use the following without introducing trait to explain:
```rust
mod a {
    pub use crate::x::y as x; // single import #1
}

mod b {
    pub mod x {
        pub mod y {}
    }
}

use a::x; // single import #2
use b::*; // glob import #3

fn main() {}
```

In current implementation, when `#1` is first resolved, `crate::x` is temporarily taken from glob import `#3` as `crate::b::x`. This happens because `single_import_can_define_name` will see that `#2` cannot define `x` (because it depends on `#1` and `#1` is ignored) and then return `false`. Later, during finalization, `crate::x` in `#1` resolves through single import `#2` instead, which no longer matches the initially cached module `crate::b::x` and triggers the ICE.

I think the resolver should keep this unresolved because `#2` may still define `x` to avoid prematurely choosing a glob import.

r? petrochenkov
2026-03-23 20:18:33 +01:00
Jonathan Brouwer 64716caf69 Rollup merge of #154182 - Unique-Usman:ua/constnotype2, r=estebank
diagnostics: avoid ICE for undeclared generic parameter in impl

Avoid an ICE for:

    struct A;
    impl A<B> {}

The compiler no longer panics and can proceed to emit existing diagnostics.

Adds `tests/ui/missing/undeclared-generic-parameter.rs`.

Fixes rust-lang/rust#154165 and introduced by rust-lang/rust#152913
2026-03-23 12:15:00 +01:00
Jonathan Brouwer 7d43ebc519 Rollup merge of #153312 - b-naber:namespaced-crate-names-pt1, r=petrochenkov
Packages as namespaces part 1

Part 1 of https://github.com/rust-lang/rust/pull/152299

r? @petrochenkov
2026-03-23 12:14:52 +01:00
Guillaume Gomez 2fcd8a71f7 Remove more BuiltinLintDiag in rustc_resolve 2026-03-22 20:32:22 +01:00
Usman Akinyemi b31dc4ab49 diagnostics: avoid ICE for undeclared generic parameter in impl
Avoid an ICE for:

    struct A;
    impl A<B> {}

The compiler no longer panics and can proceed to emit existing diagnostics.

Adds `tests/ui/missing/undeclared-generic-parameter.rs`.

Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
2026-03-21 21:54:46 +05:30
b-naber e844c24122 resolve namespaced crates with open modules 2026-03-21 12:38:17 +00:00
Jonathan Brouwer edf808b524 Rollup merge of #154154 - estebank:issue-134087, r=mati865
Emit fewer errors for incorrect rtn and `=` -> `:` typos in bindings

Make all existing `:` -> `=` typo suggestions verbose and tweak the suggested code.

Stash parse errors when pasing rtn that doesn't use `(..)`.

Emit single error on `Foo::bar(qux)` that looks like rtn in incorrect position and suggest `Foo::bar(..)`.
```
error: return type notation not allowed in this position yet
  --> $DIR/let-binding-init-expr-as-ty.rs:18:10
   |
LL | struct K(S::new(()));
   |          ^^^^^^^^^^
   |
help: furthermore, argument types not allowed with return type notation
   |
LL - struct K(S::new(()));
LL + struct K(S::new(..));
   |
```
On incorrect rtn in let binding type for an existing inherent associated function, suggest `:` -> `=` to turn the "type" into a call expression. (Fix rust-lang/rust#134087)
```
error: expected type, found associated function call
  --> $DIR/let-binding-init-expr-as-ty.rs:29:12
   |
LL |     let x: S::new(());
   |            ^^^^^^^^^^
   |
help: use `=` if you meant to assign
   |
LL -     let x: S::new(());
LL +     let x = S::new(());
   |
```
2026-03-21 00:42:46 +01:00
Esteban Küber 244322f0be Make : -> = typo suggestion verbose 2026-03-20 19:07:28 +00:00
bors 461e9738a4 Auto merge of #153489 - aerooneqq:two-phase-hir, r=petrochenkov
ty-aware delayed AST -> HIR lowering



This PR implements a prototype of ty-aware delayed AST -> HIR lowering. Part of rust-lang/rust#118212.

r? @petrochenkov

# Motivation

When lowering delegation in perfect scenario we would like to access the ty-level information, in particular, queries like `generics_of`, `type_of`, `fn_sig` for proper HIR generation with less hacks. For example, we do not want to generate more lifetimes than needed, because without ty-level queries we do not know which delegee's lifetimes are late-bound. Next, consider recursive delegations, for their proper support without ty we would have to either duplicate generics inheritance code in AST -> HIR lowering or create stubs for parts of the HIR and materialize them later. We already use those queries when interacting with external crates, however when dealing with compilation of a local crate we should use resolver for similar purposes. Finally, access to ty-level queries is expected to help supporting delegation to inherent impls, as we can not resolve such calls during AST -> HIR stage.

# Benefits

We eliminate almost all code that uses resolver in delegation lowering:
- Attributes inheritance is done without copying attributes from AST at resolve stage
- Fn signatures are obtained from `tcx.fn_sig`
- Param counts are also obtained from `tcx.fn_sig`
- `is_method` function now uses `tcx.associated_item` instead of resolver
- Generics are now inherited through `get_external_generics` that uses `tcx.generics_of`. Generics for recursive delegations should also work
- `DelegationIds` that stored paths for recursive delegations is removed, we now use only `delegee_id`
- Structs that were used for storing delegation-related information in resolver are almost fully removed
- `ast_index` is no more used

# Next steps
- Remove creating generic params through AST cloning, proper const types propagation
- Inherent impls

# High level design overview

## Queries
We store ids of delayed items to lower in `Crate` struct. During first stage of lowering, owners that correspond to delayed items are filled with `MaybeOwner::Phantom`. 

Next, we define two new queries: `lower_delayed_owner`, `delayed_owner` and a function `force_delayed_owners_lowering`.

The first query is used when lowering known (which is in `delayed_ids`) delayed owner. 

The second is fed with children that were obtained during lowering of a delayed owner (note that the result of lowering of a single `LocalDefId` is not a single `MaybeOwner`, its a list of `(LocalDefId, MaybeOwner)` where the first `MaybeOwner` corresponds to delayed `LocalDefId` and others to children that were obtained during lowering (i.e. generic params)). By default `delayed_owner` returns `MaybeOwner::Phantom`. As we do not want to predict the whole list of children which will be obtained after lowering of a single delayed item, we need to store those children somewhere. There are several options:
- Make the return type of `lower_delayed_owner` to be `FxIndexMap<LocalDefId, MaybeOwner>` and search children here. Search will be either linear or we can introduce a map somewhere which will track parent-child relations between a single delayed `LocalDefId` and its children. 
- Try to make query that will lower all delayed items in a loop and return a complete map of all delayed `LocalDefIds` and their children. In this case there will be problems with delayed items that require information about other delayed items.

By using proposed queries we handle the second concern, and in case of acyclic dependencies between delayed ids it automatically works, moreover we use queries as cache for delayed `MaybeOwners`. The only invariant here is that queries which are invoked during delayed AST -> HIR lowering of some `LocalDefId` should not in any way access children of other, yet unlowered, delayed `LocalDefId`, they should firstly materialize it.

The `force_delayed_owners_lowering` forces lowering of all delayed items and now integrated in `hir_crate_items` query. 

## Resolver for lowering

> ~Currently the `resolver_for_lowering` is stolen in `lower_to_hir` function, however we want to prolong its life until all delayed `LocalDefIds` are materialized. For this purpose we borrow `resolver_for_lowering` in `lower_to_hir` and drop it after forcing materialization of all delayed `LocalDefId` in `rustc_interface::run_required_analyses`.~

We split resolver for lowering into two parts: the first part is a readonly part that came to us from resolve, the second part is a mutable part that can be used to add or overwrite values in the readonly part. Such splitted resolver is used during delayed lowering, as we can't steal it.

## AST index

Lowering uses an AST index. It is created in `lower_to_hir` function and it references parts of AST. We want to avoid reindexing AST on every delayed `LocalDefId` lowering, however now it is not clear how to properly do it. As delayed HIR lowering is used only for delegation unstable feature it should not affect other use-cases of the compiler. But it will be reworked sooner or later.
2026-03-20 10:56:01 +00:00
aerooneqq 5c441e6ce6 ty-aware delayed AST -> HIR lowering 2026-03-20 12:31:48 +03:00
bors 86c839ffb3 Auto merge of #154123 - Zalathar:rollup-MUEvgV7, r=Zalathar
Rollup of 15 pull requests

Successful merges:

 - rust-lang/rust#152909 (sess: `-Zbranch-protection` is a target modifier)
 - rust-lang/rust#153556 (`impl` restriction lowering)
 - rust-lang/rust#154048 (Don't emit rustdoc `missing_doc_code_examples` lint on impl items)
 - rust-lang/rust#150935 (Introduce #[diagnostic::on_move(message)])
 - rust-lang/rust#152973 (remove -Csoft-float)
 - rust-lang/rust#153862 (Rename `cycle_check` to `find_cycle`)
 - rust-lang/rust#153992 (bootstrap: Optionally print a backtrace if a command fails)
 - rust-lang/rust#154019 (two smaller feature cleanups)
 - rust-lang/rust#154059 (tests: Activate `must_not_suspend` test for `MutexGuard` dropped before `await`)
 - rust-lang/rust#154075 (Rewrite `query_ensure_result`.)
 - rust-lang/rust#154082 (Updates derive_where and removes workaround)
 - rust-lang/rust#154084 (Preserve braces around `self` in use tree pretty printing)
 - rust-lang/rust#154086 (Insert space after float literal ending with `.` in pretty printer)
 - rust-lang/rust#154087 (Fix whitespace after fragment specifiers in macro pretty printing)
 - rust-lang/rust#154109 (tests: Add regression test for async closures involving HRTBs)
2026-03-20 07:22:03 +00:00
Stuart Cook 734cca0053 Rollup merge of #150935 - rperier:provide_diagnostic_on_move_for_smart_pointers, r=JonathanBrouwer
Introduce #[diagnostic::on_move(message)]

cc rust-lang/rust#149862

This is a first proposal. I have deliberately kept it simpler than `diagnostic::on_unimplemented`.

Few questions/remarks:

- Do I need to move the OnMoveDirective logic into a dedicated module perhaps ? let's say into compiler/rustc_borrowck/src/diagnostics/on_move.rs
- No problems to depend on crates like `rustc_ast` from the borrowck ?
- Notes are not supported yet. While message and label are very static , in the sense that they are emitted in the same way from the same place in the borrowck, it is not the case for the notes. It would make the code more complex. But, I can add support for notes if it does make sense.

Suggestions are welcomed !
2026-03-20 15:33:05 +11:00
Stuart Cook 85b8c32e7c Rollup merge of #153556 - CoCo-Japan-pan:impl-restriction-lowering, r=petrochenkov
`impl` restriction lowering

This PR is linked to a [GSoC proposal](https://github.com/rust-lang/google-summer-of-code?tab=readme-ov-file#implementing-impl-and-mut-restrictions) and is part of the progress toward implementing `impl` restrictions proposed in [RFC 3323](https://rust-lang.github.io/rfcs/3323-restrictions.html).
This PR implements path resolution for `impl` restrictions. The resolution is performed in `rustc_resolve/src/late.rs` using `smart_resolve_path`.
This PR also checks whether the restricted module or crate is an ancestor. If it is not, it emits a restriction-specific counterpart to visibility’s `AncestorOnly` error.

r? @Urgau
cc @jhpratt
2026-03-20 15:33:04 +11:00
CoCo-Japan-pan cec0a68d6a Resolve paths for impl restrictions using smart_resolve_path 2026-03-19 19:05:21 +09:00
Usman Akinyemi f60b6499d8 rustc_resolve: improve const generic errors
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
2026-03-19 11:49:30 +05:30
Jonathan Brouwer 626f9d39da Rollup merge of #153622 - RalfJung:rm-soft-unstable, r=JonathanBrouwer
remove concept of soft-unstable features

Implements https://github.com/rust-lang/compiler-team/issues/972

@rust-lang/libs-api last chance to speak up before this feature is gone :)

Fixes https://github.com/rust-lang/rust/issues/153572
Fixes https://github.com/rust-lang/rust/issues/64266
2026-03-17 17:51:32 +01:00
mu001999 757f224bce Use nearest non block module as the correct parent module 2026-03-16 22:22:25 +08:00
mu001999 2d419b597c Rename parent_module to parent_scope 2026-03-16 22:21:54 +08:00
mu001999 5316bab299 Avoid choosing a glob import if a single import could define the same with current import 2026-03-15 18:51:45 +08:00
Asuna 9729bb2558 Add macro matcher for guard fragment specifier 2026-03-12 02:18:34 +00:00
Romain Perier 965f1e7681 Introduce #[diagnostic::on_move]
This might be helpful for smart pointers to explains why they aren't Copy
and what to do instead or just to let the user know that .clone() is very
cheap and can be called without a performance penalty.
2026-03-11 16:56:19 +01:00
bors b2fabe39bd Auto merge of #153673 - JonathanBrouwer:rollup-cGOKonI, r=JonathanBrouwer
Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#153560 (Introduce granular tidy_ctx's check in extra_checks)
 - rust-lang/rust#153666 (Add a regression test for rust-lang/rust#153599)
 - rust-lang/rust#153493 (Remove `FromCycleError` trait)
 - rust-lang/rust#153549 (tests/ui/binop: add annotations for reference rules)
 - rust-lang/rust#153641 (Move `Spanned`.)
 - rust-lang/rust#153663 (Remove `TyCtxt::node_lint` method and `rustc_middle::lint_level` function)
 - rust-lang/rust#153664 (Add test for rust-lang/rust#109804)
2026-03-11 05:12:10 +00:00
Nicholas Nethercote c12ab08c14 Move Spanned.
It's defined in `rustc_span::source_map` which doesn't make any sense
because it has nothing to do with source maps. This commit moves it to
the crate root, a more sensible spot for something this basic.
2026-03-11 06:25:23 +11:00
Oli Scherer 40ac87e299 Mark an unreachable match arm as such
Synchronize which elements have code paths and which are unreachable betwen def collector and build reduced graph
2026-03-10 13:57:22 +00:00
Ralf Jung 59dfb9ef46 remove concept of soft-unstable features 2026-03-09 20:21:39 +01:00
Zalathar 985b41d387 Remove the rustc_data_structures::assert_matches! re-exports 2026-03-08 22:02:23 +11:00
bors e370b60cf2 Auto merge of #153544 - JonathanBrouwer:rollup-LT1ogBG, r=JonathanBrouwer
Rollup of 2 pull requests

Successful merges:

 - rust-lang/rust#153462 (Bootstrap update)
 - rust-lang/rust#152210 (Gate #![reexport_test_harness_main] properly)
2026-03-07 20:04:00 +00:00
Josh Stone 32bae1353e Update cfg(bootstrap) 2026-03-07 10:42:02 -08:00
Jonathan Brouwer 10f2c48549 Rollup merge of #153494 - aerooneqq:boxed-trait-candidates-to-hir-arena, r=petrochenkov
Replace Box<[TraitCandidate]> with &'hir [TraitCandidate<'hir>]

This PR allocates trait candidates on HIR arena and replaces `remove` with `get` in `ResolverAstLowering`. First step for rust-lang/rust#153489.

r? @petrochenkov
2026-03-07 17:37:20 +01:00
Jonathan Brouwer 4e0b64408c Fix newly detected subdiagnostics using variables from parent 2026-03-06 19:00:25 +01:00
Jonathan Brouwer 10eb844bac Remove eagerly_format_to_string from DiagCtxt 2026-03-06 18:52:11 +01:00
aerooneqq dc31c155bc Replace Box<[TraitCandidate]> with &'hir [TraitCandidate<'hir>] 2026-03-06 16:02:41 +03:00
Jonathan Brouwer d8092147fe Rename translation -> formatting 2026-03-04 17:47:24 +01:00
Stuart Cook c4f239ec6a Rollup merge of #153352 - GuillaumeGomez:migrate-diag, r=JonathanBrouwer
Migration of `LintDiagnostic` - part 6

Part of https://github.com/rust-lang/rust/issues/153099.

r? @JonathanBrouwer
2026-03-04 11:54:11 +11:00
Guillaume Gomez b5ff567193 Migrate more types from LintDiagnostic to Diagnostic 2026-03-03 21:57:02 +01:00
Jonathan Brouwer f3f04699c1 Rollup merge of #153319 - DanielEScherzer:ie-dots, r=jhpratt
Comments and docs: add missing periods to "ie."

"i.e." is short for the Latin "id est" and thus both letters should be followed by periods.
2026-03-03 13:08:45 +01:00
Jonathan Brouwer 7174c100f4 Rollup merge of #153227 - kpreid:struct-missing-field, r=estebank
Don’t report missing fields in struct exprs with syntax errors.

@Noratrieb [told me](https://internals.rust-lang.org/t/custom-cargo-command-to-show-only-errors-avoid-setting-rustflags-every-time/24032/7?u=kpreid) that “it is a bug if this recovery causes follow-up errors that would not be there if the user fixed the first error.” So, here’s a contribution to hide a follow-up error that annoyed me recently.

Specifically, if the user writes a struct literal with a syntax error, such as

```rust
StructName { foo: 1 bar: 2 }
```

the compiler will no longer report that the field `bar` is missing in addition to the syntax error.

This is my first time attempting any change to the parser or AST; please let me know if there is a better way to do what I’ve done here. ~~The part I’m least happy with is the blast radius of adding another field to `hir::ExprKind::Struct`, but this seems to be in line with the style of the rest of the code. (If this were my own code, I would consider changing `hir::ExprKind::Struct` to a nested struct, the same way it is in `ast::ExprKind`.)~~ The additional information is now stored as an additional variant of `ast::StructRest` / `hir::StructTailExpr`.

**Note to reviewers:** I recommend reviewing each commit separately, and in the case of the first one with indentation changes ignored.
2026-03-03 07:14:12 +01:00
Daniel Scherzer 158ac1a10b Comments and docs: add missing periods to "ie."
"i.e." is short for the Latin "id est" and thus both letters should be followed
by periods.
2026-03-02 18:11:13 -08:00
Kevin Reid d6eefce00d Remember whether a struct literal had syntax errors.
This adds a variant `NoneWithError` to AST and HIR representations of
the “rest” or “tail”, which is currently always treated identically to
the `None` variant.
2026-02-28 18:12:34 -08:00
bors 38c0de8dcb Auto merge of #153050 - JayanAXHF:refactor/change-is-type-const, r=BoxyUwU
refactor(mgca): Change `DefKind::Const` and `DefKind::AssocConst` to have a `is_type_const` flag



Addresses rust-lang/rust#152940 

- Changed `DefKind::Const` and `DefKind::AssocConst` to have a `is_type_const` flag.
- changed `is_type_const` query to check for this flag
- removed `is_rhs_type_const` query

r? @BoxyUwU
2026-02-28 18:27:06 +00:00
JayanAXHF efc150e5b3 refactor(mgca): Change DefKind::Const and DefKind::AssocConst to have a is_type_const flag
* refactor: add `is_type_const` flag to `DefKind::Const` and `AssocConst`
* refactor(cleanup) remove the `rhs_is_type_const` query
* style: fix formatting
* refactor: refactor stuff in librustdoc for new Const and AssocConst
* refactor: refactor clippy for the changes
* chore: formatting
* fix: fix test
* fix: fix suggestions
* Update context.rs

Co-authored-by: Boxy <rust@boxyuwu.dev>
* changed AssocKind::Const to store data about being a type const
2026-02-28 17:27:46 +00:00
Jonathan Brouwer f3035e7c18 Rollup merge of #153190 - JonathanBrouwer:subdiag_variables, r=jdonszelmann
Don't allow subdiagnostic to use variables from their parent

Tangentially related to https://github.com/rust-lang/rust/issues/151366

This is PR 1/2 for structured diagnostics, will do the unstructured ones next. After the second PR I will be able to remove some code that should compensate for this PR being positive.

Regardless of this PR having a positive diff, I feel that subdiagnostics being able to use variables from their parent is very confusing, so this is for the better,.

r? @jdonszelmann
2026-02-28 12:52:57 +01:00
Jonathan Brouwer d0a2fe848d Rollup merge of #153187 - is57primenumber:fix-ice-on-shadowing, r=petrochenkov
Fix ICE when macro-expanded extern crate shadows std

Fixes rust-lang/rust#152895

This PR fixes an ICE that occurs when a macro-expanded `extern crate` shadows a prelude name, which leaves the resolver in an inconsistent state due to `issue_145575_hack`.

I disabled the assertion when the `issue_145575_hack` flag is enabled.
The fix is applied in two locations to cover both cases (whether `is_error_from_last_segment` is true or false).
This approach was inspired by the context and fix discussed in rust-lang/rust#151213.
2026-02-28 12:52:56 +01:00
Jonathan Brouwer 350a964d7c Fix subdiagnostics that use non-local variables 2026-02-27 15:22:47 +00:00
is57primenumber 54c505fff0 Fix ICE when macro-expanded extern crate shadows std 2026-02-27 22:54:10 +09:00