Commit Graph

2979 Commits

Author SHA1 Message Date
Jonathan Brouwer 604d124199 Rollup merge of #156500 - Bryanskiy:macros_vis, r=petrochenkov
Privacy: move macros handling to early stage

The patch moves effective visibility computation for macros from `rustc_privacy` to `rustc_resolve`. It will enable this optimization: https://github.com/rust-lang/rust/pull/156228.

However, I found some problems with macro handling while I was doing this. The current implementation was written ~6 years ago and checks the reachability of a definition from a macro by nominal visibility. In general this is incorrect.

For example, in the current implementation modules are not traversed if their nominal visibility is less then the nominal visibility of a module defining macro: https://github.com/rust-lang/rust/blob/29b7590130c83542a095cdf1323ed0f78eec2bb8/compiler/rustc_privacy/src/lib.rs#L618-L626

As a result, in order to compile code like  `tests/ui/definition-reachable/auxiliary/field-method-macro.rs`. we have to additionally traverse types of adt fields: https://github.com/rust-lang/rust/blob/29b7590130c83542a095cdf1323ed0f78eec2bb8/compiler/rustc_privacy/src/lib.rs#L628-L638

This is a hack and the proper solution would be to check definitions with `EffectiveVisibilities::is_reachable`. I haven’t done this yet, as it would start to trigger many lints as more items become reachable. I think it’s better to leave the change to another commit.

r? @petrochenkov
2026-05-14 00:35:36 +02:00
Bryanskiy 3d0ee528cb Privacy: move macros handling to early stage 2026-05-13 20:04:42 +03:00
Oli Scherer 3a2ae6a204 Stop putting owner's DefId into their own node_id_to_def_id map
Now they unfortunately land in their parent, which is not necessary
2026-05-13 09:29:22 +02:00
Oli Scherer adb2b0fd57 Split the resolver tables into per-owner tables, starting with node_id_to_def_id 2026-05-13 09:22:55 +02:00
Oli Scherer 66a2b8565f Make UnordItems::flat_map take UnordItems as args, too 2026-05-13 08:47:29 +02:00
Oli Scherer 1887b9cd74 Split trait item resolving into its own method 2026-05-13 08:47:29 +02:00
Jonathan Brouwer 63322d00e9 Rollup merge of #156284 - petrochenkov:kvak2, r=mu001999
resolve: Fix a false positive "cannot reexport" error for ambiguous glob sets

Fixes https://github.com/rust-lang/rust/issues/156264.
2026-05-12 18:53:39 +02:00
Jonathan Brouwer 4ce7ac120c Rollup merge of #156287 - cyrgani:unbox-2, r=petrochenkov
move more compiler crates away from `box_patterns`

Part of https://github.com/rust-lang/rust/issues/156110.
2026-05-12 18:53:35 +02:00
Jonathan Brouwer e383845f33 Rollup merge of #154918 - fmease:fix-enum-var-lt, r=jackh726
Fix elided lifetime resolution & trait object lifetime defaulting for enum variant paths

Context: https://github.com/rust-lang/rust/pull/129543#discussion_r3035614838.
Fixes rust-lang/rust#108224.

I consider this to be a minor bug fix that doesn't demand a (T-types) FCP.

r? @lcnr or reassign
2026-05-12 07:03:56 +02:00
cyrgani 985fe34821 use deref_patterns in rustc_resolve 2026-05-10 21:05:36 +00:00
Vadim Petrochenkov a07cc548e4 resolve: Module-related refactorings 2026-05-10 20:11:34 +03:00
Vadim Petrochenkov cc630bc470 resolve: Evaluate private visibilities eagerly in eff vis computation 2026-05-10 08:06:03 +03:00
Vadim Petrochenkov 16729bcb2d resolve: Set correct parent and expansion for self declarations 2026-05-09 11:19:29 +03:00
Matthias Krüger 62396df898 Rollup merge of #156244 - el-ev:issue156060, r=mu001999
fix incorrect suggestions in private import diagnostic

Resolves rust-lang/rust#156060.

1. In nested imports like `use two::{One, ...}`, the diagnostic suggested replacing the `One` with a multi-segment path of a different module, producing invalid code like `use crate::two::{one::One, Two}`. Skip it when `single_nested == true`.
2. Stop unconditionally skipping the first segment of `import.module_path`, which can produce incorrect paths in edition 2018 and later.
3. Mark the suggestion as "directly" instead of "through the re-export" when the import's source is the definition itself.
2026-05-08 20:39:25 +02:00
Iris Shi b7d22fd68b refactor path construction
Co-Authored-By: mu001999 <mu001999@outlook.com>
2026-05-08 16:48:29 +08:00
Iris Shi 1ad59ef182 mark the suggestion as directly when it is 2026-05-08 10:42:03 +08:00
Vadim Petrochenkov 503960c9a5 resolve: Avoid some Arc cloning with syntax extensions
Clone only on interface boundaries with `rustc_expand`
2026-05-07 20:23:40 +03:00
Vadim Petrochenkov c834e9fcbd resolve: Remove MacroData
All the necessary data can be taken from `SyntaxExtension` instead.
2026-05-07 20:23:37 +03:00
Vadim Petrochenkov 235fa6b34c resolve: Fix a false positive "cannot reexport" error for ambiguous glob sets 2026-05-07 19:29:09 +03:00
Iris Shi 05b97f76a4 address review comment 2026-05-07 23:43:25 +08:00
Jacob Pratt ac468d573b Rollup merge of #156173 - oli-obk:fewer-global-lookups, r=petrochenkov
Fewer global node_id_to_def_id lookups

Several of these are unnecessary if we track the `LocalDefId` together with the `NodeId`. We can't remove the `NodeId` entirely, as it is needed for lints, but it's a useful refactoring for splitting node_id_to_def_id into a per-owner table in the future

r? @petrochenkov
2026-05-07 02:12:07 -04:00
Jonathan Brouwer b1d22623ab Rollup merge of #156151 - mejrs:non_macro_attr, r=petrochenkov
Don't return dummy MacroData in `get_macro`

I was experimenting with tool attributes and ast manipulation, and wasted some time figuring out that this was happening. AFAIK all users of `get_macro` are expecting an actual macro (and none were reading the dummy MacroData) so there should be no change in behavior.
2026-05-06 22:39:20 +02:00
Iris Shi a2c0e02be8 skip incorrect suggestion in nested imports 2026-05-06 21:49:37 +08:00
mejrs facd03622b Don't return dummy MacroData in get_macro 2026-05-06 14:39:39 +02:00
Oli Scherer a082567e7b Follow-up cleanups reusing the now-available LocalDefIds 2026-05-06 10:58:52 +02:00
Oli Scherer 400240adb4 Avoid hitting the global node_id_to_def_id table for unused macros 2026-05-06 10:58:52 +02:00
Oli Scherer 191cda55e4 Avoid using id followed by local_def_id if we can just call def_id instead 2026-05-06 10:58:52 +02:00
Oli Scherer 0afe083fd1 Avoid some global node_id_to_def_id lookups in import resolution 2026-05-06 10:58:52 +02:00
Jonathan Brouwer e35334f6d4 Rollup merge of #156014 - petrochenkov:kvak, r=mu001999
resolve: Catch "cannot reexport" errors from macros 2.0 better

After the macro 2.0 related holes are closed we can report `span_delayed_bug`s in more situations.

Merging https://github.com/rust-lang/rust/pull/155945 would make changes in this PR simpler, but that PR will probably have to wait for quite some time.

This is a continuation of my import & privacy invariant hardening changes from https://github.com/rust-lang/rust/pull/155257, https://github.com/rust-lang/rust/pull/155213, https://github.com/rust-lang/rust/pull/154149, etc.

r? @mu001999
2026-05-05 14:25:22 +02:00
Oli Scherer fd701b79a7 Remove most uses of def_id_to_node_id
Especially those happening during normal resolving, where we are actually looking at current parent scopes
2026-05-04 17:40:26 +02:00
Oli Scherer 71177d1b66 Add NodeId to ModuleKind 2026-05-04 17:40:26 +02:00
Vadim Petrochenkov 21c58d8ad9 resolve: Harden some invariant checks for visibilities 2026-05-04 15:51:36 +03:00
Vadim Petrochenkov 65e5bd6610 resolve: Catch "cannot reexport" errors from macros 2.0 better 2026-05-04 15:51:36 +03:00
Vadim Petrochenkov cf636ec649 resolve: Factor out reporting of "cannot reexport" errors into a separate function 2026-05-04 15:51:33 +03:00
Jacob Pratt bd8d0260ca Rollup merge of #156091 - Bryntet:move-tools, r=JonathanBrouwer
change field `tools` on `AttributeParser` to hold `&'tcx RegisteredTools`

Makes tools actually stored, and not just tool names

this was originally part of rust-lang/rust#155691 but was split out to make that PR smaller.

r? @petrochenkov
cc @JonathanBrouwer
2026-05-03 00:25:34 -04:00
mejrs 4a13f36265 Remove more spans from AttributeKind 2026-05-02 17:41:30 +02:00
Edvin Bryntesson 76fd1b3df3 make tools on AttributeParser hold reference to RegisteredTools 2026-05-02 16:02:34 +02:00
Jonathan Brouwer 7be04ef0a6 Rollup merge of #155948 - SynapLink:fix/pub-visibility-order, r=petrochenkov,mu001999
Fix order-dependent visibility diagnostics

Fixes rust-lang/rust#40066.
Fixes https://github.com/rust-lang/rust/issues/109657.
Delay visibility path diagnostics until module collection has finished, so paths to later non-ancestor modules report E0742 instead of an unresolved path error.
2026-05-01 13:10:34 +02:00
Jacob Pratt 894bdf298e Rollup merge of #155980 - nnethercote:mv-feature-methods, r=TaKO8Ki
Move `feature*` methods from `parse` mod to `errors` mod.

As the FIXME comment says, these no longer use `ParseSess` and so the `parse` mod is not a good place for them. The `errors` mod is a better home.

r? @TaKO8Ki
2026-04-30 22:28:33 -04:00
Jacob Pratt 654c350837 Rollup merge of #154610 - jakubadamw:issue-13065, r=TaKO8Ki
Suggest public re-exports when a private module makes an import path inaccessible

This is an attempt at solving rust-lang/rust#13065.

When a `use` path fails because it passes through a private module (E0603), and a public re-export of the target item exists elsewhere, the compiler will now suggest importing through that re-export instead.

For example, given:

```rust
mod outer {
    pub use self::inner::MyStruct;
    mod inner {
        pub struct MyStruct;
    }
}

use outer::inner::MyStruct; // error: module `inner` is private
```

the compiler will now suggest use `outer::MyStruct`; - the publicly accessible path - rather than just pointing at the private module definition and leaving the user to figure out the alternative.

When possible, relative paths are suggested, including those using `super` (currently capped at a maximum of one `super` path item).

The newly added test is parametrised by editions because of the change in behaviour around `crate::`-prefixed imports in edition 2018. Perhaps that’s an overkill – I’ll be happy to remove the variations for editions 2021 and 2024.

Closes rust-lang/rust#13065.
2026-04-30 22:28:31 -04:00
Jacob Pratt 248756dc40 Rollup merge of #153566 - JohnTitor:sugg-generic-params-from-outer-item-err, r=wesleywiser
Add suggestion for E0401 on inner const items

Fix rust-lang/rust#68373
r? @estebank
2026-04-30 22:28:30 -04:00
SynapLink f7c62f533e Fix order-dependent visibility diagnostics 2026-04-30 20:51:53 +02:00
Nicholas Nethercote 6d79bc6f01 Move feature* methods from parse mod to errors mod.
As the FIXME comment says, these no longer use `ParseSess` and so the
`parse` mod is not a good place for them. The `errors` mod is a better
home.
2026-04-30 12:02:33 +10:00
Vadim Petrochenkov d28ea81a98 resolve: Extend ambiguous_import_visibilities deprecation lint to glob-vs-glob ambiguities 2026-04-29 22:34:16 +03:00
Jonathan Brouwer 67cf0dee49 Remove most usages of Stage 2026-04-29 07:41:14 +02:00
Jonathan Brouwer e1332f4893 Remove should_emit from stage 2026-04-29 07:41:09 +02:00
mu001999 d93395c3ff Remove the special case of ::self after edition 2018 2026-04-27 14:02:25 +08:00
mu001999 8b584ebcf6 Remove type_ns_only 2026-04-27 14:02:25 +08:00
mu001999 4467940555 Support trailing self in normal paths 2026-04-27 14:02:22 +08:00
mu001999 d5a26d811b Support trailing self in import paths 2026-04-27 10:58:30 +08:00