Commit Graph

15871 Commits

Author SHA1 Message Date
Pavel Yaskevich
3e01160a2f [ConstraintSystem] Make it possible to infer subtype bindings through argument conversions
Enable solver to transitively infer bindings through argument conversion
constraints. That helps to infer bindings for (generic) parameters
from their arguments e.g.

```swift
func foo<T: ExpressibleByStringLiteral>(_: String, _: T) -> T {
  fatalError()
}

func bar(_: Any?) {}

func test() {
  bar(foo("", ""))
}
```

In this case `T` can currently only be inferred as `Any?`
(based on parameter type of `bar`) although a complete
set of bindings for that type variable includes `String`
as well, which comes from use of `T` in argument position.

Resolves: rdar://problem/56212087
2020-02-21 17:47:24 -08:00
Pavel Yaskevich
ed255596a6 [ConstraintSystem] Increase fix impact when get-only dynamic key path is used as a mutable one 2020-02-21 17:28:10 -08:00
Anthony Latsis
a11cc4fcfc Handle more built-in operators and error intersections with the unwrap collision diagnostic 2020-02-22 03:55:43 +03:00
Pavel Yaskevich
0a6b444b49 [ConstraintSystem] Diagnose argument conflicts only for "representative" generic parameters
Consider following example:

```swift
struct MyType<TyA, TyB> {
  var a : TyA, b : TyB
}

typealias B<T1> = MyType<T1, T1>

_ = B(a: "foo", b: 42)
```

Here `T1` is equal to `TyA` and `TyB` so diagnostic about
conflicting arguments ('String' vs. 'Int') should only be
produced for "representative" `T1`.
2020-02-21 16:49:37 -08:00
Holly Borla
9f09add09b Merge pull request #29966 from hborla/type-check-type-eraser-attribute
[Sema] Implement type checking for the `_typeEraser` attribute
2020-02-21 14:12:59 -08:00
Holly Borla
e93a6b9b51 [TypeCheckAttr] Allow a typealias to be a type eraser and add additional
typeEraser tests.
2020-02-21 12:12:27 -08:00
Pavel Yaskevich
dec2ad0f93 Merge pull request #29979 from xedin/rdar-56400265
[ConstraintSystem] Avoid exploring too much search space when call ar…
2020-02-21 11:16:33 -08:00
Dan Zheng
697c722a5f [AutoDiff] Type-checking support for inout parameter differentiation. (#29959)
Semantically, an `inout` parameter is both a parameter and a result.

`@differentiable` and `@derivative` attributes now support original functions
with one "semantic result": either a formal result or an `inout` parameter.

Derivative typing rules for functions with `inout` parameters are now defined.

The differential/pullback type of a function with `inout` differentiability
parameters also has `inout` parameters. This is ideal for performance.

Differential typing rules:
- Case 1: original function has no `inout` parameters.
  - Original:     `(T0, T1, ...) -> R`
  - Differential: `(T0.Tan, T1.Tan, ...) -> R.Tan`
- Case 2: original function has a non-wrt `inout` parameter.
  - Original:     `(T0, inout T1, ...) -> Void`
  - Differential: `(T0.Tan, ...) -> T1.Tan`
- Case 3: original function has a wrt `inout` parameter.
  - Original:     `(T0, inout T1, ...) -> Void`
  - Differential: `(T0.Tan, inout T1.Tan, ...) -> Void`

Pullback typing rules:
- Case 1: original function has no `inout` parameters.
  - Original: `(T0, T1, ...) -> R`
  - Pullback: `R.Tan -> (T0.Tan, T1.Tan, ...)`
- Case 2: original function has a non-wrt `inout` parameter.
  - Original: `(T0, inout T1, ...) -> Void`
  - Pullback: `(T1.Tan) -> (T0.Tan, ...)`
- Case 3: original function has a wrt `inout` parameter.
  - Original: `(T0, inout T1, ...) -> Void`
  - Pullback: `(inout T1.Tan) -> (T0.Tan, ...)`

Resolves TF-1164.
2020-02-21 09:47:53 -08:00
Dan Zheng
1cefebe227 [AutoDiff] Fix @differentiable-related override matching crash. (#29985)
Remove meaningless `assert(false)` assertion from
`hasOverridingDifferentiableAttribute` in `TypeCheckDeclOverride.cpp`.

The assertion served no purpose and can safely be removed.

Resolves TF-1167. Add test.
2020-02-21 09:29:29 -08:00
Holly Borla
fe4ba8ae22 [TypeCheckAttr] Use the correct APIs for checking protocol conformance
and replacing type parameters in a generic signature when type checking
the typeEraser attribute.
2020-02-20 19:32:15 -08:00
Pavel Yaskevich
46d945fdcf [ConstraintSystem] Avoid exploring too much search space when call arguments are holes
Detect that disjunction is going to be applied to arguments which
don't provide any additional contextual information and allow only
a single choice to be attempted in such case to avoid triggering
exponential behavior in the solver.

The problem is most visible with operators e.g.

```swift
.foo == .bar || 1 == .baz
```

If neither member could be contextually determined and solver was
allowed to attempt all of the overloads for `==` and `||` that
would lead to exponential behavior (because each has 30+ overloads)
and generation of hundreds of partial solutions.

Resolves: rdar://problem/56400265
2020-02-20 16:52:30 -08:00
Holly Borla
2185f431ad [TypeCheckAttr] Improve diagnostics and candidate notes for typeEraser in
the case where the type eraser has no viable initializers.
2020-02-20 12:50:04 -08:00
Anthony Latsis
1c0c397214 [Parse] Improve recovery from and diagnostics for invalid operator names 2020-02-20 22:11:18 +03:00
Holly Borla
3d18c07640 [TypeCheckAttr] Implement access control checking for the typeEraser
attribute.
2020-02-20 10:57:10 -08:00
Pavel Yaskevich
88ca38271a Merge pull request #29936 from xedin/rdar-49712598
[CSRanking] Detect cases where overload choices are incomparable
2020-02-20 01:22:32 -08:00
swift-ci
5e030b57a2 Merge pull request #29918 from dan-zheng/derivative-attr-diagnostics 2020-02-19 23:26:54 -08:00
Holly Borla
f141b4da3c [TypeCheckAttr] Provide better error messages for a typeEraser that
has invalid init candidates.
2020-02-19 21:23:57 -08:00
Dan Zheng
e5e9fce8bf [AutoDiff upstream] Upstream attribute type-checking changes. (#29945)
- Support `@differentiable` and `@derivative` attributes for original
  initializers in final classes. Reject original initializers in non-final
  classes.
- Synchronize tests.
2020-02-19 21:17:45 -08:00
Robert Widmann
8c2cff8c40 Merge pull request #29937 from CodaFi/semantic-gymnastics
[Sema] Always install property wrappers during qualified lookup
2020-02-19 19:54:54 -08:00
Robert Widmann
850aba5fe3 Merge pull request #29940 from CodaFi/bicyclette
Flush an old pre-Evaluator Cycle Breaking Hack
2020-02-19 19:54:38 -08:00
Pavel Yaskevich
8af4f2ddb3 Merge pull request #29493 from LucianoPAlmeida/SR-11421-checked-cast-diag
[SR-11421][Diagnostics] Tailored diagnostic for checked downcast with literals
2020-02-19 17:29:30 -08:00
Holly Borla
ebb727c0a3 [TypeCheckAttr] Allow a type eraser initializer to have more than one
generic requirement.
2020-02-19 17:07:30 -08:00
Robert Widmann
4e9ae933fc Flush an old pre-Evaluator Cycle Breaking Hack
There's no reason why we should bail early here anymore now that the
interface type is computable on-demand.
2020-02-19 15:32:30 -08:00
Robert Widmann
92b8d637f1 [Sema] Always install property wrappers during qualified lookup
Revert the property wrappers part of dd51251014.

Every part of the qualified lookup stack needs to synthesize property
wrapper members, otherwise we'll be subject to the relativistic effects
of semantic lookups in different files. Besides, Codable was the main
source of cycles and circularity under the old scheme.

Resolves rdar://59531889
2020-02-19 14:23:07 -08:00
Alexis Laferrière
0e7029dfb5 Use "SPI group" for the name used in an @_spi attribute 2020-02-19 14:18:11 -08:00
Alexis Laferrière
2e73cb44fd [Sema] Type-check the use and exposability of SPI decls 2020-02-19 14:17:14 -08:00
Alexis Laferrière
56880d7e70 [Sema] Diagnose the use of SPI on unsupported decls 2020-02-19 14:17:14 -08:00
Alexis Laferrière
d5969a9f3a [AST] Intro SPI attribute for access control and imports 2020-02-19 14:17:08 -08:00
Pavel Yaskevich
288a7765fc [CSRanking] Detect cases where overload choices are incomparable
If constraint system is underconstrained e.g. because there are
editor placeholders, it's possible to end up with multiple solutions
where each ambiguous declaration is going to have its own overload kind:

```swift
func foo(_: Int) -> [Int] { ... }
func foo(_: Double) -> (result: String, count: Int) { ... }

_ = foo(<#arg#>).count
```

In this case solver would produce 2 solutions: one where `count`
is a property reference on `[Int]` and another one is tuple access
for a `count:` element.

Resolves: rdar://problem/49712598
2020-02-19 13:41:26 -08:00
Pavel Yaskevich
71753f3ca6 [ConstraintSystem] Rank contextually unavailable overloads lower than other choices (#29921)
Currently constraint solver is only capable of detecting universally unavailable
overloads but that's insufficient because it's still possible to pick a contextually
unavailable overload choice which could be better than e.g. generic overload, or
one with defaulted arguments, marked as disfavored etc.

Let's introduce `ConstraintSystem::isDeclUnavailable` which supports both universal
and contextual unavailability and allow constraint solver to rank all unavailable
overload choices lower than any other possible choice(s).

Resolves: rdar://problem/59056638
2020-02-19 13:13:53 -05:00
Hamish Knight
87b1920efc [CS] A couple of post CSDiag cleanups (#29922)
[CS] A couple of post CSDiag cleanups
2020-02-19 10:06:37 -08:00
Dan Zheng
44d7ae6c08 Add utility for checking whether differentiable programming is enabled. 2020-02-19 09:55:26 -08:00
Dan Zheng
469ecb640f Improve @derivative type-checking diagnostics order.
Attempt to look up original function before checking whether the `value:` result
conforms to `Differentiable`.

This improves diagnostics: "original function not found" should be diagnosed as
early as possible.
2020-02-19 09:54:31 -08:00
Brent Royal-Gordon
d842fa3763 Merge pull request #29582 from brentdax/the-most-ambitious-crossover-event-in-history
Cross-Import Overlays
2020-02-19 09:10:55 -08:00
Hamish Knight
857e523deb [CS] Remove unused function
In addition to a member it set on SanitizeExpr.
Unfortunately it will take a bit more effort to
kill SanitizeExpr entirely.
2020-02-19 07:43:59 -08:00
Hamish Knight
1a9764e6d7 [CS] Remove SubExpressionDiagnostics option 2020-02-19 07:43:59 -08:00
Hamish Knight
ed1d372887 [CS] Remove DiagnosedExprs 2020-02-19 07:43:59 -08:00
Hamish Knight
4af4af457e [CS] Remove baseCS param from typeCheckExpression 2020-02-19 07:43:58 -08:00
Brent Royal-Gordon
9a09eb7683 [NFC] Replace three fields in UnboundImport with a PointerUnion
It’s not often that a PointerUnion makes the code *more* safe and clear, so let’s take it.
2020-02-19 01:01:43 -08:00
Brent Royal-Gordon
9adaddc611 [NFC] Additional readability improvements
Courtesy of Varun.
2020-02-19 01:00:41 -08:00
Robert Widmann
cee664fb9a Merge pull request #29916 from CodaFi/de-fault-is-mine
Refactor AreAllStoredPropertiesDefaultInitableRequest for Property Wrappers
2020-02-18 22:15:36 -08:00
Pavel Yaskevich
51257ddf73 Merge pull request #29906 from xedin/eliminate-csdiag
[TypeChecker] Obsolete and remove old diagnostics (CSDiag)
2020-02-18 19:46:54 -08:00
Robert Widmann
b0d741d7d1 Refactor AreAllStoredPropertiesDefaultInitableRequest for Property Wrappers
Do a bit of cleanup here. Also expand the storage check to query the
backing variable for any attached property wrappers. If these are not
default-initialized or default initializable then the generated
initializer will contain unfixable DI errors.

Resolves rdar://58495602
2020-02-18 17:14:34 -08:00
Pavel Yaskevich
2c92df4a6d [ConstraintSystem] Simplify diagnoseAmbiguityWithEphemeralPointers
Don't attempt to figure out what exactly is ambiguous, let
`diagnoseAmbiguity` take care of that. Simplify make sure
that only some of the solutions have fixes and these fixes
are all related to use of ephemeral pointers.
2020-02-18 15:06:13 -08:00
Pavel Yaskevich
d2953bb0c0 [ConstraintSystem] Let simplifyRestrictedConstraintImpl fix any failures related to arguments with restrictions 2020-02-18 13:22:22 -08:00
Brent Royal-Gordon
014bb1cd83 Don’t count submodules when cross-importing
It turns out that, if you pull in any nontrivial module, there are thousands of submodules and none of them could possibly have a  cross-import overlay. Avoid evaluating them.
2020-02-18 11:08:36 -08:00
Brent Royal-Gordon
12286197c9 [NFC] Improvements suggested in code review
Thank you, @hamishknight and @varungandhi-apple.
2020-02-18 11:08:36 -08:00
Brent Royal-Gordon
747c50725c Add a warning about redundant cross-import declarations
These are mostly harmless, except that they make the two module names synonymous in qualified lookup. A hard error seems too aggressive for something that could easily be caused by uncoordinated changes to two modules, so warn instead.
2020-02-18 11:08:36 -08:00
Brent Royal-Gordon
d6bccba9b6 Load cross-import overlays 2020-02-18 11:08:36 -08:00
Brent Royal-Gordon
5d74978268 [NFC] Heavily refactor NameBinding
This commit refactors NameBinding.cpp virtually to the point of a rewrite. The goal is to make the import handling code process a struct containing information extracted from an ImportDecl, rather than the ImportDecl itself, so that a future commit can perform the same processing on imports that are not directly represented by an ImportDecl. The result takes more code, but it disentangles a knot of complicated logic into separate threads.

One semi-functional change is that validation of scoped imports (i.e. checking that the declaration actually exists) is now deferred until all of the file’s imports have been processed. Once cross-imports are supported, this will be necessary because scoped imports can select declarations from cross-import overlays, and the full set of cross-import overlays can’t be known until all imports have been seen.

Some comments refer to things that won’t exist until the next commit, like the visibleModules property. I’ll let you in on a litlte secret: I didn’t really do this all in one go.
2020-02-18 11:08:36 -08:00