Commit Graph

12650 Commits

Author SHA1 Message Date
Pavel Yaskevich
1516a3d7ba [ConstraintSystem] Type of key path expression should be a known KeyPath type
Currently `getPotentialBindingsForRelationalConstraint` doesn't
respect the fact that type of key path expression has to be a
form of `KeyPath`, instead it could eagerly try to bind it to
`Any` or other contextual type if it's only available
information.

This patch aims to fix this situation by filtering potential
bindings available for type variable representing type of
the key path expression.

Resolves: SR-10467
2019-04-24 13:44:45 -07:00
swift-ci
63d64fb2e0 Merge pull request #24225 from nathawes/iboutlet-private-set-in-module-interface 2019-04-24 12:39:31 -07:00
Nate Chandler
843ac0b4da Removed old comment. 2019-04-24 10:09:19 -07:00
Nate Chandler
b2ad56223f Eliminated conversion from uninhabited. 2019-04-24 10:09:17 -07:00
Pavel Yaskevich
ca6cf0c022 [ConstraintSystem] Use special locator for expr representing return of single expr function 2019-04-24 10:06:44 -07:00
Pavel Yaskevich
448e04ee52 [ConstraintLocator] Add a special locator element to denote contextual type of single expr function 2019-04-24 10:05:38 -07:00
Nate Chandler
0143ce25c2 Implicitly convert single exprs from uninhabited.
When type-checking a return statement's result, pass a new
ContextualTypePurpose when that return statement appears in a function
with a single expression.  When solving the corresponding constraint
system, the conversion constraint will have a new ConstraintKind.  When
matching types, check whether the constraint kind is this new kind,
meaning that the constraint is between a function's single expression
and the function's result.  If it is, allow a conversion from
an uninhabited type (the expression's type) to anything (the function's
result type) by adding an uninhabited upcast restriction to the vector
of conversions.  Finally, at coercion time, upon encountering this
restriction, call coerceUninhabited, replacing the original expression
with an UninhabitedUpcastExpr.  Finally, at SILGen time, treat this
UninhabitedUpcastExpr as a ForcedCheckedCastExpr.

Eliminates the bare ConstraintSystem usage from
typeCheckFunctionBodyUntil, ensuring that the same code path is followed
for all function bodies.
2019-04-24 10:04:17 -07:00
Nate Chandler
92998f7e2a Don't attempt to apply solution from salvage.
Match the behavior of typeCheckExpressionImpl which simply calls solve
and applySolution but never salvage.
2019-04-24 09:59:54 -07:00
Nate Chandler
6aa8bfd27f Corrected autoclosure handling.
Set autoclosure discriminators and follow visit single expression by way
of visiting brace stmt.
2019-04-24 09:59:54 -07:00
Nate Chandler
1bb7f29680 Added comments explaining approach. 2019-04-24 09:59:54 -07:00
Nate Chandler
5262e19b8e Avoided double typechecking when possible.
Optimized for the common case where a single expression in a function
body is an implicit return and attempt to solve the constraint system
featuring that assumption.  If that fails, attempt to solve a second
constraint system where no conversion type is provided.  If that
succeeds and the expression's type is uninhabited, then remove the
return and proceed.  If that fails, show the diagnostics from the
failure to solve the first constraint system.
2019-04-24 09:59:53 -07:00
Nate Chandler
72c66517b9 Only remove return for a unique viable solution. 2019-04-24 09:59:53 -07:00
Nate Chandler
aa10bfa323 Placed prechecked expressions back in body. 2019-04-24 09:59:53 -07:00
Nate Chandler
08bbce336f Addressed issues from PR review.
- preferred dyn_cast to is and get
- restored insertion of () on bare return statements
- used dyn_cast not dyn_cast_or_null when the argument is non-null
- used getSingleExpressionBody accessor during second AST modification
- eliminated erroneous access through null reference
2019-04-24 09:59:53 -07:00
Nate Chandler
2276e2a02e Just reuse old body when reverting return insertion. 2019-04-24 09:59:53 -07:00
Nate Chandler
018ba70698 Just revert return insertion if inappropriate.
Because the BodyStmts are revered to their original contents, they are
no longer implicit.
2019-04-24 09:59:53 -07:00
Nate Chandler
64d4e309ce Used a constraint system, not typeCheckExpression. 2019-04-24 09:59:53 -07:00
Nate Chandler
74b462b3ba WIP: Allow return to be omitted from single expression functions. 2019-04-24 09:59:53 -07:00
Arnold Schwaighofer
43a7b44577 Merge pull request #24201 from aschwaighofer/opaque_result_types_dyn_replacement_and_specialization
Opaque result types: dynamic replacement
2019-04-24 07:58:50 -07:00
Doug Gregor
6218673ead [Property delegates] Use $$foo for the backing storage and make it private.
When the property delegate type overrides the delegate value by providing
a delegateValue property, name the backing storage $$foo and make it private.
2019-04-23 21:56:03 -07:00
Nathan Hawes
370713989f [ModuleInterface] Allow non-mutable IBOutlet instance properties in module interfaces
If an IBOutlet property is public private(set), it's interface only has a
getter. Consuming this interface was triggering a diagnostic that IBOutlet
properties must be mutable. This patch bypasses this check for module
interfaces.

Resolves rdar://problem/49856177
2019-04-23 16:26:07 -07:00
Pavel Yaskevich
a42bf065df [ConstraintSystem] Add a special locator for type of the key path expression 2019-04-23 15:17:24 -07:00
Doug Gregor
faa176f3ef Record and walk the semantic initializer for a custom attribute. 2019-04-23 14:15:41 -07:00
Doug Gregor
56d450b595 [Property delegates] Contextualize direct initializers of custom attributes
When a custom attribute is given a direct initializer, save and re-use
the initializer context we create so that it can be associated with the
enclosing pattern binding. Fixes assertions involving explicit closures
in property delegates.
2019-04-23 11:32:29 -07:00
Doug Gregor
fcd2fd97e8 [Property delegates] Don't create backing var for ill-formed delegate type.
Fixes the crash-on-invalid in rdar://problem/49982937.
2019-04-23 11:32:29 -07:00
Doug Gregor
7df695536e [Property delegates] Fix printing of memberwise initializer default arguments 2019-04-23 11:32:28 -07:00
Doug Gregor
261b879b54 [Property delegates] Rename storageValue to delegateValue 2019-04-23 11:32:28 -07:00
Doug Gregor
2e9f8cf981 Capture a placeholder opaque value expression when needed. 2019-04-23 11:32:28 -07:00
Doug Gregor
f187d9218e [Type checker] Don't allow a 'let' to have a property delegate. 2019-04-23 11:32:28 -07:00
Doug Gregor
4f56db2653 [Property delegates] Implement support for storageValue 2019-04-23 11:32:28 -07:00
Doug Gregor
cc68b12d1a [SILGen] Initialization of instance properties with property delegates
The initialization of an instance property that has an attached
property delegate involves the initial value written on the property
declaration, the implicit memberwise initializer, and the default
arguments to the implicit memberwise initializer. Implement SILGen
support for each of these cases.

There is a small semantic change to the creation of the implicit
memberwise initializer due to SE-0242 (default arguments for the
memberwise initializer). Specifically, the memberwise initializer will
use the original property type for the parameter to memberwise
initializer when either of the following is true:

  - The corresponding property has an initial value specified with the
    `=` syntax, e.g., `@Lazy var i = 17`, or
  - The corresponding property has no initial value, but the property
    delegate type has an `init(initialValue:)`.

The specific case that changed is when a property has an initial value
specified as a direct initialization of the delegate *and* the
property delegate type has an `init(initialValue:)`, e.g.,

```swift
struct X {
  @Lazy(closure: { ... })
  var i: Int
}
```

Previously, this would have synthesized an initializer:

```swift
init(i: Int = ???) { ... }
```

However, there is no way for the initialization specified within the
declaration of i to be expressed via the default argument. Now, it
synthesizes an initializer:

```swift
init(i: Lazy<Int> = Lazy(closure: { ... }))
```
2019-04-23 11:31:59 -07:00
Doug Gregor
6526cfa8d4 Memberwise initializer synthesis for properties with attached delegates. 2019-04-23 11:31:58 -07:00
Doug Gregor
b18a2902e5 Implement access control for property delegates 2019-04-23 11:31:58 -07:00
Doug Gregor
b8061eab34 Synthesize backing storage property for properties with attached delegates.
When a property has an attached property delegate, a backing storage
property of the corresponding delegate type will be
synthesized. Perform this synthesis, and also synthesize the
getter/setter for the original property to reference the backing
storage property.
2019-04-23 11:31:58 -07:00
Doug Gregor
9c62420809 [AST] Generalize PatternBindingEntry's "Lazy" flag to "Subsumed"
The initializer associated with a lazy property should not be executed
directly, because it is subsumed by code synthesized into the
getter. Generalize the terminology here so we can re-use this path for
property delegate initialization.
2019-04-23 11:31:58 -07:00
Doug Gregor
53b9e25502 Implement initialization of properties with attached delegates.
A property with an attached delegate can be initialized in one of two ways:

* By directly specifying initialization arguments on the attribute,
  e.g., "@SomeDelegate(x: 17, y: 42) var a".
* By initializing the original property itself, which goes through the
  delegate type's init(initialValue:) initializer, e.g.,
  "@Lazy var x = 17".

Implement support for both forms of initialization, including type
inference and checking for inconsistencies (e.g., if the annotation on
the property type doesn't match what the delegate type would
provide).
2019-04-23 11:31:58 -07:00
Doug Gregor
96279c7044 Use custom attributes to apply property delegates to variables.
A custom attribute can be resolved to a property delegate type. Allow
this for property declarations (only), and diagnose the various places
where properties cannot have property delegates.
2019-04-23 11:31:58 -07:00
Doug Gregor
f9c716df87 Add support for the @propertyDelegate attribute.
Add the @propertyDelegate attribute, enforcing all of the semantic
requirements that are placed on property delegate types.
2019-04-23 11:31:58 -07:00
Slava Pestov
e303ceae91 Merge pull request #24213 from slavapestov/diagnose-cycles
Always diagnose request evaluator cycles
2019-04-23 06:50:42 -04:00
Pavel Yaskevich
afc9e20d04 Merge pull request #24208 from xedin/diagnose-method-ref-in-keypath
[ConstraintSystem] Detect and fix use of method references in key path
2019-04-23 02:07:26 -07:00
Slava Pestov
48fa4d1319 Sema: Fix request cycle involving fixDeclarationObjCName()
NFC for now, but this problem causes some tests to fail once we
enable cycle diagnostics.
2019-04-22 21:51:05 -04:00
Slava Pestov
949fe16da5 AST: Move a bunch of random @objc diagnostics to Sema 2019-04-22 20:42:08 -04:00
Slava Pestov
0fae7e8669 AST: Replace ASTContext's ObjCUnsatisfiedOptReqs list with a per-SourceFile list 2019-04-22 20:42:08 -04:00
Brent Royal-Gordon
814d50c7e6 Merge pull request #24163 from brentdax/line-line-line-line
Add -debug-constraints-on-line flag
2019-04-22 16:54:00 -07:00
Pavel Yaskevich
6fdb5482d4 [Diagnostics] Diagnose invalid method references in key path 2019-04-22 16:40:14 -07:00
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
Brent Royal-Gordon
79cee8daad Add -debug-constraints-on-line flag 2019-04-22 13:59:50 -07:00
Arnold Schwaighofer
1fbf5f1cc3 Add AllowCompatibleOpaqueTypeArchetypes as a Type.match flag 2019-04-22 13:37:46 -07:00
Rintaro Ishizaki
502a7bf3d7 Merge pull request #24180 from rintaro/ide-completion-static-subscript-rdar49131687
[CodeCompletion] Completion support for static subscript
2019-04-22 09:59:07 -07:00
Arnold Schwaighofer
a0387eea40 Opaque result types: Dynamic replacement fixes for computed properties 2019-04-22 08:32:43 -07:00