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 in GAT & HRTB #103563

Closed
Millione opened this issue Oct 26, 2022 · 4 comments · Fixed by #103695
Closed

Unexpected trait bound not satisfied in GAT & HRTB #103563

Millione opened this issue Oct 26, 2022 · 4 comments · Fixed by #103695
Assignees
Labels
C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs

Comments

@Millione
Copy link

Millione commented Oct 26, 2022

I tried this code:

fn main() {
    let mut log_service = LogService { inner: Inner };
    log_service.call(());
}

pub trait Service<Request> {
    type Response;

    fn call(&mut self, req: Request) -> Self::Response;
}

pub struct LogService<S> {
    inner: S,
}

impl<T, U, S> Service<T> for LogService<S>
where
    S: Service<T, Response = U>,
    U: Extension + 'static,
    for<'a> U::Item<'a>: std::fmt::Debug,
{
    type Response = S::Response;

    fn call(&mut self, req: T) -> Self::Response {
        self.inner.call(req)
    }
}

pub struct Inner;

impl Service<()> for Inner {
    type Response = Resp;

    fn call(&mut self, req: ()) -> Self::Response {
        Resp::A(req)
    }
}

pub trait Extension {
    type Item<'a>;

    fn touch<F>(self, f: F) -> Self
    where
        for<'a> F: Fn(Self::Item<'a>);
}

pub enum Resp {
    A(()),
}

impl Extension for Resp {
    type Item<'a> = RespItem<'a>;
    fn touch<F>(self, _f: F) -> Self
    where
        for<'a> F: Fn(Self::Item<'a>),
    {
        match self {
            Self::A(a) => Self::A(a),
        }
    }
}

pub enum RespItem<'a> {
    A(&'a ()),
}

impl<'a> std::fmt::Debug for RespItem<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::A(arg0) => f.debug_tuple("A").field(arg0).finish(),
        }
    }
}

Meta

rustc --version --verbose:

rustc 1.66.0-nightly (bed4ad65b 2022-10-25)
binary: rustc
commit-hash: bed4ad65bf7a1cef39e3d66b3670189581b3b073
commit-date: 2022-10-25
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.2
Backtrace

error[E0599]: the method `call` exists for struct `LogService<Inner>`, but its trait bounds were not satisfied
  --> src/main.rs:3:17
   |
3  |     log_service.call(());
   |                 ^^^^ method cannot be called on `LogService<Inner>` due to unsatisfied trait bounds
...
12 | pub struct LogService<S> {
   | ------------------------
   | |
   | method `call` not found for this struct
   | doesn't satisfy `LogService<Inner>: Service<_>`
   |
note: trait bound `<_ as Extension>::Item<'a>: Debug` was not satisfied
  --> src/main.rs:20:26
   |
16 | impl<T, U, S> Service<T> for LogService<S>
   |               ----------     -------------
...
20 |     for<'a> U::Item<'a>: std::fmt::Debug,
   |                          ^^^^^^^^^^^^^^^ unsatisfied trait bound introduced here

For more information about this error, try `rustc --explain E0599`.

@Millione Millione added the C-bug Category: This is a bug. label Oct 26, 2022
@jackh726 jackh726 added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Oct 26, 2022
@Millione
Copy link
Author

It doesn't seem to be related to GAT, only influenced by TAIT.

@Millione
Copy link
Author

for<'a> U::Item<'a>: std::fmt::Debug,

I found rustc type system have inferred type U to Resp correctly, but still refused to compile. However, there is a workaround without knowing why.

for<'a> <S::Response as Extension>::Item<'a>: std::fmt::Debug,

@LYF1999
Copy link
Contributor

LYF1999 commented Oct 27, 2022

seems like another version of #94207

@LYF1999
Copy link
Contributor

LYF1999 commented Oct 27, 2022

@rustbot claim

@bors bors closed this as completed in 9bb6e60 Feb 14, 2023
RalfJung pushed a commit to RalfJung/miri that referenced this issue Feb 15, 2023
fix: Unexpected trait bound not satisfied in HRTB and Associated Type

fix rust-lang/rust#103563
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 20, 2024
fix: Unexpected trait bound not satisfied in HRTB and Associated Type

fix rust-lang/rust#103563
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
fix: Unexpected trait bound not satisfied in HRTB and Associated Type

fix rust-lang/rust#103563
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. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants