Commit Graph

1659 Commits

Author SHA1 Message Date
Pavel Yaskevich
d6a3a31ae9 [ConstraintSystem] Detect and fix use of method references in key path
Referencing (instance or static) methods in the key path is not
currently allowed, solver should be responsible for early detection
and diagnosis of both standalone e.g. `\.foo` and chained
e.g. `\.foo.bar` (where foo is a method) references in key path
components.

```swift
struct S {
  func foo() -> Int { return 42 }
}

let _: KeyPath<S, Int> = \.foo
```

Resolves: rdar://problem/49413561
2019-04-22 14:56:15 -07:00
Joe Groff
a8c2b50bd8 Merge pull request #22072 from jckarter/opaque-type-runtime
Opaque types with resilience
2019-04-18 14:52:31 -07:00
Brent Royal-Gordon
334b76a237 Merge pull request #24084 from brentdax/whos-that-key-path-component
Improve `-debug-constraints` output for key path components
2019-04-18 14:12:43 -07:00
Brent Royal-Gordon
44a472766d [ConstraintSystem] Add KeyPathComponentTypes map
Avoids digging through the long list of type vars to find the ones related to key path components when dumping an expression.
2019-04-17 21:15:16 -07:00
Joe Groff
f008019bda Sema: Infer the underlying type for opaque return types from function bodies. 2019-04-17 14:43:32 -07:00
Pavel Yaskevich
2e93165040 [ConstraintSystem] Detect use of declaration with mutating getter as a key path component
Such use is currently not allowed so variables or subscripts with
mutating getters should be rejected and diagnosed.
2019-04-16 17:56:13 -07:00
Robert Widmann
f192b92799 Merge pull request #23785 from Azoy/context-cleanups
AST NFC: Minor cleanups around ASTContext
2019-04-16 13:50:35 -04:00
Pavel Yaskevich
03ea5dce5a [ConstraintSystem] Detect and fix use of static members in a key path in the solver
Previously it was possible to create an invalid solution where
static members would be referenced in a key path, which is not
currently supported and would only be diagnosed while applying
such solution to AST e.g.

```swift
struct S {
  static var foo: Int = 42
}

_ = \S.Type.foo
```
2019-04-15 23:42:34 -07:00
Slava Pestov
7fe577fddb Sema: Clean up modeling of non-member VarDecl references
Give them substitutions just like with everything else, which
eliminates some special cases from SILGen.
2019-04-14 23:28:14 -04:00
Slava Pestov
a3c15f2f6b Sema: References to TypeDecls should always be TypeExpr
In a few corner cases we built DeclRefExpr and MemberRefExpr
for references to types. These should just be TypeExpr so that
SILGen doesn't have to deal with it.

This also fixes a bug where a protocol typealias with an
unbound generic type could not be accessed properly from
expression context, but that is just so incredibly obscure.
2019-04-14 23:28:13 -04:00
Suyash Srijan
072e84acd6 [CSSimplify] Reject key path if root type is AnyObject (#23820)
Detect situations where `AnyObject` is attempted to be used as a root type of the key path
early and diagnose via new diagnostics framework.
2019-04-14 12:18:35 -07:00
Brent Royal-Gordon
def1af6049 Merge pull request #23358 from brentdax/a-type-is-a-set-of-its-instances
Add static and class subscripts
2019-04-11 12:58:32 -07:00
Pavel Yaskevich
34960788aa Merge pull request #23741 from xedin/verify-kp-subscript-hashable-via-solver
[CSSolver] Move keypath subscript index Hashable conformance verification to solver
2019-04-11 00:48:26 -07:00
Brent Royal-Gordon
e42939d9bb Correctly apply typechecking solutions with subscripts on type instances 2019-04-10 23:09:44 -07:00
Pavel Yaskevich
439335ddb0 [Diagnostics] Extend keypath subscript index missing Hashable to support dynamic member lookup 2019-04-10 15:10:47 -07:00
Pavel Yaskevich
3288299e1e [ConstraintSystem] Make sure that each index argument of keypath subscript is Hashable
Move checking for index Hashable conformance from CSApply (post-factum)
to the solver and use recorded conformance records complete subscript
components.
2019-04-10 13:50:34 -07:00
Pavel Yaskevich
8e5dbcfde2 Merge pull request #23921 from xedin/dynamic-member-implicit-ast
[CSApply] Mark dynamic member lookup subscript expr and its argument …
2019-04-10 01:34:37 -07:00
Pavel Yaskevich
cf09a4e536 [CSApply] Mark dynamic member lookup subscript expr and its argument as implicit
These expressions are created to model an implicit call to dynamic
member lookup endpoint, which means both of them have to be implicit.
2019-04-09 19:12:10 -07:00
Slava Pestov
e214156abf Sema: Remove escaping capture diagnostics
I'm about to replace all of this with a SIL pass.
2019-04-09 15:02:14 -04:00
Sam Lazarus
ebdb3cf346 Sema: Re-allowed coercion for existentials that have already been opened 2019-04-06 15:54:50 -04:00
Sam Lazarus
8ee220a560 Merge branch 'master' into slazarus/allow-access-through-existential-metatype 2019-04-06 09:03:33 -04:00
Sam Lazarus
390c6357b1 Sema: Prevented an invalid coercion when handling member access in accesses through metatypes 2019-04-05 23:34:15 -04:00
Sam Lazarus
3a089ccc24 Merge branch 'master' into slazarus/allow-access-through-existential-metatype 2019-04-05 16:33:33 -04:00
Sam Lazarus
efc3e5ff48 Sema: Allow for same-type coercion between existential metatypes 2019-04-05 16:32:45 -04:00
Pavel Yaskevich
16b65018b4 Merge pull request #23436 from xedin/keypath-dynamic-lookup
[SE-0252][TypeChecker] Keypath Dynamic Member Lookup
2019-04-05 00:10:53 -07:00
Pavel Yaskevich
b388d83426 [ConstraintSystem] Make sure that keypath dynamic member lookup could be used recursively
For example if there a structure which requires multiple implicit
dynamic member calls to get the members:

```swift
struct Point { var x, y: Int }

@dynamicMemberLookup
struct Lens<T> {
   var obj: T

   subscript<U>(dynamicMember member: KeyPath<T, U>) -> U {
     get { return obj[keyPath: member] }
   }
}

func foo(_ lens: Lens<Lens<Point>>) {
  _ = lens.x
  _ = lens.obj.x
  _ = lens.obj.obj.x
}

_ = \Lens<Lens<Point>>.x
```
2019-04-03 19:20:04 -07:00
Azoy
7c75836d77 context cleanup part 1 2019-04-03 20:50:58 -05:00
Pavel Yaskevich
e3ad92fbfb [ConstraintSystem] Allow to use keypath dynamic member lookup inside keypath expressions 2019-04-03 11:01:48 -07:00
Slava Pestov
6bb36b5c01 Sema: Subscript default arguments
Fixes <https://bugs.swift.org/browse/SR-6118>.
2019-04-02 20:37:01 -04:00
Slava Pestov
93b205e2b9 Sema: Simplify coerceParameterListToType() 2019-04-01 22:41:16 -04:00
Pavel Yaskevich
675c5f4ec9 [TypeChecker] Add subscript + trailing closure tests for keypath dynamic member lookup 2019-04-01 12:41:55 -07:00
Pavel Yaskevich
99c27d22dc [CSApply] Avoid reusing expressions for keypath dynamic member lookup components
In presence of type-check based diagnostics we can't mutate existing
expressions, because diagnostics could be holding references to them
directly or to expressions which point to modified ones.
2019-04-01 12:41:55 -07:00
Pavel Yaskevich
d4bbcc1953 [TypeChecker] Add subscript support keypath dynamic member lookup 2019-04-01 12:40:39 -07:00
Pavel Yaskevich
4d225c785c [CSApply] Extract keypath subscript resolution logic 2019-04-01 12:40:39 -07:00
Pavel Yaskevich
32e0c807c6 [TypeChecker] Add more tests for keypath dynamic member lookup
Test inheritance, precedence, existentials, IUO, function type
returns and more.
2019-04-01 12:40:39 -07:00
Pavel Yaskevich
9eed0135fa [CSSolver] Don't generate implicit argument for keypath dynamic lookup
Implicit argument expression was necessary to generate keypath
constraint which is used to validate a choice picked for the member.
But since read-only check has been factored out it's now possible
to validate choice directly in combination with new 'keypath dynamic lookup'
locator associated with member type variable which represents result
of the dynamic lookup.
2019-04-01 12:40:39 -07:00
Pavel Yaskevich
3e7a7a2b7c [CSApply] Extract keypath property resolution logic 2019-04-01 12:40:39 -07:00
Pavel Yaskevich
71e8148bbd [CSApply] Implement AST transformation for keypath dynamic member lookup 2019-04-01 12:40:39 -07:00
Pavel Yaskevich
f95d9b092e [TypeChecker] Add new type of overload choice to support keypath dynamic lookup 2019-04-01 12:40:39 -07:00
Slava Pestov
1467f554f5 AST: Remove ArgumentShuffleExpr 2019-03-31 01:36:19 -04:00
Slava Pestov
e212d4567f Sema: Collect varargs into an ArrayExpr and use DefaultArgumentExpr
Instead of building ArgumentShuffleExprs, lets just build a TupleExpr,
with explicit representation of collected varargs and default
arguments.

This isn't quite as elegant as it should be, because when re-typechecking,
SanitizeExpr needs to restore the 'old' parameter list by stripping out
the nodes inserted by type checking. However that hackery is all isolated
in one place and will go away soon.

Note that there's a minor change the generated SIL. Caller default
arguments (#file, #line, etc) are no longer delayed and are instead
evaluated in their usual argument position. I don't believe this actually
results in an observable change in behavior, but if it turns out to be
a problem, we can pretty easily change it back to the old behavior with a
bit of extra work.
2019-03-31 01:36:19 -04:00
Slava Pestov
b9ef5708e2 Sema: Simplify representation of vararg forwarding
VarargExpansionExpr shows up in call argument lists in synthesized
initializers and modify accessors when we need to forward arguments
to a call taking varargs.

Previously we would say that the type of VarargExpansionExpr is
$T when its subexpression type is [$T]. matchCallArguments() would
then 'collect' the single VarargExpansionExpr into a variadic
argument list with a single element, and build an ArgumentShuffleExpr
for the argument list.

In turn, SILGen would peephole vararg emission of a variadic
argument list with a single entry that happens to be a
VarargExpansionExpr, by returning the subexpression's value,
which happened to be an array of the right element type,
instead of building a new array containing the elements of the
variadic argument list.

This was all too complicated. Instead, let's say that the type of
a VarargExpansionExpr is [$T], except that when it appears in a
TupleExpr, the variadic bit of the corresponding element is set.

Then, matchCallArguments() needs to support a case where both
the parameter and argument list have a matching vararg element.
In this case, instead of collecting multiple arguments into a
single variadic argument list, we treat the variadic argument like
an ordinary parameter, bypassing construction of the
ArgumentShuffleExpr altogether.

Finally, SILGen now needs to be able to emit a VarargExpansionExpr
in ordinary rvalue position, since it now appears as a child of a
TupleExpr; it can do this by simply emitting the sub-expression
to produce an array value.
2019-03-28 23:23:58 -04:00
Slava Pestov
7c7f60a9a4 Merge pull request #23618 from slavapestov/array-expr-lowering
Convert ArrayExpr to not use callWitness() or generate a SemanticExpr.
2019-03-28 11:01:29 -04:00
Parker Schuh
d0779bd771 Convert ArrayExpr to not use callWitness() or generate a SemanticExpr. 2019-03-27 23:21:08 -04:00
Slava Pestov
e2c9c52c93 AST/Sema/SILGen: Implement tuple conversions
TupleShuffleExpr could not express the full range of tuple conversions that
were accepted by the constraint solver; in particular, while it could re-order
elements or introduce and eliminate labels, it could not convert the tuple
element types to their supertypes.

This was the source of the annoying "cannot express tuple conversion"
diagnostic.

Replace TupleShuffleExpr with DestructureTupleExpr, which evaluates a
source expression of tuple type and binds its elements to OpaqueValueExprs.

The DestructureTupleExpr's result expression can then produce an arbitrary
value written in terms of these OpaqueValueExprs, as long as each
OpaqueValueExpr is used exactly once.

This is sufficient to express conversions such as (Int, Float) => (Int?, Any),
as well as the various cases that were already supported, such as
(x: Int, y: Float) => (y: Float, x: Int).

https://bugs.swift.org/browse/SR-2672, rdar://problem/12340004
2019-03-27 18:12:05 -04:00
Slava Pestov
ab81406711 Sema: Use coerceToRValue() to load tuples of lvalues instead of coerceTupleToTuple() 2019-03-27 17:41:40 -04:00
Ted Kremenek
fe215edb9b Merge pull request #19743 from Azoy/smarter-struct-init
[Sema] Synthesize default values for memberwise init
2019-03-25 17:31:01 -07:00
Joe Groff
797839f197 Merge pull request #23497 from theblixguy/fix/SR-10146
[CSApply] Fix a KeyPath crash in SIL when base type is AnyObject
2019-03-25 14:13:41 -07:00
Suyash Srijan
676d914e7c [csapply] do not proceed with a key path whose base type is AnyObject 2019-03-22 20:42:58 +00:00
Slava Pestov
428c709491 AST: Remove argument list-specific parts of TupleShuffleExpr
Before extending TupleShuffleExpr to represent all tuple
conversions allowed by the constraint solver, remove the
parts of TupleShuffleExpr that are no longer needed; this is
support for default arguments, varargs, and scalar-to-tuple and
tuple-to-scalar conversions.
2019-03-21 02:18:41 -04:00