Skip to content

Trait with type-dependent optional function forces implementation regardless of type #110219

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

Closed
cafce25 opened this issue Apr 12, 2023 · 2 comments
Labels
A-trait-system Area: Trait system C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@cafce25
Copy link
Contributor

cafce25 commented Apr 12, 2023

I tried this code (from this Stack Overflow question):

trait Doable {}

trait MyTrait<T> {
    fn set_member(&mut self, member: T);

    fn do_member(&mut self)
    where
        T: Doable;
}

struct MyMember;
struct MyTraitStruct(Option<MyMember>);

impl MyTrait<MyMember> for MyTraitStruct {
    fn set_member(&mut self, member: MyMember) {
        todo!();
    }
}

I expected to see this happen:

It compiles as do_member is only available if the type parameter T implements Doable which MyMember doesn't.

Instead, this happened:

It doesn't compile with this error message:

error[E0046]: not all trait items implemented, missing: `do_member`
  --> main.rs:14:1
   |
6  | /     fn do_member(&mut self)
7  | |     where
8  | |         T: Doable;
   | |__________________- `do_member` from trait
...
14 |   impl MyTrait<MyMember> for MyTraitStruct {
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `do_member` in implementation

Meta

rustc --version --verbose:

rustc 1.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: x86_64-unknown-linux-gnu
release: 1.68.2
LLVM version: 15.0.6

and

rustc 1.70.0-nightly (9df3a39fb 2023-04-11)
binary: rustc
commit-hash: 9df3a39fb30575d808e70800f9fad5362aac57a2
commit-date: 2023-04-11
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 16.0.2

Workaround

To work around this one can simply implement the do_member method without the where clause:

impl MyTrait<MyMember> for MyTraitStruct {
    // [...]
    fn do_member(&mut self) {}
}

but it feels rather pointless to implement a method that's not callable and doesn't do anything.

@cafce25 cafce25 added the C-bug Category: This is a bug. label Apr 12, 2023
@Finomnis
Copy link
Contributor

Finomnis commented Apr 12, 2023

One problem I see with this, though: If the compiler would allow this, implementing a trait for a type now suddenly becomes a breaking change for downstream crates. To my knowledge, implementing an additional trait for a type does not require a major semver change so far.

I might be mistaken though, and the existing rules would prevent such a trait implementation in the first place.

@fmease
Copy link
Member

fmease commented Apr 12, 2023

Side note: The status quo is well documented (ref) and it is not a bug.

@rustbot label -C-bug +C-feature-request T-lang A-traits

@rustbot rustbot added C-feature-request Category: A feature request, i.e: not implemented / a PR. A-trait-system Area: Trait system T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Apr 12, 2023
@cafce25 cafce25 closed this as completed May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants