Commit Graph

12650 Commits

Author SHA1 Message Date
Slava Pestov
93b205e2b9 Sema: Simplify coerceParameterListToType() 2019-04-01 22:41:16 -04:00
Slava Pestov
20dc9a4c6a Sema: Fix circular validation between memberwise initializer and stored property
Fixes <rdar://problem/42704745>.
2019-04-01 22:41:16 -04:00
Slava Pestov
7566f98a45 Sema: Diagnose enum inheritance clause containing subclass existential
Also, tidy up the code a bit and stop emitting redundant diagnostics for
associated types.

Fixes <https://bugs.swift.org/browse/SR-10232>.
2019-04-01 22:41:16 -04:00
Slava Pestov
691802c65c Sema: Fix override matching of inout and vararg parameters
First, if the base parameter is inout or vararg, the derived parameter
must be too. Second, we do not allow covariant overrides of inout or
vararg parameters.

Fixes <https://bugs.swift.org/browse/SR-10231>.
2019-04-01 22:41:16 -04:00
Jordan Rose
883fc5b24a Check access control for the generic requirements of typealiases.
Also oops. This one was a little more involved because the requirements
on a generic typealias don't always carry a Type anymore; sometimes all
you have is the TypeRepr. That should still be okay in practice as long
as we don't start doing that for var/let, which can have part of a type
be inferred but not all of it.
2019-04-01 18:34:18 -07:00
Jordan Rose
b4e476e61f Check access control for the generic requirements of subscripts
Oops.
2019-04-01 18:28:33 -07:00
Brent Royal-Gordon
67af97c4db [NFC] Correct constraint system assumption
We currently assume that, if a subscript is declared within a value type’s decl, it must need `self` to be passed inout. This isn’t true for static subscripts, because even though the DeclContext is a value type, the metatype is actually a reference type. Skip this check for non-instance members.

NFC until static subscripts are added.
2019-04-01 18:04:00 -07:00
Brent Royal-Gordon
5c03918c82 [NFC] Remove VarDecl guards on staticness checks
Fixes various places where we assume that only a VarDecl can be static so they operate on any AbstractStorageDecl instead. NFC until static subscripts are added.
2019-04-01 18:04:00 -07:00
Brent Royal-Gordon
8bc1887473 [NFC] Override-check decls by property, not kind
Rather than figuring out which kind of decl we’re using and then testing the traits it has, check each trait once on the decls where the AST can express it.

This change is NFC currently, but will support static subscripts.
2019-04-01 18:04:00 -07:00
Anthony Latsis
13e805ebf8 Merge the «declared here» diagnostics for Identifier and DeclName (#23703) 2019-04-01 14:53:30 -07:00
Pavel Yaskevich
bc4d01620b [TypeChecker] Fix dynamic member declaration validation to check labels 2019-04-01 12:41:55 -07:00
Pavel Yaskevich
8f880545cd [TypeChecker] Add ReferenceWritableKeyPath support to keypath dynamic member lookup 2019-04-01 12:41:55 -07:00
Pavel Yaskevich
dd810a7b24 [Diagnostics] Improve mutability diagnostics related to dynamic member lookup
Since there is a way to mutate through use of writable keypath
diagnostics have to be adjusted to point to the members found
via keypath member lookup instead to using catch-all
"immutable base" diagnostic.
2019-04-01 12:41:55 -07:00
Pavel Yaskevich
41f6eb9a15 [ConstraintSystem] Don't introduce implicit keypath choice for keypath dynamic member lookup 2019-04-01 12:41:55 -07:00
Pavel Yaskevich
7032095b53 [TypeChecker] Tighten up validation of dynamic member decls
Make sure that base decl is valid and that parameter has
a valid signature.
2019-04-01 12:41:55 -07: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
589283b2c0 [ConstraintSystem] Teach sanitizer about dynamic member lookup expressions
Type-check based diagnostics could introduce keypath dynamic member
expressions into AST, which have to be converted back to their
original member or subscript reference form before attempting
re-typecheck.
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
2c82882b8f [ConstraintSystem] Move unviable keypath dynamic member check into performMemberLookup 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
84167edde9 [ConstraintSystem] Add a special locator for keypath dynamic member lookup
New locator is going to store pointer to the base keypath declaration.
This information is useful to determine whether such keypath is appropriate
to access underlying properties/subscripts e.g. `WritableKeyPath` cannot be
used to access read-only storage.
2019-04-01 12:40:39 -07:00
Pavel Yaskevich
fb30555952 [ConstraintSystem] NFC: Extract check whether storage for keypath is read-only 2019-04-01 12:40:39 -07:00
Pavel Yaskevich
7e076441c7 [CSRanking] Dynamic member look - rank keypath choice higher than string based one
Because keypath based choice provides more type information it
should be preferred to string based when both solutions are available.
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
cde23eccc8 [ConstraintSystem] Implement keypath dynamic member lookup
Implement keypath based dynamic member lookup which augments
functionality of existing @dynamicMemberLookup attribute.

Two new subscript overloads are accepted:

```
subscript(dynamicMember member: KeyPath<T, ...>) -> ...
subscript(dynamicmember member: WritableKeyPath<T, ...>) -> ...
```

Example:

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

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

  init(_ obj: T) {
    self.obj = obj
  }

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

  subscript<U>(dynamicMember member: WritableKeyPath<T, U>) -> Lens<U> {
    get { return Lens<U>(obj[keyPath: member]) }
    set { obj[keyPath: member] = newValue.obj }
  }
}

var lens = Lens(Point(x: 0, y: 0))

_ = lens.x // converted into `lens[dynamicMember: KeyPath<Point, Int>`
_ = lens.y = Lens(10) // converted into `lens[dynamicMember: WritableKeyPath<Point, Int>]`
```
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
Pavel Yaskevich
b3cb1a159b [TypeChecker] Adjust dynamic member lookup to support keypaths
Allow `subscript(dynamicMember: {Writeable}KeyPath<...>)`
declarations to be found while looking for dynamic members.
2019-04-01 12:40:39 -07:00
Saleem Abdulrasool
bebde5dd5d Sema: avoid a use-after-free in ResolvedMemberResult
The use of the reference to a private implementation caused a silent
use-after-free which would normally not trigger a problem as the use was
pretty close by.  The reference would copy the pointer and the
destructor for the implementation would free the backing memory.  We
would then continue to use the free'd memory to query the information.

The Windows heap allocator kindly scribbles over the memory which caused
an invalid memory access, helping isolate the use-after-free.
2019-03-31 13:09:42 -07:00
Slava Pestov
89758758f0 Merge pull request #23672 from slavapestov/kill-argument-shuffle-expr
Kill ArgumentShuffleExpr
2019-03-31 11:20:30 -04: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
Doug Gregor
a848d12665 Parse unknown attributes as "custom" attributes.
Parse custom attributes with the grammar:

```
'@' type-identifier expr-paren?
```
2019-03-29 23:10:36 -07:00
Jordan Rose
9b29b3bf38 Merge pull request #21313 from jrose-apple/secret-imports
Early implementation of "implementation-only imports"
2019-03-29 14:15:41 -07:00
Slava Pestov
a823e48375 Merge pull request #23650 from slavapestov/kill-scalar-prepared-arguments
SILGen: Kill "scalar" PreparedArguments
2019-03-29 14:33:56 -04:00
Pavel Yaskevich
da3468aeca Merge pull request #23652 from xedin/rdar-49371608
[ConstraintSystem] Delay adding contextual requirements until parent …
2019-03-29 01:37:42 -07:00
Pavel Yaskevich
8e420496b2 [ConstraintSystem] Delay adding contextual requirements until parent type is opened
`openUnboundGenericType` eagerly tries to add conditional requirements
associated with chain of parents of the given type if type has been
declared inside of constrained extension. But one of the parent types
might be unbound e.g. `A.B` which means it has to be opened, which
by itself, would add such requirements.

Resolves: rdar://problem/49371608
2019-03-28 22:08:33 -07: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
Jordan Rose
0ba6c495ba Add @_implementationOnly
This is an attribute that gets put on an import in library FooKit to
keep it from being a requirement to import FooKit. It's not checked at
all, meaning that in this form it is up to the author of FooKit to
make sure nothing in its API or ABI depends on the implementation-only
dependency. There's also no debugging support here (debugging FooKit
/should/ import the implementation-only dependency if it's present).

The goal is to get to a point where it /can/ be checked, i.e. FooKit
developers are prevented from writing code that would rely on FooKit's
implementation-only dependency being present when compiling clients of
FooKit. But right now it's not.

rdar://problem/48985979
2019-03-28 15:57:53 -07:00
Jordan Rose
9ed3fe061d Change ModuleDecl::getImportedModules to take an option set
...in preparation for me adding a third kind of import, making the
existing "All" kind a problem. NFC, except that I did rewrite the
ClangModuleUnit implementation of getImportedModules to be simpler!
2019-03-28 14:44:41 -07: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
Slava Pestov
181e5d5ea8 Merge pull request #23591 from slavapestov/tuple-conversions
AST/Sema/SILGen: Implement tuple conversions
2019-03-28 00:40:44 -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
175acc65b0 Merge pull request #23485 from johnno1962/Self-fallback-followup
Exclude properties of class with type Self
2019-03-27 18:19:48 -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
Slava Pestov
fc95dcd0a5 Merge pull request #23320 from slavapestov/objc-resilient-class-stubs
Implement Swift support for Objective-C resilient class stubs
2019-03-27 08:24:15 -04:00