Commit Graph

1092 Commits

Author SHA1 Message Date
Slava Pestov
f9d77069ae Sema: Move decomposeArgType() to CSDiag and simplify it
As usual, CSDiag is a graveyard for old crap because I'm expecting
it will be gutted over time.
2018-09-10 17:22:31 -07:00
gregomni
13d02bb85c Make the rvalue-as-lvalue fix symmetric for bind constraints, which causes another set of assignment errors to be discovered in
the CS and slightly reduces the code in CSDiag.
2018-08-29 19:07:29 -07:00
Pavel Yaskevich
0dbd12922f Merge pull request #18995 from xedin/add-assignment-diagnostic
[Diagnostics] NFC: Refactor assignment diagnostics into `AssignmentFailure`
2018-08-29 16:34:26 -07:00
Slava Pestov
8928cb5b8a Sema: Stop using FunctionType::getOld() when generating constraints for SubscriptExpr
Previously we would generate the following constraint here, where
'index' and 'output' are concrete types and $input is a type
variable:

- ValueMember(base, $input -> output)
- ArgumentTupleConversion(index, $input)

The problem is that we built a function type where the entire input
was a type variable, which would then bind to an argument list.

We could instead generate this constraint:

- ValueMember(base, $member)
- ApplicableFunction(index -> output, $member)

I also had to redo how keypaths map locators back to key path
components. Previously the ArgumentTupleConversion was created
with a locator ending in KeyPathComponent.

Now the ApplicableFunction is created with this locator, which means
the argument match is performed with a locator ending in
ApplyArgument.

A consequence of this is that the SubscriptIndex and SubscriptResult
locator path elements are no longer used, so remove them.

This requires various mechanical changes in places we look at
locators to handle this change. Should be NFC.
2018-08-28 14:40:56 -07:00
Slava Pestov
865b613d5e Sema: Stop using FunctionType::getOld() in CSDiag
We were building the following constraint, where $member and
$input are type variables and 'result' is a concrete type:

- Conversion($member, $input -> result)

When $member was fixed to a function type of arbitrary
arity, this would simplify to a conversion between $member's
result type and 'result'.

Instead, express this using the newly-added FunctionResult
constraint:

- FunctionResult($member, $result)
- Conversion($result, result)

I wasn't expecting this to change diagnostics, but it looks
like it did; I'm not going to bother investigating why,
because Pavel is going to rewrite contextual diagnostics soon
anyway. All but one are clear improvements.

Fixes <rdar://41416346> and <rdar://41416647>, two cases where
diagnostics regressed from -swift-version 3 to -swift-version 4.
2018-08-28 14:38:00 -07:00
Slava Pestov
5aabd81456 Sema: Stop using FunctionType::getOld() when simplifying construction constraints
Previously we would generate the following constraints here, where
'base', 'arg' and 'result' are concrete types, and $t is a type
variable:

- ValueMember(base, $t -> result)
- ArgumentTupleConversion(arg, $t)

Instead, we now generate these constraints:

- ValueMember(base, $method)
- ApplicableFunction(arg -> result, $method)

Recall that when the right hand side of an ApplicableFunction is
fixed, it simplifies down to an ArgumentTupleConversion and a
Bind. So the above formulation is equivalent, except that again,
we avoid forming a FunctionType where the entire input type is
a single type variable.

As with the UnresolvedDotExpr case, the old code set the
TVO_PreferSubtypeBinding flag on $t, so to preserve the old
ranking behavior, we generate an additional type variable and
constraint:

- FunctionInput($method, $arg)

This is just a temporary stop-gap until TVO_PreferSubtypeBinding
is removed.

Note that if the arguments to a constructor call are invalid, we
now fail the ApplicableFunction constraint and not a
ArgumentTupleConversion. This requires a minor change in CSDiag.

The whole concept of looking at the failed constraint is going
away hopefully, in favor of more precise TypeMatchResult and
Fix-based logic.
2018-08-28 14:38:00 -07:00
Slava Pestov
e77d46f1ab Sema: Fix a trivial usage of FunctionType::getOld() in CSDiag
Trivial reading of the code shows that InputType is always a
function type, and never nullptr.
2018-08-27 21:15:23 -07:00
Slava Pestov
ba6d3850f5 Sema: Remove some apparently-dead code from CSDiag
I noticed this while working on some other changes.
2018-08-27 21:15:23 -07:00
Pavel Yaskevich
bba7112506 [Diagnostics] NFC: Refactor assignment diagnostics into AssignmentFailure
Merge logic from `diagnoseAssignmentFailure` and `diagnoseSubElementFailure`
into new `AssignmentFailure`, together with their support functions, which
decouples `CSDiagnostics` from `CSDiag` and scrubs latter from some functionality.
2018-08-27 00:35:44 -07:00
Greg Titus
c95cfc6f69 Merge pull request #18950 from gregomni/rvalue-as-lvalue
[Diagnostics] Adding assignments directly to CS and diagnosing from there.
2018-08-26 11:12:57 -07:00
Pavel Yaskevich
c69097f027 Merge pull request #18980 from xedin/move-salvage-to-cs
[ConstraintSystem] Move `salvage` and related logic away from `CSDiag`
2018-08-25 14:09:48 -07:00
Robert Widmann
661c30f93e Merge pull request #18963 from CodaFi/its-not-unusual-to-be-unused-by-anyone
[NFC] Silence a bunch of Wunused-variable diagnostics
2018-08-25 09:24:44 -07:00
Pavel Yaskevich
ea62075766 [ConstraintSystem] NFC: Move diagnoseAmbiguity methods from CSDiag to ConstraintSystem` 2018-08-24 23:18:49 -07:00
gregomni
821f63fe98 Make assignments and assignment failure diagnoses directly in the CS.
More specific diagnoses for assigning to read-only keypaths.
'computeAssignDestType' is dead code now.
ConstraintFix shouldRecordFix()
2018-08-24 20:39:03 -07:00
Pavel Yaskevich
4692e30c0a [ConstraintSystem] NFC: Move salvage from CSDiag to ConstraintSystem 2018-08-24 17:37:24 -07:00
Robert Widmann
014fd952ef [NFC] Silence a bunch of Wunused-variable diagnostics 2018-08-24 15:16:40 -07:00
Pavel Yaskevich
16fc3117cc [ConstraintSystem] Diagnose ambiguities related to solutions with fixes
If all of the solutions in the set have a single fix, which points
to the common anchor, attempt to diagnose the failure as an
ambiguity with a list of candidates and their related problems as notes.

Having richer message like that helps to understand why something is
ambiguous e.g. if there are two overloads, one requires conformance
to some protocol and another has a same-type requirement on some type,
but neither matched exactly, having both candidates in the diagnostic
message with associated errors, instead of simplify pointing to related
declarations, helps tremendously.
2018-08-24 11:20:49 -07:00
Greg Titus
e0a24ce93f Merge pull request #18827 from gregomni/rvalue-as-lvalue
[ConstraintSystem] Move more lvalue diagnostics over to being handled via ConstraintFix
2018-08-20 20:25:51 -07:00
gregomni
19fce5d36f Move another chunk of lvalue diagnostics over to being handled via ConstraintFix. 2018-08-19 13:14:55 -07:00
Greg Titus
32eacc5e80 Merge pull request #18608 from gregomni/rvalue-as-lvalue
[ConstraintSystem] New FailureDiagnostic for rvalues that should be lvalues
2018-08-18 11:43:23 -07:00
gregomni
aeb96274d2 Apply the solution to the CS before diagnosing solution fixes. 2018-08-18 08:38:16 -07:00
Slava Pestov
527ff375dc AST: Rename old form of {Generic,}FunctionType::get() to getOld()
This makes it easier to grep for and eventually remove the
remaining usages.

It also allows you to write FunctionType::get({}, ...) to call the
ArrayRef overload empty parameter list, instead of picking the Type
overload and calling it with an empty Type() value.

While I"m at it, in a few places instead of renaming just clean up
usages where it was completely mechanical to do so.
2018-08-17 19:28:17 -04:00
Jordan Rose
537954fb93 [AST] Rename several DeclContext methods to be clearer and shorter (#18798)
- getAsDeclOrDeclExtensionContext -> getAsDecl

This is basically the same as a dyn_cast, so it should use a 'getAs'
name like TypeBase does.

- getAsNominalTypeOrNominalTypeExtensionContext -> getSelfNominalTypeDecl
- getAsClassOrClassExtensionContext -> getSelfClassDecl
- getAsEnumOrEnumExtensionContext -> getSelfEnumDecl
- getAsStructOrStructExtensionContext -> getSelfStructDecl
- getAsProtocolOrProtocolExtensionContext -> getSelfProtocolDecl
- getAsTypeOrTypeExtensionContext -> getSelfTypeDecl (private)

These do /not/ return some form of 'this'; instead, they get the
extended types when 'this' is an extension. They started off life with
'is' names, which makes sense, but changed to this at some point.  The
names I went with match up with getSelfInterfaceType and
getSelfTypeInContext, even though strictly speaking they're closer to
what getDeclaredInterfaceType does. But it didn't seem right to claim
that an extension "declares" the ClassDecl here.

- getAsProtocolExtensionContext -> getExtendedProtocolDecl

Like the above, this didn't return the ExtensionDecl; it returned its
extended type.

This entire commit is a mechanical change: find-and-replace, followed
by manual reformatted but no code changes.
2018-08-17 14:05:24 -07:00
gregomni
9e0dae513c Start changing over rvalue-vs-lvalue errors to be done via constraint system fixes. For this first commit, just handling inout parameter problems. 2018-08-16 17:34:17 -07:00
gregomni
78fba7e1d0 Add optional back onto the toType rather than manually adding a '?' 2018-08-16 13:14:50 -07:00
gregomni
ca48ebf390 Improve type coercion fixits for optional-to-optional conversions. 2018-08-16 09:43:47 -07:00
gregomni
4d862d50cb In tryTypeVariableBindings, if T? fails, try T for all binding kinds (previously just allowed for Subtypes). This allows us to always find MissingOptionalUnwrapFailures, so that all the unwrap fixit code can be moved into CSDiagnostics and made static. 2018-08-14 09:14:59 -07:00
Greg Titus
8f41ee7750 Merge pull request #18324 from gregomni/opty2
[Sema] More unwrap fixits
2018-08-14 09:12:35 -07:00
Slava Pestov
d31d35a788 AST: Remove a few usages of TypeBase::getInOutObjectType() 2018-08-13 21:13:10 -07:00
gregomni
36cf24fb9e When we diagnose an unwrap, if the expression being unwrapped is a sole reference to a local let/var, and the local's type is inferred, offer fixits on the initializer as well. In this position we can also reasonably offer 'guard let'. Since we're identifying the implicitly typed initializer now, we can give a specific explanation of how IUOs get turned into optional types with the new IUO implementation.
Pass constraint system down into offering force unwrap fixits so that we can identify the type of the last member chosen for an optional chain. If there's a chain and the last member's return type isn't optional, then it's cleaner to offer to change that last '?' into a '!' to perform the force, rather than parenthesize the whole expression and force the result.
2018-08-12 12:57:20 -07:00
Slava Pestov
432924018c Sema: Remove TupleType::getVarArgsBaseType()
Or, actually, now that there's only one usage left move it to CSDiag.
Hopefully, both vararg tuple types and CSDiag are going away
eventually.
2018-08-11 22:28:36 -07:00
Slava Pestov
f2ae23a2ab AST: Remove TupleType::getElementForScalarInit()
This is a legacy holdover from when tuple types had default
arguments, and also the constraint solver's matching of function
types pre-SE-0110.

Well, move the last live usage to CSDiag, where it can die a slow
painful death over time. The other usages were not doing anything.
2018-08-11 22:26:44 -07:00
Pavel Yaskevich
538e5e89b3 [Diagnostics] Improve simplifyLocator to extract arguments from unresolved member calls
Unresolved member calls e.g. `.foo(1, 2)` are unique in a way that
they don't form regular application expressions. So let's teach
`simplifyLocator` how to extract arguments from such calls
based on locators like:
`[UnresolvedMemberExpr -> apply argument -> comparing call argument # to parameter #]`.

This helps to diagnose more failures via diagnostics attached to
constraint system fixes.
2018-08-10 16:31:13 -07:00
Slava Pestov
8d4b8e31aa Sema: Remove ScalarToTuple conversion
This either became dead shortly after the removal of Swift 3
compatibility mode from the constraint solver, or even earlier.

Note that the code completion test change is actually correct
because (Any) -> () is not convertible to () -> () in the
language.
2018-08-08 10:18:52 -07:00
Slava Pestov
3ef0991826 AST: Remove TypeBase::getWithoutImmediateLabel()
Again, it appears that looking through one-element tuples is
no longer a thing we need to do.
2018-08-08 10:18:52 -07:00
Slava Pestov
b581c63e6b AST: Remove TypeBase::getRValueObjectType()
This would look through one-element tuples, which is no longer necessary.
2018-08-08 10:18:52 -07:00
Slava Pestov
b67752a15f Minor NFC cleanups 2018-08-08 10:18:49 -07:00
Greg Titus
5e7da0e3dc Merge pull request #18329 from gregomni/7339
[Sema] Better key path failure diagnosis for unresolved roots
2018-08-01 09:01:44 -07:00
gregomni
d52d46d8e7 Needed to add one more nullptr check, on the KPE root type. 2018-08-01 07:04:02 -07:00
gregomni
d604047625 When root type of a keypath is unresolved by the constraint system, use the root interface type for diagnosis. 2018-07-31 17:15:12 -07:00
Doug Gregor
7316c3ea7a [Constraint solver] Diagnose "expression too complex" before general ambiguity.
When trying to salvage a failed expression type-check fails, diagnose
"expression too complex" before the general and uninformative "type of
expression is ambiguous without more context".
2018-07-30 20:56:53 -07:00
Doug Gregor
50eec8cb61 [Constraint solver] Check "expression too complex" more consistently.
Also, cache the result; once we've hit a too-complex expression, we
always have a too-complex expression in that constraint system.
2018-07-30 16:43:18 -07:00
Pavel Yaskevich
2ddeef7bcf [ConstraintSystem] Remove ExprCleaner and its uses 2018-07-28 20:28:41 -07:00
Pavel Yaskevich
0a404529c3 [Sema] Adjust Diagnostics/IDE to not assume that parameters always have types 2018-07-28 20:28:41 -07:00
Pavel Yaskevich
398abdfb7c [CSSolver] Add closure parameter type caching
While inferring avoid associating type variables with closure
parameters, use cache instead and only set types when everything
is properly type-checked, this avoids multiple problems one of
them - leaking type variables outside of constraint system they
belong to.
2018-07-28 20:28:41 -07:00
Greg Titus
a726deefc8 Merge pull request #18188 from gregomni/disambiguateOptionalPatternBinding
[Sema] Constrain result type of expressions in optional pattern bindings
2018-07-28 12:53:41 -07:00
Pavel Yaskevich
ae4e106491 Merge pull request #18281 from xedin/gather-constraints-improvements
[Perf Experiment][ConstraintGraph] Gather constraints improvements
2018-07-28 00:27:36 -07:00
Pavel Yaskevich
d9f0134add [Diagnostics] NFC: Clarify why we don't care about while diagnosing archetype errors 2018-07-27 19:57:07 -07:00
gregomni
775cca60b6 Constrain type checking of expressions in optional pattern bindings
so that they must result in an optional type.

Add constraint locator path for identifying constraints/variables that are part of the convert type passed into the system.
2018-07-27 18:05:21 -07:00
Pavel Yaskevich
624c183fe0 [ConstraintGraph] Change gatherConstraints to take SetVector
For stable iteration order, let's switch from `SmallPtrSet`
to `SetVector` which ensures insertion order iteration.
2018-07-27 15:34:15 -07:00