Skip to content

Warn that trait implementation is gated by a feature flag #87717

Open
@edward-shen

Description

@edward-shen

Given the following code: (Playground link not provided because this involves a crate not on playground)

use rayon::iter::IntoParallelIterator;
use dashmap::DashSet;

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Foo;

let test = DashSet::<Foo>::new();
test.par_iter();

The current output is:

error[E0599]: the method `par_iter` exists for struct `DashSet<Foo>`, but its trait bounds were not satisfied
  --> src/lib.rs:74:14
   |
74 |         test.par_iter();
   |              ^^^^^^^^ method cannot be called on `DashSet<Foo>` due to unsatisfied trait bounds
   | 
  ::: /home/edward/.cargo/registry/src/git.colasdn.top-1ecc6299db9ec823/dashmap-4.0.2/src/set.rs:19:1
   |
19 | pub struct DashSet<K, S = RandomState> {
   | -------------------------------------- doesn't satisfy `DashSet<Foo>: IntoParallelRefIterator`
   |
   = note: the following trait bounds were not satisfied:
           `&DashSet<Foo>: IntoParallelIterator`
           which is required by `DashSet<Foo>: IntoParallelRefIterator`

Ideally the output should look like:

error[E0599]: the method `par_iter` exists for struct `DashSet<Foo>`, but its trait bounds were not satisfied
  --> src/lib.rs:74:14
   |
74 |         test.par_iter();
   |              ^^^^^^^^ method cannot be called on `DashSet<Foo>` due to unsatisfied trait bounds
   | 
  ::: /home/edward/.cargo/registry/src/git.colasdn.top-1ecc6299db9ec823/dashmap-4.0.2/src/set.rs:19:1
   |
19 | pub struct DashSet<K, S = RandomState> {
   | -------------------------------------- doesn't satisfy `DashSet<Foo>: IntoParallelRefIterator`
   |
   = note: the following trait bounds were not satisfied:
           `&DashSet<Foo>: IntoParallelIterator`
           which is required by `DashSet<Foo>: IntoParallelRefIterator`
   = note: `dashmap` has an optional feature `rayon` that is currently not enabled.

I was trying to call par_iter() on the following example, but was very confused with regards to the error message. Consider the following image, snapshotted from the dashmap::DashSet docs:
image

It looks like Foo satisfies all the trait bounds needed for DashSet<Foo> to fufill InterParallelIterator, yet the trait still isn't being satisfied! What's going on? The solution is a case of either confusing docs.rs or a perhaps unintuitive diagnostic: a missing feature flag (in this case, rayon).

As a quick concept, I would suggest a note indicating that the trait implementation does exist for a struct but isn't enabled because the feature isn't enabled. As a layman on the rustc internals, I'm not sure how feasible this would be, but something like the following might be viable:

  1. On a failed trait bound check Trait,
  2. Check if struct Foo from crate crate implements Trait regardless of enabled features,
  3. If so, check if the Trait implementation or its parent mods is enabled through a feature, and that feature isn't enabled,
  4. If so, add a note saying that Trait is not satisfied because the trait implementation is gated by a feature flag.

I think this is worthwhile to implement, as I believe this is incredibly confusing to new Rust users. The compiler and docs make no indication that a trait implementation is gated, which means that a new user would be absolutely lost on next steps.
Heck, I would consider myself an intermediate or even an advanced user, and had to ask the community for help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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