Skip to content

Better messages when using ADT constructor as value #82083

Open
@estebank

Description

@estebank

Given:

struct S {}
enum E {
    V,
}
struct T();

fn foo() -> S {
    S
}

fn bar() -> E {
    E
}

fn baz() -> T {
    T
}

we currently emit

error[E0423]: expected value, found struct `S`
 --> src/lib.rs:8:5
  |
1 | struct S {}
  | ----------- `S` defined here
...
5 | struct T();
  | ----------- similarly named tuple struct `T` defined here
...
8 |     S
  |     ^
  |
help: use struct literal syntax instead
  |
8 |     S {}
  |
help: a tuple struct with a similar name exists
  |
8 |     T
  |
help: consider importing this unit variant instead
  |
1 | use unicode_bidi::BidiClass::S;
  |

error[E0423]: expected value, found enum `E`
  --> src/lib.rs:12:5
   |
5  | struct T();
   | ----------- similarly named tuple struct `T` defined here
...
12 |     E
   |     ^
   |
note: the enum is defined here
  --> src/lib.rs:2:1
   |
2  | / enum E {
3  | |     V,
4  | | }
   | |_^
help: you might have meant to use the following enum variant
   |
12 |     E::V
   |
help: a tuple struct with a similar name exists
   |
12 |     T
   |
help: consider importing one of these items instead
   |
1  | use core::f32::consts::E;
   |
1  | use core::f64::consts::E;
   |
1  | use pin_utils::core_reexport::f32::consts::E;
   |
1  | use pin_utils::core_reexport::f64::consts::E;
   |
     and 2 other candidates

error[E0308]: mismatched types
  --> src/lib.rs:16:5
   |
5  | struct T();
   | ----------- fn() -> T {T} defined here
...
15 | fn baz() -> T {
   |             - expected `T` because of return type
16 |     T
   |     ^ expected struct `T`, found fn item
   |
   = note: expected struct `T`
             found fn item `fn() -> T {T}`
help: use parentheses to instantiate this tuple struct
   |
16 |     T()
   |      ^^

which is reasonable, particularly the suggestions, but it isn't entirely clear to a newcomer that S in TypeNS isn't the same as S in ValueNS.

Ideally, the output would look closer to:

error[E0423]: expected value of type `S`, found struct constructor for type `S`
 --> src/lib.rs:8:5
  |
1 | struct S {}
  | ----------- `S` defined here
...
5 | struct T();
  | ----------- similarly named tuple struct `T` defined here
...
  | fn foo() -> S {
  |             - expected because of this return type
8 |     S
  |     ^ struct constructor used as value
  |
help: use struct literal syntax instead
  |
8 |     S {}
  |

error[E0423]: expected value of type `E`, found enum type `E`
  --> src/lib.rs:12:5
   |
2  | enum E {
   | ------ `E` defined here
...
5  | struct T();
   | ----------- similarly named tuple struct `T` defined here
...
   | fn bar() -> E {
   |              - expected because of this return type
12 |     E
   |     ^ enum type used as value
   |
help: you might have meant to use the following enum variant
   |
12 |     E::V
   |

error[E0308]: mismatched types
  --> src/lib.rs:16:5
   |
5  | struct T();
   | ----------- `T` defined here
...
15 | fn baz() -> T {
   |             - expected `T` because of return type
16 |     T
   |     ^ expected struct `T`, found fn item
   |
   = note:        expected value of type `T`
         found tuple struct constructor of type `T`
help: use parentheses to instantiate this tuple struct
   |
16 |     T()
   |      ^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-inconsistentDiagnostics: Inconsistency in formatting, grammar or style between diagnostic messages.D-papercutDiagnostics: An error or lint that needs small tweaks.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions