Skip to content

Unable to find assoc type when using tuple create syntax Self::AssocType(...) #120871

Closed
@nyurik

Description

@nyurik

I tried this code playground

struct MyValue;
struct MyError(u32);

impl MyError {
    #[allow(dead_code)]
    fn new(n: u32) -> Self {
        Self(n)
    }
}

impl std::str::FromStr for MyValue {
    type Err = MyError;
    fn from_str(_s: &str) -> Result<Self, Self::Err> {
        // -------- THIS DOES NOT COMPILE --------
        Err(Self::Err(42))

        // --------- THESE COMPILE FINE ----------
        // Err(Self::Err { 0: 42 })
        // Err(Self::Err::new(42))
        // Err(MyError(42))
    }
}

fn main() {
    assert!("x".parse::<MyValue>().is_err());
}

I expected to see this happen: it should compile

Instead, this happened:

error[E0599]: no associated item named `Err` found for struct `MyValue` in the current scope
  --> src/main.rs:15:19
   |
1  | struct MyValue;
   | -------------- associated item `Err` not found for this struct
...
15 |         Err(Self::Err(42))
   |                   ^^^ associated item not found in `MyValue`

Meta

rustc --version --verbose:

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

This might be related to #71054

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions