Commit Graph

2489 Commits

Author SHA1 Message Date
Doug Gregor
e3af684772 Merge pull request #41183 from DougGregor/implicitly-open-existentials 2022-02-18 18:04:04 -08:00
zoecarver
f73e20b795 [cxx-interop] Fix crash when inout parameter is used in some "template contexts".
We used to incorrectly forward inout paramters in the thunk for both template paramters that aren't used in the signature and in the thunk for dependent types as Any.

Now this works in the simple case. We'll need to do something more complicated when we have an `inout Any` for dependent types because we will need to somehow cast without copying. This will probably require synthesising the SIL of the thunk manually.
2022-02-18 15:11:14 -08:00
Becca Royal-Gordon
c2ccb874ab Soften actor isolation in closures passed to @preconcurrency
When a closure is not properly actor-isolated, but we know that we inferred its isolation from a `@preconcurrency` declaration, we now emit the errors as warnings in Swift 5 mode to avoid breaking source compatibility if the isolation was added retroactively.
2022-02-18 14:29:16 -08:00
Doug Gregor
e30c8a5c09 Allow opening existentials for inout parameters. 2022-02-18 11:22:57 -08:00
Doug Gregor
1e1b3427c3 Experimental support for implicitly opening existential arguments.
When calling a generic function with an argument of existential type,
implicitly "open" the existential type into a concrete archetype, which
can then be bound to the generic type. This extends the implicit
opening that is performed when accessing a member of an existential
type from the "self" parameter to all parameters. For example:

    func unsafeFirst<C: Collection>(_ c: C) -> C.Element { c.first! }

    func g(c: any Collection) {
      unsafeFirst(c)   // currently an error
                       // with this change, succeeds and produces an 'Any'
    }

This avoids many common sources of errors of the form

    protocol 'P' as a type cannot conform to the protocol itself

which come from calling generic functions with an existential, and
allows another way "out" if one has an existention and needs to treat
it generically.

This feature is behind a frontend flag
`-enable-experimental-opened-existential-types`.
2022-02-18 11:22:56 -08:00
Zoe Carver
4a67288168 Merge pull request #41440 from zoecarver/members-with-dependent-types
[cxx-interop] Fix calling methods with dependent types.
2022-02-18 11:06:16 -08:00
zoecarver
47064a0eda [cxx-interop] Fix calling methods with dependent types.
Both instance members and static methods.
2022-02-18 09:21:55 -08:00
zoecarver
970489b72f [cxx-interop] [nfc] Don't emit a cast if the cast is a no-op. 2022-02-18 09:19:43 -08:00
zoecarver
0d60c2e25d Revert "Revert "[cxx-interop] Start to import dependent types as Any""
This reverts commit fdb417a2e5.
2022-02-17 13:23:14 -08:00
Saleem Abdulrasool
fdb417a2e5 Revert "[cxx-interop] Start to import dependent types as Any" 2022-02-16 20:20:36 -08:00
zoecarver
9ff5bb1f45 [cxx-interop] Add support for dependent types as Any.
Dependent types are going to be very hard to support, especially in complex cases. This PR adds a workaround that people can use: `Any`. This requires manual type casting, but it does work.
2022-02-16 12:44:14 -08:00
zoecarver
02a621962c [nfc] Re-factor logic in resolveConcreteDeclRef into separate static functions. 2022-02-16 12:43:48 -08:00
Zoe Carver
b60dcc1d24 Merge pull request #41394 from zoecarver/revert-revert-cleanup-and-fixes-for-function-templates 2022-02-16 08:30:16 -08:00
Anthony Latsis
352b3a2f6d Merge pull request #41201 from AnthonyLatsis/opened-root
AST: Remove OpenedArchetypeType::getOpenedExistentialType()
2022-02-16 09:07:30 +03:00
zoecarver
83e3f9657d [nfc] Re-factor logic in resolveConcreteDeclRef into separate static functions. 2022-02-15 20:09:15 -08:00
zoecarver
a1f9aae63f [cxx-interop] Fix integer substitution on watchOS.
Basically watchOS had the same issue as Windows.
2022-02-15 16:51:14 -08:00
zoecarver
283fc782ec Revert "Revert "[cxx-interop] A few cleanups and fixes for function templates.""
This reverts commit 5de09631ff.
2022-02-15 13:30:10 -08:00
Zoe Carver
5de09631ff Revert "[cxx-interop] A few cleanups and fixes for function templates." 2022-02-13 09:54:39 -08:00
Zoe Carver
0e7af7a7cd Merge pull request #41217 from zoecarver/cleanup-and-fixes-for-function-templates 2022-02-12 14:10:41 -08:00
zoecarver
83e465f763 Move up the Windows hack and fix it for static members/inits. 2022-02-11 20:51:11 -08:00
zoecarver
4cb1704b0e [nfc] Clang format the PR. 2022-02-11 13:57:31 -08:00
zoecarver
6da5e7ee06 [cxx-interop] Fix (U)Int substituted params on Windows.
Re-write the parameter list to use (U)Int if that was one of the substituted parameters. This is required because Windows will incorrectly map Int -> long long -> Int64.
2022-02-11 13:57:04 -08:00
Hamish Knight
aa6942e462 [CS] Simplify visitCoerceExpr slightly
Remove the unused `choice` parameter.
2022-02-10 19:06:47 +00:00
Pavel Yaskevich
f1e602f98d Merge pull request #41189 from xedin/trailing-closures-with-callAsFunction
[ConstraintSystem] Match trailing closures to implicit `.callAsFunction` when necessary
2022-02-08 17:44:55 -08:00
zoecarver
daceecfc75 [cxx-interop] Remove generateSpecializedCXXFunctionTemplate and just import the decl directly.
1) This unifies two code paths that do essentially the same thing.
2) This fixes how we import certain types (such as return types).
2022-02-04 16:18:02 -08:00
Anthony Latsis
91bd2b1803 AST: Remove OpenedArchetypeType::getOpenedExistentialType()
Clients can explicitly ask for the opened existential type on the archetype's generic environment,
or use `getExistentialType` to obtain a specific archetype's upper bounds.
2022-02-04 16:22:50 +03:00
Pavel Yaskevich
36c02e4291 [CSApply] Implement support for implicit .callAsFunction after init with trailing closures
Situations like `T(...) { ... }` where `T` is a callable type
and trailing closure belongs to `.callAsFunction` should be
rewritten as `T.init().callAsFunction { ... }`.
2022-02-03 15:36:18 -08:00
Slava Pestov
aa67c8bf8a Parametrized => parameterized 2022-02-03 13:27:24 -05: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
Pavel Yaskevich
48ffeddbc7 [CSApply] Result coercion check should always use resolved type
`shouldCoerceToContextualType` used `solution.getType(ASTNode)`
which returns a type that has type variables in it. To properly
check whether result type needs a coercion it has to be resolved
first which is done via `solution.getResultType(ASTNode)`.

Resolves: rdar://88285682
2022-01-31 14:42:27 -08:00
Slava Pestov
e7e536705e AST: Introduce ParametrizedProtocolType 2022-01-26 00:11:38 -05: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
Doug Gregor
452eccab83 Remove NestedArchetypeType.
Nested archetypes are represented by their base archetype kinds (primary,
opened, or opaque type) with an interface type that is a nested type,
as represented by a DependentMemberType. This provides a more uniform
representation of archetypes throughout the frontend.
2022-01-14 21:28:21 -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
Evan Wilde
89af8c9d24 Removing unused ctx variable
Cleaning up build warning for unused ctx variable in CSApply.cpp.
2022-01-11 09:16:09 -08:00
Doug Gregor
7a37ad3105 Ensure that we set opaque type substitutions on local variables.
We were never setting these opaque type substitutions, but code
generation was silently failing. Now we assert, so move the code into
the proper common location so we always set opaque type substitutions
on properties.

Fixes rdar://86800325.
2022-01-10 15:32:42 -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
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
bbdbb47b25 [CSApply] Switch delayed closure processing to use participatesInInference
Instead of checking the frontend flag directly, let's use dedicated
method on a constraint system to determine whether closure did
participate in inference or not.
2021-12-25 17:08:58 -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
Robert Widmann
0471e2c58e Implement Type Sequence Parameter Opening
The heart of this patchset: An inference scheme for variadic generic functions and associated pack expansions.

A traditional variadic function looks like

func foo<T>(_ xs: T...) {}

Along with the corresponding function type

<T>(T [variadic]) -> Void

which the constraint system only has to resolve one two for. Hence it opens <T> as a type variable and uses each argument to the function to try to solve for <T>. This approach cannot work for variadic generics as each argument would try to bind to the same <T> and the constraint system would lose coherency in the one case we need it: when the arguments all have different types.

Instead, notice when we encounter expansion types:

func print<T...>(_ xs: T...)
print("Macs say Hello in", 42, "different languages")

We open this type as

print : ($t0...) -> ($t0...)

Now for the brand new stuff: We need to create and bind a pack to $t0, which will trigger the expansion we need for CSApply to see a coherent view of the world. This means we need to examine the argument list and construct the pack type <$t1, $t2, $t3, ...> - one type variable per argument - and bind it to $t0. There's also the catch that each argument that references the opened type $t0 needs to have the same parallel structure, including its arity. The algorithm is thus:

For input type F<... ($t0...), ..., ($tn..) ...> and an apply site F(a0, ..., an) we walk the type `F` and record an entry in a mapping for each opened variadic generic parameter. Now, for each argument ai in (a0, ..., an), we create a fresh type variable corresponding to the argument ai, and record an additional entry in the parameter pack type elements corresponding to that argument.

Concretely, suppose we have

func print2<T..., U...>(first: T..., second: U...) {}
print2(first: "", 42, (), second: [42])

We open print2 as

print2 : ($t0..., $t1...) -> Void

And construct a mapping

  $t0 => <$t2, $t3, $t4> // eventually <String, Int, Void>
  $t1 => <$t5> // eventually [Int]

We then yield the entries of this map back to the solver, which constructs bind constraints where each => exists in the diagram above. The pack type thus is immediately substituted into the corresponding pack expansion type which produces a fully-expanded pack type of the correct arity that the solver can actually use to make forward progress.

Applying the solution is as simple as pulling out the pack type and coercing arguments element-wise into a pack expression.
2021-12-16 01:16:45 -08:00
Robert Widmann
8ac8633f9d [NFC] Hide VarargExpansionExpr's Constructor 2021-12-16 01:16:45 -08:00
Holly Borla
69be7b17fc Merge pull request #40282 from hborla/existential-any
[SE-0335] Introduce existential `any`
2021-12-10 08:56:03 -08:00
Holly Borla
445a856652 [Type System] Introduce a dedicated type to represent existential types.
The new type, called ExistentialType, is not yet used in type resolution.
Later, existential types written with `any` will resolve to this type, and
bare protocol names will resolve to this type depending on context.
2021-12-09 23:14:50 -08:00
Richard Wei
05363cd55a Regex literal runtime plumbing.
- Frontend: Implicitly import `_StringProcessing` when frontend flag `-enable-experimental-string-processing` is set.
- Type checker: Set a regex literal expression's type as `_StringProcessing.Regex<(Substring, DynamicCaptures)>`. `(Substring, DynamicCaptures)` is a temporary `Match` type that will help get us to an end-to-end working system. This will be replaced by actual type inference based a regex's pattern in a follow-up patch (soon).
- SILGen: Lower a regex literal expression to a call to `_StringProcessing.Regex.init(_regexString:)`.
- String processing runtime: Add `Regex`, `DynamicCaptures` (matching actual APIs in apple/swift-experimental-string-processing), and `Regex(_regexString:)`.

Upcoming:
- Build `_MatchingEngine` and `_StringProcessing` modules with sources from apple/swift-experimental-string-processing.
- Replace `DynamicCaptures` with inferred capture types.
2021-12-09 16:05:34 -08:00
Hamish Knight
37f16520e6 Prototype regex literal AST and emission
With `-enable-experimental-string-processing`,
start lexing `'` delimiters as regex literals (this
is just a placeholder delimiter for now). The
contents of which gets passed to the libswift
library, which can return an error string to be
emitted, or null for success.

The libswift side isn't yet hooked up to the Swift
regex parser, so for now just emit a dummy
diagnostic for regexes starting with quantifiers.

If successful, build an AST node which will be
emitted as an implicit call to an
`init(_regexString:)` initializer of an in-scope
`Regex` decl (which will eventually be a known
stdlib decl).
2021-12-06 21:16:14 +00:00
Pavel Yaskevich
0e6e058e7c [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-12-03 10:54:07 -08:00
swift-ci
1b6ec37b06 Merge pull request #40363 from zoecarver/fix-mangler-leaks 2021-12-01 21:04:39 -08:00