rust/compiler
Matthias Krüger 82279108e1
Rollup merge of #136344 - zachs18:dot_notation_more_defkinds_3, r=davidtwco
Suggest replacing `.` with `::` in more error diagnostics.

First commit makes the existing "help: use the path separator to refer to an item" also work when the base is a type alias, not just a trait/module/struct.

The existing unconditional `DefKind::Mod | DefKind::Trait` match arm is changed to a conditional `DefKind::Mod | DefKind::Trait | DefKind::TyAlias` arm that only matches if the `path_sep` suggestion-adding closure succeeds, so as not to stop the later `DefKind::TyAlias`-specific suggestions if the path-sep suggestion does not apply. This shouldn't change behavior for `Mod` or `Trait` (due to the default arm's `return false` etc).

This commit also updates `tests/ui/resolve/issue-22692.rs` to reflect this, and also renames it to something more meaningful.

This commit also makes the `bad_struct_syntax_suggestion` closure take `err` as a parameter instead of capturing it, since otherwise caused borrowing errors due to the change to using `path_sep` in a pattern guard.

<details> <summary> Type alias diagnostic example </summary>

```rust
type S = String;

fn main() {
    let _ = S.new;
}
```

```diff
 error[E0423]: expected value, found type alias `S`
  --> diag7.rs:4:13
   |
 4 |     let _ = S.new;
   |             ^
   |
-  = note: can't use a type alias as a constructor
+  help: use the path separator to refer to an item
+  |
+4 |     let _ = S::new;
+  |              ~~
```

</details>

Second commit adds some cases for `enum`s, where if there is a field/method expression where the field/method has the name of a unit/tuple variant, we assume the user intended to create that variant[^1] and suggest replacing the `.` from the field/method suggestion with a `::` path separator. If no such variant is found (or if the error is not a field/method expression), we give the existing suggestion that suggests adding `::TupleVariant(/* fields */)` after the enum.

<details> <summary> Enum diagnostic example </summary>

```rust
enum Foo {
    A(u32),
    B,
    C { x: u32 },
}

fn main() {
    let _ = Foo.A(42); // changed
    let _ = Foo.B;     // changed
    let _ = Foo.D(42); // no change
    let _ = Foo.D;     // no change
    let _ = Foo(42);   // no change
}
```

```diff
 error[E0423]: expected value, found enum `Foo`
  --> diag8.rs:8:13
   |
 8 |     let _ = Foo.A(42); // changed
   |             ^^^
   |
 note: the enum is defined here
  --> diag8.rs:1:1
   |
 1 | / enum Foo {
 2 | |     A(u32),
 3 | |     B,
 4 | |     C { x: u32 },
 5 | | }
   | |_^
-help: you might have meant to use the following enum variant
-  |
-8 |     let _ = Foo::B.A(42); // changed
-  |             ~~~~~~
-help: alternatively, the following enum variant is available
+help: use the path separator to refer to a variant
   |
-8 |     let _ = (Foo::A(/* fields */)).A(42); // changed
-  |             ~~~~~~~~~~~~~~~~~~~~~~
+8 |     let _ = Foo::A(42); // changed
+  |                ~~

 error[E0423]: expected value, found enum `Foo`
  --> diag8.rs:9:13
   |
 9 |     let _ = Foo.B;     // changed
   |             ^^^
   |
 note: the enum is defined here
  --> diag8.rs:1:1
   |
 1 | / enum Foo {
 2 | |     A(u32),
 3 | |     B,
 4 | |     C { x: u32 },
 5 | | }
   | |_^
-help: you might have meant to use the following enum variant
-  |
-9 |     let _ = Foo::B.B;     // changed
-  |             ~~~~~~
-help: alternatively, the following enum variant is available
+help: use the path separator to refer to a variant
   |
-9 |     let _ = (Foo::A(/* fields */)).B;     // changed
-  |             ~~~~~~~~~~~~~~~~~~~~~~
+9 |     let _ = Foo::B;     // changed
+  |                ~~

 error[E0423]: expected value, found enum `Foo`
   --> diag8.rs:10:13
    |
 10 |     let _ = Foo.D(42); // no change
    |             ^^^
    |
 note: the enum is defined here
   --> diag8.rs:1:1
    |
 1  | / enum Foo {
 2  | |     A(u32),
 3  | |     B,
 4  | |     C { x: u32 },
 5  | | }
    | |_^
 help: you might have meant to use the following enum variant
    |
 10 |     let _ = Foo::B.D(42); // no change
    |             ~~~~~~
 help: alternatively, the following enum variant is available
    |
 10 |     let _ = (Foo::A(/* fields */)).D(42); // no change
    |             ~~~~~~~~~~~~~~~~~~~~~~

 error[E0423]: expected value, found enum `Foo`
   --> diag8.rs:11:13
    |
 11 |     let _ = Foo.D;     // no change
    |             ^^^
    |
 note: the enum is defined here
   --> diag8.rs:1:1
    |
 1  | / enum Foo {
 2  | |     A(u32),
 3  | |     B,
 4  | |     C { x: u32 },
 5  | | }
    | |_^
 help: you might have meant to use the following enum variant
    |
 11 |     let _ = Foo::B.D;     // no change
    |             ~~~~~~
 help: alternatively, the following enum variant is available
    |
 11 |     let _ = (Foo::A(/* fields */)).D;     // no change
    |             ~~~~~~~~~~~~~~~~~~~~~~

 error[E0423]: expected function, tuple struct or tuple variant, found enum `Foo`
   --> diag8.rs:12:13
    |
 12 |     let _ = Foo(42);   // no change
    |             ^^^ help: try to construct one of the enum's variants: `Foo::A`
    |
    = help: you might have meant to construct the enum's non-tuple variant
 note: the enum is defined here
   --> diag8.rs:1:1
    |
 1  | / enum Foo {
 2  | |     A(u32),
 3  | |     B,
 4  | |     C { x: u32 },
 5  | | }
    | |_^

 error: aborting due to 5 previous errors
```

</details>

[^1]: or if it's a field expression and a tuple variant, that they meant to refer the variant constructor.
2025-02-19 21:16:07 +01:00
..
rustc
rustc_abi Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_arena Add inherent versions of MaybeUninit methods for slices 2025-01-11 23:57:00 -05:00
rustc_ast Add a TyPat in the AST to reuse the generic arg lowering logic 2025-02-11 08:51:05 +00:00
rustc_ast_ir
rustc_ast_lowering Move some Map methods onto TyCtxt. 2025-02-17 13:21:02 +11:00
rustc_ast_passes compiler: remove rustc_target reexport of rustc_abi::HashStableContext 2025-02-11 18:55:48 -08:00
rustc_ast_pretty Add a TyPat in the AST to reuse the generic arg lowering logic 2025-02-11 08:51:05 +00:00
rustc_attr_data_structures rustc_allowed_through_unstable_modules: require deprecation message 2025-02-02 12:36:12 +01:00
rustc_attr_parsing Rustfmt 2025-02-08 22:12:13 +00:00
rustc_baked_icu_data
rustc_borrowck Auto merge of #136539 - matthewjasper:late-normalize-errors, r=compiler-errors 2025-02-19 07:49:08 +00:00
rustc_builtin_macros Rollup merge of #136646 - oli-obk:pattern-types-ast, r=BoxyUwU 2025-02-12 06:07:37 +01:00
rustc_codegen_cranelift cg_clif: Tweak formatting of global comments 2025-02-18 01:29:23 -08:00
rustc_codegen_gcc Auto merge of #136575 - scottmcm:nsuw-math, r=nikic 2025-02-14 14:21:29 +00:00
rustc_codegen_llvm Rollup merge of #137210 - workingjubilee:fixup-passmode-import, r=RalfJung 2025-02-19 01:30:12 +01:00
rustc_codegen_ssa Rollup merge of #137213 - nnethercote:rm-rustc_middle-mir-tcx, r=compiler-errors 2025-02-19 01:30:13 +01:00
rustc_const_eval Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_data_structures Enforce T: Hash for Interned<...> 2025-02-17 21:35:52 -05:00
rustc_driver
rustc_driver_impl Rollup merge of #137151 - Urgau:register-more-signals, r=workingjubilee 2025-02-18 18:34:14 +01:00
rustc_error_codes fix: Alloc new errorcode E0803 for E0495 2025-02-15 12:18:30 +08:00
rustc_error_messages tree-wide: parallel: Fully removed all Lrc, replaced with Arc 2025-02-03 13:25:57 +03:00
rustc_errors Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_expand Reject macro calls inside of #![crate_name] 2025-02-15 16:47:30 +01:00
rustc_feature Rename pattern_complexity attr as pattern_complexity_limit. 2025-02-17 09:30:40 +11:00
rustc_fluent_macro
rustc_fs_util
rustc_graphviz Rustfmt 2025-02-08 22:12:13 +00:00
rustc_hashes Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_hir Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_hir_analysis Rollup merge of #137206 - estebank:e0599-structured, r=jieyouxu 2025-02-18 18:40:54 +01:00
rustc_hir_pretty Overhaul the intravisit::Map trait. 2025-02-17 13:21:35 +11:00
rustc_hir_typeck simplify fallback-to-outer condition on old editions 2025-02-18 18:00:17 -08:00
rustc_incremental Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_index Rustfmt 2025-02-08 22:12:13 +00:00
rustc_index_macros update rustc_index_macros feature handling 2024-12-19 20:32:12 +03:00
rustc_infer Rustfmt 2025-02-08 22:12:13 +00:00
rustc_interface Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_lexer Rustfmt 2025-02-08 22:12:13 +00:00
rustc_lint Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_lint_defs Implement lint for definition site item shadowing too 2025-02-13 05:45:53 +00:00
rustc_llvm Quote embedded codeview command line arguments 2025-02-14 11:02:28 +01:00
rustc_log Avoid naming variables str 2025-01-07 14:30:02 +02:00
rustc_macros Rustfmt 2025-02-08 22:12:13 +00:00
rustc_metadata Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_middle Auto merge of #136539 - matthewjasper:late-normalize-errors, r=compiler-errors 2025-02-19 07:49:08 +00:00
rustc_mir_build Rollup merge of #137213 - nnethercote:rm-rustc_middle-mir-tcx, r=compiler-errors 2025-02-19 01:30:13 +01:00
rustc_mir_dataflow Rollup merge of #137213 - nnethercote:rm-rustc_middle-mir-tcx, r=compiler-errors 2025-02-19 01:30:13 +01:00
rustc_mir_transform Auto merge of #136539 - matthewjasper:late-normalize-errors, r=compiler-errors 2025-02-19 07:49:08 +00:00
rustc_monomorphize Move some Map methods onto TyCtxt. 2025-02-17 13:21:02 +11:00
rustc_next_trait_solver Auto merge of #137235 - matthiaskrgr:rollup-2kjua2t, r=matthiaskrgr 2025-02-18 21:08:58 +00:00
rustc_parse Fix const items not being allowed to be called r#move or r#static 2025-02-16 18:21:40 +01:00
rustc_parse_format Rustfmt 2025-02-08 22:12:13 +00:00
rustc_passes Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_pattern_analysis Add pattern_complexity_limit to Limits. 2025-02-17 09:30:33 +11:00
rustc_privacy Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_query_impl Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_query_system Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_resolve Rollup merge of #136344 - zachs18:dot_notation_more_defkinds_3, r=davidtwco 2025-02-19 21:16:07 +01:00
rustc_sanitizers check the types in ty::Value to value conversion 2025-01-30 18:13:16 +01:00
rustc_serialize Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_session Auto merge of #137164 - matthiaskrgr:rollup-dj5826k, r=matthiaskrgr 2025-02-17 11:18:33 +00:00
rustc_smir Auto merge of #136593 - lukas-code:ty-value-perf, r=oli-obk 2025-02-13 15:27:30 +00:00
rustc_span add last std diagnostic items for clippy 2025-02-18 10:54:37 +01:00
rustc_symbol_mangling Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_target x86-sse2 ABI: use SSE registers for floats and SIMD 2025-02-18 16:11:41 +01:00
rustc_trait_selection Auto merge of #136539 - matthewjasper:late-normalize-errors, r=compiler-errors 2025-02-19 07:49:08 +00:00
rustc_traits Clean up dropck code a bit 2025-02-17 11:33:07 +00:00
rustc_transmute transmutability: fix ICE when passing wrong ADT to ASSUME 2025-02-08 05:44:29 +01:00
rustc_ty_utils cosmetic changes 2025-02-18 13:22:46 +01:00
rustc_type_ir Rollup merge of #137211 - lcnr:alias-relate-accept-error, r=compiler-errors 2025-02-18 18:34:17 +01:00
rustc_type_ir_macros Rustfmt 2025-02-08 22:12:13 +00:00
stable_mir Extend the renaming to coerce_unsafe_ptr 2025-02-10 13:01:55 +00:00