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

Associated type projections don't count for local types in trait impls #87350

Closed
alexcrichton opened this issue Jul 21, 2021 · 2 comments
Closed
Labels
A-associated-items Area: Associated items (types, constants & functions) A-coherence Area: Coherence C-bug Category: This is a bug. T-compiler Relevant to the compiler 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

@alexcrichton
Copy link
Member

alexcrichton commented Jul 21, 2021

I ran into a case with generated code today where I believe the compiler is giving a false error trying to verify a trait implementation.

Given a crate foo defined as:

// foo.rs
pub trait Foo {}

and then a consumer crate bar:

// bar.rs
struct A;

trait B {
    type C;
}

impl B for A {
    type C = MyType;
}

struct MyType;

impl foo::Foo for <A as B>::C {}

when compiled this yields:

$ rustc +nightly foo.rs --crate-type lib
$ rustc +nightly bar.rs --crate-type lib --extern foo=libfoo.rlib
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> bar.rs:13:1
   |
13 | impl foo::Foo for <A as B>::C {}
   | ^^^^^^^^^^^^^^^^^^-----------
   | |                 |
   | |                 `<A as B>::C` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   |
   = note: define and implement a trait or new type instead

error: aborting due to previous error

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

I don't believe that the error here is accurate since A, B, C, and MyType are all local to the crate and the compiler would ideally follow the projection to conclude that this trait implementation is valid.

@jyn514
Copy link
Member

jyn514 commented Apr 9, 2023

This works ok if you rewrite it to impl inner::Foo for MyType {}, so this is probably a bug somewhere in coherence checking.

@jyn514 jyn514 added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. A-coherence Area: Coherence labels Apr 9, 2023
@lcnr lcnr added the T-types Relevant to the types team, which will review and decide on the PR/issue. label May 12, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 14, 2024
…=scottmcm

Explicitly specify type parameter on FromResidual for Option and ControlFlow.

~~Remove type parameter default `R = <Self as Try>::Residual` from `FromResidual`~~ _Specify default type parameter on `FromResidual` impls in the stdlib_ to work around rust-lang#99940 / rust-lang#87350 ~~as mentioned in rust-lang#84277 (comment).

This does not completely fix the issue, but works around it for `Option` and `ControlFlow` specifically (`Result` does not have the issue since it already did not use the default parameter of `FromResidual`).

~~(Does this need an ACP or similar?)~~ ~~This probably needs at least an FCP since it changes the API described in [the RFC](rust-lang/rfcs#3058). Not sure if T-lang, T-libs-api, T-libs, or some combination (The tracking issue is tagged T-lang, T-libs-api).~~ This probably doesn't need T-lang input, since it is not changing the API of `FromResidual` from the RFC? Maybe needs T-libs-api FCP?
jieyouxu added a commit to jieyouxu/rust that referenced this issue Aug 14, 2024
…=scottmcm

Explicitly specify type parameter on FromResidual for Option and ControlFlow.

~~Remove type parameter default `R = <Self as Try>::Residual` from `FromResidual`~~ _Specify default type parameter on `FromResidual` impls in the stdlib_ to work around rust-lang#99940 / rust-lang#87350 ~~as mentioned in rust-lang#84277 (comment).

This does not completely fix the issue, but works around it for `Option` and `ControlFlow` specifically (`Result` does not have the issue since it already did not use the default parameter of `FromResidual`).

~~(Does this need an ACP or similar?)~~ ~~This probably needs at least an FCP since it changes the API described in [the RFC](rust-lang/rfcs#3058). Not sure if T-lang, T-libs-api, T-libs, or some combination (The tracking issue is tagged T-lang, T-libs-api).~~ This probably doesn't need T-lang input, since it is not changing the API of `FromResidual` from the RFC? Maybe needs T-libs-api FCP?
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 14, 2024
Rollup merge of rust-lang#128954 - zachs18:fromresidual-no-default, r=scottmcm

Explicitly specify type parameter on FromResidual for Option and ControlFlow.

~~Remove type parameter default `R = <Self as Try>::Residual` from `FromResidual`~~ _Specify default type parameter on `FromResidual` impls in the stdlib_ to work around rust-lang#99940 / rust-lang#87350 ~~as mentioned in rust-lang#84277 (comment).

This does not completely fix the issue, but works around it for `Option` and `ControlFlow` specifically (`Result` does not have the issue since it already did not use the default parameter of `FromResidual`).

~~(Does this need an ACP or similar?)~~ ~~This probably needs at least an FCP since it changes the API described in [the RFC](rust-lang/rfcs#3058). Not sure if T-lang, T-libs-api, T-libs, or some combination (The tracking issue is tagged T-lang, T-libs-api).~~ This probably doesn't need T-lang input, since it is not changing the API of `FromResidual` from the RFC? Maybe needs T-libs-api FCP?
@fmease fmease added the A-associated-items Area: Associated items (types, constants & functions) label Nov 3, 2024
@fmease
Copy link
Member

fmease commented Nov 3, 2024

Fixed by #117164.

@fmease fmease closed this as completed Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-coherence Area: Coherence C-bug Category: This is a bug. T-compiler Relevant to the compiler 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

4 participants