Commit Graph

64 Commits

Author SHA1 Message Date
James Brown
3649dcbf04 [TypeChecker] Bool literals in switch
Bool literals are regarded as expr patterns when
switching over Optional<Bool>. Simplify the expr
by converting to their optional counterparts.
2025-03-05 09:33:24 -05: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
Alex Hoppen
7d7eb6e61d [Sema/IDE] Emit same diagnostics for missing switch cases independent of editor mode 2024-08-12 14:01:22 -07:00
Holly Borla
a7920ce50b [Features] Replace -enable-nonfrozen-enum-exhaustivity-diagnostics with an
upcoming feature.

The bespoke flag still works as a way to enable the `NonfrozenEnumExhaustivity`
upcoming feature. `NonfrozenEnumExhaustivity` is enabled by default in the
Swift 6 language mode as errors, and enabled by default in the Swift 5 language
mode as warnings.
2024-05-06 20:56:20 -07:00
Allan Shortlidge
3acc0d6655 SILGen/Sema: Avoid diagnosing @unknown default switch cases as unreachable.
Suppose you have an exhaustive switch statement which matches all the cases of
a Swift enum defined in a different module named `External`:

```
import External

var e: External.SomeEnum = //...

switch e {
case .a: break
}
```

If `External` is compiled with library evolution and `SomeEnum` is not frozen,
then the compiler will warn:

```
warning: switch covers known cases, but 'SomeEnum' may have additional unknown values
```

You add an `@unknown default` to the switch to resolve this warning. Now
suppose in another build configuration, `External` is built _without_ library
evolution. The compiler will complain about the unreachability of the default
case:

```
warning: Default will never be executed
```

These contradictory compiler diagnostics encourage the developer to change the
code in a way that will cause a diagnostic in the other configuration.
Developers should have the tools to address all warning diagnostics in a
reasonable fashion and this is a case where the compiler makes that especially
difficult. Given that writing `@unknown default` instead of `default` is a very
intentional action that would be the result of addressing the library evolution
configuration, it seems reasonable to suppress the `Default will never be
executed` diagnostic.
2024-03-09 12:26:22 -08:00
Nishith Shah
8e2e625543 [Diagnostics] Use imperative msg for protocol conformance & switch-case fixits
This commit changes fixit messages from a question/suggestion to an
imperative message for protocol conformances and switch-case. Addresses
https://github.com/apple/swift/issues/67510.
2023-08-13 22:34:26 -07:00
Anthony Latsis
14b70f306b DiagnosticVerifier: Default expected fix-it start line to the diagnostic's 2023-03-08 12:10:27 +03:00
Anthony Latsis
cafcb868df Gardening: Migrate test suite to GH issues: Sema (2/2) 2022-09-02 11:04:36 +03:00
Robert Widmann
cddf73ecdb [Gardening] Clean Up OS-Test Patterns Across The Codebase
Clean up a few general patterns that are now obviated by canImport

This aligns more generally with the cleanup that the Swift Package
Manager has already done in their automated XCTest-plumbing tool in
apple/swift-package-manager#1826.
2020-06-30 22:55:58 -07:00
Robert Widmann
cc68375efd Adapt to Tuple-In-OptionalSome Patterns in Space Projection
The space engine goes out of its way to rewrite OptionalSome patterns
using the postfix-? sugar into .some(...). Unfortunately, it performed
the following remapping:

(x, y, z, ...)? -> .some(x, y, z, ...)

This syntactic form used to behave correctly. However, we are no longer
flattening nested tuples so the correct rewrite is:

(x, y, z, ...)? -> .some((x, y, z))

Correct this space projection rule.

rdar://62200966
2020-05-13 09:17:12 -07:00
Varun Gandhi
d58bf546be [Diagnostics] Improve diagnostics for implicit (un)tupling. (#28076)
Fixes rdar://problem/56436226.
2020-02-13 17:38:21 -08:00
Varun Gandhi
f5cb50c1a8 [Sema] Implicitly tuple a pattern if applicable.
Fixes rdar://problem/58425942.
2020-01-14 21:14:56 -08:00
Varun Gandhi
e0a9a74e41 [NFC] Add minimal test case for rdar://problem/58578342. 2020-01-14 21:11:51 -08:00
Owen Voorhees
badcc2c442 [Diagnostics] Don't include @unknown default in the empty switch fix-it (#28476) 2019-12-02 19:43:48 +00:00
Andrew Litteken
d7e977bf68 adding space engine changes
adding full spaceengine changes

using clang-format

adding argument name size comparison

adding space engine changes

adding full spaceengine changes

using clang-format

adding argument printing to interleave function

updating tests for new print formatting

removing extra checks from constructor subspace checking

adding alternate path if the argument and space lenght are the same

clang format

adding FIXME for code performance

adding fixme for showing constructor

switching direction of beginningparenthesis
2019-10-03 15:57:24 -05:00
Varun Gandhi
881de8816d Add codegen test cases to make sure that patching patterns works.
There is a small chance that codegen and everything works fine, but the
generated code is wrong because of mismatched expectations on two sides,
so we have some tests to catch that.
2019-08-12 09:58:22 -07:00
Varun Gandhi
25e5b15870 Add test cases for more permutations.
Make sure that we still emit warnings even if the let bindings are in
different positions.
2019-08-12 09:58:22 -07:00
Varun Gandhi
e365b6be7d Implicitly untuple patterns if applicable and add warnings. 2019-08-12 09:58:22 -07:00
Varun Gandhi
afd83ba2b5 Fix related bug in pattern projection, handling the tuple case correctly. 2019-07-31 16:29:57 -07:00
Varun Gandhi
3892c55785 Fixes bug in exhaustive matching check SR-11160.
Also fixes rdar://problem/53312914.

The fact that ParenType is being used to distinguish the two cases makes me
uncomfortable but I don't have better ideas.

Related:
- Fixed a bug in pattern projection which I encountered once I fixed SR-11160.
  This is the 3 line change around conArgSpaces.
- Opened SR-11212 for ill-typed patterns that are permitted to compile.

Unrelated cleanup:
- Removed a redundant switch case in 'isSubspace'.
- Added names for referenced papers for faster lookup.
2019-07-31 16:29:57 -07:00
Varun Gandhi
bbf7e7ae68 Add test case characterizing issue in SR-11160. 2019-07-31 16:29:57 -07:00
Varun Gandhi
70d784bf46 Add compatibility tests for implicit (un)tupling in patterns (SR-11212). 2019-07-29 18:11:25 -07:00
Ben Cohen
e9d4687e31 De-underscore @frozen, apply it to structs (#24185)
* De-underscore @frozen for enums

* Add @frozen for structs, deprecate @_fixed_layout for them

* Switch usage from _fixed_layout to frozen
2019-05-30 17:55:37 -07:00
Jordan Rose
d36a7393b0 Fix issue with expression patterns in switch exhaustivity checking (#23804)
Expression patterns (and cast patterns) don't actually contribute to
the exhaustivity of a switch statement---if you're matching against a
String, matching "abc" doesn't meaningfully reduce the full space of
the values you have to match. This was already handled, but didn't do
the right thing in a particular case involving a tuple payload in an
enum after the Space Engine (exhaustivity checker) optimizations that
went out in Swift 5.

https://bugs.swift.org/browse/SR-10301
2019-04-05 09:14:54 -07:00
Slava Pestov
1159af50d9 Rename -enable-resilience to -enable-library-evolution and make it a driver flag
Fixes <rdar://problem/47679085>.
2019-03-14 22:24:26 -04:00
Jordan Rose
4a8f81db2b Special-case diagnostic for when you just need @unknown default (#21695)
This is a new feature of Swift 5 mode, so it deserves at least a
little bit of explanation right in the diagnostic. If you have an
otherwise-fully-covered switch but can't assume the enum is frozen,
you'll now get this message:

    switch covers known cases, but 'MusicGenre' may have additional
    unknown values

Furthermore, if the enum comes from a system header, it looks like
this:

    switch covers known cases, but 'NSMusicGenre' may have additional
    unknown values, possibly added in future versions

...to further suggest the idea that even though your switch is covered
/now/, it might not handle everything in the /future/. This extra bit
is limited to system headers to avoid showing up on C enums defined in
your own project, for which it sounds silly. (The main message is
still valid though, since you can cram whatever you want into a C
enum, and people use this pattern to implement "private cases".)

rdar://problem/39367045
2019-01-08 08:45:23 -08:00
Robert Widmann
3102ed0a7a Remove regression tests for the dead size heuristic 2018-12-15 23:38:35 -05:00
Robert Widmann
6115ed9f7c Test for SR-6652, SR-6316 2018-12-15 23:21:18 -05:00
Robert Widmann
04c88aa822 Drop the coverage size heuristic
In light of the invocation limits placed on space subtraction, this grossly incorrect check is being dropped.

Resolves a source of miscompiles in mostly machine-generated code
including SR-6652 and SR-6316.
2018-12-15 23:19:04 -05:00
Matt Diephouse
cd2daa931f Fix assertion from empty switch over uninhabited enum
Fixes SR-8933.

Swift's uninhabited checking is conservative. An enum might not be found to be uninhabited, but all of its cases might. In that case, the switch can be empty even though the type isn't known to be uninhabited.

Fix an assertion that assumed there would always be at least one case for types that aren't known to be uninhabited.
2018-10-22 06:46:46 -04:00
Robert Widmann
2fb5afb755 Remove the @_downgrade_exhaustivity_check hack
This hack was only needed for Swift 3 mode in a narrow case.  Flush it out of the compiler so we can simplify the space engine.
2018-08-24 10:54:43 -07:00
Jordan Rose
e783027061 Space Engine: uninhabited types map to empty spaces (#17680)
Without this, the compiler ended up complaining about missing cases
that can't actually occur, like `Optional<Never>.some(_)`. This was a
regression from Swift 4.1.

https://bugs.swift.org/browse/SR-8125
2018-07-02 18:27:46 -07:00
David Ungar
41b2281c03 Update tests to new message 2018-05-02 15:49:29 -07:00
David Ungar
9c74f8108d Fix tests for new diagnostic. 2018-05-02 15:49:29 -07:00
Robert Widmann
644846ffee [SR-7554] Re-project cast subpatterns
A corner-case left over from an earlier fix that made exhaustiveness
checker aware of irrefutable coercions.  If the coercion occured as part
of a sub-pattern, that pattern would be projected with the wrong type
and would fail the intersection test.  Project the pattern with the type
of the scrutinee instead.
2018-04-26 20:54:59 -04:00
Robert Widmann
7edc266c3d [SR-7492] Project over-parenthesized patterns properly
Projection assumed if it ever hit a case where an argument pattern
contained extra parentheses that the user was trying to create
a var pattern to bind the entire argument tuple.

enum Foo {
  case bar(Int, String, Float)
}

switch fooVal {
  case bar(let x): ...
}

This breaks in the presence of tuple patterns with extra parentheses.
Treat these patterns explicitly when projecting them.
2018-04-26 16:28:46 -04:00
Jordan Rose
797901fc41 Enable @unknown default warnings by default in Swift 5 mode (#16045)
Note that I said "warnings"; we're going to be more cautious about
rollout and just make this a warning in Swift 5 mode, with /no/
diagnostics in Swift 3 and 4. Users are still free to use `@unknown
default` in these modes, and they'll get a fatal run-time error if
they don't and an unexpected case actually shows up.

rdar://problem/29324688
2018-04-20 17:04:31 -07:00
Jordan Rose
a9f26ab893 Don't crash when using @unknown default with a non-enum type.
Also improve the error message when using it with a very large space.

https://bugs.swift.org/browse/SR-7408
2018-04-11 19:28:39 -07:00
Jordan Rose
7405d515be Don't include unavailable cases in switch exhaustivity checking (#15849)
At one point compiler wouldn't let you use them in matches, so people have
had to use catch-all cases instead. SILGen already handles this because of
@_downgrade_exhaustivity_check, as well as non-exhaustive enums in
Swift 4 mode.

rdar://problem/33246586
2018-04-10 14:00:14 -07:00
Jordan Rose
7c689c322e Prefer @unknown default over @unknown case _ in diagnostics
(and fix-its)
2018-04-05 17:54:49 -07:00
Jordan Rose
ceb51eea80 Clean up fix-it generation for missing cases, esp. with '@unknown'
- Combine the common logic for editor mode and non-editor mode.
- Do a better job minimizing fix-its.
- If '@unknown' is the only missing case, put `fatalError()` in the
  Xcode placeholder, since that's what the compiler would have done.
2018-04-05 16:35:15 -07:00
Jordan Rose
bdb8388721 Allow '@unknown' to match any enums not explicitly marked frozen
If a client wants to defend themselves against new cases in libraries
they use, or even in their own code, they're allowed to.
2018-04-05 16:35:15 -07:00
Jordan Rose
2a0f9c3eb0 '@unknown' can match unknown cases in nested positions
That is, when matching non-frozen enums at non-top-level positions:

    switch (nonFrozenEnum1, nonFrozenEnum2) {
    case (.singleKnownCase1, .singleKnownCase2): ...
    unknown: ...
    }

...it's sufficient to use '@unknown' to match

  (.singleKnownCase1, .someFutureCase2)
  (.someFutureCase1, .singleKnownCase2)
  (.someFutureCase1, .someFutureCase2)
2018-04-05 16:35:15 -07:00
Jordan Rose
054658b316 Downgrade exhaustivity errors to warnings if '@unknown' is present
The other half of '@unknown' in Sema. Again, the diagnostics here
could be improved; rather than a generic "switch must be exhaustive",
it could say something about unknown case handling known cases.

One interesting detail here: '@unknown' is only supposed to match
/fully/ missing cases. If a case is /partly/ accounted for, not
handling the rest is still an error, even if an unknown case is
present.

This only works with switches over single enum values, not values that
contain an enum with unknown cases. That's coming in a later commit.
(It was easier to get this part working first.)
2018-04-05 16:35:14 -07:00
Jordan Rose
3e2411acd4 Diagnose when switching on a non-frozen enum without a catch-all case
The first half of Sema support for '@unknown'. The other part is
handling when the user /does/ write '@unknown', which results in
/other/ things being downgraded to warnings.

The diagnostics here are still pretty minimal; they should explain
what's going on with '@unknown' to someone who hasn't read the Swift 5
release notes.
2018-04-05 16:35:14 -07:00
Slava Pestov
e1f50b2d36 SE-0193: Rename @_inlineable to @inlinable, @_versioned to @usableFromInline 2018-03-30 21:55:30 -07:00
Jordan Rose
00361df52b Put non-frozen enum exhaustivity diagnostics behind a frontend flag
...spelled '-enable-nonfrozen-enum-exhaustivity-diagnostics'. This
is for staging purposes.
2018-03-20 10:39:02 -07:00
Jordan Rose
7c60f1c895 Diagnose uncovered switches on non-frozen enums
Warn in Swift 4 mode and error in Swift 5 mode when switching on a
non-frozen enum without providing a default case.

Note that this is a preliminary implementation, in order to test the
rest of the feature.
2018-03-20 10:39:02 -07:00
gregomni
1b94727882 Further revision for SR-6975: if we're coercing with no subpattern then we've
covered the whole space of the coercion type, but if there is a subpattern, then we're really only covering the subpattern's type space still.
2018-03-14 23:45:23 -07:00
gregomni
bb6f27d852 Ignore non-useful casts during switch exhaustiveness checking. 2018-02-11 11:54:12 -08:00