Newbie question about possible wrong compiler errors


#1

Hi!
I’ve just started learning Rust, reading the Rust book, and playing around with the examples. I’ve run into what looks like a very minor bug in the compiler. A single error triggers some extra error messages which I think are incorrect.
Compare this, which compiles fine:
https://is.gd/ho62qV
with this, where I’ve just uncommented the last 2 lines:
https://is.gd/Lc75og

and it gives error messages for lines 18 and 21, which were fine before (as well as a correct error message about line 26).

  1. Is this actually a bug? I’m not sure, since I’ve just started learning Rust, and haven’t actually read the chapter about traits yet (just read the docs and tried to rely on my intuition from other languages I know).
  2. If it is a bug, is this even worth opening an issue about?

#2

The root of the problem is that your function is defined as having both arguments the same type T:

fn make_rec<T>(h_ : T, w_ : T)

but you’re calling it with two different types - u8 and i8:

Rectangle::make_rec(1 as u8, 2 as i8);

so there’s no way to satisfy T == u8 == i8, and this makes the compiler unable to figure out the types for the function.


#3

That’s OK, that error is fine.
The problem is, why is the wrong version of the code also giving three spurious error messages?


#4

Handling of integers of unknown type (called {integer} in the error message) is special, and these errors are probably unintentional.

My guess is that the type error makes the compiler abort type inference early, before figuring out actual types for the integer constants.

BTW, 1 as f64 can be written shorter as 1f64 or 1.


#5

I see. So I think this bug report is worth opening, possibly with a minimized example.


#6

Here’s a minimal example

fn main() {
	let a: u8 = 2 as i8;
	let b = 5 as f64;
}

and there’s already a recent issue about this


#7

Thank you all for you help!
I was going to write a minimal example but nemo157 beat me to it :slight_smile: and also found the existing issue.