about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src/macros.rs
AgeCommit message (Collapse)AuthorLines
2025-09-26Rollup merge of #146283 - LorrensP-2158466:resolve-cm-cell, r=petrochenkovMatthias Krüger-1/+1
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-25Introduce and use CmCell during import resolution.LorrensP-2158466-1/+1
2025-09-14Move more early buffered lints to dyn lint diagnostics (2/N)León Orell Valerian Liehr-1/+1
2025-08-22resolve: `early_resolve_ident_in_lexical_scope` -> `resolve_ident_in_scope_set`Vadim Petrochenkov-4/+4
2025-08-14resolve: Do not call `resolve_macro_path` from late resolutionVadim Petrochenkov-9/+6
`maybe_resolve_path` is less precise in corner cases, but it's only used for diagnostics and error recovery, so it's good enough.
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-33/+23
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-08Rollup merge of #144579 - joshtriplett:mbe-attr, r=petrochenkovTrevor Gross-3/+7
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 applying attribute rules with pathsJosh Triplett-3/+7
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-25/+35
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-25/+35
2025-08-06Introduce ModernIdent type to unify macro 2.0 hygiene handlingxizheyin-2/+2
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-07-31remove rustc_attr_data_structuresJana Dönszelmann-1/+2
2025-07-28Auto merge of #144469 - Kivooeo:chains-cleanup, r=SparrowLiibors-32/+31
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-32/+31
2025-07-27split up define into define_extern and define_localLorrensP-2158466-1/+1
2025-07-24Auto merge of #144272 - petrochenkov:disambunder2, r=oli-obkbors-1/+1
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-23resolve: Make disambiguators for underscore bindings module-localVadim Petrochenkov-1/+1
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-19Revert "resolve: Make disambiguators for underscore bindings module-local"Rémy Rakic-1/+1
This reverts commit 998df3a3e851908afd05c3318f16d99849af5c55.
2025-07-18Rollup merge of #144013 - petrochenkov:disambunder, r=oli-obkMatthias Krüger-1/+1
resolve: Make disambiguators for underscore bindings module-local Disambiguators attached to underscore name bindings (like `const _: u8 = something;`) do not need to be globally unique, they just need to be unique inside the module in which they live, because the bindings in a module are basically kept as `Map<BindingKey, SomeData>`. Also, the specific values of the disambiguators are not important, so a glob import of `const _` may have a different disambiguator than the original `const _` itself. So in this PR the disambiguator is just set to the current number of bindings in the module. This removes one more piece of global mutable state from resolver and unblocks https://github.com/rust-lang/rust/pull/143884.
2025-07-17resolve: Make disambiguators for underscore bindings module-localVadim Petrochenkov-1/+1
2025-07-17resolve: Change `&mut Resolver` to `&Resolver` when possibleVadim Petrochenkov-2/+2
2025-07-16resolve: Remove trait `ToNameBinding`Vadim Petrochenkov-4/+3
2025-07-15Define datastructures for `#[cfg]` attribute, move StrippedCfgItemJonathan Brouwer-4/+13
2025-07-13Auto merge of #140717 - mejrs:diagnostic_lints, r=oli-obkbors-2/+2
Split up the `unknown_or_malformed_diagnostic_attributes` lint This splits up the lint into the following lint group: - `unknown_diagnostic_attributes` - triggers if the attribute is unknown to the current compiler - `misplaced_diagnostic_attributes` - triggers if the attribute exists but it is not placed on the item kind it's meant for - `malformed_diagnostic_attributes` - triggers if the attribute's syntax or options are invalid - `malformed_diagnostic_format_literals` - triggers if the format string literal is invalid, for example if it has unpaired curly braces or invalid parameters - this pr doesn't create it, but future lints for things like deprecations can also go here. This PR does not start emitting lints in places that previously did not. ## Motivation I want to have finer control over what `unknown_or_malformed_diagnostic_attributes` does I have a project with fairly low msrv that is/will have a lower msrv than future diagnostic attributes. So lints will be emitted when I or others compile it on a lower msrv. At this time, there are two options to silence these lints: - `#[allow(unknown_or_malformed_diagnostic_attributes)]` - this risks diagnostic regressions if I (or others) mess up using the attribute, or if the attribute's syntax ever changes. - write a build script to detect the compiler version and emit cfgs, and then conditionally enable the attribute: ```rust #[cfg_attr(rust_version_99, diagnostic::new_attr_in_rust_99(thing = ..))]` struct Foo; ``` or conditionally `allow` the lint: ```rust // lib.rs #![cfg_attr(not(current_rust), allow(unknown_or_malformed_diagnostic_attributes))] ``` I like to avoid using build scripts if I can, so the following works much better for me. That is what this PR will let me do in the future: ```rust #[allow(unknown_diagnostic_attribute, reason = "attribute came out in rust 1.99 but msrv is 1.70")] #[diagnostic::new_attr_in_rust_99(thing = ..)]` struct Foo;
2025-07-11Split up the `unknown_or_malformed_diagnostic_attributes` lintmejrs-2/+2
2025-07-09Use reference for MacroRulesScopeRef instead of Interned.LorrensP-2158466-2/+1
2025-07-09MacroData in ResolverArenas + split macro_map into extern_macro_map and ↵LorrensP-2158466-4/+4
local_macro_map.
2025-07-06mbe: Change `unused_macro_rules` to a `DenseBitSet`Josh Triplett-2/+2
Now that it only contains indexes, and no other information, a bitset provides a more compact and simpler representation.
2025-07-05mbe: Defer checks for `compile_error!` until reporting an unused macro ruleJosh Triplett-10/+20
The MBE parser checks rules at initial parse time to see if their RHS has `compile_error!` in it, and returns a list of rule indexes and LHS spans that don't map to `compile_error!`, for use in unused macro rule checking. Instead, have the unused macro rule reporting ask the macro for the rule to report, and let the macro check at that time. That avoids checking rules unless they're unused. In the process, refactor the data structure used to store macro rules, to group the LHS and RHS (and LHS span) of each rule together, and refactor the unused rule tracking to only track rule indexes. This ends up being a net simplification, and reduction in code size.
2025-06-25Don't give APITs names with macro expansion placeholder fragments in itMichael Goulet-0/+4
2025-06-13Auto merge of #134841 - estebank:serde-attr-4, r=wesleywiserbors-3/+27
Look at proc-macro attributes when encountering unknown attribute ``` error: cannot find attribute `sede` in this scope --> $DIR/missing-derive-2.rs:22:7 | LL | #[sede(untagged)] | ^^^^ | help: the derive macros `Deserialize` and `Serialize` accept the similarly named `serde` attribute | LL | #[serde(untagged)] | + error: cannot find attribute `serde` in this scope --> $DIR/missing-derive-2.rs:16:7 | LL | #[serde(untagged)] | ^^^^^ | note: `serde` is imported here, but it is a crate, not an attribute --> $DIR/missing-derive-2.rs:5:1 | LL | extern crate serde; | ^^^^^^^^^^^^^^^^^^^ help: `serde` is an attribute that can be used by the derive macros `Serialize` and `Deserialize`, you might be missing a `derive` attribute | LL + #[derive(Serialize, Deserialize)] LL | enum B { | ``` Partially address #47608. This PR doesn't find [macros that haven't yet been imported by name](https://github.com/rust-lang/rust/pull/109278/commits/af945cb86e03b44a4b6dc4d54ec1424b00a2349e).
2025-06-12Detect when attribute is provided by missing `derive` macroEsteban Küber-3/+27
``` error: cannot find attribute `empty_helper` in this scope --> $DIR/derive-helper-legacy-limits.rs:17:3 | LL | #[empty_helper] | ^^^^^^^^^^^^ | help: `empty_helper` is an attribute that can be used by the derive macro `Empty`, you might be missing a `derive` attribute | LL + #[derive(Empty)] LL | struct S2; | ``` Look at proc-macro attributes when encountering unknown attribute ``` error: cannot find attribute `sede` in this scope --> src/main.rs:18:7 | 18 | #[sede(untagged)] | ^^^^ | help: the derive macros `Serialize` and `Deserialize` accept the similarly named `serde` attribute | 18 | #[serde(untagged)] | ~~~~~ error: cannot find attribute `serde` in this scope --> src/main.rs:12:7 | 12 | #[serde(untagged)] | ^^^^^ | = note: `serde` is in scope, but it is a crate, not an attribute help: `serde` is an attribute that can be used by the derive macros `Serialize` and `Deserialize`, you might be missing a `derive` attribute | 10 | #[derive(Serialize, Deserialize)] | ```
2025-06-12Tracking the old name of renamed unstable library attributexizheyin-1/+2
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-05-18Remove rustc_attr_data_structures re-export from rustc_attr_parsingmejrs-1/+1
2025-04-20Remove #[rustc_macro_edition_2021].Mara Bos-8/+1
It was only temporarily used by pin!(), which no longer needs it.
2025-04-11Avoid a node_id_to_def_id call by just storing DefIds instead of NodeIdsOli Scherer-1/+1
2025-04-11Avoid storing the `LocalDefId` twiceOli Scherer-5/+3
2025-04-11Avoid another node_id_to_def_id callOli Scherer-5/+3
2025-04-11Avoid a reverse map that is only used in diagnostics pathsOli Scherer-2/+2
2025-04-10Rename some `name` variables as `ident`.Nicholas Nethercote-2/+2
It bugs me when variables of type `Ident` are called `name`. It leads to silly things like `name.name`. `Ident` variables should be called `ident`, and `name` should be used for variables of type `Symbol`. This commit improves things by by doing `s/name/ident/` on a bunch of `Ident` variables. Not all of them, but a decent chunk.
2025-04-02Rollup merge of #139184 - Urgau:crate-root-lint-levels, r=jieyouxuTakayuki Maeda-4/+11
Add unstable `--print=crate-root-lint-levels` This PR implements `--print=crate-root-lint-levels` from MCP 833 https://github.com/rust-lang/compiler-team/issues/833. Tracking issue: https://github.com/rust-lang/rust/issues/139180 Best reviewed commit by commit.
2025-03-31Expose `registered_tools` directly without `TyCtxt`-queryUrgau-4/+11
2025-03-25Rollup merge of #138929 - oli-obk:assoc-ctxt-of-trait, r=compiler-errorsMatthias Krüger-1/+1
Visitors track whether an assoc item is in a trait impl or an inherent impl `AssocCtxt::Impl` now contains an `of_trait` field. This allows ast lowering and nameres to not have to track whether we're in a trait impl or an inherent impl.
2025-03-25Rollup merge of #138924 - nnethercote:less-kw-Empty-3, r=compiler-errorsMatthias Krüger-5/+6
Reduce `kw::Empty` usage, part 3 Remove some more `kw::Empty` uses, in support of #137978. r? `@davidtwco`
2025-03-25Track whether an assoc item is in a trait impl or an inherent implOli Scherer-1/+1
2025-03-25Rollup merge of #138580 - petrochenkov:resinstab, r=NadrierilTakayuki Maeda-2/+1
resolve: Avoid some unstable iteration 2 Continuation of https://github.com/rust-lang/rust/pull/138502.
2025-03-25Use `Option<Symbol>` in `ModuleKind::Def`.Nicholas Nethercote-5/+6
This way, `None` represents "crate root without a name" instead of `kw::Empty`. This changes makes it impossible to forget to handle the exceptional case.
2025-03-24resolve: Avoid some unstable iteration 2Vadim Petrochenkov-2/+1