Commit Graph

1945 Commits

Author SHA1 Message Date
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
zoecarver
3bb9d43ead [nfc] Fix two leaks where clang::ManglerContext was not cleaned up. 2021-12-01 17:13:24 -08:00
Pavel Yaskevich
bc54bc6bb7 Revert "[TypeChecker] SE-0326: Enable multi-statement closure inference by default" 2021-11-29 17:26:08 -08:00
Robert Widmann
1faf7edbd6 Merge pull request #40193 from CodaFi/sequential-access-patterns
Model Sequence Archetypes
2021-11-17 13:19:31 -08:00
Pavel Yaskevich
63f355fde0 Merge pull request #39989 from xedin/more-diag-improvements-for-multi-stmt-closures
[TypeChecker] SE-0326: Enable multi-statement closure inference by default
2021-11-17 10:14:58 -08:00
Robert Widmann
e7e11df927 Model Sequence Archetypes 2021-11-16 11:38:57 -08:00
Hamish Knight
551604a456 Merge pull request #40183 from hamishknight/the-path-less-traveled 2021-11-16 14:14:53 +00:00
Pavel Yaskevich
83033198c3 [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-11-15 16:42:05 -08:00
Hamish Knight
76c6254779 [AST] Refactor KeyPathExpr constructors
Now that the CSApply just uses components, we can
better split up the key path constructors to either
accept a set of resolved components, or a parsed
root or path.
2021-11-15 12:25:18 +00:00
Hamish Knight
d6ac93efb1 [CS] Don't set parsed paths for dynamic member key paths
The logic here could form AST loops due to passing
in `anchor` for the key path's parsed path.

However setting a parsed path here seems to be a
holdover from the CSDiag days, so set the path to
`nullptr` and rip out and the rest of the synthesis
and SanitizeExpr logic for it.

rdar://85236369
2021-11-15 12:25:17 +00:00
Hamish Knight
15098e22bd [AST] Rename resolveComponents -> setComponents
The naming here has bothered me for a little while
now, it just sets the components, it doesn't
resolve anything.
2021-11-15 12:25:17 +00:00
Pavel Yaskevich
fc0f05f416 [TypeChecker] Implement distributed thunk identification in expr rewritter
This logic cannot live in `ActorIsolationChecker` because
all of the relevant information is only accessible through
constraint system while applying solutions.
2021-11-12 21:15:59 -08:00
Pavel Yaskevich
115af99031 [CSApply] Mark call to distributed thunks as implicitly throwing
Use newly added `isDistributedThunk` check to determine whether
the given call is to a remote distributed actor and if so mark
it as implicitly throwing.

Resolves: rdar://83610106
2021-11-12 12:41:30 -08:00
Pavel Yaskevich
0b4ec1c31e [ConstraintSystem] Add accessor to check whether target represents async let init 2021-11-12 12:41:29 -08:00
Robert Widmann
658de80ca8 Merge pull request #39627 from CodaFi/the-replacements
Suggest Replacement Types for Invalid Placeholders
2021-11-10 16:23:29 -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
Robert Widmann
1fe2e4b3e7 Relax Restrictions on Placeholder Types Appearing in Interface Types
These restrictions are meant to keep placeholder types from escaping TypeCheckType. But there's really no harm in that happening as long as we diagnose it on the way out in the places it's banned. (We also need to make sure we're only diagnosing things in primaries, but that's a minor issue). The end result is that we lose information because a lot of the AST that has placeholders in it becomes filled with error types instead.

Lift the restriction on placeholders appearing in the interface type, teach the mangler to treat them as unresolved types, and teach serialization to treat them as error types.
2021-11-03 10:29:15 -07:00
Doug Gregor
3945ce0b1b Remove now-unused "unsafe sendable" and "unsafe main actor" closure bits. 2021-10-19 23:01:07 -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
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
Pavel Yaskevich
87f41a9b21 [CSClosure] Check multi-statement closure attrs only in if inference is enabled 2021-10-08 12:12:44 -07:00
Pavel Yaskevich
90876134f2 [CSApply] Set types for for all delayed closures 2021-10-08 10:08:03 -07:00
Pavel Yaskevich
8867a8d407 [CSApply] Delay solution application to multi-statement closure bodies
Let's delay solution application to multi-statement closure bodies,
because declarations found in the body of such closures may depend
on its `ExtInfo` flags e.g. no-escape is set based on parameter if
closure is used in a call.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
d5e9a39f65 [CSApply] Mark pattern target as fully verified 2021-10-08 10:08:03 -07:00
Pavel Yaskevich
e3aa37700e [ConstraintSystem] Allow default initialized patterns
Add support for pattern bindings with default initializable patterns
e.g. `let x: Int?`.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
99a9828a03 [ConstraintSystem] Add a new contextual purpose - CTP_CaseStmt 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
7197fac8f5 [ConstraintSystem] Add a new contextual purpose - CTP_ForEachSequence
A contextual purpose for a sequence expression associated with
`for-in` statement, that decays into a `ConformsTo` constraint
to a `Sequence` or `AsyncSequence` protocol.

Note that CTP_ForEachSequence is almost identical to CTP_ForEachStmt
but the meaning of latter is overloaded, so to avoid breaking solution
targets I have decided to add a new purpose for now.
2021-10-08 10:08:02 -07:00
swift-ci
b69eac9630 Merge pull request #39535 from guitard0g/tparam-not-used-in-fsig 2021-10-06 18:06:29 -07:00
Pavel Yaskevich
1c0a306c75 Merge pull request #37956 from xedin/implicit-swift-to-c-ptr-conversions
[TypeChecker] Implement limited set of conversions between Swift and C pointers
2021-10-06 14:35:22 -07:00
Josh Learn
f433ac2d58 Allow importing templated functions when template args do not appear
in the function signature by adding explicit metatype parameters to
the function signature.
2021-10-06 13:35:12 -07:00
Frederick Kellison-Linn
b433724735 Remove default argument from getContextualType 2021-10-05 17:04:54 -04:00
Hamish Knight
f045350266 [CS] Clean up dynamic member overload choice checking
Add `OverloadChoice::isAnyDynamicMemberLookup`,
and use it to clean up a couple of places.
2021-10-01 12:54:08 +01:00
Hamish Knight
3d4006e697 [CS] Fix locator for dynamic member key-path IUO unwrap
We need to be looking at the callee locator here
for the recorded IUO unwrap.

SR-15249
2021-10-01 12:54:07 +01:00
zoecarver
d14281f643 [cxx-interop] Allow function templates with defaulted template type parameters to be called.
If a defaulted template type parameter is not used in the function's
signature, don't create a corresponding generic argument for that
template type. This allows us to call function templates with defaulted
template type parameters. This is very common in the standard library
for things like enable_if which is used to disable various
functions/overloads with SFINAE.

The biggest part of this change is going forward not all function
templates will be imported as generic functions in Swift. This should
work OK but we may discover there was some logic which only looked for
generic function when dealing with function templates.
2021-09-22 11:11:16 -07:00
Pavel Yaskevich
3a41f7af5f [ConstraintSystem] Add a new conversion - Swift to C pointers
Following pointer conversions are supported in argument positions (when referencing C/ObjC functions):

- Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int>
- Unsafe[Mutable]Pointer<Int{8, 16, ...}> -> Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
2021-09-20 17:22:25 -07:00
Hamish Knight
3e28bbbd2c Update for review feedback
- Remove OriginalArguments in favor of storing the
pre-rewritten argument list, which simplifies things
nicely
- Adopt llvm::indexed_accessor_iterator
2021-09-01 18:40:29 +01:00
Hamish Knight
8c2b88abc0 [CS] Adopt ArgumentList
- Explicitly limit favoring logic to only handle
unary args, this seems to have always been the
case, but needs to be handled explicitly now that
argument lists aren't exprs
- Update the ConstraintLocator simplification to
handle argument lists
- Store a mapping of locators to argument lists
in the constraint system
- Abstract more logic into a getArgumentLocator
method which retrieves an argument-to-param locator
from an argument anchor expr
2021-09-01 18:40:25 +01:00
Pavel Yaskevich
1a756d1a50 Merge pull request #39079 from xedin/pb-application-improvements
[ConstraintSystem] Improvements to pattern binding checking
2021-08-30 15:52:57 -07:00
Rintaro Ishizaki
49547a5378 [NFC][Basic] Import llvm::isa_and_nonnull to 'swift' namespace
Just for convenicence.

* Replace `llvm::isa_and_nonnull` with imported `isa_and_nonnull`
* Repalce some `EXPR && isa<T>(EXPR)` with `isa_and_nonnull<T>(EXPR)`
2021-08-27 11:36:21 -07:00
Pavel Yaskevich
418e87ee06 [CSApply] Adjust flags for un-initialized pattern coercion
Coercion should be aligned with `PatternBindingEntryRequest::evaluate`
and that uses only one flag - `TypeResolverContext::PatternBindingDecl`.
2021-08-27 10:18:09 -07:00
Pavel Yaskevich
ac485b07ce [CSApply] Mark initializer as checked after it is set
Once coerced initializer expression has been set for
a particular pattern binding entry, let's mark it as
checked, otherwise decl checker might try to
re-typecheck it.
2021-08-27 10:16:26 -07:00
Pavel Yaskevich
9518286ca8 Merge pull request #38912 from xedin/generalize-unitialized-var-target
[ConstraintSystem] Generalize uninitialized wrapped variable targets
2021-08-23 13:20:55 -07:00
Frederick Kellison-Linn
ab7a95c18b [Sema] Delay function representation check until placeholders are bound 2021-08-19 14:53:33 -04:00
Pavel Yaskevich
c87e0e149a [ConstraintSystem] Implement uninitialized pattern binding support
This like `let x: Int` or `let (x, y), ...` are now supported by
the constraint solver.
2021-08-17 14:23:57 -07:00
Pavel Yaskevich
a72b057829 [ConstraintSystem] Generalize solution target to support un-initialized variables
Rename `uninitializedWrappedVar` kind to `uninitializedVar` and
allow storage of patterns and indices into pattern binding entry.
2021-08-17 14:23:57 -07:00