about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src/diagnostics.rs
AgeCommit message (Collapse)AuthorLines
2025-09-05Use `Itertools::all_equal_value()` where applicableYotam Ofek-9/+5
2025-09-05Auto merge of #144737 - petrochenkov:extprelcache, r=davidtwcobors-1/+1
resolve: Avoid finalizing extern prelude entries more than once
2025-09-04Rollup merge of #145827 - estebank:issue-51976, r=jackh726Stuart Cook-8/+164
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-02resolve: Avoid finalizing extern prelude entries more than onceVadim Petrochenkov-1/+1
2025-08-30Avoid unnecessary suggestion in or-patternEsteban Küber-18/+24
2025-08-30On binding not present in all patterns, look at consts and unit ↵Esteban Küber-2/+111
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-29resolve: Merge `ExternPreludeEntry::only_item` into `flag_binding`Vadim Petrochenkov-1/+1
2025-08-29resolve: Merge `ExternPreludeEntry::introduced_by_item` into `item_binding`Vadim Petrochenkov-2/+3
2025-08-25On binding not present in all patterns, suggest potential typoEsteban Küber-6/+47
``` 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-22resolve: Remove derive fallback lint id from `ScopeSet::Late`Vadim Petrochenkov-2/+2
2025-08-22resolve: `early_resolve_ident_in_lexical_scope` -> `resolve_ident_in_scope_set`Vadim Petrochenkov-3/+3
2025-08-21Rollup merge of #145590 - nnethercote:ModKind-Inline, r=petrochenkovJacob Pratt-1/+1
Prevent impossible combinations in `ast::ModKind`. `ModKind::Loaded` has an `inline` field and a `had_parse_error` field. If the `inline` field is `Inline::Yes` then `had_parse_error` must be `Ok(())`. This commit moves the `had_parse_error` field into the `Inline::No` variant. This makes it impossible to create the nonsensical combination of `inline == Inline::Yes` and `had_parse_error = Err(_)`. r? ```@Urgau```
2025-08-19Prevent impossible combinations in `ast::ModKind`.Nicholas Nethercote-1/+1
`ModKind::Loaded` has an `inline` field and a `had_parse_error` field. If the `inline` field is `Inline::Yes` then `had_parse_error` must be `Ok(())`. This commit moves the `had_parse_error` field into the `Inline::No` variant. This makes it impossible to create the nonsensical combination of `inline == Inline::Yes` and `had_parse_error = Err(_)`.
2025-08-15Detect missing `derive` on unresolved attribute even when not importedEsteban Küber-27/+3
``` error: cannot find attribute `sede` in this scope --> $DIR/missing-derive-3.rs:20: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-3.rs:14:7 | LL | #[serde(untagged)] | ^^^^^ | note: `serde` is imported here, but it is a crate, not an attribute --> $DIR/missing-derive-3.rs:4:1 | LL | extern crate serde; | ^^^^^^^^^^^^^^^^^^^ help: `serde` is an attribute that can be used by the derive macros `Deserialize` and `Serialize`, you might be missing a `derive` attribute | LL + #[derive(Deserialize, Serialize)] LL | enum B { | ```
2025-08-14Do not ICE on private type in field of unresolved structEsteban Küber-3/+3
2025-08-14resolve: Improve code reuse in typo candidate collectionVadim Petrochenkov-7/+18
2025-08-14resolve: Do not show deprecated helper attributes in typo recommendationsVadim Petrochenkov-22/+1
Remove one FIXME, addressing it does not reduce the hacky-ness much, and the logic is going to be removed anyway together with the `legacy_derive_helpers` deprecation lint.
2025-08-14resolve: Do not call `resolve_macro_path` from late resolutionVadim Petrochenkov-1/+1
`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-13Auto merge of #144793 - petrochenkov:extprel3, r=davidtwcobors-3/+12
resolve: Split extern prelude into two scopes One scope for `extern crate` items and another for `--extern` options, with the former shadowing the latter. If in a single scope some things can overwrite other things, especially with ad hoc restrictions like `MacroExpandedExternCrateCannotShadowExternArguments`, then it's not really a single scope. So this PR splits `Scope::ExternPrelude` into two cleaner scopes. This is similar to how https://github.com/rust-lang/rust/pull/144131 splits module scope into two scopes for globs and non-globs, but simpler.
2025-08-13resolve: Improve diagnostics for ambiguities in extern preludeVadim Petrochenkov-2/+9
2025-08-13resolve: Split extern prelude into two scopesVadim Petrochenkov-1/+3
One for `--extern` options and another for `extern crate` items.
2025-08-12Detect and report macro kind mismatches early, and more preciselyJosh Triplett-9/+13
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-12Switch to a bitflags `MacroKinds` to support macros with more than one kindJosh Triplett-9/+19
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-10review commentsEsteban Küber-48/+80
2025-08-10Detect struct construction with private field in field with defaultEsteban Küber-2/+58
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-2/+1
2025-08-08Rollup merge of #144912 - LorrensP-2158466:smart-resolver, r=petrochenkovStuart Cook-33/+33
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-33/+33
2025-08-06Introduce ModernIdent type to unify macro 2.0 hygiene handlingxizheyin-8/+10
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-08-04Rollup merge of #144746 - petrochenkov:extpreltidy, r=b-naberStuart Cook-2/+2
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-13/+12
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-9/+6
``` 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-5/+10
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-2/+2
2025-07-31remove rustc_attr_data_structuresJana Dönszelmann-7/+3
2025-07-27split up define into define_extern and define_localLorrensP-2158466-2/+2
2025-07-26resolve: Do not create `NameResolution`s on access unless necessaryVadim Petrochenkov-2/+1
2025-07-26resolve: Minimize borrow scopes for `resolutions`Vadim Petrochenkov-3/+2
2025-07-24resolve: Remove `Scope::CrateRoot`Vadim Petrochenkov-5/+0
Use `Scope::Module` with the crate root module inside instead, which should be identical.
2025-07-18inline CrateLoader inside of CStoreLorrensP-2158466-1/+2
2025-07-17Rollup merge of #143550 - petrochenkov:lessmutres, r=lcnrMatthias Krüger-19/+27
resolve: Use interior mutability for extern module map Module map for extern modules is a lazily populated cache, it's not *significantly* mutable. If some logic in name resolver is parallelized, then this cache can be populated from any thread, and without affecting results of any speculative resolution. Unblocks https://github.com/rust-lang/rust/pull/143884. This is a part of [#gsoc > Project: Parallel Macro Expansion](https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Parallel.20Macro.20Expansion/with/527348747). cc `@LorrensP-2158466`
2025-07-17resolve: Change `&mut Resolver` to `&Resolver` when possibleVadim Petrochenkov-10/+5
2025-07-17resolve: Split `module_map` into two maps for local and extern modulesVadim Petrochenkov-1/+10
2025-07-17resolve: Use `module_map` and `get_module` lessVadim Petrochenkov-17/+21
2025-07-17Improve path segment joining.Nicholas Nethercote-15/+14
There are many places that join path segments with `::` to produce a string. A lot of these use `join("::")`. Many in rustdoc use `join_with_double_colon`, and a few use `.joined("..")`. One in Clippy uses `itertools::join`. A couple of them look for `kw::PathRoot` in the first segment, which can be important. This commit introduces `rustc_ast::join_path_{syms,ident}` to do the joining for everyone. `rustc_ast` is as good a location for these as any, being the earliest-running of the several crates with a `Path` type. Two functions are needed because `Ident` printing is more complex than simple `Symbol` printing. The commit also removes `join_with_double_colon`, and `estimate_item_path_byte_length` with it. There are still a handful of places that join strings with "::" that are unchanged. They are not that important: some of them are in tests, and some of them first split a path around "::" and then rejoin with "::". This fixes one test case where `{{root}}` shows up in an error message.
2025-07-16resolve: Merge `NameBindingKind::Module` into `NameBindingKind::Res`Vadim Petrochenkov-27/+13
2025-07-15Define attribute parser & config evaluatorJonathan Brouwer-14/+7
2025-07-12replace binding and shadowed_glob on NameResolution with non_glob_binding ↵b-naber-2/+2
and glob_binding
2025-07-09MacroData in ResolverArenas + split macro_map into extern_macro_map and ↵LorrensP-2158466-2/+7
local_macro_map.