Skip to content

infer_ty(IntVar) should not print as _ in user error messages #24770

Closed
@pnkfelix

Description

@pnkfelix

spawned off of #24728 (comment)

In an example like:

fn main() {
    let vs = vec![1, 2, 3, 4];

    for v in &vs {
        match v {
            1 => {}
            _ => {}
        }
    }
}

which yields the error message:

<anon>:4:5: 9:6 error: type mismatch resolving  \ (linebreak solely for presentation here)
 `<core::slice::Iter<'_, _> as core::iter::Iterator>::Item == _`:
 expected &-ptr,
    found integral variable [E0271]
<anon>:4     for v in &vs {
<anon>:5         match v {
<anon>:6             1 => {}
<anon>:7             _ => {}
<anon>:8         }
<anon>:9     }

i find it frustrating that the first line prints the integral type variable as just _.

It is not a completely unconstrained type; it must be some kind of integer.

(It is true that one should be able to figure this out from the third line. But I think we can still do better on the first line itself.)

I am thinking something like this could suffice:

error: type mismatch resolving   \
 `<core::slice::Iter<'_, _> as core::iter::Iterator>::Item == iN`

Another slightly more verbose option (but less prone to misinterpretation when e.g. unsigned integers are involved, or when the user has defined a type named iN) would be:

error: type mismatch resolving   \
 `<core::slice::Iter<'_, _> as core::iter::Iterator>::Item == <int type>`

Yet another option, which is both succinct but will not collide with user definitions (at least not today):

error: type mismatch resolving   \
 `int? == <core::slice::Iter<'_, _> as core::iter::Iterator>::Item`

and likewise one would imagine for the FloatVar case

error: type mismatch resolving   \
 `float? == <core::slice::Iter<'_, _> as core::iter::Iterator>::Item`

I forced the int? and float? to be on the left-hand side to ensure that they did not get mistaken for a question mark ending the error message itself. (I did consider ?int and ?float but I worry a little that those would get confused with the generalization marker ?Sized ... maybe that is a small price to pay...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions