Commit Graph

2464 Commits

Author SHA1 Message Date
Pavel Yaskevich
dc4ee4b61a [CSSimplify] Allow converting tuple with pack expansions to Void for closure result
Maintains existing rule where single-expression closure is allowed
to convert to contextual function type with `Void` result.
2023-05-15 16:20:01 -07:00
Pavel Yaskevich
effa261266 [CSSimplify] Move invalid pack expansion check to enable closure result conversion
Checking for pack expansion type mismatch impedes non-Void-to-Void
conversion allowed for single-expression closures.

Resolves: rdar://108904190
2023-05-15 16:19:59 -07:00
Pavel Yaskevich
406b7a62c7 Merge pull request #65718 from xedin/diagnose-missing-each-in-expr-context
[ConstraintSystem] Detect and diagnose missing 'each' and provide a fix-it
2023-05-15 16:18:55 -07:00
Angela Laar
5c818b3fd4 [Constraint System] Fix covariant erasure for constrained existentials
Constrained existentials should be type erased to an upper bound that is dependent on other type parameters.
2023-05-15 16:17:59 -07:00
Pavel Yaskevich
181d2d1371 [CSSimplify] Allow conversions between tuples with pack expansions and Any
Don't attempt to wrap `Any` into a single-element tuple to match
against a tuple with pack expansions, this conversion would be
handled by existential promotion if it's allowed, otherwise it
would produce an error.

Resolves: rdar://109160060
2023-05-11 10:05:15 -07:00
Kavon Farvardin
3e4bc82aa8 rename _forget to discard; deprecate _forget
SE-390 concluded with choosing the keyword discard rather than forget for
the statement that disables the deinit of a noncopyable type. This commit
adds parsing support for `discard self` and adds a deprecation warning for
`_forget self`.

rdar://108859077
2023-05-08 21:42:19 -07:00
Pavel Yaskevich
5f8e3eb6f6 [CSSimplify] Detect and diagnose conformance failures related to AnyHashable conversion
Failure of an argument to AnyHashable parameter to conform to Hashable
protocol should be detected in `simplifyConformsToConstraint` and fixed
there.

Doing so requires impact assessment adjustment because regular conformance
requirements have default impact of 1, this is going to have argument
impact of 2 not avoid clashing with other failures.
2023-05-08 16:16:30 -07:00
Pavel Yaskevich
31e0a52be4 [CSFix] Add a skeleton of a fix for missing each in value pack reference 2023-05-08 13:30:51 -07:00
Kavon Farvardin
f667c867c9 Merge pull request #65673 from kavon/copyable-constraint-performance
Fix a performance issue when answering "is this tuple Copyable"?
2023-05-05 11:14:44 -07:00
Kavon Farvardin
f15dadeee7 Merge pull request #65668 from kavon/noncopyable-metatypes-redo
Noncopyable metatypes redo
2023-05-04 16:42:06 -07:00
Kavon Farvardin
ce04b84489 Fix a performance issue when answering "is this tuple Copyable"?
If we don't split up the tuple into individual constraints,
we end up spending more time querying whether a tuple is
Copyable in `lookupConformance`, because it will naively
check the types of all elements of the tuple recursively
with `lookupConformance`.

This is inefficient because if we know some of the elements
of the tuple are fixed types, we don't need to keep checking
those again. For example, if we have `($T, Int, $U)`, and
then try a binding for `$T`, we might ask again if the whole
tuple conforms. Leading to `lookupConformance` to check
whether `Int` (and all other elements of the tuple) conforms
to Copyable, when we either already know that, or can't
answer it yet because it's still a type variable.

By splitting up a Copyable constraint on a tuple into
invidivual  constraints on each of its type elements,
we can avoid this redundant work by `lookupConformance`.

While today we could short-cut this even further to say
that _all_ tuples are Copyable, since we emit an error if
a noncopyable type appears in one, that won't be true in
the near future. This is the nicer solution we'll want to
keep around long-term.

After discussing this with Pavel, we don't think there's a
good way to add a regression test for this, because the
performance issue primarily comes up in specific example
programs that aren't amenable to scale tests.

resolves rdar://107536402
2023-05-04 14:53:48 -07:00
Hamish Knight
3087afc1f8 Merge pull request #65651 from hamishknight/placeholder
Resolves https://github.com/apple/swift/issues/65650
2023-05-04 19:50:12 +01:00
Kavon Farvardin
103bd043cd Revert "Revert "Prevent noncopyable metatypes from being converted to Any""
This reverts commit 1a3b42cd63.
2023-05-04 11:38:00 -07:00
Hamish Knight
b07f7b336e [CS] Improve handling of holes for Named/AnyPatterns
Rather than eagerly binding them to holes if the
sequence element type ends up being Any, let's
record the CollectionElementContextualMismatch fix,
and then if the patterns end up becoming holes,
skip penalizing them if we know the fix was
recorded. This avoids prematurely turning type
variables for ExprPatterns into holes, which
should be able to get better bindings from the
expression provided. Also this means we'll apply
the logic to non-Any sequence types, which
previously we would give a confusing diagnostic
to.
2023-05-04 14:53:58 +01:00
Hamish Knight
1be25895b8 [CS] Remove invalid DependentMemberType workaround for tuples
We can produce a hole here now without
regressing diagnostics.
2023-05-04 14:53:58 +01:00
Hamish Knight
b305324179 [CS] Remove obsolete check for a type variable
We no longer need to check for a type variable
here, it no longer regresses diagnostics. Also,
while here, let's bump the impact for an
Any/AnyObject missing conformance, as that's
unlikely going to be helpful since they cannot
conform to protocols even if the user wanted them
to.
2023-05-04 14:53:57 +01:00
Hamish Knight
91209d4226 [CS] NFC: Factor out AllowAssociatedValueMismatch
This seems better suited as its own fix, rather
than as part of ContextualMismatch.
2023-05-04 14:53:57 +01:00
Hamish Knight
1a3b42cd63 Revert "Prevent noncopyable metatypes from being converted to Any" 2023-05-04 12:10:10 +01:00
Kavon Farvardin
1df3912794 Merge pull request #65606 from kavon/noncopyable-metatypes
Prevent noncopyable metatypes from being converted to `Any`
2023-05-03 22:05:17 -07:00
Pavel Yaskevich
8d0a977a45 [CSSimplify] Simplication of ExplicitGenericArguments constraint shouldn't produce ShapeOf
Fix `formUnsolvedConstraint` to use `ExplicitGenericArguments` instead
of `ShapeOf` when first type is not yet fully resolved.
2023-05-03 11:26:52 -07:00
Pavel Yaskevich
fdfedc62bd [ConstraintSystem] NFC: Reverse direction of ShapeOf constraint
First type is the reduced shape of the second type (the pack type).
2023-05-03 10:44:10 -07:00
Kavon Farvardin
4710fc675b Prevent noncopyable metatypes from being converted to Any
These metatypes are a gateway to more incorrect
uses of these noncopyable values because we don't
yet have the corresponding runtime support yet.
The other use cases of using metatypes of
noncopyable types in generics is not high enough to
warrant people using them yet.

resolves rdar://106452518
2023-05-03 10:23:11 -07:00
Pavel Yaskevich
d8c8a39cc5 [CSFix] Add a fix to allow value pack expansions without pack references
Situations like `repeat x` where `x` is `Int` where pack expansion
expression doesn't have any pack references.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
0db67464da [CSSimplify] If pack type has holes - its shape is a hole
Propagating holes to the shape helps avoid spurious diagnostics
about same-shape requirement failures.

Resolves: rdar://107675464
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
3006f55327 [CSSimplify] Detect and diagnose passing a tuple to pack expansion that expects individual arguments 2023-05-02 09:32:19 -07:00
Pavel Yaskevich
b9ef2cabe4 [CSFix] Add a skeleton of a fix for argument destructuring to match pack expansion 2023-05-02 09:32:19 -07:00
Pavel Yaskevich
03f88a0041 [CSSimplify] Add tailored diagnostics for same-shape mismatches related to arguments
Suggest to drop tuples, synthesized or remove extraneous arguments
based on pack shape mismatches.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
bbe305cd43 [ConstraintSystem] Add same-shape constraint
The constraint takes two pack types and makes sure that their
reduced shapes are equal. This helps with diagnostics because
constraint has access to the original pack expansion pattern
types.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
cbfec20f08 [CSSimplify] Propagate contextual types to pack expansion variables
If there are explicit generic arguments that fully resolves the
pack expansion, let's bind opened pack expansion to its contextual
type early (while resolving pack expansion variable), doing so
helps with performance and diagnostics.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
943ef19a1d [CSSimplify] Adjust replaceTypeVariablesWithFreshPacks to propagate holes
If type variable that is being replaces allowed holes, the new one
should not lose this property.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
38ee650719 [CSSimplify] Prevent contextual mismatch diagnostics from seeing one-element tuples
If solver had to introduce an unlabeled single-element tuple around
contextual type, let's strip it and fix as a regular contextual mismatch
(instead of a tuple mismatch) because this action should be transparent
to diagnostics.
2023-05-02 09:32:19 -07:00
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