Skip to content

no associated item found for struct in the current scope while it exists #71054

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

Open
Lonami opened this issue Apr 12, 2020 · 3 comments
Open
Labels
A-associated-items Area: Associated items (types, constants & functions) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Lonami
Copy link
Contributor

Lonami commented Apr 12, 2020

The following code (playground):

trait Trait {
    type Associated;

    fn instance() -> Self::Associated;
}

struct Associated;
struct Struct;

impl Trait for Struct {
    type Associated = Associated;

    fn instance() -> Self::Associated {
        Self::Associated
    }
}

Fails with this error:

error[E0599]: no associated item named `Associated` found for struct `Struct` in the current scope
  --> src/lib.rs:14:15
   |
8  | struct Struct;
   | -------------- associated item `Associated` not found for this
...
14 |         Self::Associated
   |               ^^^^^^^^^^ associated item not found in `Struct`

However, if we alter instance() slightly to either of these, the code compiles successfully:

    fn instance() -> Self::Associated {
        Self::Associated {} // {} even though we have `struct S;`, not `struct S {}`
    }
    fn instance() -> Self::Associated {
        Associated // outer scope struct definition
    }

It is worth mentioning that explicitly using as Trait does not solve the issue, and fails with a different error:

    fn instance() -> Self::Associated {
        <Self as Trait>::Associated
    }

Error:

error[E0575]: expected method or associated constant, found associated type `Trait::Associated`
  --> src/lib.rs:14:9
   |
14 |         <Self as Trait>::Associated
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: can't use a type alias as a constructor

Adding {}:

    fn instance() -> Self::Associated {
        <Self as Trait>::Associated {}
    }

Fails with:

error: expected one of `.`, `::`, `;`, `?`, `}`, or an operator, found `{`
  --> src/lib.rs:14:37
   |
14 |         <Self as Trait>::Associated {}
   |                                     ^ expected one of `.`, `::`, `;`, `?`, `}`, or an operator
@jonas-schievink jonas-schievink added A-associated-items Area: Associated items (types, constants & functions) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Apr 12, 2020
@dancrossnyc
Copy link

This just came up! I'm curious if anyone's had a look?

@nyurik
Copy link
Contributor

nyurik commented Feb 10, 2024

I just filed #120871 - which seem similar, and has a few extra test cases that all pass.

@cjgillot cjgillot removed their assignment Feb 18, 2024
@j-a-m-l
Copy link

j-a-m-l commented Dec 12, 2024

A workaround could be implementing Default to get an instance of the associated type:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2ff89b344b184dd52252d0abcbea4d86

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) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants