Skip to content

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

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
nyurik opened this issue Feb 10, 2024 · 1 comment · Fixed by #135090
Closed

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

nyurik opened this issue Feb 10, 2024 · 1 comment · Fixed by #135090
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

@nyurik
Copy link
Contributor

nyurik commented Feb 10, 2024

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

@nyurik nyurik added the C-bug Category: This is a bug. label Feb 10, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 10, 2024
@fmease fmease changed the title Unable to find assoc type when using tuple create syntax Self::<AssocType>(...) Unable to find assoc type when using tuple create syntax Self::AssocType(...) Feb 10, 2024
@jieyouxu jieyouxu added A-associated-items Area: Associated items (types, constants & functions) T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 14, 2024
@adwinwhite
Copy link
Contributor

Tuple struct exists in both type and value/fn namespace. When you initialize a tuple struct, it calls the compiler-generated function with the same name.
Unfortunately type alias/associated type only registers the name in type namespace, thus the compiler cannot find the constructor function.
This might be unfixable due to backwards compatibility.

jhpratt added a commit to jhpratt/rust that referenced this issue Jan 7, 2025
…-projection, r=lqd,jieyouxu

Suggest to replace tuple constructor through projection

See the code example. when `Self::Assoc` normalizes to a struct that has a tuple constructor, you cannot construct the type via `Self::Assoc(field, field)`. Instead, suggest to replace it with the correct named struct.

Fixes rust-lang#120871
@bors bors closed this as completed in 8b54f95 Jan 7, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 7, 2025
Rollup merge of rust-lang#135090 - compiler-errors:invalid-tuple-ctor-projection, r=lqd,jieyouxu

Suggest to replace tuple constructor through projection

See the code example. when `Self::Assoc` normalizes to a struct that has a tuple constructor, you cannot construct the type via `Self::Assoc(field, field)`. Instead, suggest to replace it with the correct named struct.

Fixes rust-lang#120871
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

Successfully merging a pull request may close this issue.

4 participants