Skip to content

[CSDiagnostics] Prevent nested type references in KeyPath components #83625

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

felipemfpeixoto
Copy link

This change adds detection for nested type references in KeyPath components and applies the appropriate fix to generate meaningful error messages, following the same pattern already established for method references.

The fix ensures that invalid KeyPath references fail gracefully in normal mode and provide helpful diagnostics in diagnostic mode, improving the developer experience when working with KeyPaths.

Resolves: #83197

@xedin
Copy link
Contributor

xedin commented Aug 9, 2025

This is not about constraint generation but avoid preventing references to nested types in key path components, the title should reflect that.

@felipemfpeixoto felipemfpeixoto changed the title [CSDiagnostics] Fix KeyPath constraint generation for nested types [CSDiagnostics] Add diagnostic for nested type references in KeyPath components Aug 9, 2025
@felipemfpeixoto
Copy link
Author

This is not about constraint generation but avoid preventing references to nested types in key path components, the title should reflect that.

Fixed!

@xedin xedin changed the title [CSDiagnostics] Add diagnostic for nested type references in KeyPath components [CSDiagnostics] Prevent nested type references in KeyPath components Aug 9, 2025
@xedin
Copy link
Contributor

xedin commented Aug 9, 2025

@felipemfpeixoto I adjusted it some more, could you please amend your commit to reflect the change as well?

This change adds detection for nested type references in KeyPath components and applies the appropriate fix to generate meaningful error messages, following the same pattern already established for method references.

The fix ensures that invalid KeyPath references fail gracefully in normal mode and provide helpful diagnostics in diagnostic mode, improving the developer experience when working with KeyPaths.

Resolves: swiftlang#83197
Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this looks great! I left a small comment about formatting inline.

func testKeypathWithTypeReference() {
struct S {
enum Q {
static let i = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the indentation is off here a bit, we use 2 spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash accessing a KeyPath to generic nested container
2 participants