about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src
AgeCommit message (Collapse)AuthorLines
2025-08-13Rollup merge of #145153 - joshtriplett:macro-kinds-plural, r=petrochenkovGuillaume Gomez-77/+78
Handle macros with multiple kinds, and improve errors (I recommend reviewing this commit-by-commit.) Switch to a bitflags `MacroKinds` to support macros with more than one kind Review everything that uses `MacroKind`, and switch anything that could refer to more than one kind to use `MacroKinds`. Add a new `SyntaxExtensionKind::MacroRules` for `macro_rules!` macros, using the concrete `MacroRulesMacroExpander` type, and have it track which kinds it can handle. Eliminate the separate optional `attr_ext`, now that a `SyntaxExtension` can handle multiple macro kinds. This also avoids the need to downcast when calling methods on `MacroRulesMacroExpander`, such as `get_unused_rule`. Integrate macro kind checking into name resolution's `sub_namespace_match`, so that we only find a macro if it's the right type, and eliminate the special-case hack for attributes. This allows detecting and report macro kind mismatches early, and more precisely, improving various error messages. In particular, this eliminates the case in `failed_to_match_macro` to check for a function-like invocation of a macro with no function-like rules. Instead, macro kind mismatches now result in an unresolved macro, and we detect this case in `unresolved_macro_suggestions`, which now carefully distinguishes between a kind mismatch and other errors. This also handles cases of forward-referenced attributes and cyclic attributes. ---- In this PR, I've minimally fixed up `rustdoc` so that it compiles and passes tests. This is just the minimal necessary fixes to handle the switch to `MacroKinds`, and it only works for macros that don't actually have multiple kinds. This will panic (with a `todo!`) if it encounters a macro with multiple kinds. rustdoc needs further fixes to handle macros with multiple kinds, and to handle attributes and derive macros that aren't proc macros. I'd appreciate some help from a rustdoc expert on that. ---- r? ````````@petrochenkov````````
2025-08-13resolve: Improve diagnostics for ambiguities in extern preludeVadim Petrochenkov-2/+9
2025-08-13resolve: Split extern prelude into two scopesVadim Petrochenkov-67/+99
One for `--extern` options and another for `extern crate` items.
2025-08-13suggest using `pub(crate)` for E0364Makai-1/+22
2025-08-12Detect and report macro kind mismatches early, and more preciselyJosh Triplett-9/+19
This eliminates the case in `failed_to_match_macro` to check for a function-like invocation of a macro with no function-like rules. Instead, macro kind mismatches now result in an unresolved macro, and we detect this case in `unresolved_macro_suggestions`, which now carefully distinguishes between a kind mismatch and other errors. This also handles cases of forward-referenced attributes and cyclic attributes. Expand test coverage to include all of these cases.
2025-08-12Only suggest changing `#[derive(mymacro)]` to `#[mymacro]` for attribute macrosJosh Triplett-0/+1
2025-08-12Switch to a bitflags `MacroKinds` to support macros with more than one kindJosh Triplett-68/+58
Review everything that uses `MacroKind`, and switch anything that could refer to more than one kind to use `MacroKinds`. Add a new `SyntaxExtensionKind::MacroRules` for `macro_rules!` macros, using the concrete `MacroRulesMacroExpander` type, and have it track which kinds it can handle. Eliminate the separate optional `attr_ext`, now that a `SyntaxExtension` can handle multiple macro kinds. This also avoids the need to downcast when calling methods on `MacroRulesMacroExpander`, such as `get_unused_rule`. Integrate macro kind checking into name resolution's `sub_namespace_match`, so that we only find a macro if it's the right type, and eliminate the special-case hack for attributes.
2025-08-11Extract ast TraitImplHeaderCameron Steffen-12/+9
2025-08-10review commentsEsteban Küber-48/+80
2025-08-10Detect struct construction with private field in field with defaultEsteban Küber-36/+138
When trying to construct a struct that has a public field of a private type, suggest using `..` if that field has a default value. ``` error[E0603]: struct `Priv1` is private --> $DIR/non-exhaustive-ctor.rs:25:39 | LL | let _ = S { field: (), field1: m::Priv1 {} }; | ------ ^^^^^ private struct | | | while setting this field | note: the struct `Priv1` is defined here --> $DIR/non-exhaustive-ctor.rs:14:4 | LL | struct Priv1 {} | ^^^^^^^^^^^^ help: the field `field1` you're trying to set has a default value, you can use `..` to use it | LL | let _ = S { field: (), .. }; | ~~ ```
2025-08-09remove `P`Deadbeef-17/+14
2025-08-08Rollup merge of #144579 - joshtriplett:mbe-attr, r=petrochenkovTrevor Gross-8/+25
Implement declarative (`macro_rules!`) attribute macros (RFC 3697) This implements [RFC 3697](https://github.com/rust-lang/rust/issues/143547), "Declarative (`macro_rules!`) attribute macros". I would suggest reading this commit-by-commit. This first introduces the feature gate, then adds parsing for attribute rules (doing nothing with them), then adds the ability to look up and apply `macro_rules!` attributes by path, then adds support for local attributes, then adds a test, and finally makes various improvements to errors.
2025-08-08mbe: Handle local `macro_rules` attr resolutionJosh Triplett-4/+16
Teach the resolver to consider `macro_rules` macros when looking for a local attribute. When looking for an attribute and considering a `macro_rules` macro, load the macro in order to see if it has attribute rules. Include a FIXME about tracking multiple macro kinds for a Def instead.
2025-08-08mbe: Handle applying attribute rules with pathsJosh Triplett-4/+9
Add infrastructure to apply an attribute macro given argument tokens and body tokens. Teach the resolver to consider `macro_rules` macros when looking for an attribute via a path. This does not yet handle local `macro_rules` attributes.
2025-08-08Rollup merge of #144912 - LorrensP-2158466:smart-resolver, r=petrochenkovStuart Cook-189/+337
Resolver: introduce a conditionally mutable Resolver for (non-)speculative resolution. This pr introduces a `CmResolver`, a wrapper around the main resolver which gives out mutable access given a condition. `CmResolver` only allows mutation when we’re not in speculative import resolution. This ensures we can’t accidentally mutate the resolver during this process, which is important as we move towards a batched resolution algorithm. This also changes functions that are used during speculative import resolution to take a `CmResolver` instead of a `&mut Resolver`. Also introduces a new kind of "smart pointer" which has the behaviour described above: ```rust /// A wrapper around a mutable reference that conditionally allows mutable access. pub(crate) struct RefOrMut<'a, T> { p: &'a mut T, mutable: bool, } type CmResolver<'r, 'ra, 'tcx> = RefOrMut<'r, Resolver<'ra, 'tcx>>; ``` r? petrochenkov
2025-08-07Introduce, implement and use CmResolver.LorrensP-2158466-189/+337
2025-08-06Introduce ModernIdent type to unify macro 2.0 hygiene handlingxizheyin-39/+47
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-08-04Rollup merge of #144746 - petrochenkov:extpreltidy, r=b-naberStuart Cook-63/+72
resolve: Cleanups and micro-optimizations to extern prelude This is what can be done without changing the structure of `ExternPreludeEntry`, like in https://github.com/rust-lang/rust/pull/144737. See individual commits for details.
2025-08-02Auto merge of #129183 - estebank:cfg-visitor, r=davidtwcobors-37/+55
Detect more `cfg`d out items in resolution errors Use a visitor to collect *all* items (including those nested) that were stripped behind a `cfg` condition. ``` error[E0425]: cannot find function `f` in this scope --> $DIR/nested-cfg-attrs.rs:4:13 | LL | fn main() { f() } | ^ not found in this scope | note: found an item that was configured out --> $DIR/nested-cfg-attrs.rs:2:4 | LL | fn f() {} | ^ note: the item is gated here --> $DIR/nested-cfg-attrs.rs:1:35 | LL | #[cfg_attr(all(), cfg_attr(all(), cfg(FALSE)))] | ^^^^^^^^^^ ```
2025-08-01Tweak rendering of cfg'd out itemEsteban Küber-29/+38
``` error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner` --> $DIR/diagnostics-cross-crate.rs:18:23 | LL | cfged_out::inner::doesnt_exist::hello(); | ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner` | note: found an item that was configured out --> $DIR/auxiliary/cfged_out.rs:6:13 | LL | #[cfg(false)] | ----- the item is gated here LL | pub mod doesnt_exist { | ^^^^^^^^^^^^ ```
2025-08-01tiny cleanupEsteban Küber-8/+5
2025-08-01Detect more `cfg`d out items in resolution errorsEsteban Küber-9/+21
Use a visitor to collect *all* items (including those nested) that were stripped behind a `cfg` condition. ``` error[E0425]: cannot find function `f` in this scope --> $DIR/nested-cfg-attrs.rs:4:13 | LL | fn main() { f() } | ^ not found in this scope | note: found an item that was configured out --> $DIR/nested-cfg-attrs.rs:2:4 | LL | fn f() {} | ^ note: the item is gated here --> $DIR/nested-cfg-attrs.rs:1:35 | LL | #[cfg_attr(all(), cfg_attr(all(), cfg(FALSE)))] | ^^^^^^^^^^ ```
2025-07-31resolve: Cleanup some uses of extern prelude in diagnosticsVadim Petrochenkov-15/+6
2025-07-31resolve: Avoid double table lookup in `extern_prelude_get`Vadim Petrochenkov-22/+29
Do not write dummy bindings to extern prelude. Use more precise `Used::Scope` while recording use and remove now redundant `introduced_by_item` check.
2025-07-31resolve: Do not add erroneous names to extern preludeVadim Petrochenkov-8/+13
2025-07-31resolve: Clarify extern prelude insertion for `extern crate` itemsVadim Petrochenkov-18/+24
2025-07-31remove rustc_attr_data_structuresJana Dönszelmann-10/+7
2025-07-31Move `ResolverOutputs` out of `rustc_middle`.Nicholas Nethercote-2/+7
It's not used in `rustc_middle`, and `rustc_resolve` is a better place for it.
2025-07-29Rollup merge of #144605 - LorrensP-2158466:cache-extern-prelude, r=petrochenkovJacob Pratt-12/+11
Resolve: cachify `ExternPreludeEntry.binding` through a `Cell` Provides interior mutability to the `binding` field of `ExternPreludeEntry` as this field behaves like a cache. Per [zulip thread](https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Parallel.20Macro.20Expansion/near/531390914). A little preparatory work for batched import resolution, which is part of [#gsoc > Project: Parallel Macro Expansion](https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Parallel.20Macro.20Expansion). r? `@petrochenkov`
2025-07-29"Cachify" `ExternPreludeEntry.binding` through a `Cell`.LorrensP-2158466-12/+11
2025-07-29Make resolve_fn_signature responsible for its own rib.Camille GILLOT-75/+54
2025-07-28Auto merge of #144469 - Kivooeo:chains-cleanup, r=SparrowLiibors-138/+129
Some `let chains` clean-up Not sure if this kind of clean-up is welcoming because of size, but I decided to try out one r? compiler
2025-07-28use let chains in mir, resolve, targetKivooeo-138/+129
2025-07-27split up define into define_extern and define_localLorrensP-2158466-56/+99
2025-07-26resolve: Do not create `NameResolution`s on access unless necessaryVadim Petrochenkov-15/+23
2025-07-26resolve: Minimize borrow scopes for `resolutions`Vadim Petrochenkov-49/+46
2025-07-25Rollup merge of #144368 - petrochenkov:rmrootscope, r=b-naberMatthias Krüger-57/+39
resolve: Remove `Scope::CrateRoot` Use `Scope::Module` with the crate root module inside instead, which should be identical. This is a simplification by itself, but it will be even larger simplification if something like https://github.com/rust-lang/rust/pull/144131 is implemented, because `Scope::CrateRoot` is also a module with two actual scopes in it (for globs and non-globs). I also did some renamings for consistency: - `ScopeSet::AbsolutePath` -> `ScopeSet::ModuleAndExternPrelude` - `ModuleOrUniformRoot::CrateRootAndExternPrelude` -> `ModuleOrUniformRoot::ModuleAndExternPrelude` - `is_absolute_path` -> `module_and_extern_prelude`
2025-07-24Rollup merge of #144334 - lolbinarycat:rustdoc-span_of_fragments-revert, ↵León Orell Valerian Liehr-13/+4
r=GuillaumeGomez rustc_resolve: get rid of unused rustdoc::span_of_fragments_with_expansion This function can cause false negatives if used incorrectly (usually "do any of the doc fragments come from a macro" is the wrong question to ask), and thus it is unused. r? `````@GuillaumeGomez`````
2025-07-24resolve: Remove `Scope::CrateRoot`Vadim Petrochenkov-57/+39
Use `Scope::Module` with the crate root module inside instead, which should be identical.
2025-07-24Auto merge of #144272 - petrochenkov:disambunder2, r=oli-obkbors-45/+62
resolve: Make disambiguators for underscore bindings module-local (take 2) The difference with https://github.com/rust-lang/rust/pull/144013 can be seen in the second commit. Now we just keep a separate disambiguator counter in every `Module`, instead of a global counter in `Resolver`. This will be ok for parallel import resolution because we'll need to lock the module anyway when updating `resolutions` and other fields in it. And for external modules the disabmiguator could be just passed as an argument to `define_extern`, without using any cells or locks, once https://github.com/rust-lang/rust/pull/143884 lands. Unblocks https://github.com/rust-lang/rust/pull/143884.
2025-07-23Auto merge of #144360 - matthiaskrgr:rollup-b6ej0mm, r=matthiaskrgrbors-2/+7
Rollup of 9 pull requests Successful merges: - rust-lang/rust#144173 (Remove tidy checks for `tests/ui/issues/`) - rust-lang/rust#144234 (Fix broken TLS destructors on 32-bit win7) - rust-lang/rust#144239 (Clean `rustc/parse/src/lexer` to improve maintainability) - rust-lang/rust#144256 (Don't ICE on non-TypeId metadata within TypeId) - rust-lang/rust#144290 (update tests/ui/SUMMARY.md) - rust-lang/rust#144292 (mbe: Use concrete type for `get_unused_rule`) - rust-lang/rust#144298 (coverage: Enlarge empty spans during MIR instrumentation, not codegen) - rust-lang/rust#144311 (Add powerpc64le-unknown-linux-musl to CI rustc targets) - rust-lang/rust#144315 (bootstrap: add package.json and package-lock.json to dist tarball) r? `@ghost` `@rustbot` modify labels: rollup
2025-07-23resolve: Change the underscore disambiguator to avoid regressionsVadim Petrochenkov-1/+5
2025-07-23resolve: Make disambiguators for underscore bindings module-localVadim Petrochenkov-45/+58
2025-07-23Ports `#[macro_use]` and `#[macro_escape]` to the new attribute parsing ↵Jonathan Brouwer-44/+30
infrastructure
2025-07-22rustc_resolve: get rid of unused rustdoc::span_of_fragments_with_expansionbinarycat-13/+4
This function can cause false negatives if used incorrectly (usually "do any of the doc fragments come from a macro" is the wrong question to ask), and thus it is unused.
2025-07-22mbe: Use concrete type for `get_unused_rule`Josh Triplett-2/+7
Rather than adding `get_unused_rule` to the `TTMacroExpander` trait, put it on the concrete `MacroRulesMacroExpander`, and downcast to that type via `Any` in order to call it. Suggested-by: Vadim Petrochenkov <vadim.petrochenkov@gmail.com>
2025-07-19Auto merge of #144172 - lqd:revert-144013, r=petrochenkovbors-58/+45
Prepare revert of 144013 This is a possible revert for rust-lang/rust#144013 causing issue rust-lang/rust#144168 (imo p-crit) to give us time to figure out a correct fix for rust-lang/rust#144013 without pressure. Feel free to close if it's an easy fix instead: r? `@petrochenkov`
2025-07-19Auto merge of #144166 - matthiaskrgr:rollup-wccepuo, r=matthiaskrgrbors-1/+1
Rollup of 10 pull requests Successful merges: - rust-lang/rust#141076 (fix Zip unsoundness (again)) - rust-lang/rust#142444 (adding run-make test to autodiff) - rust-lang/rust#143704 (Be a bit more careful around exotic cycles in in the inliner) - rust-lang/rust#144073 (Don't test panic=unwind in panic_main.rs on Fuchsia) - rust-lang/rust#144083 (miri sleep tests: increase slack) - rust-lang/rust#144092 (bootstrap: Detect musl hosts) - rust-lang/rust#144098 (Do not lint private-in-public for RPITIT) - rust-lang/rust#144103 (Rename `emit_unless` to `emit_unless_delay`) - rust-lang/rust#144108 (Ignore tests/run-make/link-eh-frame-terminator/rmake.rs when cross-compiling) - rust-lang/rust#144115 (fix outdated comment) r? `@ghost` `@rustbot` modify labels: rollup
2025-07-19Revert "resolve: Make disambiguators for underscore bindings module-local"Rémy Rakic-58/+45
This reverts commit 998df3a3e851908afd05c3318f16d99849af5c55.
2025-07-19rename `emit_unless` to `emit_unless_delay`xizheyin-1/+1
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>