Commit Graph

15871 Commits

Author SHA1 Message Date
Hamish Knight
000572d56c Merge pull request #30371 from hamishknight/insider-information
[CS] Use getParameterType for KeyPath as function
2020-03-11 19:32:47 -07:00
omochimetaru
484606dc3a [ConstraintSystem] Fix bug of argument reordering in matchCallArguments 2020-03-12 11:19:28 +09:00
Hamish Knight
b62c9b6b01 [CS] Use getParameterType for KeyPath as function
Previously we were using `getPlainType` to match
the parameter type against the key path's base
type. This gave us the external parameter type,
which would be the element type for a variadic
parameter.  However the code we generate expects
the internal parameter type, which is provided by
`getParameterType`.

Resolves rdar://problem/59445486.
2020-03-11 17:21:28 -07:00
Doug Gregor
a3979da1cd Merge pull request #30322 from DougGregor/function-builders-extras
[Function builders] Additional entry points
2020-03-11 16:17:27 -07:00
swift-ci
000928d260 Merge pull request #30360 from CodaFi/results-may-vary 2020-03-11 13:23:10 -07:00
Robert Widmann
d23082ff44 Merge pull request #30359 from CodaFi/unopened-reference-semantics
[CS] Move getUnopenedTypeOfReference out of TypeChecker
2020-03-11 13:11:10 -07:00
Pavel Yaskevich
94ffe9afed Merge pull request #30336 from xedin/adjust-pattern-locators
[CSGen] Clarify locators for some of the patterns
2020-03-11 13:10:26 -07:00
Robert Widmann
266f3ad26f [NFC] Use LookupTypeResult's public API
Drop an extraneous `friend` declaration.
2020-03-11 11:26:45 -07:00
Robert Widmann
4e67eec878 [CS] Move getUnopenedTypeOfReference out of TypeChecker
For some reason, the changed caller in CS wasn't actually going to use any of the types in the constraint system from the entrypoint it was calling. Switch over to using the constraint-system-based entrypoint so we can pick up expression types consistently. Then, move the TypeChecker entrypoint onto ConstraintSystem to reduce the duplication here.

The remaining callers of buildCheckedRefExpr should be migrated.
2020-03-11 11:14:10 -07:00
Nathan Hawes
a368434432 [SourceKit/CodeFormat] Re-work and improve the indentation implementation.
This restructures the indentation logic around producing a single IndentContext
for the line being indented. An IndentContext has:
- a ContextLoc, which points to a source location to indent relative to,
- a Kind, indicating whether we should align with that location exactly, or
  with the start of the content on its containing line, and
- an IndentLevel with the relative number of levels to indent by.

It also improves the handling of:
- chained and nested parens, braces, square brackets and angle brackets, and
  how those interact with the exact alignment of parameters, call arguments,
  and tuple, array and dictionary elements.
- Indenting to the correct level after an incomplete expression, statement or
  decl.

Resolves:
rdar://problem/59135010
rdar://problem/25519439
rdar://problem/50137394
rdar://problem/48410444
rdar://problem/48643521
rdar://problem/42171947
rdar://problem/40130724
rdar://problem/41405163
rdar://problem/39367027
rdar://problem/36332430
rdar://problem/34464828
rdar://problem/33113738
rdar://problem/32314354
rdar://problem/30106520
rdar://problem/29773848
rdar://problem/27301544
rdar://problem/27776466
rdar://problem/27230819
rdar://problem/25490868
rdar://problem/23482354
rdar://problem/20193017
rdar://problem/47117735
rdar://problem/55950781
rdar://problem/55939440
rdar://problem/53247352
rdar://problem/54326612
rdar://problem/53131527
rdar://problem/48399673
rdar://problem/51361639
rdar://problem/58285950
rdar://problem/58286076
rdar://problem/53828204
rdar://problem/58286182
rdar://problem/58504167
rdar://problem/58286327
rdar://problem/53828026
rdar://problem/57623821
rdar://problem/56965360
rdar://problem/54470937
rdar://problem/55580761
rdar://problem/46928002
rdar://problem/35807378
rdar://problem/39397252
rdar://problem/26692035
rdar://problem/33760223
rdar://problem/48934744
rdar://problem/43315903
rdar://problem/24630624
2020-03-10 21:04:21 -07:00
Pavel Yaskevich
f1f3b60713 [CSGen] Clarify locators for some of the patterns
- optional object type of `.some` pattern ends with `OptionalPayload`
- type of sub-pattern used in a cast points to underlying sub-pattern declaration
- Enum element:
  - parent type locator ends with `ParentType`
  - member lookup constraint locator ends at `Member`
2020-03-10 13:56:32 -07:00
Pavel Yaskevich
aa35a500a8 Merge pull request #30328 from apple/revert-30164-fix/SR-12290
Revert "[Typechecker] Diagnose key paths with contextual root type but no leading dot"
2020-03-10 11:00:24 -07:00
John McCall
81ba9fd223 Merge pull request #30309 from rjmccall/accessor-subst-function-types
Use pattern substitutions to consistently abstract yields
2020-03-10 13:05:28 -04:00
Holly Borla
b12bd4e919 Merge pull request #30307 from hborla/merge-partial-solutions-too-complex
[ConstraintSystem] Respect the constraint solver performance thresholds,
2020-03-10 09:44:32 -07:00
Pavel Yaskevich
e86558fe4f Merge pull request #30277 from LucianoPAlmeida/SR-12019-dynamically-call-generic-constraint
[SR-12019] Fix assert when dynamicallyCall parameter does not satisfy generic constraint
2020-03-10 09:12:33 -07:00
Pavel Yaskevich
2374502a04 Revert "[Typechecker] Diagnose key paths with contextual type but no leading dot (#30164)"
This reverts commit 13487edd09.
2020-03-10 09:06:50 -07:00
John McCall
07f03bd287 Use pattern substitutions to consistently abstract yields.
The design implemented in this patch is that we lower the types of accessors with pattern substitutions when lowering them against a different accessor, which happens with class overrides and protocol witnesses, and that we introduce pattern substitutions when substituting into a non-patterned coroutine type.  This seems to achieve consistent abstraction without introduce a ton of new complexity.

An earlier version of this patch tried to define witness thunks (conservatively, just for accessors) by simply applying the requirement substitutions directly to the requirement.  Conceptually that should work, but I ran into a lot of trouble with things that assumed that pattern substitutions didn't conceal significant substitution work.  for example, resolving a dependent member in a component type could find a new use of an opaque archetype when the code assumed that such types had already been substituted away.  So while I think that is definiteely a promising direction, I had to back that out in order to make the number of changes manageable for a single PR.

As part of this, I had to fix a number of little bugs here and there, some of which I just introduced.  One of these bugfixes is a place where the substitution code was trying to improperly abstract function types when substituting them in for a type parameter, and it's been in the code for a really long time, and I'm really not sure how it's never blown up before.

I'm increasingly of the opinion that invocation substitutions are not actually necessary, but that --- after we've solved the substitution issues above --- we may want the ability to build multiple levels of pattern substitution so that we can guarantee that e.g. witness thunks always have the exact component structure of the requirement before a certain level of substitution, thus allowing the witness substitutions to be easily extracted.
2020-03-10 01:26:31 -04:00
Doug Gregor
f0530d0a77 [Function builders] Support buildOptional(_:) in lieu of buildIf(_:).
Line up with the function builders pitch, which uses buildOptional(_:)
to build optional values.
2020-03-09 21:48:15 -07:00
Holly Borla
c1c6a884a4 [ConstraintSystem] Respect the constraint solver performance thresholds,
including time and allocated memory, in mergePartialSolutions.
2020-03-09 14:44:12 -07:00
Pavel Yaskevich
4a06b13abf [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-03-09 12:10:29 -07:00
Doug Gregor
ce97d22f21 [Function builders] Add support for buildFinalResult().
When present in a function builder, buildFinalResult() will be called on
the value of the outermost block to form the final result of the closure.
This allows one to collapse the full function builder computation into
a single result without having to do it in each buildBlock() call.
2020-03-09 00:10:07 -07:00
Luciano Almeida
6ca7e383a9 [tests] Add more test cases for SR-12019 dynamic callable 2020-03-08 22:56:26 -03:00
Luciano Almeida
f796e27b65 [TypeChecker] Requestify DynamicCallableAttribute check 2020-03-08 20:44:23 -03:00
Pavel Yaskevich
a1987ec274 Merge pull request #30283 from xedin/rdar-60185506
Revert "[ConstraintSystem] Make it possible to infer subtype bindings…
2020-03-07 22:34:10 -08:00
Pavel Yaskevich
0ecedfa5ea Revert "[ConstraintSystem] Make it possible to infer subtype bindings through argument conversions"
Reverts apple/swift#30006. It caused a regression that we'd like to address before re-landing:

```swift
struct X {
  var cgf: CGFloat
}

func test(x: X?) {
  let _ = (x?.cgf ?? 0) <= 0.5
}
```

This reverts commit 0a6b444b49.
This reverts commit ed255596a6.
This reverts commit 3e01160a2f.
This reverts commit 96297b7e39.

Resolves: rdar://problem/60185506
2020-03-07 20:16:56 -08:00
John McCall
1f62fe591f Merge pull request #30272 from rjmccall/invocation-sil-function-subs
Distinguish invocation and pattern substitutions on SILFunctionType
2020-03-07 18:45:29 -05:00
John McCall
ceff414820 Distinguish invocation and pattern substitutions on SILFunctionType.
In order to allow this, I've had to rework the syntax of substituted function types; what was previously spelled `<T> in () -> T for <X>` is now spelled `@substituted <T> () -> T for <X>`.  I think this is a nice improvement for readability, but it did require me to churn a lot of test cases.

Distinguishing the substitutions has two chief advantages over the existing representation.  First, the semantics seem quite a bit clearer at use points; the `implicit` bit was very subtle and not always obvious how to use.  More importantly, it allows the expression of generic function types that must satisfy a particular generic abstraction pattern, which was otherwise impossible to express.

As an example of the latter, consider the following protocol conformance:

```
protocol P { func foo() }
struct A<T> : P { func foo() {} }
```

The lowered signature of `P.foo` is `<Self: P> (@in_guaranteed Self) -> ()`.  Without this change, the lowered signature of `A.foo`'s witness would be `<T> (@in_guaranteed A<T>) -> ()`, which does not preserve information about the conformance substitution in any useful way.  With this change, the lowered signature of this witness could be `<T> @substituted <Self: P> (@in_guaranteed Self) -> () for <A<T>>`, which nicely preserves the exact substitutions which relate the witness to the requirement.

When we adopt this, it will both obviate the need for the special witness-table conformance field in SILFunctionType and make it far simpler for the SILOptimizer to devirtualize witness methods.  This patch does not actually take that step, however; it merely makes it possible to do so.

As another piece of unfinished business, while `SILFunctionType::substGenericArgs()` conceptually ought to simply set the given substitutions as the invocation substitutions, that would disturb a number of places that expect that method to produce an unsubstituted type.  This patch only set invocation arguments when the generic type is a substituted type, which we currently never produce in type-lowering.

My plan is to start by producing substituted function types for accessors.  Accessors are an important case because the coroutine continuation function is essentially an implicit component of the function type which the current substitution rules simply erase the intended abstraction of.  They're also used in narrower ways that should exercise less of the optimizer.
2020-03-07 16:25:59 -05:00
Luciano Almeida
6df132de1a [ConstraintSystem] Make getCalleLocator to support an apply involving an dynamicCallable 2020-03-07 16:43:37 -03:00
Robert Widmann
9046799a2c Merge pull request #30274 from CodaFi/the-mac-in-the-mirror
[ObjC] Only Diagnose Selector Conflicts From Classes
2020-03-06 22:19:49 -08:00
Robert Widmann
bf6fe033ad [ObjC] Only Diagnose Selector Conflicts From Classes
Clang 1200 appears to be returning us results from protocol
declarations when we run lookup for an Objective-C selector. This code
path can therefore see protocol requirements, and would erroneously
diagnose witnesses from Swift subclasses of Objective-C classes
as an attempt to conflict with the requirement in the
protocol itself.

This fix is super narrow to try to unblock our builds. Tests will follow
once I can reduce the issue.

Resolves rdar://60005962
2020-03-06 18:30:15 -08:00
Slava Pestov
adbf8da7c2 Merge pull request #23489 from AnthonyLatsis/where-clause-nongeneric-decl
[SE] Allow where clauses on non-generic declarations in generic contexts
2020-03-06 17:53:14 -05:00
Pavel Yaskevich
c825ed8481 Merge pull request #30262 from xedin/remove-basecs
ConstraintSystem] Remove `baseCS` since CSDiag has been removed
2020-03-06 11:48:46 -08:00
Pavel Yaskevich
e4d3eaaf2a [ConstraintSystem] Don't store base constraint system in shrink candidate
Since there is no more sub-expression re-typechecking solver wouldn't
be trying to ran shrink on sub-expressions.
2020-03-06 09:46:12 -08:00
Pavel Yaskevich
7c19d1ccd1 [ConstraintSystem] Remove baseCS since CSDiag has been removed
There is no more sub-expression re-typechecking so no need to keep
parent constraint systems around.
2020-03-06 09:35:52 -08:00
Doug Gregor
1a981f917c [Constraint system] Clean up constraints associated with patterns.
Wherever we have constraints that involve pattern matching, use the
PatternMatch locator element. Additionally, don't use the TupleElement
locator element for tuple patterns, because it violates assumptions used
for diagnostics.

The new test was crashing; now it has a terrible diagnostic for which I
need to think harder about a fix.
2020-03-05 23:06:21 -08:00
Doug Gregor
6a5069d017 Merge pull request #30245 from xedin/rdar-60061646
[ConstraintSystem] Make sure pattern matching tuple destructuring works both ways
2020-03-05 21:48:30 -08:00
Holly Borla
f8de43b63b Merge pull request #30251 from hborla/invalid-optional-chaining
[ConstraintSystem] Don't assume the lhs type is for a generic parameter in simplifyConformsToConstraint.
2020-03-05 16:45:37 -08:00
Holly Borla
6898fbb0ef [ConstraintSystem] Don't assume the lhs type is for a
generic parameter in simplifyConformsToConstraint.
2020-03-05 14:39:30 -08:00
Pavel Yaskevich
1f222f307e [ConstraintSystem] Make sure pattern matching tuple destructuring works both ways
Consider following example:

```swift
enum E {
  case foo((x: Int, y: Int))
  case bar(x: Int, y: Int)
}

func test(e: E) {
  if case .foo(let x, let y) = e {}
  if case .bar(let tuple) = e {}
}
```

Both of `if case` expressions have to be supported:

1. `case .foo(let x, let y) = e` allows a single tuple
   parameter to be "destructured" into multiple arguments.

2. `case .bar(let tuple) = e` allows to match multiple
   parameters with a single tuple argument.

Resolves: rdar://problem/60061646
2020-03-05 14:05:35 -08:00
Hamish Knight
c26eb11baf Merge pull request #30190 from hamishknight/the-beginning-of-the-pipeline-end
Requestify SourceFile parsing
2020-03-05 11:17:24 -08:00
Pavel Yaskevich
d6a9399f7b Merge pull request #30202 from omochi/refactor-matchCallArguments
[ConstraintSystem] Refactor matchCallArguments
2020-03-05 09:45:53 -08:00
Anthony Latsis
f762644175 Diagnose nested type references with unsatisfied requirements 2020-03-05 20:23:49 +03:00
Pavel Yaskevich
8b6f66b245 Merge pull request #30223 from xedin/rdar-60048356
[ConstraintSystem] Preserve label matching rules in pattern matching …
2020-03-05 09:23:45 -08:00
Pavel Yaskevich
8ce5c7794b Merge pull request #30232 from xedin/rdar-60047439
[ConstraintSystem] Check availability as part of resolving overload c…
2020-03-05 09:23:04 -08:00
Doug Gregor
e780c194fc Merge pull request #30230 from DougGregor/function-builder-switch-checking
[Constraint system] Semantic checking for switch statements in function builders
2020-03-05 06:23:19 -08:00
Alexis Laferrière
34353b8a40 Merge pull request #30215 from xymus/spi-isnt-internal
[SPI] Requestify, inherit SPI groups and better diagnostics
2020-03-05 06:13:01 -08:00
Pavel Yaskevich
4c600843d6 [ConstraintSystem] Check availability as part of resolving overload choice
This is a follow up to changes related to contextual availability
(https://github.com/apple/swift/pull/29921) which increased score
for unavailable declarations only if they were overloaded but
overlooked a case of a single unavailable choice.

Resolve: rdar://problem/60047439
2020-03-05 00:35:47 -08:00
Pavel Yaskevich
87878a65eb [ConstraintSystem] Preserve label matching rules in pattern matching context
Since constraint system now handles pattern matching it has
to preverse label matching semantics which existed in original
code: if pattern element has a label it has to match the one
in the tuple type it's matched against.

Resolves: rdar://problem/60048356
2020-03-04 23:09:31 -08:00
Doug Gregor
c0cf407565 [Constraint solver] Check case variable mutability consistency. 2020-03-04 22:46:36 -08:00
Doug Gregor
3887498e55 [Constraint system] Diagnose @unknown cases in function builders. 2020-03-04 22:46:36 -08:00