Skip to content

Integer type inference confusion #5462

Closed
@alexcrichton

Description

@alexcrichton

A program like:

fn foo(_: u8) {}

fn main() {
  let a = 1;
  foo(a.to_u8());
  foo(a);
}

yields the error

$ rustc foo.rs
foo.rs:5:6: 5:16 error: multiple applicable methods in scope
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:5:6: 5:16 note: candidate #1 is `core::i32::__extensions__::to_u8`
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:5:6: 5:16 note: candidate #2 is `core::i8::__extensions__::to_u8`
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:5:6: 5:16 note: candidate #3 is `core::i16::__extensions__::to_u8`
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:5:6: 5:16 note: candidate #4 is `core::u64::__extensions__::to_u8`
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:5:6: 5:16 note: candidate #5 is `core::u16::__extensions__::to_u8`
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:5:6: 5:16 note: candidate #6 is `core::uint::__extensions__::to_u8`
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:5:6: 5:16 note: candidate #7 is `core::int::__extensions__::to_u8`
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:5:6: 5:16 note: candidate #8 is `core::u32::__extensions__::to_u8`
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:5:6: 5:16 note: candidate #9 is `core::u8::__extensions__::to_u8`
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:5:6: 5:16 note: candidate #10 is `core::i64::__extensions__::to_u8`
foo.rs:5   foo(a.to_u8());
               ^~~~~~~~~~
foo.rs:6:6: 6:7 error: mismatched types: expected `u8` but found `i32` (expected u8 but found i32)
foo.rs:6   foo(a);
               ^
error: aborting due to 2 previous errors

I'm not really sure what's quite going on here. The first function invocation looks like it doesn't know what type a is, so it could be anything.

The second invocation I thought would force the compiler to infer that a is a u8, but it seems to have determined it's an i32 at that point which I thought would have suppressed the first error?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions