Commit Graph

1082 Commits

Author SHA1 Message Date
Holly Borla
66e85721cb Merge pull request #30807 from hborla/property-wrapper-refactoring
[Property Wrappers] Refactor property wrappers so the synthesized backing init is only type checked once
2020-04-14 10:48:22 -07:00
Hamish Knight
a61223a255 [CS] Visit all fixed bindings for constraint re-activation (#30886)
[CS] Visit all fixed bindings for constraint re-activation
2020-04-10 12:27:47 -07:00
Hamish Knight
d69a42d656 [CS] Preserve compatibility for collection coercions
Previously we could allow some invalid coercions to
sneak past Sema. In most cases these would either
cause crashes later down the pipeline or
miscompiles. However, for coercions between
collections, we emitted somewhat reasonable code
that performed a force cast.

This commit aims to preserve compatibility with
those collection coercions that previously
compiled, and emits a warning telling the user to
use either 'as?' or 'as!' instead.
2020-04-10 10:16:07 -07:00
Doug Gregor
6999c318b7 Merge pull request #30924 from DougGregor/for-each-solution-application-target
[Constraint solver] Migrate for-each statement checking into SolutionApplicationTarget
2020-04-10 07:28:21 -07:00
Doug Gregor
1f232f7f53 [Constraint solver] Remove ExprTypeCheckListener.
The last client of this listener-based interface has been refactored,
so remove it.
2020-04-09 11:18:53 -07:00
Doug Gregor
87d86f3545 [Constraint solver] Migrate for-each statement checking into SolutionApplicationTarget.
Pull the entirety of type checking for for-each statement headers (i.e., not the
body) into the constraint system, using the normal SolutionApplicationTarget-based
constraint generation and application facilities. Most of this was already handled
in the constraint solver (although the `where` filtering condition was not), so
this is a smaller change than it looks like.
2020-04-09 11:02:56 -07:00
Hamish Knight
76881a39a5 [CS] Adjust applied overload simplification (#30716)
[CS] Adjust applied overload simplification
2020-04-07 19:04:27 -07:00
Holly Borla
c301176ca6 [ConstraintSystem] Allow SolutionApplicationTarget to store the
innermost call to init(wrappedValue:) if the target is for initialization
of a property wrapper's backing storage.
2020-04-03 15:12:12 -07:00
Robert Widmann
92c8a65f09 Drop references to name binding as a phase
A lot of places appear to mean "name lookup".  A few places meant "import resolution".
2020-03-29 18:51:09 -07:00
Hamish Knight
71882996ca [CS] Adjust applied overload simplification
Currently `simplifyAppliedOverloads` depends on
the order in which constraints are simplified,
specifically that a lookup constraint for a
function gets simplified before the applicable
function constraint. This happens to work out
just fine today with the order in which we
re-activate constraints, but I'm planning on
changing that order.

This commit changes the logic such that it it's no
longer affected by the order in which constraints
are simplified. We'll now run it when either an
applicable function constraint is added, or a new
bind overload disjunction is added. This also
means we no longer need to run it potentially
multiple times when simplifying the applicable fn.
2020-03-29 18:33:59 -07:00
Hamish Knight
234270c941 [CS] NFC: Tweak addExplicitConversionConstraint
Have it take a RememberChoice_t instead of an
'allowFixes' bool, as the parameter no longer
really corresponds to fixes.
2020-03-27 15:13:24 -07:00
Pavel Yaskevich
4b3d075fb7 [ConstraintSystem] Produce fallback diagnostic on mismatch between fixes/holes
If score indicates that a solution should have at least one fix
but it doesn't let's attempt to produce a fallback diagnostic inline.
2020-03-24 16:51:44 -07:00
Pavel Yaskevich
da2023c9a0 [ConstraintSystem] Score solutions based on number of holes
Introduce `SK_Hole` which is used to count a number of "holes" in
a given solution. It is used to distinguish solutions with fewer holes.

Also it makes it possible to check whether a solution has holes but
no fixes, which is an issue and such solution shouldn't be applied
to AST.
2020-03-24 16:51:44 -07:00
Pavel Yaskevich
d673ed0989 Merge pull request #30519 from xedin/rdar-52204414
[ConstraintSystem] Don't allow explicit closure result to be implicitly converted to `Void`
2020-03-20 09:36:17 -07:00
Doug Gregor
3abea6be65 [Constraint system] Use the PatternBindingDecl context when possible.
Make sure that we're resolving types and patterns using the
PatternBindingDecl context, both for the type resolver context and the
contextual pattern used for pattern resolution.

Fixes a regression with implicitly-unwrapped options reported as
SR-11998 / rdar://problem/58455441.
2020-03-18 21:09:06 -07:00
Pavel Yaskevich
2d6170b847 [TypeChecker] Requestify check to determine if closure has explicit result
Determine whether closure body has any explicit `return`
statements which could produce a non-void result.
2020-03-18 17:06:15 -07:00
Robert Widmann
14c073ba0b Remove friend references to TypeChecker
TypeChecker has no friends
2020-03-16 19:19:02 -07:00
Pavel Yaskevich
2dd74706c9 Merge pull request #30411 from xedin/diags-to-use-solution-instead-of-cs
[Diagnostics] Switch `FailureDiagnostic` to use a solution
2020-03-13 20:20:58 -07:00
Pavel Yaskevich
3ab8710329 [Diagnostics] Cleanup use of constraint system by ConstraintFix/FailureDiagnostic 2020-03-13 15:02:10 -07:00
Pavel Yaskevich
0fc9d1b84a [Diagnostics] Move getFunctionArgApplyInfo to Solution
Instead of relying on constraint system having solution applied
let's pass a solution directly to `getFunctionArgApplyInfo`.
2020-03-13 12:46:25 -07:00
Pavel Yaskevich
7c1df6ad51 [ConstraintSystem] Remove getType(Expr*) accessor from Solution
It's obsolete and has been superseded by `getType(TypedNode)`.
2020-03-12 10:28:18 -07:00
Robert Widmann
4e67eec878 [CS] Move getUnopenedTypeOfReference out of TypeChecker
For some reason, the changed caller in CS wasn't actually going to use any of the types in the constraint system from the entrypoint it was calling. Switch over to using the constraint-system-based entrypoint so we can pick up expression types consistently. Then, move the TypeChecker entrypoint onto ConstraintSystem to reduce the duplication here.

The remaining callers of buildCheckedRefExpr should be migrated.
2020-03-11 11:14:10 -07:00
Holly Borla
b12bd4e919 Merge pull request #30307 from hborla/merge-partial-solutions-too-complex
[ConstraintSystem] Respect the constraint solver performance thresholds,
2020-03-10 09:44:32 -07:00
Holly Borla
c1c6a884a4 [ConstraintSystem] Respect the constraint solver performance thresholds,
including time and allocated memory, in mergePartialSolutions.
2020-03-09 14:44:12 -07:00
Pavel Yaskevich
e4d3eaaf2a [ConstraintSystem] Don't store base constraint system in shrink candidate
Since there is no more sub-expression re-typechecking solver wouldn't
be trying to ran shrink on sub-expressions.
2020-03-06 09:46:12 -08:00
Pavel Yaskevich
7c19d1ccd1 [ConstraintSystem] Remove baseCS since CSDiag has been removed
There is no more sub-expression re-typechecking so no need to keep
parent constraint systems around.
2020-03-06 09:35:52 -08:00
Doug Gregor
8191aa4924 Merge pull request #30174 from DougGregor/function-builder-switch
[Constraint system] Implement switch support for function builders.
2020-03-03 12:34:08 -08:00
Doug Gregor
4b43573693 [Constraint system] Implement switch support for function builders.
Implement support for switch statements within function builders. Cases can
perform arbitrary pattern matches, e.g.,

    tuplify(true) { c in
      "testSwitchCombined"
      switch e {
      case .a:
        "a"
      case .b(let i, _?), .b(let i, nil):
        i + 17
      }
    }

subject to the normal rules of switch statements. Cases within function
builders cannot, however, include “fallthrough” statements, because those
(like “break” and “continue”) are control flow.

The translation of performed for `switch` statements is similar to that of
`if` statements, using `buildEither(first:)` and `buildEither(second:)` on
the function builder type.

This is the bulk of switch support, tracked by rdar://problem/50426203.
2020-03-02 17:25:25 -08:00
Hamish Knight
39d988da9c [CS] Remove ReturnAllDiscoveredSolutions flag
This is now no longer used.
2020-03-02 14:50:16 -08:00
Doug Gregor
13b200ddd5 [Constraint system] Generalize saved ASTNode -> solution target mapping.
We are currently only using the “solution targets” map for statement
conditions, but we will need it for more entities soon. Start generalizing
now.
2020-03-02 08:43:55 -08:00
Holly Borla
87bb7755c2 Merge pull request #30101 from hborla/dynamic-replacement-type-erasure
[Sema] Implement type erasure for dynamic replacement.
2020-02-28 09:37:33 -08:00
Holly Borla
3cdc30ffeb [Sema] Support type erasure for dynamic replacement in function
builders.
2020-02-27 09:01:16 -08:00
Doug Gregor
141f3e7f07 [Constraint system] Expand SolutionApplicationTarget to StmtConditions.
Handle StmtCondition as part of SolutionApplicationTarget, so we can
generate constraints from it and rewrite directly as part of a solution,
rather than open-coding the operation in the function builder transform.
2020-02-25 13:47:26 -08:00
Doug Gregor
4830c48960 [Constraint system] Support if let / if case in function builders.
Use the generalized constraint generation and binding for patterns to
introduce support for if-let and if-case in function builders, handling
arbitrary patterns.

Part of function builder generalization, rdar://problem/50426203.
2020-02-25 09:44:06 -08:00
Doug Gregor
3f2f79a699 [Constraint system] Fold pattern variable binding into constraint gen.
Rather than re-walk the pattern to create type bindings for the variables
that show up in the pattern, assign types to each of the variables as part
of constraint generation for the pattern. Only do this in contexts
where we will need the types, e.g., function builders.
2020-02-25 08:51:56 -08:00
Doug Gregor
dcf7ddeb3a [Constraint system] Generate constraints for EnumElement patterns.
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.
2020-02-24 00:48:15 -08:00
Doug Gregor
d2b2a50772 [Constraint system] Record application targets for statement conditions.
We’ll need this to generate constraints for statement conditions within the
constraint system. This is unused boilerplate at the moment.
2020-02-24 00:48:14 -08:00
Pavel Yaskevich
71753f3ca6 [ConstraintSystem] Rank contextually unavailable overloads lower than other choices (#29921)
Currently constraint solver is only capable of detecting universally unavailable
overloads but that's insufficient because it's still possible to pick a contextually
unavailable overload choice which could be better than e.g. generic overload, or
one with defaulted arguments, marked as disfavored etc.

Let's introduce `ConstraintSystem::isDeclUnavailable` which supports both universal
and contextual unavailability and allow constraint solver to rank all unavailable
overload choices lower than any other possible choice(s).

Resolves: rdar://problem/59056638
2020-02-19 13:13:53 -05:00
Hamish Knight
857e523deb [CS] Remove unused function
In addition to a member it set on SanitizeExpr.
Unfortunately it will take a bit more effort to
kill SanitizeExpr entirely.
2020-02-19 07:43:59 -08:00
Hamish Knight
1a9764e6d7 [CS] Remove SubExpressionDiagnostics option 2020-02-19 07:43:59 -08:00
Hamish Knight
ed1d372887 [CS] Remove DiagnosedExprs 2020-02-19 07:43:59 -08:00
Hamish Knight
4af4af457e [CS] Remove baseCS param from typeCheckExpression 2020-02-19 07:43:58 -08:00
Pavel Yaskevich
8722530943 [CSDiag] NFC: Move diagnoseDeprecatedConditionalConformanceOuterAccess to CSApply 2020-02-17 16:09:11 -08:00
Jonas Devlieghere
784006911b Merge pull request #29822 from plotfi/master
Fixing build breaks against latest llvm.org (that can go into master).
2020-02-13 22:58:20 -08:00
Puyan Lotfi
b707b7f0b3 Fixing build breaks for master-next against latest llvm.org.
There were changes due to the StringRef to std::string conversion, changes
in the Debug Info DIBuilder::createModule API, and a drop in the using for
PointerUnion4 since PointerUnion is now a variadic template and will do in its
place.
2020-02-13 14:12:21 -08:00
Doug Gregor
5e28c7c886 [Constraint System] Move bindVariablesInPattern into the constraint system.
This general notion of wiring up the types of variables that occur
within a pattern to the types in the produced pattern type is useful
outside of function builders, too.
2020-02-13 10:51:26 -08:00
Pavel Yaskevich
355148dbe8 Merge pull request #29746 from hborla/ambiguity-with-fixes
[Diagnostics] Refactor diagnoseAmbiguityWithFixes.
2020-02-13 02:42:43 -08:00
Pavel Yaskevich
4d5c676ca9 [ConstraintSystem] NFC: Remove obsolete code related to fixes from Solution 2020-02-12 17:57:36 -08:00
Pavel Yaskevich
5437622d2d [Diagnostics] Diagnose ambiguity with conflicting arguments to generic parameters
It's done by first retrieving all generic parameters from each solution,
filtering boundings into distrinct set and diagnosing any differences.

For example:

```swift
func foo<T>(_: T, _: T) {}
func bar(x: Int, y: Float) {
  foo(x, y)
}
```
2020-02-12 17:53:31 -08:00
Doug Gregor
2347829324 [Constraint System] Allow initialized let/var declarations in function builders.
Introduce support for initialized let/var declarations within function
builder closures, e.g.,

    let (a, b) = c()

We generate constraints for the declarations as elsewhere, but the types of
the declared variables (a and b in this case) are bound to the type of the
pattern by one-way constraints, to describe the flow of type information
through the closure.

Implements rdar://problem/57330696.
2020-02-12 17:51:48 -08:00