-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Serialization/TypeChecker] Introduce ExtensibleEnums
feature
#79580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %empty-directory(%t/src) | ||
// RUN: split-file %s %t/src | ||
|
||
/// Build the library | ||
// RUN: %target-swift-frontend -emit-module %t/src/Lib.swift \ | ||
// RUN: -module-name Lib \ | ||
// RUN: -emit-module-path %t/Lib.swiftmodule \ | ||
// RUN: -enable-experimental-feature ExtensibleEnums | ||
|
||
// Check that the errors are produced when using enums from module with `ExtensibleEnums` feature enabled. | ||
// RUN: %target-swift-frontend -typecheck %t/src/TestChecking.swift \ | ||
// RUN: -swift-version 5 -module-name Client -I %t \ | ||
// RUN: -verify | ||
|
||
// Test to make sure that if the library and client are in the same package enums are checked exhaustively | ||
|
||
/// Build the library | ||
// RUN: %target-swift-frontend -emit-module %t/src/Lib.swift \ | ||
// RUN: -module-name Lib \ | ||
// RUN: -package-name Test \ | ||
// RUN: -emit-module-path %t/Lib.swiftmodule \ | ||
// RUN: -enable-experimental-feature ExtensibleEnums | ||
|
||
// Different module but the same package | ||
// RUN: %target-swift-frontend -typecheck %t/src/TestSamePackage.swift \ | ||
// RUN: -swift-version 5 -module-name Client -I %t \ | ||
// RUN: -package-name Test \ | ||
// RUN: -verify | ||
|
||
// REQUIRES: swift_feature_ExtensibleEnums | ||
|
||
//--- Lib.swift | ||
|
||
public enum E { | ||
case a | ||
} | ||
|
||
@frozen | ||
public enum F { | ||
case a | ||
case b | ||
} | ||
|
||
func test_same_module(e: E, f: F) { | ||
switch e { // Ok | ||
case .a: break | ||
} | ||
|
||
switch f { // Ok | ||
case .a: break | ||
case .b: break | ||
} | ||
} | ||
|
||
//--- TestChecking.swift | ||
import Lib | ||
|
||
func test(e: E, f: F) { | ||
// `E` is not marked as `@frozen` which means it gets new semantics | ||
|
||
switch e { | ||
// expected-error@-1 {{switch covers known cases, but 'E' may have additional unknown values, possibly added in future versions}} | ||
// expected-note@-2 {{handle unknown values using "@unknown default"}} | ||
case .a: break | ||
} | ||
|
||
switch e { // Ok (no warnings) | ||
case .a: break | ||
@unknown default: break | ||
} | ||
|
||
// `F` is marked as `@frozen` which means regular rules apply even with `ExtensibleEnums` feature enabled. | ||
|
||
switch f { // Ok (no errors because `F` is `@frozen`) | ||
case .a: break | ||
case .b: break | ||
} | ||
|
||
switch f { // expected-error {{switch must be exhaustive}} expected-note {{dd missing case: '.b'}} | ||
case .a: break | ||
} | ||
|
||
switch f { // expected-warning {{switch must be exhaustive}} expected-note {{dd missing case: '.b'}} | ||
case .a: break | ||
@unknown default: break | ||
} | ||
} | ||
|
||
//--- TestSamePackage.swift | ||
import Lib | ||
|
||
func test_no_default(e: E, f: F) { | ||
switch e { // Ok | ||
case .a: break | ||
} | ||
|
||
switch e { // expected-warning {{switch must be exhaustive}} expected-note {{dd missing case: '.a'}} | ||
@unknown default: break | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a test for this
|
||
switch f { // expected-error {{switch must be exhaustive}} expected-note {{dd missing case: '.b'}} | ||
case .a: break | ||
} | ||
|
||
switch f { // expected-warning {{switch must be exhaustive}} expected-note {{dd missing case: '.b'}} | ||
case .a: break | ||
@unknown default: break | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here can we add a test for this
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a test that exhaustively switches over f but also has an
@unknown default
here. That should generate a warning as well since the default isn't needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look into that in a separate PR. Regular exhaustivity rules apply here, so what ever that implementation does is going to be reflected.