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

Unable to write blanket impl for subtrait on RPITIT trait #124729

Open
traviscross opened this issue May 4, 2024 · 1 comment
Open

Unable to write blanket impl for subtrait on RPITIT trait #124729

traviscross opened this issue May 4, 2024 · 1 comment
Labels
C-bug Category: This is a bug. F-return_position_impl_trait_in_trait `#![feature(return_position_impl_trait_in_trait)]` F-return_type_notation `#[feature(return_type_notation)]` T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@traviscross
Copy link
Contributor

traviscross commented May 4, 2024

If we have a trait that uses RPITIT, e.g.:

trait Trait {
    fn f() -> impl Sized;
}

It's currently impossible to define a subtrait and to write a blanket impl for that subtrait, e.g.:

trait Sub: Trait</* Need bound here, but how? */> {}
impl<T: Sub> Trait for T {
    fn f() -> impl Sized {}
    //~^ ERROR method `f` has an incompatible type for trait
    //~| ERROR expected associated type, found opaque type
    //~| ERROR help: change the output type to match the trait: `impl Sized`
}

The error we give here is, of course, particularly bad since we suggest that the user change the type to what is already written there syntactically.

We can simplify the example a bit by refining the impl:

#![allow(refining_impl_trait)]

trait Trait {
    fn f() -> impl Sized;
}

trait Sub: Trait</* Need bound here, but how? */> {}
impl<T: Sub> Trait for T {
    fn f() -> () {}
    //~^ ERROR method `f` has an incompatible type for trait
    //~| ERROR expected associated type, found `()`
    //~| ERROR help: change the output type to match the trait: `impl Sized`
}

If we desugar this, we can see more clearly what's going on:

trait Trait {
    type _0: Sized;
    fn f() -> Self::_0;
}

trait Sub: Trait</* Need bound here. */> {}
impl<T: Sub> Trait for T {
    type _0 = ();
    fn f() -> () {}
    //~^ ERROR method `f` has an incompatible type for trait
    //~| ERROR expected associated type, found `()`
    //~| ERROR help: change the output type to match the trait: `impl Sized`
}

We need to bound the associated type of the trait when defining the subtrait. If we do that, then it works:

trait Trait {
    type _0: Sized;
    fn f() -> Self::_0;
}

trait Sub: Trait<_0 = ()> {}
impl<T: Sub> Trait for T {
    type _0 = ();
    fn f() -> () {}
}

The trouble for RPITIT is that 1) there's no way to bound the associated type in that way, and 2) even if there were (e.g. with RTN with type equality bounds), we probably don't want to require that people do that.

Because of the special relationship between a function with an RPITIT and the implicit associated type, we may be able to make this just work in the case of RPITITs.

cc @compiler-errors

This is related to some existing issues, but is probably a more minimal statement of the underlying issue here, so we're filing it separately.

Related

@traviscross traviscross added the C-bug Category: This is a bug. label May 4, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 4, 2024
@traviscross traviscross added F-return_position_impl_trait_in_trait `#![feature(return_position_impl_trait_in_trait)]` F-return_type_notation `#[feature(return_type_notation)]` T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 4, 2024
@compiler-errors
Copy link
Member

This has to do with how where clause bounds shadow associated type normalization:

trait Trait {
    type Assoc;
    fn f() -> Self::Assoc;
}

trait Sub: Trait {}
impl<T: Sub> Trait for T {
    type Assoc = ();
    fn f() -> Self::Assoc {}
}

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-return_position_impl_trait_in_trait `#![feature(return_position_impl_trait_in_trait)]` F-return_type_notation `#[feature(return_type_notation)]` T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants