Commit Graph

1727 Commits

Author SHA1 Message Date
Holly Borla
e6f29f8f93 Merge pull request #64119 from hborla/pack-expansion-shape-crash
[Diagnostics] Diagnose pack element expressions containing a non-pack a subexpression.
2023-03-05 10:11:02 -08:00
Holly Borla
8012e45109 [Diagnostics] Diagnose pack element expressions containing a non-pack subexpression. 2023-03-05 00:11:54 -08:00
Kavon Farvardin
091d63a5c8 revise how the Copyable constraint is added
The _Copyable constraint was implemented as a marker protocol.
That protocol is part of the KnownProtocol's in the compiler.
When `ASTContext::getProtocol(KnownProtocolKind kind)` tries
to find the ProtocolDecl for Copyable, it will look in the
stdlib module (i.e., Swift module), which is where I initially
planned to put it.

That created problems initially when some regression tests
use `-parse-stdlib` failed to do that protocol lookup, which is
essential for adding the constraint (given the current implementation).

That led to believe we need to pull Copyable out of the stdlib, but that's
wrong. In fact, when building the Swift module itself, we do `-parse-stdlib`
but we also include `-module-name Swift`. This causes the _Copyable protocol
defined in the Stdlib to be correctly discovered while building the stdlib
itself (see the test case in this commit). So, the only downside of
having the Copyable protocol in the Stdlib is that `-parse-stdlib` tests
in the compiler can't use move-only types correctly, as they'll be
allowed in generic contexts. No real program would build like this.

Until I have time to do a further refactoring, this is an acceptable trade-off.

fixes rdar://104898230
2023-03-04 11:42:09 -08:00
Pavel Yaskevich
17b09a801e [ConstraintSystem] Add a locator element to represent a generic type
This locator is going to be used as a "parent" element for
"generic argument" elements.
2023-03-03 18:50:46 -08:00
Holly Borla
ad3a385cd2 [Sema] Eliminate single-element tuples after parameter pack substitution. 2023-03-02 19:37:52 -08:00
Andrew Trick
f1ff6958a3 Merge pull request #63825 from atrick/diagnose-implicit-raw-bitwise
Warn on implicit pointer conversion from nontrivial inout values.
2023-03-02 10:57:30 -08:00
Holly Borla
b78b6b9a77 Merge pull request #63991 from hborla/materialize-pack-from-tuple
[ConstraintSystem] Implement type checking for converting a tuple to a pack using the `.element` syntax.
2023-03-01 16:19:33 -08:00
Kavon Farvardin
cc0b668efb Merge pull request #63922 from kavon/standard-issue-neuralyzer
Implement the `forget` statement (as `_forget`)
2023-03-01 11:10:25 -08:00
Doug Gregor
bd35cdb865 Merge pull request #63985 from DougGregor/expression-macros-effects 2023-03-01 06:52:43 -08:00
Alex Hoppen
2108674ec1 Merge pull request #63849 from ahoppen/ahoppen/equivalent-type-vars
[IDE] Inspect all equivalent type variables to replace a type variable by an archetype
2023-03-01 14:36:30 +01: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
Kavon Farvardin
f41ed5926b implement the forget statement
Currently, this is staged in as `_forget`,
as part of SE-390. It can only be used on
`self` for a move-only type within a consuming
method or accessor. There are other rules, see
Sema for the details.

A `forget self` really just consumes self and
performs memberwise destruction of its data.
Thus, the current expansion of this statement
just reuses what we inject into the end of a
deinit.

Parsing of `forget` is "contextual".
By contextual I mean that we do lookahead to
the next token and see if it's identifier-like.
If so, then we parse it as the `forget` statement.
Otherwise, we parse it as though "forget" is an
identifier as part of some expression.

This way, we won't introduce a source break for
people who wrote code that calls a forget
function.

This should make it seamless to change it from
`_forget` to `forget` in the future.

resolves rdar://105795731
2023-02-28 21:15:17 -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
08e62a35ed Merge pull request #63947 from xedin/rdar-105149979
[ConstraintSystem] Dependent member simplification should be attempte…
2023-02-28 09:12:51 -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
Alex Hoppen
e4f82ea71d [IDE] Pass whether we have applied self to replaceParamErrorTypeByPlaceholder 2023-02-27 21:59:47 +01:00
Pavel Yaskevich
e450047c6b [ConstraintSystem] Dependent member simplification should be attempted if it has type variable in any position
Current logic attempts simplification of the base type only if it's
a type variable or a dependent member type. That's valid for correct
code but not for invalid code e.g. `(($T) -> Void).Element` is not
going to be simplified even if `$T` is bound, which causes crashes
in diagnostic mode because `matchTypes` is going to re-introduce
constraint with partially resolved dependent member types and by
doing so make it stale forever which trips constraint system state
verification logic.

Resolves: rdar://105149979
2023-02-27 12:41:42 -08:00
Luciano Almeida
f5c68dbeb9 [Sema] Handle FunctionArgument anchored in coerce expr in getCalleeLocator 2023-02-22 21:03:44 -03:00
Alex Hoppen
8951373032 [IDE] Inspect all equivalent type variables to replace a type variable by an archetype
Sometimes the type variable itself doesn't have have an originator that can be replaced by an archetype but one of its equivalent type variable does.
Search thorough all equivalent type variables, looking for one that can be replaced by a generic parameter.
2023-02-22 23:00:48 +01:00
Alex Hoppen
8bdf68d483 [IDE] Check whether an AST node contains the IDE inspection point based on CharSourceRange
This fixes an issue if the range ends with a string literal that contains the IDE inspection target. In that case the end of the range will point to the start of the string literal but the IDE inspection target is inside the string literal and thus after the range’s end.
2023-02-20 15:37:38 +01:00
Pavel Yaskevich
e313c533b6 [ConstraintSystem] Teach getCalleeLocator about pattern matching
Requesting a callee locator from locator in pattern matching context
should always yield pattern match.
2023-02-07 11:13:32 -08:00
Hamish Knight
c87b1b8bef Merge pull request #63022 from hamishknight/express-yourself 2023-02-03 16:40:09 +00:00
swift-ci
bf2816b7fd Merge pull request #62599 from kavon/ban-moveonly-generic-subst
Emit error when move-only type is used as a generic type
2023-02-02 03:23:14 -08:00
Kavon Farvardin
18b1389542 [nfc] improve some comments 2023-02-01 23:38:28 -08:00
Kavon Farvardin
ab130883a3 Initial ban of move-only types from being used generically
Since values of generic type are currently assumed to always
support copying, we need to prevent move-only types from
being substituted for generic type parameters.

This approach leans on a `_Copyable` marker protocol to which
all generic type parameters implicitly must conform.

A few other changes in this initial implementation:

- Now every concrete type that can conform to Copyable will do so. This fixes issues with conforming to a protocol that requires Copyable.
- Narrowly ban writing a concrete type `[T]` when `T` is move-only.
2023-02-01 23:38:28 -08:00
Alex Hoppen
acca597cd0 Merge pull request #59984 from ahoppen/pr/typecheck-multistamtent-closures-in-result-builders
[Sema] Type check multi-statement closures inside result builders
2023-02-02 08:08:22 +01: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
Alex Hoppen
2c5b193892 [Sema] Type check multi-statement closures inside result builders 2023-02-01 10:29:56 +01:00
Holly Borla
9b5cf1d2ff [Diagnostics] Remove the MacroMissingArguments constraint fix and its
associated failure diagnostic.

This constraint fix is unused now that MacroExpansionExpr always has an
argument list, and goes through the AddMissingArguments constraint fix for
this error.
2023-01-31 17:45:31 -08:00
Holly Borla
7c50f95e1b [Macros] Use MacroExpansionExpr when resolving attached and freestanding macros. 2023-01-31 09:27:06 -08:00
Pavel Yaskevich
e99f0e21a1 [ConstraintSystem] Handle requirement failures associated with injected result builder methods
Only way to aggregate build* methods is via source locations because
each result builder kind would create a new build* expression node.
2023-01-27 08:47:19 -08:00
Pavel Yaskevich
c7ba055c38 [ConstraintSystem] Handle ambiguities caused by requirement failures
Aggregate all requirement failures (regardless of kind) that belong
to the same locator and diagnose them as an ambiguity (if there is
more than one overload) or as the singular failure if all solutions
point to the same overload.
2023-01-26 15:55:50 -08:00
Hamish Knight
76da268b4b [CS] Debug: Print ASTNode for contextual type
If we don't have an associated TypeRepr for a
contextual type in the constraint system, dump the
ASTNode that it's for instead.

Before:

```
Contextual Type: $T2
```

After:

```
Contextual Type: $T2 at DeclRef@/Users/hamish/src/swift-test-arena/swift-test-arena/main.swift:1493:21
```
2023-01-26 17:21:49 +00:00
Holly Borla
94abc252bf [Macros] Type-check attached macro arguments. 2023-01-22 11:26:02 -08:00
Luciano Almeida
e513d23c0b [Sema] Improving global actor function mismatch diagnostic 2023-01-13 20:47:23 -03:00
Holly Borla
2d893d23bf [GenericEnvironment] For opened pack element environments, only include element type
parameters whose originating packs are in a given shape equivalence class.
2023-01-08 12:59:08 -08:00
Holly Borla
715de13dbf [Sema] Key opened pack element generic on the shape class of the expansion. 2023-01-07 15:07:51 -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
Anthony Latsis
ad5d55c36e [NFC] AST: Rename IdentTypeReprDeclRefTypeRepr 2023-01-07 07:14:44 +03:00
Anthony Latsis
2113e175f5 [NFC] AST: Remove IdentTypeRepr::ComponentRange 2023-01-07 07:11:30 +03:00
Pavel Yaskevich
2e25c2fc80 Merge pull request #62791 from xedin/rdar-103739206
[Diagnostics] Relax contextual type presence from assertion to a check
2023-01-04 09:47:19 -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
Pavel Yaskevich
eaf0532a04 [Diagnostics] Relax contextual type presence from assertion to a check
In some situations `getContextualType` for a contextual type
locator is going to return then empty type. This happens because
e.g. optional-some patterns and patterns with incorrect type don't
have a contextual type for initialization expression but use
a conversion with contextual locator nevertheless to indicate
the purpose. This doesn't affect non-ambiguity diagnostics
because mismatches carry both `from` and `to` types.

Resolves: rdar://problem/103739206
2023-01-02 21:11:07 -08:00
Holly Borla
42ede18fb0 Merge pull request #62713 from hborla/open-pack-element-type-repr
[ConstraintSystem] Enable pack element type reprs in expressions.
2022-12-21 10:48:42 -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
454913aeb5 [TypeResolution] Plumb a pack element opener function through type resolution. 2022-12-20 11:36:58 -05:00
Doug Gregor
402ba1492f Set closure descriminators via a request.
Rather than set closure discriminators in both the parser (for explicit
closures) and then later as part of contextualizing closures (for
autoclosures), do so via a request that sets all of the discriminators
for a given context.
2022-12-19 15:23:45 -08:00