Skip to content

(Identical) function call with Generic arguments breaks compilation when called from within another function that has unrelated Generic arguments #54128

@ButterflyGarden

Description

@ButterflyGarden

I searched and couldn't find a similar issue for now.

The following code shows an example of a function interpolate_linear that when called with identical (literal) arguments, causes a complier error in the function percentile (line 16), but no error in the function call_interpolate_linear (line 21). I haven't yet had time to try and narrow the issue yet, but nevertheless I think the code is valid.

use std::convert::From;
use std::fmt::Debug;

pub fn percentile<T: Debug + Copy>(
    data_set: &Vec<T>,
    n: f64,
) -> f64
where
    f64: From<T>,
{
    let rank = n * (data_set.len() as f64 - 1f64);
    let below = rank.floor() as usize;
    let mut above = rank.floor() as usize + 1;
    if above == data_set.len() {
        above = data_set.len() - 1;
    }
    let lower_value = data_set[below];
    let higher_value = data_set[above];
    interpolate_linear(&1.0, &2.0, 0.5); // this line does not compile
    interpolate_linear(&lower_value, &higher_value, n)
}

pub fn call_interpolate_linear() {
    interpolate_linear(&1.0, &2.0, 0.5); // this line does compile
}

pub fn interpolate_linear<T: Copy>(
    a: &T,
    b: &T,
    how_far_from_a_to_b: f64,
) -> f64
where
    f64: From<T>,
{
    let a = f64::from(*a);
    let b = f64::from(*b);
    a + (how_far_from_a_to_b * (b - a))
}
fn main() {}

Here is the compiler error:

error[E0308]: mismatched types
  --> functions_issue.rs:16:24
   |
16 |     interpolate_linear(&1.0, &2.0, 0.5); // this line does not compile
   |                        ^^^^ expected type parameter, found floating-point variable
   |
   = note: expected type `&T`
              found type `&{float}`

error[E0308]: mismatched types
  --> functions_issue.rs:16:30
   |
16 |     interpolate_linear(&1.0, &2.0, 0.5); // this line does not compile
   |                              ^^^^ expected type parameter, found floating-point variable
   |
   = note: expected type `&T`
              found type `&{float}`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions