Commit Graph

1263 Commits

Author SHA1 Message Date
Pavel Yaskevich
e60afb3446 [ConstraintSystem] Augment ExpressionTimer to carry the threshold
Instead of asking callers of `isExpired` to provide the threshold,
let's ask for that upfront. This change also allows us to check how
much time remains in the timer and build timers with different
thresholds without having to safe that information somewhere else.
2022-02-08 14:33:46 -08:00
Pavel Yaskevich
49b181136b [AST] Augment ArgumentList::createImplicit to accept first trailing closure index 2022-02-03 19:41:00 -08:00
Pavel Yaskevich
b48a064f62 [ConstraintSystem] Add storage for implicitly generated roots of .callAsFunction
Some implicit calls to `.callAsFunction` require that a new root
expression to be created for them in order to record argument list
and resolved overload choice.
2022-02-03 15:35:50 -08:00
Anthony Latsis
c7fd60f2dd Merge pull request #39492 from AnthonyLatsis/se-309-2
SE-309: Covariant erasure for dependent member types
2022-02-02 07:07:17 +03:00
Anthony Latsis
24c19d4ad4 Sema: Enable covariant erasure for dependent member types 2022-02-02 02:10:05 +03:00
Anthony Latsis
b3ee4b0718 AST, Sema: Use the opened archetype's generic signature to determine existential member availability 2022-02-02 02:09:59 +03:00
Anthony Latsis
b2fe028885 [NFC] Turn SelfReferenceKind into a standalone utility type 2022-02-01 20:55:47 +03:00
Anthony Latsis
427190d348 Sema: Handle generic-signature-based existential availability 2022-02-01 20:55:46 +03:00
Anthony Latsis
3206f5ab34 [NFC] Preemptively relocate ProtocolDecl::isAvailableInExistential() and co. 2022-02-01 20:55:46 +03:00
Evan Wilde
4b182f6a61 Move findAsyncNode to BraceStmt
I need to determine when top-level code contains an `await` to determine
whether to make the source file an async context. This logic is
perfectly encapsulated in the `FindInnerAsync` AST walker.
Unfortunately, that is pushed down in Sema/ConstraintSystem and isn't
available at the AST level. I've pulled it up into the brace statement
so that I can use that as part of determining whether the source file is
async in `DeclContext::isAsyncContext`.

Unfortunately, statements don't have an AST context or evaluator or I
would make this a request.
2022-01-28 07:55:14 -08:00
Holly Borla
76e6156857 [Sema] Construct OpenedArchetypeType with a canonical existential type.
Opened archetypes can be created in the constraint system, and the
existential type it wraps can contain type variables. This can happen
when the existential type is inferred through a typealias inside a
generic type, and a member reference whose base is the opened existential
gets bound before binding the generic arguments of the parent type.

However, simplifying opened archetypes to replace type variables is
not yet supported, which leads to type variables escaping the constraint
system. We can support cases where the underlying existential type doesn't
depend on the type variables by canonicalizing it when opening the
existential. Cases where the underlying type requires resolved generic
arguments are still unsupported for now.
2022-01-19 22:53:52 -08:00
Holly Borla
626bea2bc6 [ConstraintSystem] Return an existential type in getTypeOfMemberReference
when the member is a typealias to a protocol or composition.
2022-01-13 22:31:55 -08:00
Holly Borla
5dced8e5f9 [ConstraintSystem] Fix a few constraint system corner cases with explicit
existential types.
2022-01-13 19:30:44 -08:00
Holly Borla
d971d489d6 [Type System] With explicit existential types, make sure existential
metatypes always wrap the constraint type directly, and the instance
type of an existential metatype is an existential type.
2022-01-13 19:30:44 -08:00
Holly Borla
6cee193fc0 [Type System] When explicit existential types are enabled, wrap Error
in ExistentialType for the type of error values.
2022-01-13 19:30:44 -08:00
Doug Gregor
99a9e95a93 Merge pull request #40747 from DougGregor/opaque-type-archetype-environment 2022-01-06 06:50:50 -08:00
Pavel Yaskevich
9f7d3fccad Merge pull request #40708 from xedin/disable-multi-stmt-inference-inside-result-builder-bodies
[ConstraintSystem] SE-0326: Temporarily prevent multi-statement closure inference in result builder contexts
2022-01-06 01:04:22 -08:00
Doug Gregor
859110cc97 Stop opening the "bound" generic signature of an opaque type declaration.
Creation of the "bound" generic signature isn't possible with interface
types or type variables, so open up the opaque interface signature
instead and separately bind the outer type parameters as appropriate.
2022-01-05 12:04:48 -08:00
Doug Gregor
791c9c7d13 Implement most of the semantic analysis for named opaque result types.
Address small gaps in several places to make named opaque result types
partially work:

* Augment name lookup to look into the generic parameters when inside the
result type, which is used both to create structure and add requirements
via a `where` clause.
* Resolve opaque generic type parameter references to
OpaqueTypeArchetypeType instances, as we do for the "some" types
* Customize some opaque-type-specific diagnostics and type printing to
refer to the opaque generic parameter names specifically
* Fix some minor issues with the constraint system not finding
already-opened opaque generic type parameters and with the handling of
the opaque result type candidate set.

The major limitation on opaque types, where we cannot add requirements
that aren't strictly protocol or superclass requirements on the
generic parameters, remains. Until then, named opaque result types are
no more expressive than structural opaque result types.
2022-01-04 21:14:04 -08:00
Doug Gregor
b97ef02d85 Opaque opaque types and compute substitutions in the constraint system
Opaque opaque types and record them within the "opened types" of the
constraint system, then use that information to compute the set of
substitutions needed for the opaque type declaration using the normal
mechanism of the constraint solver. Record these substitutions within
the underlying-to-opaque conversion.

Use the recorded substitutions in the underlying-to-opaque conversion
to set the underlying substitutions for the opaque type declaration
itself, rather than reconstructing the substitutions in an ad hoc manner
that does not account for structural opaque result types.
2021-12-26 20:33:58 -08:00
Pavel Yaskevich
efafe02a30 [ConstraintSystem] SE-0326: Temporarily prevent multi-statement closure inference in result builder contexts
Multi-statement closure inference doesn't play well with result builders
at the moment because it expects all of the information from the parent
statements to be inferred before solver starts working on the body of a
nested closure. Let's prevent inference for nested multi-statement closures
until result builders are ported to use conjunctions and solve the body
incrementally top-down instead of in one shot.
2021-12-25 17:09:19 -08:00
Robert Widmann
e5bfda7c6e Merge pull request #40587 from CodaFi/substitute-teacher
Initial Semantics for Variadic Generics
2021-12-20 11:25:25 -08:00
Pavel Yaskevich
34575e3a0d [TypeChecker] Support _openExistential with async functions 2021-12-17 16:37:49 -08:00
Robert Widmann
d44d8ec043 Allow Converting Pack Types to Tuples
Insert an implicit conversion from pack types to tuples with equivalent parallel structure. That means

1) The tuple must have the same arity
2) The tuple may not have any argument labels
3) The tuple may not have any variadic or inout components
4) The tuple must have the same element types as the pack
2021-12-16 08:51:38 -08:00
Doug Gregor
cc7904cf52 Use the closure type during type checking for establishing use of new features
When evaluating whether code is within a closure that uses concurrency
features, use the type of the closure as it's known during type checking,
so that contextual information (e.g., it's passed to a `@Sendable` or
`async` parameter of function type) can affect the result. This
corrects the definition for doing strict checking within a minimal
context for the end result of the type-check, rather than it's initial
state, catching more issues.

Fixes SR-15131 / rdar://problem/82535088.
2021-12-14 07:15:49 -08:00
Doug Gregor
c3b6160af8 Generalize and cache the "closure effects" determined from the closure body.
Use this to enable better detection of async contexts when determining
whether to diagnose problems with concurrency.

Part of SR-15131 / rdar://problem/82535088.
2021-12-13 21:32:28 -08:00
Pavel Yaskevich
0e6e058e7c [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-12-03 10:54:07 -08:00
Pavel Yaskevich
bc54bc6bb7 Revert "[TypeChecker] SE-0326: Enable multi-statement closure inference by default" 2021-11-29 17:26:08 -08:00
Anthony Latsis
9fc633e0b8 Merge pull request #39652 from AnthonyLatsis/no-context
TypeResolution: Abolish TypeResolutionStage::Contextual
2021-11-29 16:44:27 +03:00
Anthony Latsis
3c17d35f34 CS: Use TypeResolutionStage::Interface for applying generic arguments in 'openUnboundGenericType' 2021-11-19 16:44:07 +03:00
Anthony Latsis
e48f4830dd TypeResolution: Replace more 'forContextual' uses with 'resolveContextualType' 2021-11-19 16:44:03 +03:00
swift-ci
37ea254cc1 Merge pull request #40220 from DougGregor/predates-concurrency 2021-11-19 01:14:42 -08:00
Doug Gregor
f32572b4cd Apply @_predatesConcurrency adjustments to variables, subscripts. 2021-11-18 21:12:28 -08:00
Pavel Yaskevich
83033198c3 [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-11-15 16:42:05 -08:00
Pavel Yaskevich
106922545c Merge pull request #40074 from xedin/rdar-84879566
[ConstraintLocator] Allow simplification of constructor member with overloaded base
2021-11-08 10:13:49 -08:00
Pavel Yaskevich
0c70f5650f Merge pull request #39592 from Jumhyn/contextual-placeholder-followup
Remove default argument from getContextualType
2021-11-08 10:00:36 -08:00
Pavel Yaskevich
609841710f [ConstraintLocator] Allow simplification of constructor member with overloaded base
`init` calls to redeclared types would end up diagnosed as ambiguity,
so locator simplification needs to account for the fact that "base"
of constructor might be overloaded type reference.

Resolves: rdar://84879566
2021-11-05 16:44:07 -07:00
Pavel Yaskevich
4c415abc44 [ConstraintSystem] Add missing cases to getLoc(ASTNode)
Recently added `StmtCondition` and `CastLabelItem` wheren't
handled at all.
2021-11-05 13:36:10 -07:00
Hamish Knight
fb3796971e [CS] Simplify getAlternativeLiteralTypes
Caching the result here feels a little overkill as
it's only useful for one protocol, and the
`TypeChecker::getDefaultType` computation is
cached by the request evaluator.
2021-11-02 13:55:04 +00:00
Pavel Yaskevich
568c27f551 [ConstraintSystem] Convert Opened{Existential}Types to a map vector to avoid duplicates
Same reasons as with `DefaultConstraints` but also improves
code ergonomics around opened types.
2021-10-22 10:09:08 -07:00
Holly Borla
0601498ce7 Merge pull request #39831 from hborla/invalid-selector-property
[ConstraintSystem] Don't increase the score for unapplied function references if the expression is an argument to `#selector`.
2021-10-20 19:37:48 -07:00
Holly Borla
beaaee7af1 [ConstraintSystem] Don't increase the score for unapplied function references
if the expression is an argument to `#selector`.

For `#selector` arguments, functions and properties are syntactically distinct
with the getter/setter label, so the solver should not unconditionally prefer
properties to unapplied functions. A better fix for this is to port over the
`#selector` diagnostics from CSApply, and not attempt invalid disjunction choices
based on the selector kind on the valid code path.
2021-10-20 09:19:25 -07:00
Doug Gregor
913650dab1 Centralize Dispatch-related @_unsafe(Sendable|MainActor) hacks 2021-10-19 22:50:19 -07:00
Doug Gregor
1b8f562852 Adjust the referenced function type for @_unsafeSendable and @_unsafeMainActor.
When referencing a function that contains parameters with the hidden
`@_unsafeSendable` or `@_unsafeMainActor` attributes, adjust the
function type to make the types of those parameters `@Sendable` or
`@MainActor`, respectively, based on both the context the expression:

* `@Sendable` will be applied when we are in a context with strict
concurrency checking.
* `@MainActor` will be applied when we are in a context with strict
concurrency checking *or* the function is being directly applied so
that an argument is provided in the immediate expression.

The second part of the rule of `@MainActor` reflects the fact that
making the parameter `@MainActor` doesn't break existing code (because
there is a conversion to add a global actor to a function value), but
it does enable such code to synchronously use a `@MainActor`-qualified
API.

The main effect of this change is that, in a strict concurrency
context, the type of referencing an unapplied function involving
`@_unsafeSendable` or `@_unsafeMainActor` in a strict context will
make those parameters `@Sendable` or `@MainActor`, which ensures that
these constraints properly work with non-closure arguments. The former
solution only applied to closure literals, which left some holes in
Sendable checking.

Fixes rdar://77753021.
2021-10-19 22:50:17 -07:00
Hamish Knight
d3618ef73c Merge pull request #39302 from hamishknight/iuo-a-refactoring 2021-10-15 23:35:47 +01:00
Pavel Yaskevich
6de2e63251 Merge pull request #39774 from xedin/fix-no-assert-warnings
[Sema] Fix unused function/variable warnings in no-assertion configuration
2021-10-15 15:31:56 -07:00
Pavel Yaskevich
84fa8a35e6 [Sema] Fix unused function/variable warnings in no-assertion configuration 2021-10-15 13:22:44 -07:00
Pavel Yaskevich
ffca407138 [ConstraintSystem] NFC: Remove unnecessary castToExpr from diagnoseAmbiguity 2021-10-14 12:51:15 -07:00
Pavel Yaskevich
df44c4f4d7 [ConstraintSystem] Fix non-determinism in diagnoseAmbiguity
Simplification of member locator would produce a base expression,
this is what we want for diagnostics but not for comparisons in
`diagnoseAmbiguity` because base expression is located at a different
depth which would lead to incorrect results if both reference and base
expression are ambiguous e.g. `test[x].count` if both `[x]` and `count`
are ambiguous than simplification of `count` would produce `[x]` which
is incorrect.

This is the test-case (already in the suite) that exibits this behavior:

```
func test_ambiguity_with_placeholders(pairs: [(rank: Int, count: Int)]) -> Bool {
  return pairs[<#^ARG^#>].count == 2
}
```

Here subscript would either return a tuple or `ArraySlice` and
`count` is ambiguous because both have it.
2021-10-14 11:26:38 -07:00
Hamish Knight
287fa8e8de [CS] Refactor IUO handling
The current IUO design always forms a disjunction
at the overload reference, for both:

- An IUO property `T!`, forming `$T := T? or T`
- An IUO-returning function `() -> T!`, forming `$T := () -> T? or () -> T`

This is simple in concept, however it's suboptimal
for the latter case of IUO-returning functions for
a couple of reasons:

- The arguments cannot be matched independently of
  the disjunction
- There's some awkwardness when it comes e.g wrapping
  the overload type in an outer layer of optionality
  such as `(() -> T!)?`:
  - The binding logic has to "adjust" the correct
    reference type after forming the disjunction.
  - The applicable fn solving logic needs a special
    case to handle such functions.
- The CSApply logic needs various hacks such as
  ImplicitlyUnwrappedFunctionConversionExpr to
  make up for the fact that there's no function
  conversion for IUO functions, we can only force
  unwrap the function result.
  - This lead to various crashes in cases where
    we we'd fail to detect the expr and peephole
    the force unwrap.
  - This also lead to crashes where the solver
    would have a different view of the world than
    CSApply, as the former would consider an
    unwrapped IUO function to be of type `() -> T`
    whereas CSApply would correctly see the overload
    as being of type `() -> T?`.

To remedy these issues, IUO-returning functions no
longer have their disjunction formed at the overload
reference. Instead, a disjunction is formed when
matching result types for the applicable fn
constraint, using the callee locator to determine
if there's an IUO return to consider. CSApply then
consults the callee locator when finishing up
applies, and inserts the force unwraps as needed,
eliminating ImplicitlyUnwrappedFunctionConversionExpr.

This means that now all IUO disjunctions are of the
form `$T := T? or T`. This will hopefully allow a
further refactoring away from using disjunctions
and instead using type variable binding logic to
apply the correct unwrapping.

Fixes SR-10492.
2021-10-12 14:14:33 +01:00