Skip to content

[Constraint system] Generate constraints from patterns #29879

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 16 commits into from
Feb 25, 2020

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Feb 17, 2020

Generate constraints from the various forms of patterns, so that the well-formedness of the
pattern itself is established while solving the constraint system. Within this pull request:

  • Constraints optional "some" patterns (?)
  • Constraints for "is" patterns, which handle downcasts (spelled via as)
  • Constraints for "enum element" patterns, which match enums (spelled, e.g., .caseName(sub pattern))
  • Mapping "bool" patterns to the Bool type
  • Constraints for "typed" patterns, which introduce conversion constraints.

These constraints are currently used when type checking if case and guard case, but not the
more general switch cases (yet).

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor force-pushed the constraint-system-patterns branch from f53a25d to 03b8217 Compare February 17, 2020 16:26
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor DougGregor force-pushed the constraint-system-patterns branch from 03b8217 to 43f57a8 Compare February 24, 2020 08:44
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor DougGregor requested a review from xedin February 24, 2020 08:46
@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 25210b41cad9387cab02a5c4f770f4f80a665b8f

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 25210b41cad9387cab02a5c4f770f4f80a665b8f

We’ll need this to generate constraints for statement conditions within the
constraint system. This is unused boilerplate at the moment.
Generate a checked-cast constraint for an “is” pattern, which otherwise
doesn’t change the type. This is hard to validate because checked-cast
constraints never actually fail.
For typed patterns, the sub pattern type must be convertible to the type
provided to the pattern.
Generate a complete set of constraints for EnumElement patterns, e.g.,

    case let .something(x, y)

Most of the complication here comes from the implicit injection of optionals,
e.g., this case can be matched to an optional of the enum type of which
`something` is a member. To effect this change, introduce a locator for
pattern matching and use it to permit implicit unwrapping during member
lookup without triggering an error.

Note also labels are dropped completely when performing the match,
because labels can be added or removed when pattern matching. Label
conflict are currently diagnosed as part of coercePatternToType, which
suffices so long as overloading cases based on argument labels is not
permitted.

The primary observable change from this commit is in diagnostics: rather
than diagnostics being triggered by `TypeChecker::coercePatternToType`,
diagnostics for matching failures here go through the diagnostics machinery
of the constraint solver. This is currently a regression, because
there are no custom diagnostics for pattern match failures within the
constraint system. This regression will be addressed in a subsequent
commit; for now, leave those tests failing.
Extend the constraint system’s diagnostics with specific handling for
matching an enum element pattern that has a subpattern (i.e., to capture
associated values) against an enum case that does not have any associated
value. This brings diagnostics for the new code path on par with the existing
diagnostics of coercePatternToType.
@DougGregor DougGregor force-pushed the constraint-system-patterns branch from 06ead94 to d607d3a Compare February 24, 2020 08:48
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test compiler performance

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test compiler performance

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@swift-ci
Copy link
Contributor

Compilation-performance test failed

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

2 similar comments
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

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.

Looks great! Changes in constraint generation make we wonder whether it would make sense to extend visit* methods in Constraint{Walker, Generator} to all of these different patterns too (e.g. visit*Pattern(...)), instead of using a giant switch statement in getTypeForPattern? It seems like this way constraint generation won't need any special handling and type recording (via setType) which is correctly happening.

@DougGregor
Copy link
Member Author

Changes in constraint generation make we wonder whether it would make sense to extend visit* methods in Constraint{Walker, Generator} to all of these different patterns too (e.g. visit*Pattern(...)), instead of using a giant switch statement in getTypeForPattern?

Maybe! My upcoming PR (once this one finally lands) has to start passing down more state information in getTypeForPattern, but that could also be modeled via a separate PatternConstraint{Walker, Generator} and might be easier.

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@swift-ci please smoke test macOS

@DougGregor DougGregor merged commit fe5c068 into swiftlang:master Feb 25, 2020
@DougGregor DougGregor deleted the constraint-system-patterns branch February 25, 2020 04:41
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