Commit Graph

1156 Commits

Author SHA1 Message Date
Pavel Yaskevich
28a66a23eb [CSSimplify] Delay binding metatype instance types if one side is type variable
While matching two metatypes where one side is a class (which could have
supertypes) and another is a type variable use `subtype` constraint to
delay binding types together because there is a real possibility that
there exists a superclass associated with yet free type variable which
would be a better match when attempted.

Resolves: rdar://problem/44816848
2018-11-27 14:36:46 -08:00
Pavel Yaskevich
cc780e3292 [ConstraintSystem] Make sure that @autoclosure argument detection works for subscripts/members
Currently logic in `matchCallArguments` could only detect argument
being an @autoclosure parameter for normal calls and operators.

This patch extends it to support subscripts and unresolved member calls.
2018-11-21 12:17:25 -08:00
Pavel Yaskevich
f9dae942f8 [CSFix] Add fix to track invalid @autoclosure forwarding 2018-11-21 12:17:25 -08:00
Mark Lacey
018498fb2f Merge pull request #16942 from bjhomer/bjhomer/optional-try-flattening
Flatten nested optionals resulting from try? and optional sub-expressions
2018-11-16 11:14:05 -08:00
Mark Lacey
a5d627844a Merge pull request #20618 from rudkx/favor-equality
[ConstraintSystem] When we have multiple conversions/fixes, make equa…
2018-11-16 06:27:42 -08:00
Mark Lacey
33b6e43efa [ConstraintSystem] When we have multiple conversions/fixes, make equality favored.
This allows us to skip attempting actual conversions.

This speeds up one of our slow test cases, and perturbs the output of
another test. In the latter case, we stop emitting conversions as part
of the non-semantic piece of the array_expr. The fact that we're not
putting conversions in on that path is something I've seen before in
other instances. I'll open a bug if I cannot find one for it, although
I believe it's entirely cosmetic in this case since we don't rely on
the conversion being there.
2018-11-15 17:38:22 -08:00
Dan Zheng
0d2787fb31 [NFC] Remove extraneous comment. (#20567) 2018-11-14 08:41:04 -05:00
Dan Zheng
eebccb7a09 Fix @dynamicMemberLookup for protocol/archetype types. (#20516)
- Enable `subscript(dynamicMember:)` as a requirement for
  `@dynamicMemberLookup` protocols.
- Add tests.
- Minor `@dynamicCallable`-related gardening.

Resolves SR-8077.
2018-11-13 18:13:33 -05:00
Dan Zheng
2863b6cc64 Gardening for @dynamicMemberLookup. (#20498)
* Gardening for `@dynamicMemberLookup`.

- Unify code style of `@dynamicMemberLookup` and `@dynamicCallable` implementations.
  - Use consistent variable names, diagnostic messages, doc comments, etc.
- Move `@dynamicMemberLookup` test to `test/attr` directory.
2018-11-11 21:54:19 -05:00
Pavel Yaskevich
ed84f18686 [AST] NFC: Remove a FIXME related to ParamDecl::IsAutoClosure field 2018-11-10 11:59:29 -08:00
Pavel Yaskevich
6224d12fe6 [ConstraintSystem] Remove now redundant @autoclosure checking
There is one place which handles `@autoclosure` attribute
associated with parameters - `matchCallArguments`.
2018-11-10 11:59:28 -08:00
Pavel Yaskevich
f462521078 [ConstraintSystem] Refactor arg/param matching to handle autoclosures
Make sure that presence of `@autoclosure` attribute handled
in one place - `matchCallArguments`, which makes it possible
to remove the rest of (now redundant) autoclosure related
logic scattered throughout solver.
2018-11-10 11:59:28 -08: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
BJ Homer
1789d44d6c Don't consider fixups that won't do anything.
Since 'try?' no longer unconditionally adds a layer of optional, converting it
to 'try!' will no longer unconditionally remove a layer of optional. So let's not
suggest it. This leads to better diagnostics anyway.
2018-11-06 23:31:02 -07:00
Greg Titus
d20fdf5f82 Merge pull request #19920 from gregomni/8757
[Sema][QoI] Call out missing conformances in protocol witness candidates.
2018-10-22 16:39:51 -07:00
gregomni
939de4fb4a Extend candidate missing conformance checking to other types of requirements so that we check superclass and same type requirements in the same way. 2018-10-19 10:02:30 -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
Pavel Yaskevich
effd86f1ad [ConstraintSystem] Remove rudimental TMF_ApplyingOperatorParameter flag
It was useful when logic related to `BridgingConstraint` was part of
`Conversion` constraint, which could be generated as a result of implicit
conversion for an operator parameter.
2018-09-27 09:54:20 -07:00
Slava Pestov
8c94b3818f Merge pull request #19560 from slavapestov/remove-functype-getinput
Remove FunctionType::getInput()
2018-09-26 19:39:03 -07:00
Pavel Yaskevich
c25515b6a8 [CSDiagnostics] Don't try to fix conformances for Void and Never
There are cases when adding missing conformance fix doesn't really
make sense like in case of `Void` and `Never` types because that
wouldn't result in a useful diagnostic.

This is a follow-up for rdar://problem/44770297
2018-09-26 11:39:00 -07:00
Slava Pestov
3b60ae153d AST: Rename AnyFunctionType::Param::getType() to getOldType() 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
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
Saleem Abdulrasool
d281b98220 litter the tree with llvm_unreachable
This silences the instances of the warning from Visual Studio about not all
codepaths returning a value.  This makes the output more readable and less
likely to lose useful warnings.  NFC.
2018-09-13 15:26:14 -07:00
Slava Pestov
35ddd5a8d6 Sema: Remove TypeChecker::getSuperClassOf() 2018-09-10 12:30:47 -07:00
Slava Pestov
83cc9755ed Sema: Refactor matchFunctionTypes() to walk parameter lists directly
This lets us remove a variety of ParenType-preserving hacks.
2018-09-10 12:30:47 -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
Slava Pestov
83c32da93c Sema: Refactor constraints::matchCallArguments() to take parameters and not input tuples 2018-08-28 22:36:02 -07:00
Slava Pestov
fb77bb4eb7 Sema: Refactor 'single Any parameter' hack a bit
It should be a no-op to remove it entirely but it has an effect
on ranking I don't understand. Refactor it in preparation for
changing matchCallArguments() to work on parameter lists instead
of types.
2018-08-28 22:36:02 -07:00
Slava Pestov
e0303f05ac Sema: Check for type variable first in matchExistentialTypes()
Even if the RHS is 'Any', we don't want to solve the constraint if
'LHS' is a type variable, because the type variable might be bound
to a noescape function type later.

Noticed by inspection and I don't have a test case; it may well
be NFC because of how CSBindings works.
2018-08-28 22:36:02 -07:00
Slava Pestov
2975f145a1 Sema: Remove ArgumentTupleConversion constraint now that its no longer used 2018-08-28 14:40:56 -07:00
Slava Pestov
383e93b428 Sema: Call matchCallArguments() directly instead of matchTypes()
Now that function types cannot have a naked type variable as
their input type it's no longer possible to have an unsolved
ArgumentTupleConversion constraint, so we can bypass most of
the logic in matchTypes() and call matchCallArguments() instead.
2018-08-28 14:40:56 -07:00
Slava Pestov
a002aae495 Sema: Remove matchFunctionParamTypes()
Now that function types cannot have a naked type variable as
their input type we should never end up down this code path
with an associated declaration and argument labels, so it's
OK to just call matchTypes() on the input types instead.
2018-08-28 14:40:56 -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
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
65edce7591 Sema: Add FunctionInput and FunctionResult constraints
These are temporary.

FunctionInput is conditional on fixing some ranking behavior to not
depend on type variables having argument tuples bound to them.
Hopefully we can replace TVO_PreferSubtypeBinding with a better
mechanism that compares overload types instead.

FunctionResult is used in CSDiag's contextual type diagnostics.
This is also on the chopping block.
2018-08-28 14:37:57 -07:00
gregomni
6d5a69c89b Remove excess namespacing 2018-08-25 12:46:09 -07:00
gregomni
31ca8db0f4 Move all shouldRecordFix logic back into CS itself keying off of FixKind. 2018-08-25 11:40:28 -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
Slava Pestov
4ed51fbf48 Sema: Add a FIXME 2018-08-24 16:54:45 -07:00
Slava Pestov
1c5452df6f Sema: Change a few AnyFunctionType => FunctionType
There can't actually be GenericFunctionTypes in the constraint system itself.
2018-08-24 16:53:41 -07:00
Pavel Yaskevich
fa45b3b675 [Diagnostics] NFC: ConstraintFix::{print, dump} no longer need SourceManager passed-in
Since `ConstraintFix` references `ConstraintSystem` directly now,
we can get `SourceManager` from `ASTContext` associated with that
`ConstraintSystem` instead of passing it in every time.
2018-08-22 00:15:24 -07:00
Pavel Yaskevich
811e97913e Merge pull request #18857 from xedin/rdar-43525641
[CSSimplify] Fix `matchCallArguments` not to claim un-labeled argumen…
2018-08-21 09:18:03 -07:00
Pavel Yaskevich
621204b7c6 Merge pull request #18870 from xedin/add-superclass-req-failure-diagnostic
[Diagnostics] Add `superclass` requirement fix/diagnostic
2018-08-21 09:16:41 -07:00
Pavel Yaskevich
16dfa6be72 [Diagnostics] Add superclass requirement fix/diagnostic
Extend new requirement failure diagnostics by adding "superclass"
generic requirement failures.
2018-08-21 00:39:21 -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