Skip to content

Orphan rule checking too strict for associated types #102728

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

Closed
axos88 opened this issue Oct 6, 2022 · 1 comment
Closed

Orphan rule checking too strict for associated types #102728

axos88 opened this issue Oct 6, 2022 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@axos88
Copy link

axos88 commented Oct 6, 2022

I tried this code:

// trait Crypto is foreign

pub struct MyAccountKey;
pub struct MyCrypto;

impl Crypto for MyCrypto {
  type AccountKey = MyAccountKey
}

// impl<'a> From<&'a <MyAccountKey as Crypto>::AccountKey> for ForeignFoo {
//     fn from(_: &'a <MyAccountKey as Crypto>::AccountKey) -> Self {
//         todo!()
//     }
// }

impl<'a> From<&'a AcmeEspAccountKey> for ForeignFoo {
    fn from(_: &'a AcmeEspAccountKey) -> Self {
        todo!()
    }
}

I expected to see this happen: both commented code and uncommented code should compile fine

Instead, this happened: orphan rule is triggered:

error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> src/my_crypto.rs:95:1
|
95 | impl<'a> From<&'a ::AccountKey> for ForeignFoo {
| ^^^^^^^^^---------------------------------------------------^^^^^-------------
| | | |
| | | ForeignFoo is not defined in the current crate
| | <MyAccountKey as Crypto>::AccountKey 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

Meta

rustc --version --verbose:

rustc 1.66.0-nightly (57f097ea2 2022-10-01)
binary: rustc
commit-hash: 57f097ea25f2c05f424fc9b9dc50dbd6d399845c
commit-date: 2022-10-01
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.2
Backtrace

N/A

@axos88 axos88 added the C-bug Category: This is a bug. label Oct 6, 2022
@lcnr
Copy link
Contributor

lcnr commented Oct 6, 2022

your snippet fails the orphan check. the orphan check does not look into associated types as it has to run before we are able to do so.

It is therefore not able to tell that <MyAccountKey as Crypto>::AccountKey would be a local type and this is expected behavior. Opened #102734 to potentially improve the error message here

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.
Projects
None yet
Development

No branches or pull requests

2 participants