Commit Graph

2325 Commits

Author SHA1 Message Date
Hamish Knight
f3b16fd7ad [CS] Fix a couple of constraints in getTypeForPattern
The TypedPattern and IsPattern constraints were
incorrectly written, with conversions propagating
out of the patterns, when really conversions
ought to propagate into patterns. In any case, it
seems like we really want equality here. Fix the
constraints to use equality, and have the cast
constraint operate on the external pattern type
instead of the subpattern type.
2023-06-07 00:35:00 +01:00
Luciano Almeida
1d90de16c7 Merge pull request #66208 from LucianoPAlmeida/diag-ternary
[Sema] Increase impact of ternary then branch aiming better diagnostics
2023-05-31 01:24:43 -03:00
Luciano Almeida
fef908efac [Sema] Increase impact of ternary then branch aiming better diagnostics 2023-05-30 20:55:50 -03:00
Sophia Poirier
93864f6c15 [Variadic Generics] drop requirement of .element for tuple expansion rdar://107160966 2023-05-30 11:37:55 -04:00
Slava Pestov
b2bc2c72ec AST: Introduce PackElementType 2023-05-25 11:17:30 -04:00
Pavel Yaskevich
acf64e3c15 Merge pull request #66080 from xedin/rdar-109586440
[ConstraintSystem] Some more variadic generic fixes
2023-05-24 14:49:09 -07:00
Pavel Yaskevich
fe6cfe2d85 [CSDiagnostics] Diagnose conformance failures related to pack expansion patterns 2023-05-23 09:47:48 -07:00
Pavel Yaskevich
3b1f392d0a Merge pull request #65785 from angela-laar/fix-covariant-erasure-for-constrained-existentials
Fix covariant erasure for constrained existentials
2023-05-18 16:58:45 -07:00
Pavel Yaskevich
a987861d1c Merge pull request #65806 from xedin/rdar-108977234
[CSSimplify] Detect and diagnose conformance failures related to AnyHashable conversion
2023-05-16 09:56:41 -07:00
Pavel Yaskevich
fdf6d62ece Merge pull request #65830 from xedin/rdar-108904190
[CSSimplify] Allow converting pack expansion types and tuples with pack expansions to Void for closure result
2023-05-16 09:56:27 -07:00
Pavel Yaskevich
96cbc01822 Merge pull request #65861 from xedin/rdar-109160060
[CSSimplify] Allow conversions between tuples with pack expansions and `Any`
2023-05-15 16:20:50 -07:00
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