Skip to content

[TypeChecker] Diagnose key paths with contextual type but no leading dot #30867

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

Merged
merged 2 commits into from
Apr 8, 2020

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Apr 8, 2020

If a Swift key path has root type inferred but does not have a leading dot, then diagnose it, because it's not valid. For example:

struct Foo {
  let property: [Int] = []
  let kp: KeyPath<Foo, Int> = \property.count // error
}

Resolves SR-12290
Resolves rdar://problem/59874355

xedin and others added 2 commits April 7, 2020 16:16
This is going to be useful to detect whether contextual root
is really expected during key path resolution in Sema.
If a Swift key path has root type inferred but does not have a leading dot,
then diagnose it, because it's not valid.

For example:

```swift
struct Foo {
  let property: [Int] = []
  let kp: KeyPath<Foo, Int> = \property.count // error
}
```

Resolves SR-12290
Resolves rdar://problem/59874355
@xedin xedin requested review from theblixguy and dan-zheng April 8, 2020 00:00
@xedin
Copy link
Contributor Author

xedin commented Apr 8, 2020

@theblixguy @dan-zheng This is a modified version of #30164 which got reverted because Tensorflow relied on implicit type expressions. I think this problem should be addressed now because KeyPathExpr implicit generated for KeyPathIterable fields could set hasLeadingDot to true which would avoid a diagnostic. WDYT?

@xedin
Copy link
Contributor Author

xedin commented Apr 8, 2020

@swift-ci please smoke test

Copy link
Contributor

@dan-zheng dan-zheng 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 again for reimplementing #30164!

Just for reference: tensorflow branch may remove KeyPathIterable.allKeyPaths derived conformances in favor of a runtime metadata based implementation. That could re-enable #30164 in case this parsing-based approach is less desirable.

Copy link
Collaborator

@theblixguy theblixguy left a comment

Choose a reason for hiding this comment

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

One suggestion but otherwise LGTM. Thanks!

@xedin
Copy link
Contributor Author

xedin commented Apr 8, 2020

I think we’d want to keep information about the leading dot just to avoid mismatches in the future.

@xedin
Copy link
Contributor Author

xedin commented Apr 8, 2020

@swift-ci please test source compatibility

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.

3 participants