Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generic float inference type is unified with f64 prematurely #14382

Closed
pnkfelix opened this issue May 23, 2014 · 2 comments · Fixed by #27158
Closed

generic float inference type is unified with f64 prematurely #14382

pnkfelix opened this issue May 23, 2014 · 2 comments · Fixed by #27158
Labels
A-typesystem Area: The type system E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@pnkfelix
Copy link
Member

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
  1. Ideally we would actually manage to properly assign f32 to the type of x, since I do not see any reason for the generic float type to be constrained to f64. (Note that if you add a : f32 type annotation to the let x then compilation terminates successfully.)
  2. 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.

@steveklabnik
Copy link
Member

Inference is totally different now, does this issue still persist, @pnkfelix ?

@steveklabnik steveklabnik added the A-typesystem Area: The type system label Jan 29, 2015
@apasel422
Copy link
Contributor

The following compiles successfully now:

#[derive(Debug)]
pub struct Matrix4<S>(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() {
    let x = 1.0;
    let m : Matrix4<f32> = translate(x);
    println!("m: {:?}", m);
}

@Gankra Gankra added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jul 20, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jul 21, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jul 21, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jul 21, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jul 22, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jul 22, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jul 22, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jul 22, 2015
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants