Commit Graph

54 Commits

Author SHA1 Message Date
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
Pavel Yaskevich
7034ef92b6 Revert "Stricter enforcement of the "large space" heuristic" 2018-01-14 19:37:44 -08:00
Robert Widmann
43f06afef2 Defer analysis of large switch bodies
This presents a regression in diagnostic quality that is definitely
worth it not to lie to SILGen about whether a switch is covered or not.

At the same time, disable SIL’s unreachable diagnostic for ‘default’
clauses which would previously cause a warning to be emitted if the
default was proven to be unreachable.  This analysis is incomplete
anyways and can be done by Sema in the future if we desire.
2017-12-13 16:22:48 -05:00
Robert Widmann
8990bd00b6 Stricter enforcement of the "large space" heuristic
The Space Engine includes a heuristic that attempted a combinatorics-
based check to see if a pattern covered an insufficient amount of cases.
In straight-line switches this avoided computing a subspace match that
would have been quite expensive computationally.  However, when
expressive patterns (like tuple patterns) are used, it can fool the
check because the covered space is much larger than the actual size of
the space due to pattern overlap that counting like this simply can't detect.

Instead, just do the right thing and perform a space subtraction after
the existing preconditions for the heuristic are satisfied.

Resolves SR-6316
2017-12-13 14:35:53 -05:00
Robert Widmann
2823a6960f Properly interpolate negative AP literals
Integer and Floating literals are aware of their negation but
do not store the sign in the text of the value.  Retrieve the
sign bit and properly interpolate the text of the literal value
with it to distinguish negative and positive literals.
2017-08-01 09:48:53 -07:00
Florian Friedrich
e3fa190232 Check for duplicate literal switch cases
This adds a check for duplicate literal switch cases. It covers string, integer and float literals. Corresponding tests were added as well.
2017-07-31 21:19:18 +02:00
Robert Widmann
3292e6bc99 Lower Never-typed patterns to unreachable
Cuts off a crash in SILGen for switches over uninhabited types.
We can take this a step further and extend the definition of
"uninhabited" to product types with uninhabited components.
2017-07-17 18:59:15 -07:00
Beta
924ddd2678 Expand implicit tuple patterns in the AST 2017-06-08 16:15:30 -07:00
Beta
09c3f8f33b Rewrite sugared optional tuple patterns 2017-06-06 22:25:20 -07:00
Robert Widmann
bffa22a5c9 Add a size heuristic to the Space Engine
<rdar://32480026>

This is a particularly tricky tradeoff to have to make here.  On the one
hand, we want diagnostics about incomplete patterns to provide as much
information as possible.  On the other, pattern matrices grow
quasi-factorially in the size of the argument.  That is, an enum with 10
cases that is switched on as a 2-tuple/argument list creates a total
subspace covering of size 100.  For sufficiently large inputs, this can
DOS the Swift compiler.

It is simply not useful to present more than about 100 notes to the
user, nor is it useful to waste an enormous amount of time trying to
compute these subspaces for the limited information the diagnostic will
provide.  Instead, short circuit if the size of the enum is above some
arbitrary threshold (currently 128) and just offer to add a 'default'.

Notably, this change is not *technically* source compatible in that it
ignores the new '@_downgrade_exhaustivity_check'. However to hit up
against this heuristic would require the user to be switching over four
DispatchTimeIntervals in a quadruple or using an equivalently-sized
enormous enum case.  At which point we're hitting on the very reason
this heuristic exists.

There are definitely other, more informative, paths that we can take
here.  GHC, for example, seems to run a highly limited form of space
subtraction when the input space is too large, and simply reports the
top 3 missing cases along with some ellipses.  For now, I just want to
cut off this hang in the compiler.
2017-05-31 14:07:07 -07:00
Robert Widmann
af5ac911fa Add a safeguard for diagnostic minimality
To further cut down on the number of cases present,
add a filter that catches if the flattening algorithm ever hands us back
a pattern matrix that contains duplicate entries.  Note that this does
not, by definition, affect pattern completeness.
2017-05-25 11:26:03 -07:00