implied bounds: normalize in the proper param_env by aliemjay · Pull Request #105982 · rust-lang/rust (original) (raw)

compiler-errors added a commit to compiler-errors/rust that referenced this pull request

May 12, 2023

@compiler-errors

use implied bounds when checking opaque types

During opaque type inference, we check for the well-formedness of the hidden type in the opaque type's own environment, not the one of the defining site, which are different in the case of TAIT.

However in the case of associated-type-impl-trait, we don't use implied bounds from the impl header. This caused us to reject the following:

trait Service<Req> {
    type Output;
    fn call(req: Req) -> Self::Output;
}

impl<'a, Req> Service<&'a Req> for u8 {
    type Output= impl Sized; // we can't prove WF of hidden type  `WF(&'a Req)` although it's implied by the impl
    //~^ ERROR type parameter Req doesn't live long enough
    fn call(req: &'a Req) -> Self::Output {
        req
    }
}

although adding an explicit bound would make it pass:

- impl<'a, Req> Service<&'a Req> for u8 {
+ impl<'a, Req> Service<&'a Req> for u8  where Req: 'a, {

I believe it should pass as we already allow the concrete type to be used:

impl<'a, Req> Service<&'a Req> for u8 {
-    type Output= impl Sized;
+    type Output= &'a Req;

Fixes rust-lang#95922

Builds on rust-lang#105982

cc @lcnr (because implied bounds)

r? @oli-obk