Commit Graph

448 Commits

Author SHA1 Message Date
Hamish Knight
e7f5ca954b [Sema] Better handle recovery for structurally invalid ReturnStmts
Make sure we preserve the result expression for an out-of-place
`return`, or a non-`nil` result in an initializer. This ensures we
can still provide semantic functionality from them and fixes a crash
where we would fail to type-check a binding.
2025-11-17 13:07:36 +00:00
Hamish Knight
61a5ae8e01 [CS] Eagerly bind hole in recordInvalidNode
We know this is where the issue is so we can immediately bind to a hole,
ensuring we don't produce unnecessary downstream diagnostics from
things we can't infer.
2025-11-16 11:47:21 +00:00
Max Desiatov
1061f95a53 Add REQUIRES: executable_test to for-expr.swift (#84751)
The lack of this condition made some jobs fail, e.g. https://ci.swift.org/job/oss-swift_tools-RA_stdlib-DA_test-device-non_executable/9749

rdar://162140584
2025-10-08 10:01:12 -07:00
Max Desiatov
b545a28bd3 Add missing // REQUIRES: swift_feature_ForExpressions 2025-10-07 12:34:49 +01:00
Max Desiatov
8cddae2471 Make nested expr work, convert test to executable 2025-10-07 11:16:54 +01:00
Henrik G. Olsson
cbc0ec3b88 Add -verify-ignore-unrelated where necessary (NFC)
These are tests that fail in the next commit without this flag. This
does not add -verify-ignore-unrelated to all tests with -verify, only
the ones that would fail without it. This is NFC since this flag is
currently a no-op.
2025-10-04 14:19:52 -07:00
Hamish Knight
9f5a754b77 [Sema] Ban placeholders in typed throws
This never worked correctly and would crash in SILGen, ban the use
of placeholder types. While here, ensure we replace any ErrorTypes
with holes when solving the closure in the constraint system.
2025-09-17 20:41:20 +01:00
Doug Gregor
5391887b01 [Effects] Ensure that we properly substitute function types in ByClosure checks
We weren't substituting generic arguments into function types. In the
presence of parameter packs, this could mean that the parameter and
argument lists no longer match up, which would cause the effects
checker to prematurely bail out after treating this as "invalid" code.
The overall effect is that we would not properly check for throwing
behavior in this case, allowing invalid code (as in the example) and
miscompiling valid code by not treating the call as throwing.

Fixes rdar://153926820.
2025-06-25 10:01:34 -07:00
Pavel Yaskevich
4132aa04f9 [Tests] NFC: Update all of the test-cases improved by changes to generic argument mismatch handling 2025-06-03 00:49:06 -07:00
Hamish Knight
abf0808173 [Sema] Continue type-checking for body when preamble fails
Skipping type-checking the body when the preamble fails to type-check
seems to be more of a historical artifact than intentional behavior.
Certain elements of the body may still get type-checked through
request evaluation, and as such may introduce autoclosures that won't
be properly contextualized. 

Make sure we continue type-checking the body even if the preamble
fails. We already invalidate any variables bound in the element
pattern, so downstream type-checking should be able to handle it
just fine. This ensures autoclosures get contextualized, and that
we're still able to provide semantic diagnostics for other issues in
the body.

rdar://136500008
2025-05-15 21:23:40 +01:00
Doug Gregor
1b94c3b3d6 Always emit "unsafe does not cover any unsafe constructs" warning
Check for unsafe constructs in all modes, so that we can emit the
"unsafe does not cover any unsafe constructs" warning consistently.
One does not need to write "unsafe" outside of strict memory safety
mode, but if you do... it needs to cover unsafe behavior.
2025-04-25 23:22:09 -07:00
Hamish Knight
2e1c37f1ab [ASTScope] Allow try in unfolded sequence to cover following elements
Rather than fixing-up in the parser, adjust the ASTScope logic such
that a `try` element in a SequenceExpr is considered as covering all
elements to the right of it. Cases where this isn't true are invalid,
and will be diagnosed during sequence folding. e.g:

```
0 * try foo() + bar()
_ = try foo() ~~~ bar() // Assuming `~~~` has lower precedence than `=`
```

This ensures we correctly handle `try` in assignment sequences, and
allows ASTGen to get the behavior for free.

rdar://132872235
2025-04-02 12:46:21 +01:00
Hamish Knight
b644cd87a9 [Sema] Ensure performStmtDiagnostics is called for CaseStmts
Previously we would check if we have a SwitchStmt,
and apply diagnostics such as `checkExistentialTypes`
to the CaseStmts individually. This however would
have been missed for `catch` statements. The change
to consistently call `performStmtDiagnostics` in
closures fixed this for `do-catch`'s in closures,
this commit fixes it for those outside of closures.
Because this is source breaking, the existential
diagnostic is downgraded to a warning until Swift
7 for catch statements specifically.

While here, also apply the ambiguous where clause
diagnostic to `catch` statements.
2024-11-12 18:26:54 +00:00
Daniel Rodríguez Troitiño
ba68faaed5 [test] Mark tests that use experimental/upcoming features as such
Find all the usages of `--enable-experimental-feature` or
`--enable-upcoming-feature` in the tests and replace some of the
`REQUIRES: asserts` to use `REQUIRES: swift-feature-Foo` instead, which
should correctly apply to depending on the asserts/noasserts mode of the
toolchain for each feature.

Remove some comments that talked about enabling asserts since they don't
apply anymore (but I might had miss some).

All this was done with an automated script, so some formatting weirdness
might happen, but I hope I fixed most of those.

There might be some tests that were `REQUIRES: asserts` that might run
in `noasserts` toolchains now. This will normally be because their
feature went from experimental to upcoming/base and the tests were not
updated.
2024-11-02 11:46:46 -07:00
Hamish Knight
89a3390aa1 [Sema] Move SingleValueStmtUsageChecker into pre-checking
These diagnostics are better suited for pre-checking
since we ought to be emitting them even if there's
some other error with the expression.
2024-08-30 18:55:48 +01:00
Hamish Knight
840ed6adff Merge pull request #75891 from hamishknight/fellthrough
[Sema] Improve handling of `fallthrough` in `if`/`switch` expressions
2024-08-19 17:59:24 +01:00
Hamish Knight
769805e714 [Sema] Fully allow fallthrough in switch expressions
Allow `fallthrough` to appear as the last statement
in the case of a `switch` expression. We already
allowed it in other positions, this was just an
oversight.

rdar://127670432
2024-08-14 19:59:05 +01:00
Alex Hoppen
577727ad55 Remove DiagnosticsEditorMode
Migrate the last diagnostic to be independent of `DiagnosticEditorMode` and remove that option.

rdar://133111163
2024-08-14 11:24:31 -07:00
Alex Hoppen
7d7eb6e61d [Sema/IDE] Emit same diagnostics for missing switch cases independent of editor mode 2024-08-12 14:01:22 -07:00
Alex Hoppen
66104395d7 [Sema/SourceKit] Emit same diagnostics for missing protocol requirements on the command line and in SourceKit
Some editors use diagnostics from SourceKit to replace build issues. This causes issues if the diagnostics from SourceKit are formatted differently than the build issues. Make sure they are rendered the same way, removing most uses of `DiagnosticsEditorMode`.

To do so, always emit the `add stubs for conformance` note (which previously was only emitted in editor mode) and remove all `; add <something>` suffixes from notes that state which requirements are missing.

rdar://129283608
2024-08-07 14:01:30 -07:00
Hamish Knight
d6d8d08608 [CS] Better diagnose conformance failures for EnumElementPatterns
Previously we could end up with a
ContextualMismatch fix and a MissingConformance fix
for different elements of the `matchTypes` disjunction,
leading to an ambiguity. Instead, avoid recording
the ContextualMismatch if we're matching an
existential, and tweak the MissingConformance
failure to have a custom diagnostic for
EnumElementPattern matching.
2024-07-15 12:47:50 +01:00
Holly Borla
775191cd94 [TypeCheckEffects] Diagnose type mismatches for thrown errors in contexts that
throw `Never`.

Previously, this mistake slipped through the effects checker because it used
a fallback type of `any Error` if a catch node had a null thrown error type.
This change also fixes an issue where thrown error type mismatches were not
diagnosed at all in autoclosures.
2024-06-15 20:38:20 -07:00
Holly Borla
32aba12db3 [Features] FullTypedThrows should only be enable-able in asserts builds.
The implementation of this experimental feature is not complete and it's
not ready to be enabled in production.
2024-06-14 21:21:27 -07:00
Pavel Yaskevich
d7a06284d3 [CSBindings] Optional object type variable should be connected to the optional
If both sides of an `OptionalObject` constraint are un-inferred, their
binding sets need to correctly reflect adjacency - a type variable
that represents optional would get "object" as an adjacency through
its potential binding (the binding is - "object" wrapped in a single
level of optional) and "object" type variable needs to get its parent
optional type variable added to its adjacency list explicitly.
Without this it would be possible to prematurely pick "object" before
its parent optional type variable.

Resolves: https://github.com/apple/swift/issues/73207
Resolves: rdar://126960579
2024-05-28 15:45:03 -07:00
saehejkang
28353765f3 [Diagnostics-Qol]: update c_style_for_stmt_removed error string 2024-04-27 22:35:54 -07:00
Rintaro Ishizaki
0e122544ca [CodeCompletion] Fix completion for 'catch' pattern bound values
Previously code completion for 'catch' pattern bound values didn't work
correctly because code completion type checker fails to type check the
value decl in the pattern.
That was because the body of the 'do' statement is not type checked, so
the thrown error is not determined, then falled backed to the default
'Never', which doesn't matches any patterns.
To resolve this, always type check the body when typechecking 'catch'
patterns. Also, pretends 'do {}' throws 'any Error' even without
any throwing expressions in the body.

rdar://126699879
2024-04-22 11:09:12 -07:00
banjun
a23810c9a4 fix typo to be CHECK-LABEL: 2024-03-26 19:18:53 +09:00
Doug Gregor
11715054e1 Move FullTypedThrows from "upcoming" to "experimental"
This feature is not fully implemented, and cannot be considered to be
"upcoming" until it is.
2024-03-01 12:24:19 -08:00
Sima Nerush
066f253d2e Merge pull request #70196 from simanerush/nested-pack-iteration
[SE-0408] Enable nested iteration
2024-01-27 10:18:12 -08:00
Sima Nerush
0b167b55b1 [ConstraintSystem] Cache pack element generic environments associated withfor-in loops over parameter packs to apply in getPackElementEnvironment. 2024-01-27 00:12:33 -08:00
Doug Gregor
be6393b7ca Use any Error for caught error type of an exhaustive, non-throwing do..catch
Prior to the introduction of typed throws, a `do..catch` always had a caught
error type of `any Error`, even if there were no throwing sites within
the `do` body. With typed throws, such a `do..catch` would have a
caught error type of `Never`, because it never throws. Unfortunately,
this causes code like the following to produce an error, because
`Never` cannot be pattern-matched to `HomeworkError.forgot`:

    func test() {
      do {
        try nonthrowing()
      } catch HomeworkError.forgot {
      } catch {
      }
    }

For source-compatibility reasons, adjust the caught error type to
`any Error` in this case, so we continue to accept the code above.
Don't do this adjustment under `FullTypedThrows`, because it's only
there for compatibility.

Fixes rdar://121526201.
2024-01-24 14:42:56 -08:00
Doug Gregor
b9c5aca15f Enable SE-0413 "Typed Throws" by default 2024-01-02 15:34:07 -08:00
Doug Gregor
ae5f66ac11 Record throwing property accesses as potential throw sites 2023-12-13 16:36:14 -08:00
Doug Gregor
956c330611 [Constraint solver] Encode thrown error type in references to subscripts
A reference to a subscript (whether instance or static) produces a
function type, which is then applied as part of an application
constraint. Make sure that we encode whether the subscript can throw,
and the thrown error type if present, in that function type. This lets
us correctly treat subscripts as a potential throw sites.
2023-12-13 15:22:21 -08:00
Doug Gregor
8ad137fb51 [Typed throws] Infer thrown error type for do..catch blocks within closures.
Start classifying all potential throw sites within a constraint
system and associate them with the nearest enclosing catch node. Then,
determine the thrown error type for a given catch node by taking the
union of the thrown errors at each potential throw site. Use this to
compute the error type thrown from the body of a `do..catch` block
within a closure.

This behavior is limited to the upcoming feature `FullTypedThrows`.
2023-12-13 14:59:23 -08:00
Doug Gregor
3369e3349f [Typed throws] Handle explicitly-specified throws on do..catch in closures
Teach the constraint system to use the same primitives as elsewhere to
determine the type context for a `throw` statement and the caught
error type within a `do..catch` statement. This makes
explicitly-specified `throws` work on `do..catch` that occurs in
closures.

Cleans up some redundant computations for caught error types.
2023-12-12 00:06:17 -08:00
Sima Nerush
b6d0afba1f Merge pull request #67594 from simanerush/simanerush/pack-iteration-impl
[SE-0408] Enable Pack Iteration
2023-12-07 17:09:48 -08:00
Doug Gregor
49b05cec84 [Typed throws] Make try? and try! into catch nodes
Both `try?` and `try!` are catch nodes, because they catch an error
thrown in their subexpression and handle it. Introduce an ASTScope for
all `try/try?/try1` expressions so we can find them, and model them as
catch nodes.

Fixes rdar://119216455.
2023-12-06 15:00:58 -08:00
Pavel Yaskevich
d9abf188cc Revert "[CSSimplify] Fix handling of holes by OptionalObject constraint"
This reverts commit 8b5e3848a1.
2023-12-04 11:36:20 -08:00
Sima Nerush
23485990e5 Diagnose that where clause is not supported 2023-12-03 21:51:41 -08:00
Sima Nerush
5832181077 Sema 2023-12-03 21:51:22 -08:00
Doug Gregor
cfe2b3c87d [Typed throws] Implement support for do throws(...) syntax
During the review of SE-0413, typed throws, the notion of a `do throws`
syntax for `do..catch` blocks came up. Implement that syntax and
semantics, as a way to explicitly specify the type of error that is
thrown from the `do` body in `do..catch` statement.
2023-12-02 07:37:47 -08:00
Rintaro Ishizaki
47f18d492e [ASTGen] Move regex literal parsing from SwiftCompilerSources to ASTGen
ASTGen always builds with the host Swift compiler, without requiring
bootstrapping, and is enabled in more places. Move the regex literal
parsing logic there so it is enabled in more host environments, and
makes use of CMake's Swift support. Enable all of the regex literal
tests when ASTGen is built, to ensure everything is working.

Remove the "AST" and "Parse" Swift modules from SwiftCompilerSources,
because they are no longer needed.
2023-11-16 10:59:23 -08:00
Doug Gregor
de16b53ab4 [Typed throws] Add compatibility carve-out within rethrows functions
Allow a rethrows function to call a typed-throws function that has a
specific form that looks like it only rethrows, i.e., it is generic
over its thrown error type and carries the thrown error type from its
closure parameters to itself. This is a compatibility carve-out to
allow existing rethrows functions to move to typed throws without
breaking their clients.

Note that it is possible to write a sneaky function that passes this
check but throws when it shouldn't, so this compatibility carve-out is
phased out with the upcoming feature FullTypedThrows.
2023-11-12 02:47:39 -08:00
Doug Gregor
47a6d67c40 Merge pull request #69653 from DougGregor/try-question-bang-never
Make sure we record "Never" as the thrown error type for try!/try?
2023-11-03 23:52:12 -07:00
Doug Gregor
b49d8b63bc Make sure we record "Never" as the thrown error type for try!/try? 2023-11-03 16:33:08 -07:00
Pavel Yaskevich
8b5e3848a1 [CSSimplify] Fix handling of holes by OptionalObject constraint
When "optional type" is a hole the simplification logic has to
simplify "object type" and mark all of its unresolved components
are potential holes, otherwise hole propagation won't work since
sometimes there is no other contextual information for such types
but "optional type".

Resolves: rdar://117871338
2023-11-03 10:13:09 -07:00
Doug Gregor
c90c055fb6 Record the thrown error type for try? and try! in the AST
SILGen wants this so it knows what kind of error its going to end up
with.
2023-10-31 14:18:31 -07:00
Pavel Yaskevich
5a66b62147 Merge pull request #69304 from xedin/rdar-117220710
[ConstraintSystem] Bring back one-way pattern solving for for-in stat…
2023-10-25 09:43:33 -07:00
Doug Gregor
5ad39c84e0 [Typed throws] Record thrown error types and conversions in the AST
For any operation that can throw an error, such as calls, property
accesses, and non-exhaustive do..catch statements, record the thrown
error type along with the conversion from that thrown error to the
error type expected in context, as appropriate. This will prevent
later stages from having to re-compute the conversion sequences.
2023-10-24 12:40:22 -07:00