Commit Graph

1945 Commits

Author SHA1 Message Date
Hamish Knight
c745c17c38 Revert naming changes to getCalleeLocator & getChoiceFor
In addition, add a document comment to `getCalleeLocator` to clarify
its semantics.
2019-09-09 22:08:52 +01:00
Hamish Knight
67ee821123 [CS] Allow getCalleeLocator to find key path component callees
In order to do this we need it to take a ConstraintLocator argument so
we can tell which component we want the callee for. To make it clear
that we're looking for a callee at the anchor, also rename the member
to getAnchormostCalleeLocator.
2019-09-09 14:14:35 +01:00
Slava Pestov
22cb6f1176 AST: Introduce ProtocolDecl::get{AssociatedType,ProtocolRequirement}() 2019-09-03 22:39:35 -04:00
Pavel Yaskevich
5210e9b7c2 Revert "[AST] Paren'd reference to an IUO function crashes the compiler in SILGen" 2019-09-02 11:02:01 -07:00
Suyash Srijan
532498bae5 [CSApply] Use the choiceLocator, not locator when checking if a disjunction choice exists
Obviously we won't have a disjunction choice for the locator, it's only possible if the locator is extended with ImplicitlyUnwrappedDisjunctionChoice path element
2019-08-28 23:57:35 +01:00
Suyash Srijan
a2daa11109 Merge branch 'master' into fix/SR-10492 2019-08-28 23:48:08 +01:00
Suyash Srijan
28d0c089bc [CSApply] shouldForceUnwrapResult() should return false when we don't have an disjunction choice
This will only be false if we have an IUO method call wrapped in parens (like '(s.foo)()'), so assert that the type is a function type as well
2019-08-28 23:24:32 +01:00
Dan Zheng
f44064cbbc [SE-0253] Introduce callables. (#24299)
Introduce callables: values of types that declare `func callAsFunction`
methods can be called like functions. The call syntax is shorthand for
applying `func callAsFunction` methods.

```swift
struct Adder {
  var base: Int
  func callAsFunction(_ x: Int) -> Int {
    return x + base
  }
}
var adder = Adder(base: 3)
adder(10) // desugars to `adder.callAsFunction(10)`
```

`func callAsFunction` argument labels are required at call sites.
Multiple `func callAsFunction` methods on a single type are supported.
`mutating func callAsFunction` is supported.

SR-11378 tracks improving `callAsFunction` diagnostics.
2019-08-26 23:56:36 -07:00
Slava Pestov
2dbeeb0d3f AST: Make SubstFlags::UseErrorType the default behavior
We've fixed a number of bugs recently where callers did not expect
to get a null Type out of subst(). This occurs particularly often
in SourceKit, where the input AST is often invalid and the types
resulting from substitution are mostly used for display.

Let's fix all these potential problems in one fell swoop by changing
subst() to always return a Type, possibly one containing ErrorTypes.

Only a couple of places depended on the old behavior, and they were
easy enough to change from checking for a null Type to checking if
the result responds with true to hasError().

Also while we're at it, simplify a few call sites of subst().
2019-08-22 01:07:50 -04:00
Pavel Yaskevich
98a54a1e73 Merge pull request #26297 from hamishknight/pass-by-value-by-name
[CS] Add specific accessors for path element info
2019-08-20 01:04:59 -07:00
Greg Titus
604852d82d Merge pull request #26712 from gregomni/sr-11234
[CSGen] Coerce key path index expr to handle upcast correctly.
2019-08-19 14:34:19 -07:00
Hamish Knight
6e45ef1347 [CS] Add locator find[First/Last] members
These members provide a convenient way of getting the first or last
occurrence of a given kind of element in a locator's path.
2019-08-19 11:58:50 +01:00
Hamish Knight
a0919f73b0 [CS] Use subclasses to expose locator element info
Instead of adding specific accessors directly to
ConstraintLocator::PathElement, add subclasses that expose these
accessors.
2019-08-19 11:58:49 +01:00
Hamish Knight
b5b41a5bbe [CS] Add specific accessors for path element info
This commit replaces the `getValue()` and `getValue2()` members on
`ConstraintLocator::PathElement` with specific accessors for each
expected path component kind. IMO this adds some clarity to the call
sites, especially for `getArgIdx()` and `getParamIdx()`.

In addition, this commit adds a private `getValue` member that can
access a value at a given index, which will make it easier to add a
third value in the future.
2019-08-19 11:58:49 +01:00
Greg Titus
cf047295d7 Coerce key path index expr to handle upcast correctly. 2019-08-18 18:44:34 -07:00
Doug Gregor
17ea39accd [Constraint solver] Simplify one-way constraints to Equal, not Bind.
One-way constraint expressions, which are the only things that
introduce one-way constraints at this point, want to look through
lvalue types to produce values. Rename OneWayBind to OneWayEqual, map
it down to an Equal constraint when it is simplified (to drop
lvalue-ness), and apply that coercion during constraint application.

Part of rdar://problem/50150793.
2019-08-16 14:13:21 -07:00
Pavel Yaskevich
b26272f191 Merge pull request #26606 from theblixguy/fix/SR-11016
[Typechecker] Fix fix-it location for missing try when called on optional protocol value
2019-08-16 12:18:53 -07:00
Suyash Srijan
5795f88ad4 [Sema] Change OpaqueValueExpr to take a SourceRange instead of just a single SourceLoc 2019-08-16 18:31:58 +01:00
Suyash Srijan
1024447055 Merge branch 'master' into fix/SR-11016 2019-08-16 16:03:08 +01:00
Slava Pestov
1c3ac86796 AST: Banish OptionalTypeKind to ClangImporter.h
The only place this was used in Decl.h was the failability kind of a
constructor.

I decided to replace this with a boolean isFailable() bit. Now that
we have isImplicitlyUnwrappedOptional(), it seems to make more sense
to not have ConstructorDecl represent redundant information which
might not be internally consistent.

Most callers of getFailability() actually only care if the result is
failable or not; the few callers that care about it being IUO can
check isImplicitlyUnwrappedOptional() as well.
2019-08-15 18:41:42 -04:00
Slava Pestov
19d283d9dc AST: Replace ImplicitlyUnwrappedOptionalAttr with Decl::{is,set}ImplicitlyUnwrappedOptional() 2019-08-15 18:41:41 -04:00
Doug Gregor
3c69f6a305 [Constraint solver] Introduce one-way constraints.
Introduce the notion of "one-way" binding constraints of the form

  $T0 one-way bind to $T1

which treats the type variables $T0 and $T1 as independent up until
the point where $T1 simplifies down to a concrete type, at which point
$T0 will be bound to that concrete type. $T0 won't be bound in any
other way, so type information ends up being propagated right-to-left,
only. This allows a constraint system to be broken up in more
components that are solved independently. Specifically, the connected
components algorithm now proceeds as follows:

1. Compute connected components, excluding one-way constraints from
consideration.
2. Compute a directed graph amongst the components using only the
one-way constraints, where an edge A -> B indicates that the type
variables in component A need to be solved before those in component
B.
3. Using the directed graph, compute the set of components that need
to be solved before a given component.

To utilize this, implement a new kind of solver step that handles the
propagation of partial solutions across one-way constraints. This
introduces a new kind of "split" within a connected component, where
we collect each combination of partial solutions for the input
components and (separately) try to solve the constraints in this
component. Any correct solution from any of these attempts will then
be recorded as a (partial) solution for this component.

For example, consider:

  let _: Int8 = b ? Builtin.one_way(int8Or16(17)) :
  Builtin.one_way(int8Or16(42\
))

where int8Or16 is overloaded with types `(Int8) -> Int8` and
`(Int16) -> Int16`. There are two one-way components (`int8Or16(17)`)
and (`int8Or16(42)`), each of which can produce a value of type `Int8`
or `Int16`. Those two components will be solved independently, and the
partial solutions for each will be fed into the component that
evaluates the ternary operator. There are four ways to attempt that
evaluation:

```
  [Int8, Int8]
  [Int8, Int16]
  [Int16, Int8]
  [Int16, Int16]

To test this, introduce a new expression builtin `Builtin.one_way(x)` that
introduces a one-way expression constraint binding the result of the
expression 'x'. The builtin is meant to be used for testing purposes,
and the one-way constraint expression itself can be synthesized by the
type checker to introduce one-way constraints later on.

Of these two, there are only two (partial) solutions that can work at
all, because the types in the ternary operator need a common
supertype:

  [Int8, Int8]
  [Int16, Int16]

Therefore, these are the partial solutions that will be considered the
results of the component containing the ternary expression. Note that
only one of them meets the final constraint (convertibility to
`Int8`), so the expression is well-formed.

Part of rdar://problem/50150793.
2019-08-13 11:48:42 -07:00
Suyash Srijan
b4eb28fb73 [CS] Store the base expression's start location as the loc for the OpaqueValueExpr in openExistentialReference() 2019-08-12 20:02:25 +01:00
Slava Pestov
9dd835c71b Sema: Replace the finalizeDecl() mechanism with ClassDecl::getEmittedMembers()
The only thing remaining in finalizeDecl() is synthesis of
certain class members. Let's turn that into a request.
2019-08-09 19:08:47 -04:00
Greg Titus
db3b0d949c Merge pull request #26054 from gregomni/kp_closures
[ConstraintSystem][SE-0249] Key Path Expressions as Functions
2019-08-07 20:43:11 -07:00
Slava Pestov
a4bb3101ea Sema: Remove addExpectedOpaqueAccessorsToStorage() 2019-08-06 16:30:13 -04:00
gregomni
469e06ae2c Update to HEAD, fix Param type changes, merging. 2019-08-06 07:52:56 -07:00
Brent Royal-Gordon
66f914d20f Make keypath literal in closure only once 2019-08-06 07:52:56 -07:00
gregomni
5e98f3e8e5 Implicit conversion of KeyPath<Root,Value> to (Root) -> Value.
Fix autoclosure param interface type when it involves archetypes.

Had some repeated locals from moving this block of code around - cleaned up.

Alternate implementation where KeyPathExpr is essentially a literal type and can be either a KeyPath(R,V) or (R)->V.

Some unneccessary code now.

Implicit closure pieces need valid sourceLocs in case a coerce expr needs to refer to the beginning of the closure in `\.foo as (A) -> B`.
Removed explicit noescape from function type so `let a: (A) -> B = \.foo` is valid.
Remove optimization that optional path is always read-only KP type in CSGen, since it can also now be of function type.
2019-08-06 07:52:56 -07:00
Slava Pestov
0c5d52d860 AST: Introduce AbstractStorageDecl::get{Parsed,Opaque}Accessor()
Also, change visitOpaqueAccessors() to call getOpaqueAccessor() instead of
asserting if the expected accessor does not exist.
2019-08-02 19:34:43 -04:00
Slava Pestov
64c32c695b AST: Remove a few utility methods from AbstractStorageDecl
Since the return value of getAccessor() depends on mutable state, it
does not make sense in the request evaluator world. Let's begin by
removing some utility methods derived from getAccessor(), replacing
calls to them with calls to getAccessor().
2019-08-01 18:31:58 -04:00
Slava Pestov
37f0d6a613 Sema: Refactor away maybeAddAccessorsToStorage()
This centralizes the decision to add or not to add accessors in one place.
2019-07-31 14:54:57 -04:00
pschuh
3fdb42f463 Upstream bug fix related to keyword argument dynamic callable. (#26322) 2019-07-25 13:32:00 -07:00
Parker Schuh
2d97c3475b Delete TypeChecker::callWitness. 2019-07-16 10:41:04 -07:00
pschuh
26c4cccb4b Convert InterpolatedStringLiteralExpr to not use tc.callWitness(). (#26076)
For reference, all other callers of callWitness have been migrated.
2019-07-16 10:24:45 -07:00
Slava Pestov
5022d8be5d AST: Remove LiteralExpr::shallowClone()
At one point it was used to work around problems with re-type checking
literals in CSApply, but all of our tests appear to pass with this removed.
2019-07-12 19:07:46 -04:00
Slava Pestov
703c39d3fa Merge pull request #23845 from Azoy/sr-10313
[CSApply] Restructure the implicit AST when applying @dynamicCallable
2019-07-06 13:48:27 -04:00
Slava Pestov
4c499fd4ac AST: Stop passing around LazyResolvers in various places 2019-07-06 00:43:22 -04:00
Azoy
d0c727dd36 Restructure implicit AST for @dynamicCallable
add tests

dont assume dict type

dont assume member ref ast

formatting

more formatting

Update getTypeWitnessByName
2019-07-05 20:46:12 -04:00
Slava Pestov
c889ca5a14 Sema/SILGen: Move invalid @convention(c) conversion check to SILGen
This removes a bit of global state from the TypeChecker class, and
allows C function pointers to be formed to closures that capture
local functions, but have no transitive captures.
2019-07-02 22:03:30 -04:00
Slava Pestov
d475546eb8 Merge pull request #24511 from LucianoPAlmeida/attemp-fix-compiler-crash-28818
[TypeChecker] Attempt fix a compiler crasher: Expecting a type set
2019-06-27 23:32:16 -04:00
Slava Pestov
5826db5d56 AST: Add AbstractFunctionDecl::hasDynamicSelfResult() and use it
This calculates a result directly from the function's result type
instead of checking a bit that was previously set by the type
checker. Also, always returns true for constructors to simplify
some callers.
2019-06-26 01:10:12 -04:00
John Holdsworth
c1c1801448 CovariantReturnConversionExpr required. 2019-06-26 01:10:11 -04:00
John Holdsworth
bd4f49c1b5 Revert some tidy-ups. 2019-06-26 01:10:11 -04:00
John Holdsworth
f3e2282f65 Tidy-up 2019-06-26 01:10:11 -04:00
John Holdsworth
b3512298f1 Remove debug messages 2019-06-26 01:10:11 -04:00
John Holdsworth
498280d324 Seems to work 2019-06-26 01:10:11 -04:00
John Holdsworth
444027667c Some progress 2019-06-26 01:10:11 -04:00
John Holdsworth
97f102c74b Story so far 2019-06-26 01:10:11 -04:00
Pavel Yaskevich
4d6a18a0cf Merge pull request #25518 from xedin/rdar-51167632
[Diagnostics] Make sure that fixes associated with function builders …
2019-06-18 00:46:45 -07:00