Resolve Self::AssocType as ctor when it's an associated type#152247
Draft
nyurik wants to merge 1 commit intorust-lang:mainfrom
Draft
Resolve Self::AssocType as ctor when it's an associated type#152247nyurik wants to merge 1 commit intorust-lang:mainfrom
nyurik wants to merge 1 commit intorust-lang:mainfrom
Conversation
… type When path resolution fails for a value like Self::Associated or Self::Err(42), check whether we're inside an impl and the name is an associated type that resolves to a struct (unit or tuple). If so, treat the path as that struct's constructor. - Fixes rust-lang#71054 (unit struct: Self::Associated) - Fixes rust-lang#120871 (tuple struct: Self::Err(42)) Add try_associated_type_constructor() in FnCtxt and use it from resolve_ty_and_res_fully_qualified_call on NoMatch. Extend to support both CtorKind::Const and CtorKind::Fn. Tests: issue-71054.rs (unit + brace form), issue-120871.rs (FromStr tuple ctor). Convert associated-type-call.rs and invalid-ctor.rs to check-pass and remove their .stderr/.fixed.
Member
|
We ask that you understand the code that you wrote and are able to argue, persuasively, for its correctness. Your PR description does not explain why. |
Contributor
|
To elaborate my comment a bit: trait Trait {
type Tuple;
// macros might generate unconventional names like this.
const Tuple: isize;
}We may have an associated const of the same name in the value space. |
Contributor
|
Technically this needs to go through the language team, but I'm skeptical about the change. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Vibe code alert: I manually did all sorts of cleanup for this, but the core of the
try_associated_type_constructorwas vibe-coded, and despite being "seemingly" correct, it might be completely bogus due to my lack of full internals understanding... than again, not many ppl on this planet have "full" understanding of the rust compiler's code :)When path resolution fails for a value like Self::Associated or Self::Err(42), check whether we're inside an impl and the name is an associated type that resolves to a struct (unit or tuple). If so, treat the path as that struct's constructor. This only happens if all other type resolutions have failed. BTW, @adwinwhite suggested that this might be unfixable due to backwards compat in #120871 (comment) -- so my approach could be totally wrong.
Self::AssocType(...)#120871 (tuple struct: Self::Err(42))Add try_associated_type_constructor() in FnCtxt and use it from resolve_ty_and_res_fully_qualified_call on NoMatch. Extend to support both CtorKind::Const and CtorKind::Fn.
Tests: issue-71054.rs (unit + brace form), issue-120871.rs (FromStr tuple ctor). Convert associated-type-call.rs and invalid-ctor.rs to check-pass and remove their .stderr/.fixed.
?r @petrochenkov