Skip to content

Explicitly suggest dereferencing when type mismatch differs by reference only #34562

Closed
@kornelski

Description

@kornelski

Type mismatch error could be more specific and friendlier if it had special handling of case where the compiler found &type, but expected type.

error: mismatched types [--explain E0308]

^ expected enum std::option::Option, found &-ptr

The current message is correct and (with notes) it's possible to understand it, but "expected type, found &-ptr" obscures what type has actually been found (#25426), and makes it sound like a worse mismatch than it really is.

note: expected type std::option::Option<_>
note: found type &std::option::Option<foo>

Notes for this error make the problem clear, but they're repeating the first part of the message, so it's already using four lines to say one thing. Listing both types requires me to compare them and spot the difference myself.

I hope there are no weird edge cases in this situation, and the compiler could simplify the message by proposing solution outright. Something like:

error: value must be dereferenced
Expected std::option::Option<_>. Use *var instead of var to dereference &std::option::Option<foo>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions