Commit Graph

804 Commits

Author SHA1 Message Date
Pavel Yaskevich
2dccdbfabf [ConstraintSystem] NFC: Remove workarounds related to (now deprecated) CSDiag 2020-02-18 10:13:09 -08:00
Pavel Yaskevich
81c3f62638 [CSGen] Validate presence of _MaxBuiltinFloatType before generating constraints for float literals 2020-02-17 16:09:11 -08:00
Pavel Yaskevich
3edefea8d3 [ConstraintSystem] Don't attempt to solve closures with error expressions inside
Problem(s) which caused `ErrorExpr` to be added to AST should be
diagnosed already and solver wouldn't be able to produce a viable
solution in such case anyway.
2020-02-17 16:09:11 -08:00
Pavel Yaskevich
e17df6bcdf [CSGen] Don't try to infer closure types with destructured parameters
If one of the parameters represents a destructured tuple
e.g. `{ (x: Int, (y: Int, z: Int)) in ... }` let's fail
inference and not attempt to solve the constraint system because:

a. Destructuring has already been diagnosed by the parser;
b. Body of the closure would have error expressions for
   each incorrect parameter reference and solver wouldn't
   be able to produce any viable solutions.
2020-02-17 16:09:11 -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
Doug Gregor
a308a7e53c [Constraint system] Sink down debug logging.
This way we’ll see constraints whenever they’re generated, and remove some
of the expression-centric nature of the top-level solve() and solveImpl().
2020-02-11 17:33:08 -08:00
Doug Gregor
bddee5cff2 [Constraint system] Move shrinking back into typeCheckExpression()
We only want this at the top level, not during the middle of constraint
generation.
2020-02-11 17:24:38 -08:00
Doug Gregor
17e1c91b4b [Constraint system] Sink optional “some” pattern handling into constraint gen 2020-02-11 17:11:31 -08:00
Doug Gregor
4d1de7fd3d [Constraint solver] Sink down initialization pattern constraint generation.
Sink the constraint generation for initialization patterns, including all
of the logic for property wrappers, from the high-level entry point
`typeCheckBinding` down into the lower-level constraint generation for
solution application targets.
2020-02-10 22:01:37 -08:00
Doug Gregor
caf2f62ea3 Merge pull request #29717 from DougGregor/init-via-solution-target
[Constraint system] Sink initialization logic into SolutionApplicationTarget
2020-02-08 20:38:09 -08:00
Doug Gregor
efd4152061 [Constraint solver] Factor constraint generation logic for solution targets
Move more constraint generation logic for an expression target into a
new generateConstraints() on a solution target, so we can further
generalize the main “solve” logic.
2020-02-07 20:56:32 -08:00
Pavel Yaskevich
2875aa8e2b [Diagnostics] Diagnose an attempt to init/use dictionary with array literal in CSGen
It's much easier to detect that contextual type is a dictionary
but expression is an array literal while generating constraints.
2020-02-06 09:32:07 -08:00
Doug Gregor
bbc0a95723 [Constraint system] Simplify creation of contextual conversion constraint.
We were storing more state than necessary in the constraint system.
2020-02-03 22:38:24 -08:00
Doug Gregor
46a65009e3 [Constraint system] Eliminate global flag UnderlyingTypeForOpaqueReturnType
Make this information contextual (per type) rather than global (per
constraint system) so we don’t misapply it.
2020-01-27 15:44:54 -08:00
Holly Borla
af847c2f79 Merge pull request #29415 from hborla/ambiguity-special-cases
[CSDiag] Start chipping away at FailureDiagnosis::diagnoseAmbiguity
2020-01-24 13:59:43 -08:00
Doug Gregor
465e5da0a3 [Constraint system] Set the contextual type for condition expressions.
Set the contextual type of conditional expressions (to Bool) with the
"condition" purpose so we get customized diagnostics for mistakes such
as

    if x.property = y { }

Prior to this, we'd get generic "() isn't convertible to Bool"
diagnostic. Now we get

    use of '=' in a boolean context, did you mean '=='?
2020-01-24 10:17:59 -08:00
Doug Gregor
23615ba55d [Function builders] Add support for "if #available".
Availability checks in if statements don't need any actual semantic
checking, so enable them within function builders.
2020-01-23 22:00:41 -08:00
Doug Gregor
7217cfa4a5 [Constraint system] Move statement condition constraint generation.
Move constraint generation for statement conditions onto the
constraint system; it doesn't really have any reason to be located
within the function builder transform.
2020-01-23 21:51:53 -08:00
Holly Borla
74f65ba2ce [ConstraintSystem] Find solutions for code with invalid '_' using
holes.
2020-01-23 10:48:55 -08:00
Pavel Yaskevich
4b46043494 Merge pull request #29304 from LucianoPAlmeida/port-object-literal-diagnostics
[Diagnostics] Port diagnostics from FailureDiagnosis::visitObjectLiteralExpr
2020-01-22 01:10:34 -08:00
Luciano Almeida
716e11f575 [Constraint System] Recording SpecifyObjectLiteralTypeImport fix when attempting literal result type variable binding and handle object literal in MissingArgumentsFailure 2020-01-21 20:39:44 -03:00
Doug Gregor
d0981292ab [Constraint generation] Collect all closure body references to type variables.
Collect all references to parameters whose types involve type variables,
including in closures that aren’t single-expression. This fixes a type
checker assertion that occurs when the constraint graph can get disconnected
with the combination of delayed constraint generation for single-expression
closures and the use of function builders.

Fixes rdar://problem/58695803.
2020-01-21 10:15:28 -08:00
Doug Gregor
8283a67648 Revert "Revert "Reimplement function builders as statement transformations."" 2020-01-21 10:07:20 -08:00
Doug Gregor
86c13d3c74 Revert "Reimplement function builders as statement transformations." 2020-01-17 15:52:49 -08:00
Doug Gregor
fccee4f77b [Constraint generation] Cope with odd handling of parameter types.
When generating constraints in the context of the "old" diagnostic
path that type-checks subexpressions independently, we might not have
types for closure parameters. Cope with the case with a narrower form
of the original hack.
2020-01-16 13:18:33 -08:00
Doug Gregor
3a51d5b9b2 [WIP] Reimplement function builders as statement transformations. 2020-01-16 13:18:33 -08:00
Pavel Yaskevich
4eda04297f [CSGen] Unify handling of single- and multi-statement closures
Both multi- and single-statement closures now behave the same way
when it comes to constraint generation, because the body is opened
only once contextual type becomes available or it's impossible
to find one e.g. `_ = { 1 }`.
2020-01-15 14:19:54 -08:00
Pavel Yaskevich
25433b3805 [CSGen] Use getType when refering to closure parameters in expression context 2020-01-14 00:09:33 -08:00
Pavel Yaskevich
449ef613f3 [CSGen] NFC: Remove obsolete closure handling logic
Since we no longer walk into closures it's possible to remove
`{enter, exit}Closure` and `getClosureParams`.
2020-01-14 00:09:32 -08:00
Pavel Yaskevich
65adc18139 [ConstraintSystem] Delay constraint generation from single-statement closure body
Attempt to infer a closure type based on its parameters and body
and put it aside until contextual type becomes available or it
has been determined that there is no context.

Once all appropriate conditions are met, generate constraints for
the body of the closure and bind it the previously inferred type.

Doing so makes it possible to pass single-statement closures as
an argument to a function builder parameter.

Resolves: SR-11628
Resolves: rdar://problem/56340587
2020-01-14 00:09:32 -08:00
Pavel Yaskevich
465a948e67 [ConstraintSystem] Add a separate constraint generation for closure body
Separate closure from top-level constraint generation because
its body shouldn't be considered when it comes to parent/weight
computation, otherwise top expression in body is going to get
disconnected from the closure.
2020-01-14 00:09:32 -08:00
Holly Borla
9ddb5eddc5 Merge pull request #29065 from hborla/subscript-error-diag
[Diagnostics] Finish porting subscript errors
2020-01-08 16:05:04 -08:00
Holly Borla
7e056671a3 [CSGen] Abstract checking for a nil literal in visitSubscriptExpr into a
general isValidBaseOfMemberRef function.
2020-01-08 13:36:32 -08:00
Holly Borla
42c73334f0 [CSGen] Diagnose subscripting a nil literal in CSGen. 2020-01-07 17:37:38 -08:00
Pavel Yaskevich
9a62701e95 [ConstraintSystem] Use new branch element and fix mismatch between ternary branches 2020-01-07 13:50:09 -08:00
Doug Gregor
61ac2534e7 [Type checker] Make coercePatternToType return the new Pattern.
Rather than mutating the parameter pattern in place and separately
return whether an error occurred, return the new pattern or NULL if an
error occurred. While here, switch over to ContextualPattern for the
input.

And get rid of that infernal "goto".
2020-01-03 17:27:55 -08:00
Doug Gregor
033f9c7927 [Type checker] Introduce "contextual patterns".
Contextual pattern describes a particular pattern with enough
contextual information to determine its type. Use this to simplify
TypeChecker::typeCheckPattern()'s interface in a manner that will
admit request'ification.
2020-01-03 15:41:21 -08:00
Doug Gregor
9c8351b206 [Type checker] Make typeCheckPattern() functional.
Make TypeChecker::typeCheckPattern() return the computed type, rather
than returning an "error" flag. More importantly, make it functional,
so that it doesn't set the type it computes on the pattern.

Use UnresolvedType as a placeholder for types that need to be
inferred, rather than a null type. This allows us to produce
structural types involving types that need to be inferred.

Note that with this change as-is, we get a number of duplicated
diagnostics, because typeCheckPattern() will be called multiple times
for the same pattern and will emit some diagnostics each time. This
will be addressed in a subsequent commit.
2020-01-03 11:25:05 -08:00
Slava Pestov
bdbe062aa2 Sema: Tweak SanitizeExpr handling of AutoClosureExprs
Curry thunks create AutoClosureExprs with parameters, and we can't just
fold those away.

Also, remove an older AutoClosureExpr cleanup that seems to be
redundant.
2019-12-19 23:51:53 -05:00
Pavel Yaskevich
8bcc192591 [Diagnostics] Diagnose inability to infer (complex) closure return type 2019-12-19 12:16:30 -08:00
Brent Royal-Gordon
6a8598a99c [NFC] Remove DeclNameRef staging calls 2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
addbe3e5ed [NFC] Thread DeclNameRef through most of the compiler
This huge commit contains as many of the mechanical changes as possible.
2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
63ec1cf5af Introduce a separate #filePath, remove -pound-file
This makes the path behavior more first-class. The feature is now hidden behind an experimental flag, -enable-experimental-concise-pound-file.
2019-12-04 16:35:13 -08:00
Pavel Yaskevich
5cacd1bb36 [ConstraintSystem] Fix situations when contextual base type can't be inferred
It might be either impossible to infer the base because there is
no contextual information e.g. `_ = .foo` or there is something
else wrong in the expression which disconnects member reference
from its context.
2019-12-03 12:07:16 -08:00
Hamish Knight
19e199e567 Use DefaultArgumentExpr for caller-side defaults (#28279)
Use DefaultArgumentExpr for caller-side defaults
2019-12-02 13:57:17 -08:00
Hamish Knight
7edbd9327e [CS] Remove an unused variable 2019-11-27 17:13:50 -08:00
Pavel Yaskevich
e3a4b4ffc9 [ConstraintSystem] Fix ConstraintGenerator to change phase to "constraint generation" and back 2019-11-21 00:08:58 -08:00
Hamish Knight
7e788b8ab9 [AST] Remove CallerDefaultArgumentExpr
Now that we use DefaultArgumentExpr for both kinds
of default arguments, this is no longer needed.
2019-11-20 15:07:33 -08:00
Slava Pestov
ef05accd31 Sema: Remove even more vestigial TypeChecker usages 2019-11-19 17:40:00 -05:00
Doug Gregor
2d915f60ab [Constraint solver] Remove expression from the constructor.
Rather than setting up the constraint solver with a single expression
(that gets recorded for parents/depths), record each expression that
goes through constraint generation.
2019-11-14 15:41:38 -08:00