Commit Graph

1476 Commits

Author SHA1 Message Date
Sima Nerush
5832181077 Sema 2023-12-03 21:51:22 -08:00
Pavel Yaskevich
316e8f9366 [CSGen] Any type vars in subscript index arguments should be connected to a key path type
This is currently limited to `InferSendableFromCaptures` feature
but a valid thing to do in general because otherwise it won't be
possible to determine the sendability of the key path type.
2023-11-28 13:02:17 -08:00
Pavel Yaskevich
1f42585fdd [AST] NFC: Rename KeyPathExpr::{get, set}RootType to {get, set}ExplicitRootType 2023-11-28 13:01:43 -08:00
Pavel Yaskevich
7877c0703c [CSGen] Fix one last incorrect key path locator use
Optional chaining is related to the key path value and not the
key path type itself.
2023-11-09 21:31:28 -08:00
Pavel Yaskevich
62d15cda1b [CSGen] Use a correct locator for subtype between explicit and inferred root types
Covariant conversions between explicit and contextual/inferred root
types of a key path are simulated through a subtype constraint which
has to be attached to a `KeyPathRoot` element.
2023-11-01 09:15:14 -07:00
Pavel Yaskevich
161e126a91 [CSDiagnostics] Diagnose a key path literal without components early
Detect that diagnose key path literals that do not have any components.
2023-11-01 09:15:14 -07:00
Pavel Yaskevich
4ace882c12 [ConstraintSystem] Introduce a fallback type for key path literals
This is a very first step in attempt to move some of the logic
from `simplifyKeyPathConstraint` to the inference. This type is
going to be used as an anchor to trigger capability inference.
2023-11-01 09:14:15 -07:00
Doug Gregor
4b3d2f47d9 [Typed throws] Handle function conversions involving different thrown errors
Teach the constraint solver about the subtyping rule that permits
converting one function type to another when the effective thrown error
type of one is a subtype of the effective thrown error type of the
other, using `any Error` for untyped throws and `Never` for
non-throwing.

With minor other fixes, this allows us to use typed throws for generic
functions that carry a typed error from their arguments through to
themselves, which is in effect a typed `rethrows`:

```swift
func mapArray<T, U, E: Error>(_ array: [T], body: (T) throws(E) -> U)
throws(E) -> [U] {
  var resultArray: [U] = .init()
  for value in array {
    resultArray.append(try body(value))
  }
  return resultArray
}
```
2023-10-05 11:55:05 -07:00
Pavel Yaskevich
49a63fcc7d Merge pull request #68787 from xedin/separate-solving-for-for-loop-condition
[ConstraintSystem] Solve `where` clauses of for-in loops separately
2023-10-02 09:22:53 -07:00
Pavel Yaskevich
3ca54ae9da [CSGen] Use OptionalObject constraint to connect element type and result of next()
Previously this wasn't possible because of `one-way` bind constraints
involved in pattern inference but this no longer the case.
2023-09-29 17:33:07 -07:00
Pavel Yaskevich
72caaa8cfe [ConstraintSystem] Solve where clauses of for-in loops separately
Doing so fits better into conjunction model which leads to more
granular control over what variables are brought into scope during
`where` clause expression checking.

These changes also remove "one-way bind" flag from "for-in" statement
target.
2023-09-29 13:28:46 -07:00
Doug Gregor
7f82b2a9aa [Typed throws] Enable checking of thrown types on closures.
Enable typed throws on explicit closures, either due to contextual
type information or due to explicit specification.
2023-09-29 10:51:54 -07:00
Pavel Yaskevich
204d496c36 Merge pull request #63889 from xedin/cache-conformance-checks-in-cs
[ConstraintSystem] Add cache for conformance lookups
2023-09-28 14:00:17 -07:00
Pavel Yaskevich
32d265154f [ConstraintSystem] Add cache for conformance lookups
`lookupConformance` request is not cached and constraint solver
performs a lot of them for the same type (i.e. during disjunction
solving), let's try to cache previously performed requests to
see whether additional memory use is worth the performance benefit.
2023-09-20 00:59:22 +01:00
Ben Barham
041691184c [CMake] Replace early swift-syntax with FetchContent
Use FetchContent to include swift-syntax directly in swift. This can be
thought of as an `add_subdirectory` for a directory outside the root.

The default build directory will be `_deps/swiftsyntax-subbuild/`, though
the modules and shared libraries will be built in `lib/swift/host` by
passing down `SWIFT_HOST_LIBRARIES_DEST_DIR` to avoid copying them as we
were doing previously.
2023-09-18 14:44:10 -07:00
Hamish Knight
0760290387 [CS] Connect conjunctions with ReturnStmts to return type
Augment the TypeVarRefCollector such that it
picks up any type variables present in the result
type for a closure DeclContext when visiting a
ReturnStmt. This ensures we correctly handle
if/switch expressions that contain `return`
statements.

rdar://114402042
2023-09-05 21:25:05 +01:00
Hamish Knight
cc507fc74b [CS] NFC: Rename VarRefCollector -> TypeVarRefCollector 2023-09-05 21:25:03 +01:00
Pavel Yaskevich
d0a3082b06 Merge pull request #68254 from xedin/issue-67815
[PreCheck] Filter out macro declarations from result set
2023-09-01 16:22:09 -07:00
Hamish Knight
6ee44f09b4 Introduce then statements
These allow multi-statement `if`/`switch` expression
branches that can produce a value at the end by
saying `then <expr>`. This is gated behind
`-enable-experimental-feature ThenStatements`
pending evolution discussion.
2023-09-01 14:32:14 +01:00
Pavel Yaskevich
cf257aa64a [PreCheck] Filter out macro declarations from result set
Move logic from `ConstraintGenerator::visitOverloadedDeclRefExpr`
to pre-check to avoid including macro declarations referenced
without `#`. This means that pre-checking would synthesize
`TypeExpr` in situations when there is a type reference that
is shadowed by a stdlib macro.

Resolves: https://github.com/apple/swift/issues/67815
Resolves: rdar://114796811
2023-08-31 17:30:14 -07:00
Pavel Yaskevich
f1f74f0b0b Merge pull request #67663 from amritpan/kp-root-value-tv-inference
[ConstraintSystem] Allow inference to bind AnyKeyPath as a KeyPath
2023-08-03 09:36:32 -07:00
Amritpan Kaur
e8425bf4c8 [CSGen] Update LocatorPathElt::KeyPathType usage. 2023-08-01 09:18:15 -07:00
Amritpan Kaur
9e4f23b7c2 [CSGen] Update Constraint Locator for KeyPathValue 2023-07-27 12:48:09 -07:00
Hamish Knight
a64ba23d7b [CS] NFC: Store ContextualTypeInfo in SyntacticElementTarget
Move the contextual type locator onto
ContextualTypeInfo, and consolidate the separate
fields in SyntacticElementTarget into storing a
ContextualTypeInfo. This then lets us plumb down
the locator for the branch contextual type of an
if/switch expression from the initial constraint
generation, rather than introducing it later. This
should be NFC.
2023-07-26 16:46:54 +01:00
Pavel Yaskevich
59908eeb0d [CSGen] Suppress favoring in presence of non-disfavored variadic generic overloads
Since this type of early favoring checks number of arguments and matches
labels it would always favor non-variadic overloads which is incorrect.
2023-07-20 10:02:59 -07:00
Hamish Knight
067583a1e3 [CS] Merge VarRefCollector & UnresolvedVarCollector
These now do basically the same thing, merge their
implementations.
2023-07-17 17:32:47 +01:00
Sophia Poirier
fa41015ae4 [ConstraintSystem] implement implicit pack materialization for abstract tuples instead of explicit '.element' 2023-07-14 10:32:38 -07:00
Pavel Yaskevich
62719b05ac Merge pull request #67157 from amritpan/kp-declcontext
[Constraint System] Store key path root, value, and decl context for use across constraint system.
2023-07-10 23:09:09 -07:00
Amritpan Kaur
1d8e7ef2fe [CSGen] Record keypath for use across constraint system. 2023-07-10 15:54:22 -07:00
Pavel Yaskevich
77bd8ad746 [CSGen/SyntaticElement] Remove logic that connects tap expressions to closures
Both single- and multi-statement closures now use variable reference
collector to identify variables used in the interpolation body, which
means that it's not longer necessary to connect to the closure explicitly
(if interpolation is contained in one).
2023-07-07 19:50:46 +02:00
Pavel Yaskevich
2fd5b5fb04 [CSGen] Interpolations: Collect all vars referenced in the body to preserve connection to context
Since "tap" bodies are now type-checked together with the context,
it's imperative that the variable that represents an interpolation
never gets disconnected from its context in the constraint system,
otherwise it wouldn't be possible to determine types for the
references.
2023-07-07 19:50:46 +02:00
Pavel Yaskevich
ac6ea62cec [CSGen] NFC: Extract var reference collector to be used in more places 2023-07-07 19:50:46 +02:00
Pavel Yaskevich
e337a3947d [ConstraintSystem] Implement tap expression checking in the solver
Generate a conjunction for each tap expression body as soon as it
gets a contextual type instead of separate post-factum type-checking
via `typeCheckTapBody`.
2023-07-07 19:50:46 +02:00
Amritpan Kaur
dab7dbfdfd [NFC] Correct variable names 2023-07-07 08:47:40 -07:00
Sophia Poirier
63e30b5525 [Variadic Generics] add tracking of pack environments for pack elements to Constraint System 2023-07-06 13:44:10 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Pavel Yaskevich
199616a49b Merge pull request #66708 from xedin/generalize-default-closure-type-constraint
[ConstraintSystem] Use fallback type constraint to default pack expansion
2023-06-19 00:47:18 -07:00
Pavel Yaskevich
2ee646f47b [ConstraintSystem] NFC: Generalize DefaultClosureType constraint
This constraint is useful in more places than closures because
it gives a way to provide a "fallback type" without it having
effect on inference.
2023-06-16 11:15:15 -07:00
Hamish Knight
f10d0df62f Merge pull request #66593 from hamishknight/pattern-error 2023-06-16 18:06:47 +01:00
Holly Borla
612a2e79fa Merge pull request #66657 from hborla/variadic-macro-specialization
[ConstraintSystem] Use an explicit generic argument constraint for `UnresolvedSpecializeExpr`.
2023-06-15 09:35:25 -07:00
Holly Borla
d357315661 [ConstraintSystem] Handle typealiases in ExplicitGenericArguments
constraint simplification.
2023-06-15 00:27:18 -07:00
Holly Borla
eb858d938e [ConstraintSystem] Use an explicit generic argument constraint for
UnresolvedSpecializeExpr.

The eager generic argument matching code in CSGen for unresolved
specializations was incorrect because it didn't account for parameter
packs. Fortunately, a constraint to delay explicit generic argument
matching already exists for macros. Use it here.
2023-06-14 18:24:58 -07:00
Hamish Knight
c6dd3ad839 [CS] Diagnose UnresolvedPatternExprs as part of constraint solving
Instead of diagnosing in CSApply, let's create a
fix and diagnose in the solver instead.
Additionally, make sure we assign ErrorTypes to
any VarDecls bound by the invalid pattern, which
fixes a crash.

rdar://110638279
2023-06-13 12:14:25 +01:00
Hamish Knight
762cd4d10e Merge pull request #66565 from hamishknight/connect-two 2023-06-13 08:45:53 +01:00
Hamish Knight
1616e167b9 [CS] Walk into patterns when looking for closure referenced vars
Previously we would skip over ExprPatterns, but
we need to ensure that we walk them, as they may
have interesting variable references that the
closure needs to be connected to the type
variables for.

rdar://110617471
2023-06-12 18:27:24 +01:00
Holly Borla
393b4ceb95 [NameLookup] Move macro-related name lookup operations into the namelookup
namespace.

This moves the `isInMacroArgument` predicate and `lookupMacros` into `namelookup`.
ASTScope still encapsulates the scope tree and contains the operation to lookup
the enclosing macro scope, which then invokes a callback to determine whether a
potential macro scope is indeed a macro, because answering this question requires
name lookup.
2023-06-11 23:10:43 -07:00
Holly Borla
2943064511 [Macros] Always exclude macro expansions in TypeChecker::lookupMacros. 2023-06-11 23:09:47 -07:00
Hamish Knight
b43d351197 [CS] Improve diagnostics a bit for pattern mismatch
There's still plenty of more work to do here for
pattern diagnostics, including introducing a
bunch of new locator elements, and handling things
like argument list mismatches. This at least lets
us fall back to a generic mismatch diagnostic.
2023-06-07 00:35:02 +01:00
Hamish Knight
7a137d6756 [CS] Allow ExprPatterns to be type-checked in the solver
Previously we would wait until CSApply, which
would trigger their type-checking in
`coercePatternToType`. This caused a number of
bugs, and hampered solver-based completion, which
does not run CSApply. Instead, form a conjunction
of all the ExprPatterns present, which preserves
some of the previous isolation behavior (though
does not provide complete isolation).

We can then modify `coercePatternToType` to accept
a closure, which allows the solver to take over
rewriting the ExprPatterns it has already solved.

This then sets the stage for the complete removal
of `coercePatternToType`, and doing all pattern
type-checking in the solver.
2023-06-07 00:35:01 +01:00
Hamish Knight
2a3b15c3e8 [CS] Remove null Type handling from getTypeForPattern
We should never CSGen a null Type for patterns.
2023-06-07 00:35:01 +01:00