Skip to content

Correctly distinguish between tuple fields and associated values in patterns #26357

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 7 commits into from
Aug 12, 2019
Merged

Correctly distinguish between tuple fields and associated values in patterns #26357

merged 7 commits into from
Aug 12, 2019

Conversation

varungandhi-apple
Copy link
Contributor

@varungandhi-apple varungandhi-apple commented Jul 25, 2019

See the second commit message for more details.

I'm not sure whether I should've broken things up into multiple commits, since there are couple of unrelated things that I noticed and cleaned up, but they're not really related to the main content of the PR.

Fixes SR-11160 and related rdar://problem/53312914.

@varungandhi-apple
Copy link
Contributor Author

@swift-ci please smoke test

@varungandhi-apple
Copy link
Contributor Author

varungandhi-apple commented Jul 25, 2019

Skimming through the failure logs, I think the only failure is because of SwiftPM where they're relying on buggy behavior. 😢 Looking at this line and this one:

enum Z { case z(Int, Int) }
func f() {
  switch Z.z(1, 2) {
  case .z(let (a, b)): print(a, b)
  }
}

should compile fine for source compat but it doesn't now. There is a similar problem with this switch.

I'm trying to think of how we could make the fix without breaking them...

@theblixguy
Copy link
Collaborator

cc @aciidb0mb3r

@aciidgh
Copy link
Contributor

aciidgh commented Jul 26, 2019

I am fine with fixing the SwiftPM code but the source compatibility probably needs to be discussed somewhere ~

@varungandhi-apple
Copy link
Contributor Author

@aciidb0mb3r thanks! Actually, I think it is a blessing in disguise -- we caught this sooner rather than later thanks to SwiftPM 😅. I've added this hazard to SR-11212.

@varungandhi-apple
Copy link
Contributor Author

Current status: we have a couple of cases failing on master (non-exhaustive match) thanks to SR-11212.

https://github.com/apple/swift/blob/039bd76159f34c770b272fde3f7a62e888a9a7e1/test/Sema/exhaustive_switch.swift#L1226-L1236

Everything else still works.

Also fixes rdar://problem/53312914.

The fact that ParenType is being used to distinguish the two cases makes me
uncomfortable but I don't have better ideas.

Related:
- Fixed a bug in pattern projection which I encountered once I fixed SR-11160.
  This is the 3 line change around conArgSpaces.
- Opened SR-11212 for ill-typed patterns that are permitted to compile.

Unrelated cleanup:
- Removed a redundant switch case in 'isSubspace'.
- Added names for referenced papers for faster lookup.
@varungandhi-apple
Copy link
Contributor Author

@swift-ci please smoke test

@varungandhi-apple
Copy link
Contributor Author

varungandhi-apple commented Aug 2, 2019

Notes for reviewer(s):

  1. As an aside, I have added some examples for PatternKinds but I'm not sure of all of those, so I'd appreciate some help with fixing them.

  2. I've addressed the core issues (we emit a warning, not an error), so the PR is ready for review (barring point 3).

  3. I will add more test cases that make sure everything runs as expected (i.e. we don't have a type-checks fine but some inconsistency causes a runtime crash). Done.

@varungandhi-apple
Copy link
Contributor Author

@swift-ci please smoke test

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.

I think this looks great, @varungandhi-apple! I have left a small comment about Type usage.

// 2. pat ~ (P1, ..., Pm) (m >= 2)
void implicitlyUntuplePatternIfApplicable(TypeChecker *TC,
Pattern *&enumElementInnerPat,
const Type &enumPayloadType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just an FYI - Type is an immutable word sized container for TypeBase * so there is no need to pass it by const &

@xedin
Copy link
Contributor

xedin commented Aug 12, 2019

@varungandhi-apple I think exhaustiveness checker should be consistent with SE-0110 so we should consider these source breaks as fixing invalid implementation behavior. It still needs to be discussed on the forums though.

Make sure that we still emit warnings even if the let bindings are in
different positions.
There is a small chance that codegen and everything works fine, but the
generated code is wrong because of mismatched expectations on two sides,
so we have some tests to catch that.
@varungandhi-apple
Copy link
Contributor Author

@swift-ci please smoke test

@varungandhi-apple varungandhi-apple merged commit 68fb3b1 into swiftlang:master Aug 12, 2019
@varungandhi-apple varungandhi-apple deleted the vg-fix-nested-exhaustiveness-check branch August 12, 2019 20:59
@jrose-apple
Copy link
Contributor

I think exhaustiveness checker should be consistent with SE-0110 so we should consider these source breaks as fixing invalid implementation behavior.

That's not a sufficient reason for a source-break these days. Remember, we're not just talking about a developer's code; we're also talking about the packages they are using, and starting with Swift 5.1 any already-compiled binary frameworks with inlinable code as well.

@xedin
Copy link
Contributor

xedin commented Aug 13, 2019

I'm trying to say that we should, I'm simplify pointing out my opinion on the matter.

drodriguez added a commit to drodriguez/swift that referenced this pull request Sep 5, 2019
Since the introduction of swiftlang#26357/SR-11160 the TestJSONEncoder seems to
have been emitting the new warnings related to tuples being incorrectly
pattern matched.

The changes remove the incorrect code (removing the parenthesis around
the pattern).

Should not have any other effect (and the effect was not normally
visible if the test was successful, anyway).
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.

5 participants