Commit Graph

77 Commits

Author SHA1 Message Date
Slava Pestov
05daaae0fd ASTPrinter: Tweak PackExpansionType printing 2023-06-06 14:18:37 -04:00
Sophia Poirier
93864f6c15 [Variadic Generics] drop requirement of .element for tuple expansion rdar://107160966 2023-05-30 11:37:55 -04:00
Pavel Yaskevich
fe6cfe2d85 [CSDiagnostics] Diagnose conformance failures related to pack expansion patterns 2023-05-23 09:47:48 -07:00
Pavel Yaskevich
1e03cdc102 [AST] PackMacher: Match pack expansion elements pair-wise if pack arity matches
This covers situations like `Pack{repeat each T1, repeat each T2}`
vs. `Pack{repeat $T1, repeat $T2}` where type variables are allowed
to bind to packs.

Resolves: rdar://109539394
2023-05-22 14:20:31 -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
Slava Pestov
e9e6a51c82 Unconditionally enable variadic generic types now that SE-0398 has been accepted 2023-05-15 23:32:52 -04: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
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
Pavel Yaskevich
4a2d139f07 [Diagnostics] NFC: Add a test-case for rdar://108977234 2023-05-09 14:11:45 -07:00
Pavel Yaskevich
440bc39207 [Diagnostics] Don't print pack element archetypes in diagnostics 2023-05-09 14:11:45 -07:00
Pavel Yaskevich
e3d1317208 [ConstraintSystem] Detect and diagnose missing each for value pack reference
Detect that a value pack is missing 'each' keyword during constraint
generation and fix-it by injecting `PackElementExpr`.
2023-05-08 13:30:51 -07:00
Slava Pestov
2e9fd98933 Sema: Diagnose variadic types with more than one type parameter pack 2023-05-08 12:57:33 -04: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
Pavel Yaskevich
bff6a896b2 [CSGen] Detect and diagnose invalid pack expansion expressions
Detect that pack expansion expression doesn't have any pack
references during constraint generation.

Resolves: rdar://107835215
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
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
218ccb145b [ConstraintSystem] Adjust openType and openUnboundGenericType to open pack expansion types
Replace all `PackExpansionType` with a special type variable
which would be resolved once the solver determines that there
is enough contextual information.
2023-05-02 09:32:16 -07:00
Pavel Yaskevich
dd813af0a7 Merge pull request #65445 from xedin/rdar-108064941
[Diagnostics] Suppress unused expression warning if result is discard…
2023-05-02 09:26:55 -07:00
Pavel Yaskevich
dc8c05ac13 [Diagnostics] Suppress unused expression warning if result is discardable pack expansion
Examine pack expansion pattern to determine whether expression
result could be discarded without a warning (applies to tuples
with a single unlabeled pack expansion element as well).

Resolves: rdar://108064941
2023-04-26 13:43:49 -07:00
Slava Pestov
290701cb4d Sema: Ban shadowing generic parameters from outer scopes
Code like that is usually indicative of programmer error, and does not
round-trip through module interface files since there is no source
syntax to refer to an outer generic parameter.

For source compatibility this is a warning, but becomes an error with
-swift-version 6.

Fixes rdar://problem/108385980 and https://github.com/apple/swift/issues/62767.
2023-04-25 17:41:23 -04:00
Slava Pestov
df5d531dc6 ASTPrinter: Fix printing of pack requirements
Also remove the flag for printing 'each' and make it always on.
2023-04-19 13:04:51 -04:00
Holly Borla
234b5dc660 [SE-0393] Require the repeat keyword for generic requirement expansions. 2023-04-12 22:04:01 -07:00
Sophia Poirier
a3baee9498 [Variadic Generics] fixit to remove keyword 'each' on non-pack types 2023-04-03 17:26:25 -07:00
Pavel Yaskevich
cf991ae99c Merge pull request #64733 from xedin/rdar-106775969
[CSSimplify] Exploaded tuple argument should assume param's label if …
2023-03-31 10:10:55 -07:00
Pavel Yaskevich
00fbdc7cb2 [CSApply] Use correct parameter index for default argument that follow pack expansions
`coerceCallArguments` has to be `ParameterList::getOrigParamIndex`
to determine "original" parameter version which could be used with
un-substituted parameter list.
2023-03-30 16:47:15 -07:00
Pavel Yaskevich
190081376d [CSApply] Teach coerceCallArguments about variadic generics
For variadic generic declarations we need to compute a substituted
version of bindings because all of the packs are exploded in the
substituted function type.

```swift
func fn<each T>(_: repeat each T) {}

fn("", 42)
```

The type of `fn` in the call is `(String, Int) -> Void` but bindings
have only one parameter at index `0` with two argument positions: 0, 1.
2023-03-30 15:02:34 -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
5571de116c Merge pull request #64676 from xedin/rdar-107280056
[ConstraintSystem] `openOpaqueType`: remove logic that duplicates `bindArchetypesFromContext`
2023-03-29 00:15:07 -07:00
Pavel Yaskevich
fb574c8cc4 [ConstraintSystem] openOpaqueType: remove logic that duplicates bindArchetypesFromContext
`openGenericParameters` calls `bindArchetypesFromContext` which would bind all of the
external generic parameters in the opaque type signature.

Resolves: rdar://107280056
2023-03-28 13:14:30 -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
433cbfcd62 [AST] PackExpansionMatcher: use common prefix/suffix algorithm for tuple 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
Holly Borla
cb19fc3a71 [ConstraintSystem] Enforce TVO_CanBindToPack, and diagnose pack references outside
of pack expansion expressions.
2023-03-20 20:13:48 -07:00
Sophia Poirier
19e671b5f2 [Variadic Generics] distinctly diagnose more types of pack expansion errors + add fixits 2023-03-20 15:51:19 -07:00
Slava Pestov
2fa344bf85 AST: Tweak some variadic generic diagnostics 2023-03-15 23:04:50 -04:00
Holly Borla
9358f2fca5 [ConstraintSystem] Allow type variables for pack expansion patterns to bind
to noescape.
2023-03-15 10:53:14 -07:00
Pavel Yaskevich
62d25bae8a [ConstraintSystem] Variadics: Pack expansion materialization operates on loaded tuples
If the base tuple of `.element` is l-value, it has to be loaded
before pack expansion could be materialized.
2023-03-10 16:53:14 -08:00
Sophia Poirier
b7a2fa28bf Merge pull request #64186 from sophiapoirier/each_non_pack_fixit
[Variadic Generics] fixit for pack expansion on a non-pack generic type
2023-03-10 15:09:42 -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