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

Stable rustc 1.82 suggests using an unstable trait #133511

Closed
jaskij opened this issue Nov 26, 2024 · 6 comments · Fixed by #133522
Closed

Stable rustc 1.82 suggests using an unstable trait #133511

jaskij opened this issue Nov 26, 2024 · 6 comments · Fixed by #133522
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jaskij
Copy link

jaskij commented Nov 26, 2024

Code

use std::collections::HashSet;
use std::hash::Hash;
use num_traits::Bounded;

fn for_each_value_with_skip<T>(skipped: &HashSet<T>, func: impl Fn(T))
where
    T: Bounded + Hash + Eq,
{
    for v in T::min_value()..T::max_value() {
        if skipped.contains(&v) {
            continue;
        }
        func(v);
    }
}

Current output

error[E0277]: the trait bound `T: Step` is not satisfied
  --> s7-comm/src/tests/helpers.rs:11:14
   |
11 |     for v in T::min_value()..T::max_value() {
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Step` is not implemented for `T`, which is required by `std::ops::Range<T>: IntoIterator`
   |
   = note: required for `std::ops::Range<T>` to implement `Iterator`
   = note: required for `std::ops::Range<T>` to implement `IntoIterator`
help: consider further restricting this bound
   |
9  |     T: Bounded + Hash + Eq + std::iter::Step,
   |                            +++++++++++++++++

Desired output

No idea what the alternative is, if it's even possible to achieve what I want using stable Rust, but a stable compiler should not suggest using unstable features.

Rationale and extra context

No response

Other cases

Rust Version

$ rustc --version
rustc 1.82.0 (f6e511eec 2024-10-15)

Anything else?

Rust Playground

@jaskij jaskij added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 26, 2024
@jaskij jaskij changed the title Stable rustc 1.82 suggests using unstable features Stable rustc 1.82 suggests using an usntable trait Nov 26, 2024
@shepmaster shepmaster changed the title Stable rustc 1.82 suggests using an usntable trait Stable rustc 1.82 suggests using an unstable trait Nov 26, 2024
@jaskij
Copy link
Author

jaskij commented Nov 26, 2024

That original code I posted can be further simplified, not sure if I should edit.

playground link

use num_traits::Bounded;

fn print_all<T>()
where
    T: Bounded,
{
    for v in T::min_value()..T::max_value() {
        println!("{v}");
    }
}

@estebank
Copy link
Contributor

Further minimized:

trait Bounded {
    fn min_value() -> Self;
    fn max_value() -> Self;
}

fn print_all<T>()
where
    T: Bounded,
{
    for v in T::min_value()..T::max_value() {}
}

@clubby789
Copy link
Contributor

clubby789 commented Nov 27, 2024

I was typing this comment when the above appeared, but: further further minimized 😁

pub fn demo<T>(range: std::ops::Range<T>) {
    for _ in range {}
}

@cuviper
Copy link
Member

cuviper commented Nov 28, 2024

No idea what the alternative is, if it's even possible to achieve what I want using stable Rust, but a stable compiler should not suggest using unstable features.

I don't know if the compiler could easily suggest this, but you can make your code work by adding where Range<T>: IntoIterator<Item = T>.

@jaskij
Copy link
Author

jaskij commented Nov 28, 2024

thanks @cuviper , shepmaster helped me figure it out in the discord right after I made this issue

@estebank
Copy link
Contributor

estebank commented Nov 28, 2024

We currently don't suggest complex where clauses, only restricting type parameters or associated types. We could suggest where Range<T>: IntoIterator<Item = T> when T is a type parameter... I'll prototype to get a sense of how noisy that would be. Edit: It wouldn't necessarily be too common, but the filtering needed to make it so would likely require a bit of a refactor to note_obligation_cause_code...

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 3, 2024
…it, r=compiler-errors

Don't suggest restricting bound with unstable traits on stable and mention it's unstable on nightly

On nightly, we mention the trait is unstable

```
error[E0277]: the trait bound `T: Unstable` is not satisfied
  --> $DIR/unstable-trait-suggestion.rs:13:9
   |
LL |     foo(t)
   |     --- ^ the trait `Unstable` is not implemented for `T`
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `foo`
  --> $DIR/unstable-trait-suggestion.rs:9:11
   |
LL | fn foo<T: Unstable>(_: T) {}
   |           ^^^^^^^^ required by this bound in `foo`
help: consider restricting type parameter `T` but it is an `unstable` trait
   |
LL | pub fn demo<T: Unstable>(t: T) {
   |              ++++++++++
```

On stable, we don't suggest the trait at all

```
error[E0277]: the trait bound `T: Unstable` is not satisfied
  --> $DIR/unstable-trait-suggestion.rs:13:9
   |
LL |     foo(t)
   |     --- ^ the trait `Unstable` is not implemented for `T`
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `foo`
  --> $DIR/unstable-trait-suggestion.rs:9:11
   |
LL | fn foo<T: Unstable>(_: T) {}
   |           ^^^^^^^^ required by this bound in `foo`
```

Fix rust-lang#133511.
jhpratt added a commit to jhpratt/rust that referenced this issue Dec 3, 2024
…it, r=compiler-errors

Don't suggest restricting bound with unstable traits on stable and mention it's unstable on nightly

On nightly, we mention the trait is unstable

```
error[E0277]: the trait bound `T: Unstable` is not satisfied
  --> $DIR/unstable-trait-suggestion.rs:13:9
   |
LL |     foo(t)
   |     --- ^ the trait `Unstable` is not implemented for `T`
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `foo`
  --> $DIR/unstable-trait-suggestion.rs:9:11
   |
LL | fn foo<T: Unstable>(_: T) {}
   |           ^^^^^^^^ required by this bound in `foo`
help: consider restricting type parameter `T` but it is an `unstable` trait
   |
LL | pub fn demo<T: Unstable>(t: T) {
   |              ++++++++++
```

On stable, we don't suggest the trait at all

```
error[E0277]: the trait bound `T: Unstable` is not satisfied
  --> $DIR/unstable-trait-suggestion.rs:13:9
   |
LL |     foo(t)
   |     --- ^ the trait `Unstable` is not implemented for `T`
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `foo`
  --> $DIR/unstable-trait-suggestion.rs:9:11
   |
LL | fn foo<T: Unstable>(_: T) {}
   |           ^^^^^^^^ required by this bound in `foo`
```

Fix rust-lang#133511.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 3, 2024
…it, r=compiler-errors

Don't suggest restricting bound with unstable traits on stable and mention it's unstable on nightly

On nightly, we mention the trait is unstable

```
error[E0277]: the trait bound `T: Unstable` is not satisfied
  --> $DIR/unstable-trait-suggestion.rs:13:9
   |
LL |     foo(t)
   |     --- ^ the trait `Unstable` is not implemented for `T`
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `foo`
  --> $DIR/unstable-trait-suggestion.rs:9:11
   |
LL | fn foo<T: Unstable>(_: T) {}
   |           ^^^^^^^^ required by this bound in `foo`
help: consider restricting type parameter `T` but it is an `unstable` trait
   |
LL | pub fn demo<T: Unstable>(t: T) {
   |              ++++++++++
```

On stable, we don't suggest the trait at all

```
error[E0277]: the trait bound `T: Unstable` is not satisfied
  --> $DIR/unstable-trait-suggestion.rs:13:9
   |
LL |     foo(t)
   |     --- ^ the trait `Unstable` is not implemented for `T`
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `foo`
  --> $DIR/unstable-trait-suggestion.rs:9:11
   |
LL | fn foo<T: Unstable>(_: T) {}
   |           ^^^^^^^^ required by this bound in `foo`
```

Fix rust-lang#133511.
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Dec 8, 2024
…it, r=compiler-errors

Don't suggest restricting bound with unstable traits on stable and mention it's unstable on nightly

On nightly, we mention the trait is unstable

```
error[E0277]: the trait bound `T: Unstable` is not satisfied
  --> $DIR/unstable-trait-suggestion.rs:13:9
   |
LL |     foo(t)
   |     --- ^ the trait `Unstable` is not implemented for `T`
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `foo`
  --> $DIR/unstable-trait-suggestion.rs:9:11
   |
LL | fn foo<T: Unstable>(_: T) {}
   |           ^^^^^^^^ required by this bound in `foo`
help: consider restricting type parameter `T` but it is an `unstable` trait
   |
LL | pub fn demo<T: Unstable>(t: T) {
   |              ++++++++++
```

On stable, we don't suggest the trait at all

```
error[E0277]: the trait bound `T: Unstable` is not satisfied
  --> $DIR/unstable-trait-suggestion.rs:13:9
   |
LL |     foo(t)
   |     --- ^ the trait `Unstable` is not implemented for `T`
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `foo`
  --> $DIR/unstable-trait-suggestion.rs:9:11
   |
LL | fn foo<T: Unstable>(_: T) {}
   |           ^^^^^^^^ required by this bound in `foo`
```

Fix rust-lang#133511.
@bors bors closed this as completed in f415c07 Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants