Closed
Description
I don't exactly know what terminology to use to describe this bug; feel free to update the title to reflect reality...
This was something I encountered while using bjz's cgmath library ; this test is derived from that library (but independent of it).
pub struct Matrix4<S>;
pub trait POrd<S> {}
pub fn translate<S: POrd<S>>(_: S) -> Matrix4<S> { unimplemented!() }
impl POrd<f32> for f32 {}
impl POrd<f64> for f64 {}
fn main() {
// This is a generic float, not constrained to f64.
let x = 1.0;
// (And it might occur arbitrarily far from its use below.)
// error: mismatched types: expected `Matrix4<f32>` but found `Matrix4<f64>` (expected f32 but found f64)
let m : Matrix4<f32> = translate(x);
// ^~~~~~~~~~~~
println!("m: {:?}", m);
}
Transcript of compilation attempt:
% rustc -O -o bug bug.rs
bug.rs:15:28: 15:40 error: mismatched types: expected `Matrix4<f32>` but found `Matrix4<f64>` (expected f32 but found f64)
bug.rs:15 let m : Matrix4<f32> = translate(x);
^~~~~~~~~~~~
error: aborting due to previous error
- Ideally we would actually manage to properly assign
f32
to the type ofx
, since I do not see any reason for the generic float type to be constrained tof64
. (Note that if you add a: f32
type annotation to thelet x
then compilation terminates successfully.) - But even if we cannot accomplish that goal for some hypothetical reason, we still need to a better job with the error message here.
Note also that the impl POrd<f64> for f64 {}
is necessary to reproduce the poor-quality error message given here. If you remove it, you get this instead: error: failed to find an implementation of trait POrd<<generic float #0>> for <generic float #0>
(which also is not great, but at least then I have a hint that my problem originates with some use of a generic float literal somewhere in my code.