Commit Graph

2383 Commits

Author SHA1 Message Date
Pavel Yaskevich
9a395f091e [CSSimplify] Merge binding inference logic with PackElementOf constraint simplification
It's only safe to infer element type from `PackElementOf` constraint
when pattern type is fully resolved (because it can have pack element
archetypes which should be mapped out of context), the same applies
to the pattern type inference as well. Since constraints are re-activated
every time a referenced type variable is bound, simplication logic
can act as inference source by decaying into `Equal` constraints
where pattern/element type are resolved via surrounding information
first.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
fd060f5dde [ConstraintSystem] Add a locator to openType and some of its callers
`openType` didn't need a locator before it was simply replacing generic
parameters with corresponding type variables but now, with opening of
pack expansions types, a locator is needed for pack expansion variables.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
ed23aec47e [ConstraintSystem] Record opened pack expansion type in the locator element 2023-05-02 09:32:19 -07:00
Pavel Yaskevich
cc2b6ceefa [CSSimplify] Type matching should maintain tuples when matching types with pack expansions
Pack expansion flattening could result in lose of tuple structure
or introduce a single element tuples (which are effectively parens),
to aid in matching `matchTypes` should introduce/maintain tuples on
both sides of the comparison until the structure is known.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
2ebade1b4f [CSSimplify] Delay matching if one of the types is a tuple with unresolved pack expansions
This handles situations like `Int <conv> (Int, (_: $T_exp)` or
`String arg conv (_: $T_exp)`. The matching has to be delayed
because the structure of the tuple type is not known until `$T_exp`
(which represents a pack expansion type) is resolved.
2023-05-02 09:31:57 -07:00
Pavel Yaskevich
07387803fe [CSSimplify] Introduce containsPackExpansionType that handles pack expansion variables
`TypeBase::constainsPackExpansionType` cannot handle this because
it leaves in AST.
2023-05-02 09:31:57 -07:00
Pavel Yaskevich
b0abb7f034 [CSSimplify] Delay .element lookup if pack expansion is not yet resolved 2023-05-02 09:31:57 -07:00
Pavel Yaskevich
d7c320821d [CSSimplify] Handle presence of pack expansion if tuples and shape constraints
Tuple types cannot be matched until all pack expansion variables
are resolved, the same is applicable to `shape of` constraints
because the structure of the pack is not fully resolved in such
cases.
2023-05-02 09:31:57 -07:00
Pavel Yaskevich
5340725df2 [CSSimplify] Prevent merging of type expansion type variables
Even if both sides are pack expansion variables they cannot be
merged because merging of them means merging of pattern and
shape types as well, which is easier to do when one of both
sides are bound.
2023-05-02 09:31:57 -07:00
Pavel Yaskevich
c130351634 [ConstraintSystem] Implement pack expansion variable resolution
A pack expansion type variable gets bound to its original pattern
type when solver can infer a contextual type for it. This makes
sure that pack expansion types are always matched via `matchTypes`
without `simplifyType` involvement which can expand them prematurely.
2023-05-02 09:31:57 -07:00
Pavel Yaskevich
ca534ef60a [ConstraintSystem] Handle presence of pack expansion type variables while matching 2023-05-02 09:31:57 -07:00
Pavel Yaskevich
6437531dd7 [ConstraintSystem] Introduce TVO_PackExpansion
This flag indicates that a type variable could only be bound
to PackExpansionType and nothing else.
2023-05-02 09:31:56 -07:00
Pavel Yaskevich
5c2f77c5dc [AST] PackMatching: Make pack expansion type check pluggable
The constraint solver would supply its own version of the function
that can check for pack expansion type variables as well.
2023-05-02 09:31:56 -07:00
Pavel Yaskevich
dba1b3436a [CSBindings] Diagnose leading-dot inference failure when base type is attempted as placeholder
Diagnose base inference failure only if base gets inferred to be
a placeholder, any transitive placeholder inference points to the
problem being elsewhere.
2023-05-01 14:19:14 -07:00
Pavel Yaskevich
8a26df8166 [ConstraintSystem] NFC: Standardize the way of type holefication
Introduce `ConstraintSystem::recordTypeVariablesAsHoles` as a
standard way to record that unbound type variables found in a
type are holes in the given constraint system.
2023-04-21 15:22:13 -07:00
Pavel Yaskevich
70eeccb3cc [CSSimplify] Fix attempt to rebind type variables in sequence element pattern 2023-04-21 11:45:20 -07:00
Holly Borla
0ed4b3325f [SE-0393] Enable parameter packs for generic functions.
The VariadicGenerics flag still exists and is required for generic types
with parameter packs
2023-04-15 15:40:05 -07:00
Pavel Yaskevich
16190b0256 Merge pull request #64955 from xedin/external-wrapper-check-should-use-param-idx
[CSSimplify] Fix external property wrapper check to use parameter index
2023-04-06 00:25:15 -07:00
Pavel Yaskevich
0b41ea84b9 [CSSimplify] Fix external property wrapper check to use parameter index
Both `ParameterListInfo` and `getParameterAt` expect parameter index
because they operate on a declaration referenced by a call.
2023-04-05 15:53:06 -07:00
Pavel Yaskevich
c43c970001 Merge pull request #64931 from xedin/rdar-100369066
[Diagnostics] Forgo all restrictions if one side of conversion has in…
2023-04-05 13:31:11 -07:00
Pavel Yaskevich
9816fffcd3 [Diagnostics] Forgo all restrictions if one side of conversion has invalid use of &
None of the restrictions like existential conversion or optional
promotion would actually match, so there is no point in trying them
just to add more unrelated fixes.

Resolves: rdar://100369066
2023-04-04 18:00:13 -07:00
Pavel Yaskevich
3714c48af0 [CSSimplify] Prevent invalid pack references from causing infinite loops
When `matchTypesBindTypeVar` detects invalid pack reference it should
reset the type to a placeholder and allow binding to go through,
otherwise, if binding comes from inference, returning without binding
would create an infinite loop because type variable would perpetually
be the best choice to attempt.
2023-04-04 09:57:31 -07:00
Pavel Yaskevich
b07dafab0f [CSSimplify] Exploaded tuple argument should assume param's label if first element doesn't have one
If tuple doesn't have a label for its first element
and parameter does, let's assume parameter's label
to aid argument matching. For example:

```swift
func test(val: Int, _: String) {}

test(val: (42, "")) // expands into `(val: 42, "")`
```

Resolves: rdar://106775969
2023-03-29 10:49:05 -07:00
Pavel Yaskevich
736fc8b375 Merge pull request #64608 from xedin/pack-expansion-param-in-init
[ConstraintSystem] Fix support for a single pack expansion parameter …
2023-03-28 09:32:58 -07:00
Pavel Yaskevich
e968ea1b48 Merge pull request #64591 from xedin/rework-tuple-with-pack-matching
[AST] PackExpansionMatcher: use common prefix/suffix algorithm for tuple matching
2023-03-27 13:12:30 -07:00
Pavel Yaskevich
24625bb809 [ConstraintSystem] Fix support for a single pack expansion parameter in init references
In Swift 5 and earlier initializer references are handled in a special
way that uses a type variable to represent a type of the parameter
list. Such type variables should be allowed to bind to a pack expansion
type to support cases where initializer has a single unlabeled variadic
generic parameter - `init(_ data: repeat each T)`.
2023-03-24 09:03:19 -07:00
Pavel Yaskevich
65cfef6555 [CSSimplify] Account for the fact that variadic generic parameters could be specialized
If generic parameter comes from a variadic type declaration it's
possible that it got specialized early and is no longer represented
by a pack expansion type. For example, consider expression -
`Test<Int>(42)` where `Test<each T>` and the initializer
is declared as `init(_: repeat each T)`. Although declaration
based information reports parameter at index 0 as variadic generic
the call site specializes it to `Int`.

Resolves: rdar://107151854
2023-03-23 21:25:29 -07:00
Pavel Yaskevich
1b42e55039 [CSSimplify] NFC: Refactor TupleMatcher to consolidate pack expansion matching 2023-03-23 15:07:28 -07:00
Pavel Yaskevich
b4937be253 [CSSimplify] Don't propagate contextual parameter type if it's a pack expansion
Pack expansion types are handled by matching logic, optimization
that attempts to propagate types into the body of the closure should
consider them unsuitable even if the pack expansion matches a single
parameter.
2023-03-22 09:10:23 -07:00
Holly Borla
da3079de05 [ConstraintSystem] Diagnose pack expansion expressions in non-variadic contexts. 2023-03-21 21:58:05 -07:00
Holly Borla
c60b4860ab Merge pull request #64498 from hborla/diagnose-pack-outside-expansion-expr
[ConstraintSystem] Enforce `TVO_CanBindToPack`, and diagnose pack references outside of pack expansion expressions.
2023-03-21 14:00:52 -07:00
Alex Hoppen
9bd880ddde Merge pull request #64405 from ahoppen/ahoppen/fix-locator-simplification
[CS] Fix locator simplification with 'Member' path element
2023-03-21 09:19:14 -07:00
Holly Borla
cb19fc3a71 [ConstraintSystem] Enforce TVO_CanBindToPack, and diagnose pack references outside
of pack expansion expressions.
2023-03-20 20:13:48 -07:00
Alex Hoppen
cebc084367 [CS] Fix locator simplification with 'Member' path element
Previously, a `Member` path element in a `ConstraintLocator` was simplified to the base on which the member was accessed. This is incorrect.
2023-03-20 09:19:21 -07:00
Pavel Yaskevich
ff51291d30 [CSSimplify] Don't enable OneWayBindParam for result builder transformed closures
This means two things:

- transformed closures behave just like regular multi-statement closures
- It's now possible to pass partially resolved parameter types into
  the closure which helps with diagnostics.
2023-03-16 16:16:57 -07:00
Luciano Almeida
4d981e3c0b Merge pull request #63904 from LucianoPAlmeida/ambiguous-overload
[Sema] Diagnose function coercion ambiguity
2023-03-15 23:18:00 -03:00
Pavel Yaskevich
a7fbc04307 [CSSimplify] PointerToCPointer: Allow element type inference for "argument" pointers
If the type of the "argument" pointer is not yet resolved the solver
should equate it to that of the "parameter" pointer as regular
conversions do.
2023-03-14 15:06:24 -07:00
Pavel Yaskevich
e4eb271359 [CSSimplify] Implement ArrayToCPointer conversion
Extends implementation to support conversions between arrays
and pointers that are allowed by the SE-0324 proposal.

Resolved: rdar://99089335
2023-03-14 14:36:52 -07:00
Pavel Yaskevich
438ee330f1 [ConstraintSystem] Add new conversion kind - ArrayToCPointer
It's `ArrayToPointer` conversion that has PointerToCPointer
semantics for (un-)signed integer element types.
2023-03-14 11:22:52 -07:00
Hamish Knight
c77caf2c72 [CS] NFC: Simplify SyntacticElementTarget construction for ExprPattern 2023-03-14 11:56:01 +00:00
Pavel Yaskevich
c106705883 [CSSolver] NFC: Replace some uses of last() with endsWith() 2023-03-13 11:24:15 -07:00
Luciano Almeida
d5a9d6ab83 [ConstraintSystem] Introduce new locator path elemet to identify coercion 2023-03-11 18:47:53 -03:00
Pavel Yaskevich
a52800031a Merge pull request #64259 from xedin/variadic-matching-fix
[CSSimplify] Variadics: Allow unlabeled matching after pack expansion…
2023-03-10 08:06:46 -08:00
Pavel Yaskevich
36200101f8 [CSSimplify] Variadics: Allow unlabeled matching after pack expansion argument
Remove a bit of logic from `matchCallArgumentsImpl` that prevented
unlabeled argument matching after pack expansion argument until next
labeled argument because it incorrectly assumed that it represents
variadic forwarding.
2023-03-09 15:25:43 -08:00
Pavel Yaskevich
70c370d0b6 [CSSimplify] Propagate contextual result type into result builder transformed closure
Propagate fully or partially resolved contextual type down into
the body of result builder transformed closure by eagerly binding
intermediate body result type to the contextual one. This helps to
determine when closure body could be solved early.

Resolves: rdar://106364495
2023-03-08 10:46:02 -08:00
Holly Borla
df755d4f90 Merge pull request #64192 from xedin/rdar-106364495
[CSSimplify] Propagate contextual result type into result builder tra…
2023-03-08 09:53:38 -08:00
Hamish Knight
fdc9817471 Merge pull request #64174 from hamishknight/pattern-requests 2023-03-08 10:52:40 +00:00
Pavel Yaskevich
3ff03469e7 [CSSimplify] Propagate contextual result type into result builder transformed closure
Propagate fully or partially resolved contextual type down into
the body of result builder transformed closure by eagerly binding
intermediate body result type to the contextual one. This helps to
determine when closure body could be solved early.

Resolves: rdar://106364495
2023-03-07 16:17:10 -08:00
Hamish Knight
98855963ac [Sema] Introduce EnumElementExprPatternRequest
This is needed to ensure we only ever synthesize
a single unique ExprPattern when solving an
EnumElementPattern that we failed to lookup a
member for.
2023-03-07 15:16:36 +00:00
Hamish Knight
37b0a6074f [Sema] Introduce ExprPatternMatchRequest
This replaces `synthesizeTildeEqualsOperatorApplication`,
and synthesizes the match expression and var
on-demand.

Additionally, it pushes the lookup logic into
pre-checking.
2023-03-07 15:16:35 +00:00