Commit Graph

1177 Commits

Author SHA1 Message Date
Pavel Yaskevich
6791ef3624 Merge pull request #21756 from xedin/rdar-36989788
[Diagnostics] Diagnose missing members via fixes
2019-01-10 10:36:17 -08:00
Slava Pestov
e276b12d9a Sema: Remove unreachable code from CSDiag 2019-01-10 08:44:18 -05:00
Pavel Yaskevich
74a8ee177e [Diagnostics] Diagnose missing members via fixes
Try to fix constraint system in a way where member
reference is going to be defined in terms of its use,
which makes it seem like parameters match arguments
exactly. Such helps to produce solutions and diagnose
failures related to missing members precisely.

These changes would be further extended to diagnose use
of unavailable members and other structural member failures.

Resolves: rdar://problem/34583132
Resolves: rdar://problem/36989788
Resolved: rdar://problem/39586166
Resolves: rdar://problem/40537782
Resolves: rdar://problem/46211109
2019-01-09 17:29:49 -08:00
Pavel Yaskevich
06a7ad63f8 [Diagnostics] Diagnose subscript operator misuse via fixes
Fix to use subscript operator instead of spelled out name helps
to produce a solution, that makes it much easier to diagnose
problems precisely and provide proper fix-its, it also helps to
diagnose ambiguous cases, and stacks up nicely with other errors.
2019-01-08 12:06:40 -08:00
Pavel Yaskevich
35202ab5b0 [TypeChecker] Always emit a fallback error if type-check failed without producing one
Sometimes constraint solver fails without producing any diagnostics,
it could happen during different phases e.g. pre-check, constraint
generation, or even while attempting to apply solution. Such behavior
leads to crashes down the line in AST Verifier or SILGen which are
hard to diagnose.

Let's guard against that by tracking if solver produced any diagnostics
upon its failure and if no errors were or are scheduled to be produced,
let's produce a fallback fatal error pointing at affected expression.

Resolves: rdar://problem/38885760
2019-01-07 10:42:00 -08:00
Pavel Yaskevich
09cb803e79 [CSDiag] Always attempt to erase open existentials after re-typecheck
All of the open existentials should be removed, along with their
opaque value expressions, after sub-expression type-check. Because
diagnostics might pick next sub-expression from constraint and its
anchor could point to sub-expression which has only opaque value
without enclosing open existential, which is going to trip up sanitizer.

Resolves: rdar://problem/46544601
2018-12-21 00:34:27 -08:00
Slava Pestov
2f61230627 Sema: Remove vestigial rvalue varargs code path 2018-12-16 01:02:56 -05:00
Pavel Yaskevich
9ecc0abb29 [CSDiag] FailureDiagnosis needs to account for the fact that expressions are sanitized
After calling `get{Possible}Type{s}WithoutApplying` types have to be
re-cached afterwards because sanitizer (run as part of the constraint
generator) can mutate AST to e.g. re-introduce member references.

Resolves: rdar://problem/46497155
2018-12-13 20:31:50 -08:00
Joe Groff
20a2f3ea9f Merge pull request #21244 from jckarter/archetype-subclasses
Split subclasses out of ArchetypeType.
2018-12-12 11:49:48 -08:00
Joe Groff
f1648a1b3e Split subclasses out of ArchetypeType.
Context archetypes and opened existential archetypes differ in a number of details, and this simplifies the overlapping storage of the kind-specific fields. This should be NFC; for now, this doesn't change the interface of ArchetypeType, but should allow some refinements of how the special handling of certain archetypes are handled.
2018-12-12 08:55:56 -08:00
Pavel Yaskevich
9818b55266 [CSDiagnostics] Add MissingCall failure
It's used to diagnose cases when function/method are used as
a property instead of being called.
2018-12-11 18:44:41 -08:00
Pavel Yaskevich
138105bd98 [CSDiag] Verify that @autoclosure param has function type before casting
When parameter refers to something undefined its type is going to
be `ErrorType`, so diagnostic path needs to make sure that parameter
has a valid function type before trying to extract result from it.

Resolves: rdar://problem/46377919
2018-12-03 18:20:41 -08:00
Michael Gottesman
e379ab9beb Merge pull request #20557 from brentdax/this-is-why-swift-is-a-memory-safe-language
Fix lookupDirect() use-after-scope bugs
2018-11-14 00:01:10 -08:00
Brent Royal-Gordon
270ad33a72 Fix lookupDirect() use-after-scope bugs
In #7530, NominalTypeDecl::lookupDirect() started returning TinyPtrVector instead of ArrayRef so that it wouldn’t be returning a pointer into a mutable data structure. Unfortunately, some callees assigned its return value into an ArrayRef; C++ happily converted the TinyPtrVector to an ArrayRef and then treated the TinyPtrVector as out-of-scope, so the ArrayRef would now point to an out-of-scope object. Oops.
2018-11-13 16:31:59 -08:00
Pavel Yaskevich
c1dba165a9 [CSDiag] Diagnose KeyPath tuple access as unsupported even when sugared
Follow-up for [SR-9213](https://bugs.swift.org/browse/SR-9213)
2018-11-12 12:57:22 -08:00
Pavel Yaskevich
2ad754d470 Merge pull request #20152 from xedin/add-autoclosure-flag-to-param-decl
[AST/Sema] Associate `@autoclosure` flag with parameter declaration
2018-11-10 14:53:31 -08:00
Suyash Srijan
21f97a55b4 Uses isa instead of is for type check 2018-11-10 20:10:43 +00:00
Pavel Yaskevich
6b62ac83e1 [CSDiag] Switch expression diagnostics to check @autoclosure from parameter flags
There is only one place where we need to do that - `typeCheckArgumentChildIndependently`
2018-11-10 11:59:28 -08:00
Suyash Srijan
7d32a749d6 Access kind only if currentType is not null and simplify check for tuple 2018-11-10 18:46:16 +00:00
Suyash Srijan
15ebc07421 Fixes incorrect diagnostic when using keypath with tuples 2018-11-10 17:19:09 +00:00
Dan Zheng
2a4e1b83fd Implement @dynamicCallable. (#20305)
* Implement dynamically callable types (`@dynamicCallable`).

- Implement dynamically callable types as proposed in SE-0216.
  - Dynamic calls are resolved based on call-site syntax.
  - Use the `withArguments:` method if it's defined and there are no
    keyword arguments.
  - Otherwise, use the `withKeywordArguments:` method.
- Support multiple `dynamicallyCall` methods.
  - This enables two scenarios:
    - Overloaded `dynamicallyCall` methods on a single
      `@dynamicCallable` type.
    - Multiple `dynamicallyCall` methods from a `@dynamicCallable`
      superclass or from `@dynamicCallable` protocols.
  - Add `DynamicCallableApplicableFunction` constraint. This, used with
    an overload set, is necessary to support multiple `dynamicallyCall`
    methods.
2018-11-09 09:49:14 -08:00
Pavel Yaskevich
66a79301b4 [CSDiagnostics] Diagnose contextual closure result mismatches via fixes
Let's keep track of type mismatch between type deduced
for the body of the closure vs. what is requested
contextually, it makes it much easier to diagnose
problems like:

```swift
func foo(_: () -> Int) {}
foo { "hello" }
```

Because we can pin-point problematic area of the source
when the rest of the system is consistent.

Resolves: rdar://problem/40537960
2018-11-07 14:28:50 -08:00
Pavel Yaskevich
290bd0425c [CSDiag] Fix crash related to failures in contextual type requirements
If failed constraint mentions member declaration which is not
generic, it means that generic requirements came from context
and should not be diagnosed by `diagnoseUnresolvedDotExprTypeRequirementFailure`.

Resolved: rdar://problem/45511837
2018-10-24 18:20:15 -07:00
Pavel Yaskevich
c219af099e [Diagnostics] NFC: Rename OverloadCandidate::getUncurried{Function}Type -> OverloadCandidate::get{Function}Type 2018-10-19 14:56:06 -07:00
Pavel Yaskevich
a0085e1f29 [Diagnostics] NFC: Rename UncurriedCandidate -> OverloadCandidate 2018-10-19 13:37:05 -07:00
Pavel Yaskevich
e20723f6c8 [ConstraintSystem] Replace curry level with a boolean flag
Arbitrary currying is no longer allowed so level could be switched
to a boolean flag for methods like `computeDefaultMap` to identify
if they need to look through curried self type or not.
2018-10-18 17:50:05 -07:00
Pavel Yaskevich
0e7d761e8b [ConstraintSystem] Form argument constraints instead of using matchTypes directly
Forming constraints instead of using `matchTypes` while matching argument/parameter
types makes sure that `failedConstraint` points to failed argument conversion
instead of `applicable function` constraint, which is better for diagnostics.
2018-10-03 14:47:25 -07:00
Slava Pestov
2d4b25960d Sema: Type variables for opened generic parameters store the generic parameter type and not an archetype
There's no need to instantiate archetypes in the generic environment
of the declaration being opened.

A couple of diagnostics changed. They were already misleading, and the
new diagnostics, while different, are not any more misleading than
before.
2018-09-27 20:49:23 -07:00
Slava Pestov
3b60ae153d AST: Rename AnyFunctionType::Param::getType() to getOldType() 2018-09-26 11:05:23 -07:00
Slava Pestov
4975f3a15f AST: Remove FunctionType::getInput() 2018-09-26 11:05:23 -07:00
Slava Pestov
34fd5fa6c4 AST: Replace remaining uses of FunctionType::getInput() with getParams() 2018-09-26 11:05:23 -07:00
Greg Titus
a37c1f05e2 Merge pull request #19490 from gregomni/closure_typevars
[ConstraintSystem] [NFC] Clean up type variables for closure parameters.
2018-09-25 08:44:37 -07:00
gregomni
6deb401cc6 Previously we were sharing a single type variable for a param of a closure's function type and it's paramDecl's type inside the closure itself. With inout parameters this caused vardecls of inout type and some hacks to deal with them.
Instead, create two TVs and constrain them appropriately.
2018-09-23 17:41:18 -07:00
Michael Gottesman
c62f31f5dc Inject llvm::SmallBitVector into namespace swift;
I also eliminated all llvm:: before SmallBitVector in the code base.
2018-09-21 09:49:25 -07:00
Joe Groff
93b5de61e7 Implement the final approved syntax for SE-227 identity key paths.
`\.self` is the final chosen syntax. Implement support for this syntax, and remove the stopgap builtin and `WritableKeyPath._identity` property that were in place before.
2018-09-19 11:45:13 -07:00
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