Commit Graph

111 Commits

Author SHA1 Message Date
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
Sophia Poirier
51824937f2 [Variadic Generics] fixit for pack expansion on a non-pack generic type 2023-03-07 14:00:44 -08:00
Holly Borla
74cc62735b [RequirementMachine] Diagnose same-element requirements.
The rewrite rules are not quite right yet for same-element requirements, so
let's ban them for now.
2023-03-06 21:32:30 -08:00
Holly Borla
721b846e16 [ConstraintSystem] Don't propagate errors for invalid DeclRefs in the pattern
of a pack expansion.
2023-03-06 10:51:43 -08:00
Holly Borla
8012e45109 [Diagnostics] Diagnose pack element expressions containing a non-pack subexpression. 2023-03-05 00:11:54 -08:00
Holly Borla
e063f02a42 [ConstraintSystem] Handle invalid pack element bindings during PackElementOf
simplification.
2023-03-04 19:43:43 -08:00
Holly Borla
34b720c0a3 [CSGen] Constraint generation for PackExpansionExpr should return a null type
if the constraint system has not recorded a type for an expanded pack element
type repr.
2023-03-04 13:14:16 -08:00
Holly Borla
a8ca989072 [PreCheckExpr] Allow pack references when resolving types during TypeExpr folding. 2023-03-04 12:35:01 -08:00
Sophia Poirier
70cffb06f3 [Variadic Generics] type parameter pack syntax change: T... -> each T 2023-03-02 14:42:28 -08:00
Holly Borla
509188630b [ConstraintSystem] Implement type checking for converting a tuple to a
pack using the `.element` syntax.
2023-02-28 22:56:59 -08:00
Sophia Poirier
0e99f9c6e5 [Variadic Generics] require pack ident type within a pack expansion to form a pack reference 2023-01-27 13:17:12 -08:00
Holly Borla
6725f365ba [Variadic Generics] Update tests for syntax changes. 2023-01-07 09:50:14 -08:00
Holly Borla
89fa17d74c [Parser] Replace postfix ellipsis with a prefix 'repeat' keyword for pack
expansion expressions, and create them in the parser instead of during
pre-checking in the constraint system.
2023-01-07 09:49:14 -08:00
Holly Borla
e966b4ef7d [CSDiagnostics] Add an error message for pack expansion expressions over
packs that don't have the same shape.
2022-12-21 08:25:10 -05:00
Holly Borla
b8ce8997e5 [Test] Add tests for pack element reprs outside pack expansion expressions. 2022-12-20 17:08:46 -05:00
Holly Borla
7b35e85d31 [PreCheckExpr] Consider postfix ellipsis expressions containing 'each' type
reprs to be pack expansion expressions.
2022-12-20 11:36:58 -05:00
Holly Borla
42020792c6 [Sema] Represent pack element references in pack expansion patterns as
PackElementExpr.
2022-12-14 20:45:52 -08:00