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

Unexpected "trait bound not satisfied" when bounding associated type #107952

Closed
whoiscc opened this issue Feb 12, 2023 · 4 comments
Closed

Unexpected "trait bound not satisfied" when bounding associated type #107952

whoiscc opened this issue Feb 12, 2023 · 4 comments
Labels
C-bug Category: This is a bug.

Comments

@whoiscc
Copy link

whoiscc commented Feb 12, 2023

I tried this code (not sure how to further minimize it):

trait Protocol<E> {
    type Effect;
}

trait Composite {}

trait ReactiveGenerate {
    type Event;
    
    fn update<P>()
    where
        P: Protocol<Self::Event>,
        P::Effect: Composite;
}

impl ReactiveGenerate for () {
    type Event = ();

    fn update<P>()
    where
        P: Protocol<Self::Event>,
        P::Effect: Composite
    {
        todo!()
    }
}

I expect it to compile, but get error:

error[[E0277]](https://doc.rust-lang.org/stable/error_codes/E0277.html): the trait bound `P: Protocol<()>` is not satisfied
  --> src/main.rs:21:5
   |
21 | /     fn update<P>()
22 | |     where
23 | |         P: Protocol<Self::Event>,
24 | |         P::Effect: Composite
   | |____________________________^ the trait `Protocol<()>` is not implemented for `P`
   |
help: consider further restricting this bound
   |
23 |         P: Protocol<Self::Event> + Protocol<()>,
   |                                  ++++++++++++++

error[[E0277]](https://doc.rust-lang.org/stable/error_codes/E0277.html): the trait bound `P: Protocol<()>` is not satisfied
  --> src/main.rs:21:8
   |
21 |     fn update<P>()
   |        ^^^^^^ the trait `Protocol<()>` is not implemented for `P`
   |
help: consider further restricting this bound
   |
23 |         P: Protocol<Self::Event> + Protocol<()>,
   |                                  ++++++++++++++

The code would compile is I remove P::Effect: Composite on both sites, and I don't see how this bound prevent type check.

I'm not sure whether this code should be compiled or not. If not please kindly point out the problem. But anyway, the error message makes no sense. If I blindly follow the hint, it becomes even more nonsense:

error[[E0277]](https://doc.rust-lang.org/stable/error_codes/E0277.html): the trait bound `P: Protocol<()>` is not satisfied
  --> src/main.rs:19:5
   |
19 | /     fn update<P>()
20 | |     where
21 | |         P: Protocol<Self::Event> + Protocol<()>,
22 | |         <P as Protocol<()>>::Effect: Composite
   | |______________________________________________^ the trait `Protocol<()>` is not implemented for `P`
   |
help: consider further restricting this bound
   |
21 |         P: Protocol<Self::Event> + Protocol<()> + Protocol<()>,
   |                                                 ++++++++++++++

error[[E0276]](https://doc.rust-lang.org/stable/error_codes/E0276.html): impl has stricter requirements than trait
  --> src/main.rs:21:12
   |
10 | /     fn update<P>()
11 | |     where
12 | |         P: Protocol<Self::Event>,
13 | |         P::Effect: Composite;
   | |_____________________________- definition of `update` from trait
...
21 |           P: Protocol<Self::Event> + Protocol<()>,
   |              ^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `P: Protocol<()>`

Meta

rustc --version --verbose:

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: x86_64-unknown-linux-gnu
release: 1.67.1
LLVM version: 15.0.6
Backtrace

<backtrace>

@whoiscc whoiscc added the C-bug Category: This is a bug. label Feb 12, 2023
@XrXr
Copy link
Contributor

XrXr commented Feb 16, 2023

I believe you're trying to write:

    fn update<P>()
    where
        P: Protocol<Self::Event, Effect: Composite>;

But

error[E0658]: associated type bounds are unstable
= note: see issue #52662 for more information

A Stable equivalent should be:

    fn update<P, E>()
    where
        P: Protocol<Self::Event, Effect = E>,
        E: Composite;

Playground

I agree the error message is confusing. I probably would've wrote the same code as OP to express the bound.
I'm not sure what the type checker's reasoning for the error is, actually. Maybe t-types can chime in.

@cynecx
Copy link
Contributor

cynecx commented Feb 16, 2023

cc #58231, #100177

@whoiscc
Copy link
Author

whoiscc commented Feb 17, 2023

Thanks for the correction! I wonder what is the semantic difference between the two versions? To me the correct version seems a little bit counterintuitive.

@whoiscc
Copy link
Author

whoiscc commented Feb 17, 2023

I guess this issue is a duplicated to #58231, so I would close it. There has been more than hundred of "trait bound not satisfied" issues, really hard to check all of them :|

@whoiscc whoiscc closed this as completed Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants