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

the type of the expression with an operator doesn't inferred correctly #20803

Closed
mandel59 opened this issue Jan 9, 2015 · 4 comments · Fixed by #28067
Closed

the type of the expression with an operator doesn't inferred correctly #20803

mandel59 opened this issue Jan 9, 2015 · 4 comments · Fixed by #28067
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

@mandel59
Copy link

mandel59 commented Jan 9, 2015

use std::ops::Add;

#[cfg(error)]
fn foo<T>(x: T) -> <i32 as Add<T>>::Output where i32: Add<T> {
    42i32 + x
}

#[cfg(not(error))]
fn foo<T>(x: T) -> <i32 as Add<T>>::Output where i32: Add<T> {
    Add::add(42i32, x)
}

fn main() {
    println!("{}", foo(0i32));
}

This fails to compile:

% rustc assoc.rs --cfg error
assoc.rs:5:13: 5:14 error: mismatched types: expected `i32`, found `T` (expected i32, found type parameter)
assoc.rs:5     42i32 + x
                       ^
assoc.rs:5:5: 5:14 error: mismatched types: expected `<i32 as core::ops::Add<T>>::Output`, found `i32` (expected associated type, found i32)
assoc.rs:5     42i32 + x
               ^~~~~~~~~
error: aborting due to 2 previous errors
@arturoc
Copy link
Contributor

arturoc commented Jan 9, 2015

probably related, this:

pub fn lerp<T: Add + Mul<f32>>(p0: T, p1: T, pct: f32) -> T{
    p0 * (1.0 - pct) + p1 * pct
}

fails to compile with:
error: binary operation + cannot be applied to type <T as core::ops::Mul<f32>>::Output

this used to compile but not sure if it's a bug or the syntax has changed

@mandel59
Copy link
Author

mandel59 commented Jan 9, 2015

@arturoc This works:

pub fn lerp<T: Add<Output = T> + Mul<f32, Output = T>>(p0: T, p1: T, pct: f32) -> T {
    p0 * (1.0 - pct) + p1 * pct
}

It should be the intended behavior.

@arturoc
Copy link
Contributor

arturoc commented Jan 9, 2015

thanks!

@kmcallister kmcallister added the A-typesystem Area: The type system label Jan 9, 2015
@huonw
Copy link
Member

huonw commented Aug 24, 2015

This now works after Niko's refactoring work. (Adding needstest, since I can't find a test that tests this exactly, although I wasn't looking very hard.)

@huonw huonw added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Aug 24, 2015
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