Skip to content

Swift: Dataflow for keypaths #12807

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 9 commits into from
Apr 19, 2023
Merged

Conversation

MathiasVP
Copy link
Contributor

@MathiasVP MathiasVP commented Apr 12, 2023

This PR implements dataflow-support for key-path expressions. This has been made possible thanks for Sasha's recent PR which extracted the right things 🎉.

After this PR, we model a key-path expression as a lambda with a sequence of read steps. For example, consider this snippet:

func testKeyPath() {
  let s = S(x: source())
  let f = \S.x
  sink(s[keyPath: f])
}

this is now modelled like we model:

func testKeyPath() {
  let s = S(x: source())
  let f = { (s : S) in s.x }
  sink(f(s))
}

There's still some future work to be done: I've only supported simple field lookups inside those key-path chains. We also need to handle wrapping and unwrapping of optionals, and writeable key-path expressions. All of this should be fairly straightforward once the infrastructure from this PR is merged, though.

Commit-by-commit review recommended.

@d10c I've assigned you as a reviewer because I think you've touched many of these files before (and will probably be touching them again soon). Let me know if you'd like to go through this PR over Zoom or something.

@MathiasVP MathiasVP requested a review from a team as a code owner April 12, 2023 16:44
@MathiasVP MathiasVP requested a review from d10c April 12, 2023 16:44
@github-actions github-actions bot added the Swift label Apr 12, 2023
@MathiasVP MathiasVP added the no-change-note-required This PR does not need a change note label Apr 12, 2023
@MathiasVP MathiasVP force-pushed the dataflow-for-keypaths branch from 9b12f29 to f32d77b Compare April 13, 2023 12:13
@MathiasVP
Copy link
Contributor Author

DCA looks fine: the new lost result is due to extractor-wobbliness. When I run SideStore/SideStore locally, I see the result when running this PR on the database produced for the main run.

Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

Results LGTM but I would like to see more diverse test cases. You mentioned writeable key-path expressions and optional handling as planned follow-up. I've also come across:

  • keypaths of the form \.field where the object can be inferred from context
  • keypaths into arrays \[Int][0]

@MathiasVP
Copy link
Contributor Author

Sure, I'll add more tests. I wasn't aware of the inferred-context version. I'm also sure \[Int][0] won't work currently, but will work automatically once we add support for ArrayContent (but obviously I might as well add the tests now).

@MathiasVP
Copy link
Contributor Author

231b0fc adds a few more tests.

@geoffw0
Copy link
Contributor

geoffw0 commented Apr 18, 2023

Great, thanks. Happy with the tests now!

I'm also sure [Int][0] won't work currently, but will work automatically once we add support for ArrayContent (but obviously I might as well add the tests now).

Good point. It will be nice when we have ArrayContent working and this comes together.

Copy link
Contributor

@d10c d10c left a comment

Choose a reason for hiding this comment

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

Looking good! See my comment though.

let s = S(x: source())
let s2 = S2_Optional(s: s)
let f = \S2_Optional.s?.x
sink(opt: s2[keyPath: f]) // $ MISSING: flow=611
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should have all the pieces now for making this work, no? We have content flow for optionals, it just needs to be linked to key paths like you did with field contents.

I'm also fine with leaving this as a follow-up issue though, but if it's not too complicated it would be nice to see this test pass.

Copy link
Contributor Author

@MathiasVP MathiasVP Apr 19, 2023

Choose a reason for hiding this comment

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

Yeah, I think everything should be in place to make this work. I'd like to save it as follow-up, though :)

I'll create an issue with what needs to happen as a follow-up

@MathiasVP MathiasVP merged commit 8d62d59 into github:main Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Swift
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants