Commit Graph

1442 Commits

Author SHA1 Message Date
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
Doug Gregor
200f2340d9 [Macros] Be deliberate about walking macro arguments vs. expansions
Provide ASTWalker with a customization point to specify whether to
check macro arguments (which are type checked but never emitted), the
macro expansion (which is the result of applying the macro and is
actually emitted into the source), or both. Provide answers for the
~115 different ASTWalker visitors throughout the code base.

Fixes rdar://104042945, which concerns checking of effects in
macro arguments---which we shouldn't do.
2023-02-28 17:48:23 -08:00
Pavel Yaskevich
dc85cb0b55 Merge pull request #63762 from xedin/fix-init-via-coercion-performance
[CSGen] SE-0213: Literal initialization via coercion shouldn't try br…
2023-02-28 09:14:11 -08:00
Andrew Trick
185e6fabd5 Add TypeBase::isArrayType helper API.
This is also needed in SIL diagnostics, not just Sema diagnostics,
because implicit Array conversion generates special SIL patterns.
2023-02-27 21:51:17 -08:00
Pavel Yaskevich
45ebc56151 [CSGen] SE-0213: Literal initialization via coercion shouldn't try bridging
`addExplicitConversionConstraint` generates a disjunction to check
whether type could be coerced via bridging. That is not useful for
literal initialization which could use direct equality if the type
of cast is valid because necessary conformance checks have been
performed before the transformation.
2023-02-17 15:08:26 -08:00
Hamish Knight
f7191b35b6 [CS] Add contextual Bool type to switch-case where clause
Seems this was accidentally omitted, which would
crash in CSApply for any non-Bool-convertible type.
2023-02-17 20:58:46 +00:00
Hamish Knight
eac3fb4506 [CS] NFC: Default the allowFreeTypeVariables parameter
All but one client wants to set this to anything
other than `FreeTypeVariableBinding::Disallow`.
2023-02-17 20:58:46 +00:00
Slava Pestov
e0c341d0ac AST: Rename PackReferenceTypeRepr => PackElementTypeRepr 2023-02-15 22:37:07 -05:00
Alex Hoppen
754e9d7d54 [CodeCompletion] Fix issue when completing inside a switch in a closure 2023-02-14 09:40:56 +01:00
Pavel Yaskevich
8e82f1d68b [CSGen] Detect nested out-of-scope variables in recursive declarations
Follow-up to https://github.com/apple/swift/pull/63505

Since, when the type is not stated, a variable assumes the
type of its initializer that enables out-of-scope variables
to be nested inside of some other concrete type i.e. Optional.

Resolves: https://github.com/apple/swift/issues/63455
2023-02-11 19:19:09 -08:00
Pavel Yaskevich
bffb8d9ee2 Revert "[CSGen] Handle recursive use of variable declarations" 2023-02-10 16:45:59 -08:00
Pavel Yaskevich
83dd93ad2e [CSGen] Handle recursive use of variable declarations
It's possible for out-of-scope type variable to be the type of
declaration if such declaration is recursively referenced in
the body of a closure located in its initializer expression.
In such cases type of the variable declaration cannot be connected
to the closure because its not known in advance (determined by the
initializer itself).

Resolves: https://github.com/apple/swift/issues/63455
2023-02-07 17:48:26 -08:00
Hamish Knight
c87b1b8bef Merge pull request #63022 from hamishknight/express-yourself 2023-02-03 16:40:09 +00:00
Holly Borla
e41cdfbdd4 Merge pull request #63341 from hborla/generalize-macro-expansion-expr
[Macros] Generalize `MacroExpansionExpr` and use it for both freestanding and attached macros.
2023-02-01 11:04:39 -08:00
Hamish Knight
a40f1abaff Introduce if/switch expressions
Introduce SingleValueStmtExpr, which allows the
embedding of a statement in an expression context.
This then allows us to parse and type-check `if`
and `switch` statements as expressions, gated
behind the `IfSwitchExpression` experimental
feature for now. In the future,
SingleValueStmtExpr could also be used for e.g
`do` expressions.

For now, only single expression branches are
supported for producing a value from an
`if`/`switch` expression, and each branch is
type-checked independently. A multi-statement
branch may only appear if it ends with a `throw`,
and it may not `break`, `continue`, or `return`.

The placement of `if`/`switch` expressions is also
currently limited by a syntactic use diagnostic.
Currently they're only allowed in bindings,
assignments, throws, and returns. But this could
be lifted in the future if desired.
2023-02-01 15:30:18 +00:00
Pavel Yaskevich
51b2481091 [AST] Expand TypeJoin expression to support joining over a type
(cherry picked from commit 4efc35a76c)
2023-02-01 15:13:58 +00:00
Holly Borla
26786c1e09 [Macros] Make macro expansions always have an argument list. If an explicit
argument list isn't written, an implicit, empty list is created.
2023-01-31 17:04:35 -08:00
Holly Borla
7c50f95e1b [Macros] Use MacroExpansionExpr when resolving attached and freestanding macros. 2023-01-31 09:27:06 -08:00
Alex Hoppen
17d47bb691 [Sema] Don’t fail constraint generation if a closure contains an ErrorExpr
It appears like this was missed in #60062.
2023-01-30 17:36:36 +01:00
Pavel Yaskevich
6ea82b21e3 Merge pull request #63196 from xedin/connect-captures-to-closure
[CSGen] Captures should always be connected to their closure
2023-01-26 11:03:06 -08:00
Pavel Yaskevich
516e616bdc Merge pull request #63149 from Vespen/fix-non-api-wrapper-parameter
[Sema] Fix for non-API level property wrappers with closure.
2023-01-26 09:45:27 -08:00
Doug Gregor
72ddbebc08 [Macros] Assign macro expansion discriminators more lazily.
This is a punt. They'll be unique, and they'll be per-context, but
they'll be lazy and therefore not stable.
2023-01-25 22:03:38 -08:00
Anton Lagutin
a28776e847 [Sema] applyPropertyWrapperToParameter fails when wrapper is not applied 2023-01-25 13:46:27 +10:00
Pavel Yaskevich
60720f4e07 [CSGen] Captures should always be connected to their closure
Otherwise it might be possible (once references are not resolved
eagerly for example) to get a situation where closure is solved
before captures are.
2023-01-24 10:43:29 -08:00
Anton Lagutin
f1c6c1ba41 [Sema] Fix for non-API level property wrappers with closure.
[Sema] Added parameter tests for non-API-level wrappers.
[Sema] Added solution for non-API level property wrappers with closure.
2023-01-23 14:22:25 +10:00
Pavel Yaskevich
c2ad27ccc6 Merge pull request #62899 from xedin/closure-analyzer-shouldnt-walk-local-decls
[CSGen] Closure analyzer shouldn't walk into local some decls/patterns
2023-01-19 10:25:58 -08:00
Doug Gregor
de16b47875 [Macros] Introduce the @attached attribute for declaring attached macros.
Describe attached macros with the `@attached` attribute, providing the
macro role and affected names as arguments to the macro. The form of
this macro will remain the same as it gains other kinds of attached
macro roles beyond "accessor".

Remove the "accessors" role from `@declaration`, which will be going
away.
2023-01-13 22:47:59 -08:00
Richard Wei
f17b7c48bf [Macros] Freestanding declaration macros
Add support for freestanding declaration macros.

- Parse `@declaration` attribute.
- Type check and expand `MacroExpansionDecl`.

Known issues:
- Generic macros are not yet handled.
- Expansion does not work when the parent decl context is `BraceStmt`. Need to parse freestanding declaration macro expansions in `BraceStmt` as `MacroExpansionDecl`, and add expanded decls to name lookup.
2023-01-10 19:09:11 -08:00
Holly Borla
6e28f996ce [ConstraintSystem] Create opened pack element generic environments lazily
in the constraint system.
2023-01-07 09:50:14 -08:00
Holly Borla
74bf2f3e7a [AST] Introduce a syntactic distinction betweek vararg type reprs and
pack expansion type reprs.

Classic variadic parameters still use the postfix ellipsis syntax, and
pack expansion types now use a prefix 'repeat' keyword.
2023-01-07 09:50:11 -08:00
Pavel Yaskevich
ca126bc433 [CSGen] Closure analyzer shouldn't walk into local some decls/patterns
While trying to gather all references to external variables,
analyzer shouldn't walk into patterns and any local declarations
except to pattern bindings, because decls do not participate
in inference and patterns cannot contain references analyzer
is looking for.
2023-01-06 15:46:35 -08:00
Doug Gregor
6bb9cb8b5d [Macros] Always parse macro expansions, diagnose later
Always parse macro expansions, regardless of language mode, and
eliminate the fallback path for very, very, very old object literals
like `#Color`. Instead, check for the feature flag for macro
declaration and at macro expansion time, since this is a semantic
restriction.

While here, refactor things so the vast majority of the macro-handling
logic still applies even if the Swift Swift parser is disabled. Only
attempts to expand the macro will fail. This allows us to enable the
macro-diagnostics test everywhere.
2023-01-02 21:22:04 -08:00
Doug Gregor
71ca9c86e6 [Macros] Diagnose when we forget to provide macro arguments.
Unlike functions, you can't curry macros; diagnose when one omits the
arguments in a macro expansion of a macro that has a parameter list.
2023-01-02 21:22:04 -08:00
Doug Gregor
806b5a8777 [Macros] Diagnose errors where a macro is used without the '#'. 2023-01-02 21:22:04 -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
58953567b3 [CSGen] Use PackExpansionExpr::getExpandedPacks to generate ShapeOf constraints
rather than relying on PackElementExprs collected by preCheck.

This handles pack element expressions and pack element type reprs, and enforces that
all packs expanded by a given pack expansion expression all have the same shape.

Once the parser creates PackExpansionExpr directly (based on a dedicated syntax instead
of postfix '...'), the code in preCheck for identifying and creating pack expansions
can simply be deleted.
2022-12-20 22:34:16 -05:00
Holly Borla
2df72f6bcc [ConstraintSystem] Strip pack expansions off of pack reference types in
getUnopenedTypeOfReference.
2022-12-20 22:13:40 -05:00
Holly Borla
334efb9975 [ConstraintSystem] Always pass in a direct instance of OpenPackElementType instead
of assigning it to std::function_ref.

std::function_ref does not own the function reference, so assigning an OpenPackElementType
to a local function_ref lead to stale constraint system references when invoking
operator () later on.
2022-12-20 15:44:42 -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
27aa3651e8 [ConstraintSystem] Open pack element types using a PackElementOf constraint
when resolving pack reference type reprs inside pack expansion expressions.
2022-12-20 11:36:58 -05:00
Holly Borla
454913aeb5 [TypeResolution] Plumb a pack element opener function through type resolution. 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
Holly Borla
48fdafe08d [ConstraintSystem] Implement constraint generation and solution application for
PackElementExpr.
2022-12-14 20:45:51 -08:00
Holly Borla
0d4e487e06 [CSGen] Store a stack of pack element generic environments in constraint
generation.
2022-12-14 20:45:51 -08:00
Holly Borla
ea68720e12 [CSBindings] Instead of breaking a PackElementOf constraint down into Bind
constraints, vend potential bindings through PotentialBindings::infer.

This allows for bidirectional binding inference from the pack type to the
element type and vice versa.
2022-12-14 20:45:51 -08:00
Holly Borla
f1fd9acb23 [AST] Add 'PackElementExpr' for explicit pack elements spelled with the 'each'
keyword.
2022-12-14 20:45:51 -08:00
Pavel Yaskevich
9a22bac825 Merge pull request #62315 from hank121314/fix/issue-60806
[CSGen]: `getTypeForPattern` should perform member lookup into the external pattern metatype
2022-12-10 00:22:38 -05:00
hank121314
9ddf49738f [CSGen]: getTypeForPattern should perform member lookup into the external pattern metatype if enum element has externalPatternType 2022-11-30 21:45:15 +08:00
Doug Gregor
64f2c8ef0c Address a few comments about the specialization arguments constraint. 2022-11-28 18:33:11 -08:00
Doug Gregor
a9ee5dd142 [Macros] Don't allow local names to shadow macro names. 2022-11-28 18:33:10 -08:00