Skip to content

Suggest how to workaround E0212 #69000

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
nagisa opened this issue Feb 9, 2020 · 0 comments · Fixed by #69048
Closed

Suggest how to workaround E0212 #69000

nagisa opened this issue Feb 9, 2020 · 0 comments · Fixed by #69048
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nagisa
Copy link
Member

nagisa commented Feb 9, 2020

It is not impossible for one to end up with a code that looks like this:

trait Banana<'a> { 
    type Assoc: Default;
}

struct Peach<X>(std::marker::PhantomData<X>);

impl<X: for<'a> Banana<'a>> Peach<X> {
    fn mango(&self) -> X::Assoc {
        Default::default()
    }
}

But fairly unintuitively the compiler rejects this code:

error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
 --> src/lib.rs:8:24
  |
8 |     fn mango(&self) -> X::Assoc {
  |                        ^^^^^^^^

This can be made to work by specifying the lifetime for the associated type:

trait Banana<'a> { 
    type Assoc: Default;
}

struct Peach<X>(std::marker::PhantomData<X>);

impl<X: for<'a> Banana<'a>> Peach<X> {
    fn mango(&self) -> <X as Banana<'_>>::Assoc {
        Default::default()
    }
}

But it is fairly not obvious how to get there. We should suggest a similar transformation in the diagnostic.

cc @estebank

@nagisa nagisa added A-type-system Area: Type system A-diagnostics Area: Messages for errors, warnings, and lints labels Feb 9, 2020
@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 9, 2020
@estebank estebank added A-associated-items Area: Associated items (types, constants & functions) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Feb 11, 2020
@bors bors closed this as completed in 8d00adf Feb 13, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants