Skip to content

Suggest qualifying struct literal path to "escape the shadow" cast by struct-like enum variant #139601

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
fmease opened this issue Apr 9, 2025 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-more_qualified_paths `#![feature(more_qualified_paths)]` P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fmease
Copy link
Member

fmease commented Apr 9, 2025

Given

#![feature(more_qualified_paths)]

enum E { S { gotten: () } }
struct S { wanted: i32 }

trait O { type S; fn f(); }
impl O for E {
    type S = S;
    fn f() { // (A)
        let _ = Self::S { wanted: 0 }; //~ ERROR variant `E::S` has no field named `wanted`
    }
}

fn main() { // (B)
    let _ = E::S { wanted: 0 }; //~ ERROR variant `E::S` has no field named `wanted`
}

Self::S (A) / E::S (B) resolves to the enum variant, not the associated type and thus

error[E0559]: variant `E::S` has no field named `wanted`
  --> src/main.rs:10:27
   |
10 |         let _ = Self::S { wanted: 0 };
   |                           ^^^^^^ `E::S` does not have this field
   |
   = note: available fields are: `gotten`

error[E0559]: variant `E::S` has no field named `wanted`
  --> src/main.rs:15:20
   |
15 |     let _ = E::S { wanted: 0 };
   |                    ^^^^^^ `E::S` does not have this field
   |
   = note: available fields are: `gotten`

Ideally, since (and only since) more_qualified_paths is enabled, rustc would suggest qualifying Self::S and E::S via <Self as O>::S and <E as O>::S, respectively.

These might be quite annoying to diagnose due to the sheer number of possible symptoms (e.g., missing fields, extra fields, fields of wrong type). Since this input is a bit contrived, marking P-low.


Case (A) is reminiscent of #57644 but still distinct (however, do we want to do anything here?).

@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-more_qualified_paths `#![feature(more_qualified_paths)]` P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 9, 2025
@fmease fmease changed the title Suggest qualifying struct literal path to "escape the shadow" of struct-like enum variant Suggest qualifying struct literal path to "escape the shadow" cast by struct-like enum variant Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-more_qualified_paths `#![feature(more_qualified_paths)]` P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant