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

Possible regression: Updating 2.8.0 to 2.9.0 causes "type annotations needed" for BTreeSet::range etc. #105

Open
TonalidadeHidrica opened this issue Jan 8, 2022 · 5 comments

Comments

@TonalidadeHidrica
Copy link

See the following code:

use std::collections::BTreeSet;
use ordered_float::NotNan;

fn main() {
    let a = BTreeSet::<NotNan<f64>>::new();
    let b = a.range(..);
}

In 2.8.0, this code compiles. In 2.9.0 and 2.10.0, it causes the following error:

image

Copyable text
error[E0283]: type annotations needed
 --> src/main.rs:6:15
  |
6 |     let b = a.range(..);
  |               ^^^^^ cannot infer type for type parameter `K` declared on the associated function `range`
  |
  = note: cannot satisfy `_: Ord`

I cannot figure out at all why this error happens, but this is indeed a regression.

$ rustc --version
rustc 1.57.0 (f1edd0429 2021-11-29)
@TonalidadeHidrica
Copy link
Author

The workaround is to explicitly specify the type parameter K.

use std::collections::BTreeSet;
use ordered_float::NotNan;

fn main() {
    let a = BTreeSet::<NotNan<f64>>::new();
    let b = a.range::<NotNan<f64>, _>(..);
}

@mbrubeck
Copy link
Collaborator

mbrubeck commented Jan 8, 2022

Hmm, this was caused by #98.

@TonalidadeHidrica
Copy link
Author

Isn't it strange that implementing a trait make type inference fail? I'm wondering if this tends to happen, or it is a bug (or something can be improved) of Rust compiler.

@mbrubeck
Copy link
Collaborator

The BTreeSet::range method is generic over a type K: Ord where T: Borrow<K> + Ord. So it seems plausible that adding a new Borrow implementation to NotNan could allow calling this function with additional types.

However, NotNan<f64> only implements Borrow<f64>, and f64 does not implement Ord. So it should not affect this function call. It seems the compiler is not smart enough to see this, or it is overly conservative since it believes that f64 might implement Ord in the future. I'm not sure which.

@mbrubeck mbrubeck reopened this Apr 30, 2022
@mbrubeck
Copy link
Collaborator

Closed accidentally. I meant to close #91, not this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants