Commit Graph

673 Commits

Author SHA1 Message Date
Hamish Knight
8aa7401ba5 [Sema] Add getDeclOrNull to OverloadChoice
This helps clean up a bunch of call sites.
2019-05-16 12:07:41 +01:00
Doug Gregor
165133e3dd Merge pull request #24799 from DougGregor/disfavored-overload-attr
Add @_disfavoredOverload attribute to affect overload resolution.
2019-05-15 16:03:28 -07:00
Doug Gregor
5aea1315cd Add @_disfavoredOverload attribute to affect overload resolution.
Introduce an attribute @_disfavoredOverload that can be used to state
that a particular declaration should be avoided if there is a
successful type-check for a non-@_disfavoredOverload. It's a way to
nudge overload resolution away from particular solutions.
2019-05-14 23:07:26 -07:00
Slava Pestov
e65161ea2e Sema: Build type checked bodies for lazy getters 2019-05-14 19:18:58 -04:00
Slava Pestov
37412c2de2 AST: Add ParamDecl::toFunctionParam() 2019-05-10 15:00:49 -04:00
Slava Pestov
1dedba1f12 Sema: Pass argument match kind down to matchCallArguments() 2019-05-07 23:10:48 -04:00
swift-ci
d24bc38797 Merge pull request #23701 from DougGregor/property-delegates-as-custom-attributes 2019-04-24 22:51:18 -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
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
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
Brent Royal-Gordon
760d4be5e0 [ConstraintSystem] Add a KeyPathComponentResult locator path element
We’ll use this instead of the function result, except for subscript components, which are already structured similarly to subscript expressions.
2019-04-17 21:15:16 -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
Brent Royal-Gordon
9cb896b828 [ConstraintSolver] Give type vars for keypath components useful locators
Previously, nearly all of the type vars for a key path component were dumped under the KeyPathExpr itself, whcih made them difficult to differentiate when debugging. Attach each one to “key path component #N -> function result” instead.

# Conflicts:
#	lib/Sema/CSGen.cpp
2019-04-17 14:34:45 -07:00
Slava Pestov
f3bb3d92f0 Sema: Don't allow closure return type to contain noescape types 2019-04-16 23:01:39 -04:00
Slava Pestov
eed84abda9 Sema: Add TVO_CanBindToNoEscape
We have a systemic class of issues where noescape types end up bound to
type variables in places that should not. The existing diagnostic for
this is ad-hoc and duplicated in several places but it doesn't actually
address the root cause of the problem.

For now, I've changed all call sites of createTypeVariable() to set the
new flag. I plan on removing enough occurrences of the flag to replicate
the old diagnostics. Then we can continue to refine this over time.
2019-04-16 23:01:33 -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
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
Anthony Latsis
13e805ebf8 Merge the «declared here» diagnostics for Identifier and DeclName (#23703) 2019-04-01 14:53:30 -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
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
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
Pavel Yaskevich
53a9b1f9b9 Merge pull request #23312 from xedin/form-complete-member-overload-sets
[ConstraintSystem] Include unviable-by-fixable choices into member overload sets
2019-03-18 13:43:52 -07:00
Pavel Yaskevich
852169a5f5 [ConstraintSystem] Split unviable lookup result storage into candidates & reasons
That makes it easy to process unviable choices the same way as viable
ones and request rejection reasons only when necessary.
2019-03-14 22:20:20 -07:00
Rintaro Ishizaki
b006c7c9b8 [AST] Don't return inapplicable decls in lookupVisibleDecls
rdar://problem/45340583 / https://bugs.swift.org/browse/SR-9027
rdar://problem/36594731
2019-03-14 12:57:37 -07:00
Doug Gregor
4c16107365 [Constraint solver] Unify overload favoring on getEffectiveOverloadType().
The overload-favoring code had a couple of different ways in which it
tried to figure out the parameter lists of a given declaration. Have
those all depend on ConstraintSystem::getEffectiveOverloadType(),
which deals with the various member/non-member cases uniformly.
2019-03-04 20:48:48 -08:00
Doug Gregor
daf4610be1 [Constraint solver] Don't remove/reintroduce disjunction when favoring.
When favoring particular constraints in a disjunction, don't remove
the old disjunction and create a new one---it's just churn in the
constraint system. Instead, favor the constraints that need it.

This technically flips the SR-139 test case from "too complex" to
being fast enough, but it's still fairly slow.
2019-03-04 20:31:06 -08:00
Doug Gregor
9086b2beef [Constraint solver] Flatten the disjunction created by call favoring.
The call-favoring code was creating a two-level disjunction, when would
then immediately get flattened into a single level. Instead, create a
single-level disjunction directly.
2019-03-04 16:21:24 -08:00
Doug Gregor
ae4949d27c [Constraint solver] Remove a hand-rolled clone of getUnboundBindOverloadDisjunction
Use the real one instead.
2019-03-04 15:59:59 -08:00
Doug Gregor
69ef7895a8 [Constraint solver] Match argument labels for unresolved member expressions. 2019-03-01 23:11:34 -08:00
Doug Gregor
603d5d6f20 [Constraint solver] Synchronize argument label setting/retrievable.
The walker that was setting argument labels was looking through ! and ?
postfix expressions, but the lookup code itself was not, leading to missed
opportunities for filtering based on argument labels. Generalize the
transformation that maps from the function expression down to the locator
for which we will retrieve argument labels.
2019-03-01 21:45:39 -08:00
Doug Gregor
c6631ec83d [Constraint solver] Match call arguments for subscripts.
Record the argument labels provided to a subscript in the solver, and
use that to filter out subscript declarations with non-matching
argument labels during function application. This reduces the number of
overloaded subscript declarations that will be considered in later
steps in the solver, reducing the solution space.

*Disable* this optimization in the normal member-lookup path, which is
intended to go away in the near future. This limits the scope of the
change somewhat, so we can separately tackle the diagnostics issue.
The one diagnostics change here is probably an improvement, because
the user explicitly stated the argument labels, and is more likely
missing a conversion on the argument than having typed the wrong
label.
2019-03-01 09:18:53 -08:00
Doug Gregor
202d52135d [Constraint system] Generalize effective-overload-type computation.
Extend the computation of effective overload types, used in common result
type and common type computations, to also handle subscripts and variables.
This allows the optimization to also apply to subscripts, which did not
previously have a peephole in constraint generation.
2019-02-27 23:30:03 -08:00
Doug Gregor
5f99d91ea8 [Constraint solver] Compute common apply result type in the solver.
Constraint generation for function application expressions contains a simple
hack to try to find the common result type for an overload set containing
callable things. Instead, perform this “common result type” computation
when simplifying an applicable function constraint, so it is more
widely applicable.
2019-02-27 23:30:03 -08:00
Doug Gregor
86d14a8be8 Merge pull request #22858 from DougGregor/constraint-system-common-type-overloads
[Constraint system] Compute and use a common type among overloads.
2019-02-26 15:39:18 -08:00
Joe Groff
bb67cf815c Merge pull request #21355 from technicated/tuple-keypaths-2
Tuple KeyPaths
2019-02-25 12:56:05 -08:00
Doug Gregor
90e5957787 [Constraint solver] Use type variable constraints to find literal types.
Rather than looking directly at the subexpressions of a function
application to determine whether they are literal expressions, look
instead at the type variables for the argument types: if they have a
literal-conforms-to constraint on them, use the named literal protocol
to determine favored declarations.

This refactoring is intended to broaden the applicability of the
existing "favored declaration" machinery to also consider the literal
constraints of other type variables that are equivalent to the type
variable named by the argument.
2019-02-23 22:50:17 -10:00
Doug Gregor
1ab417cd8e [Constraint system] Compute and use a common type among overloads.
Given an overload set, attempt to compute a "common type" that
abstracts over all entries in the overload set, providing more
structure for the constraint solver.
2019-02-22 21:53:34 -10:00
technicated
d7324b977e Added more tests
Testing SILGen & IRGen tuple keypath generation
Added tuple element type check in SILVerifier
2019-02-18 10:19:42 +01:00
Andrea Tomarelli
ede47cafbd Partial AST & Sema implementation of TKP 2019-02-18 09:04:42 +01:00
Azoy
e9f4217f03 remove lookupBoolType
diagnose broken bool

fix
2019-02-09 00:13:51 -06:00
Pavel Yaskevich
7f262a7003 [Sema] Extract @autoclosure diagnostics from type resolver
Since @autoclosure attribute is associated with declarations
it makes more sense to move diagnostics to where type of the
parameter has been completely resolved. This also helps to support
parameters with typealiases pointing to function types without
any extra logic in the resolver.
2019-02-05 10:57:05 -08:00
Pavel Yaskevich
7d830cee8b [ConstraintSystem] Add a fix to detect invalid partial application
Currently supported only partial applications of instance methods
marked as `mutating`.
2019-01-25 13:29:11 -08:00
Slava Pestov
e99607c421 Sema: Use ConstraintKind::Bind where possible instead of ::Equal
Solving Bind is a little easier than Equal. The only remaining uses of Equal
are in the .member syntax and keypaths; if we can refactor those, we might be
able to simplify LValue handling in the type checker in general.
2019-01-14 14:55:16 -05:00
Slava Pestov
33871548b3 Clean up TypeVariableType::Implementation::mustBeMaterializable(), etc 2019-01-12 14:10:11 -05:00
Slava Pestov
bb34620bf8 Sema: Validate extension if needed in isExtensionApplied()
We're looking at the generic signature of the extension, so it must
have been validated at this point.

I don't have a test case for this and maybe it never came up, but
nothing forces it to be validated here so let's make it more robust
by doing it explicitly.
2019-01-08 22:58:12 -05:00
Pavel Yaskevich
91724c9918 [TypeChecker] Address review comments
* Incomplete conformance is not necessarily going to produce a diagnostic
* Use `userFacingName` to avoid assert related to special names.
2019-01-07 10:53:50 -08:00
Pavel Yaskevich
64fa0ee729 [TypeChecker] Add Builtin operation to trigger/test fallback diagnostic 2019-01-07 10:42:00 -08:00
Robert Widmann
2efbeb3912 Merge pull request #21451 from CodaFi/logicd
[SR-8272] Drop the last remnants of LogicValue
2018-12-20 23:33:20 -05:00
Robert Widmann
426fe886dc [SR-8272] Drop the last remnants of LogicValue
Removes the _getBuiltinLogicValue intrinsic in favor of an open-coded
struct_extract in SIL.  This removes Sema's last non-literal use of builtin
integer types and unblocks a bunch of cleanup.

This patch would be NFC, but it improves line information for conditional expression codegen.
2018-12-19 23:14:59 -05:00