-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #9776: Don't issue a @switch warning when fewer than 4 cases #9852
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
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import scala.annotation.switch | ||
|
||
sealed trait Fruit | ||
|
||
object Fruit { | ||
case object Apple extends Fruit | ||
case object Banana extends Fruit | ||
case object Lemon extends Fruit | ||
case object Lime extends Fruit | ||
case object Orange extends Fruit | ||
|
||
def isCitrus(fruit: Fruit): Boolean = | ||
(fruit: @switch) match { // error Could not emit switch for @switch annotated match | ||
case Orange => true | ||
case Lemon => true | ||
case Lime => true | ||
case _ => false | ||
} | ||
} | ||
|
||
|
||
sealed trait TaggedFruit { | ||
def tag: Int | ||
} | ||
|
||
object TaggedFruit { | ||
case object Apple extends TaggedFruit { | ||
val tag = 1 | ||
} | ||
case object Banana extends TaggedFruit { | ||
val tag = 2 | ||
} | ||
case object Orange extends TaggedFruit { | ||
val tag = 3 | ||
} | ||
|
||
def isCitrus(fruit: TaggedFruit): Boolean = | ||
(fruit.tag: @switch) match { // error Could not emit switch for @switch annotated match | ||
case Apple.tag => true | ||
case 2 => true | ||
case 3 => true | ||
case _ => false | ||
} | ||
|
||
// fewer than four cases, so no warning | ||
def succ1(fruit: TaggedFruit): Boolean = | ||
(fruit.tag: @switch) match { | ||
case 3 => false | ||
case 2 | Apple.tag => true | ||
} | ||
|
||
// fewer than four cases, so no warning | ||
def succ2(fruit: TaggedFruit): Boolean = | ||
(fruit.tag: @switch) match { | ||
case 3 => false | ||
case 2 => true | ||
case Apple.tag => true | ||
} | ||
} |
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,38 @@ | ||
import scala.annotation.switch | ||
|
||
sealed trait Fruit | ||
|
||
object Fruit { | ||
case object Apple extends Fruit | ||
case object Banana extends Fruit | ||
case object Orange extends Fruit | ||
|
||
def isCitrus(fruit: Fruit): Boolean = | ||
(fruit: @switch) match { | ||
case Orange => true | ||
case _ => false | ||
} | ||
} | ||
|
||
|
||
sealed trait TaggedFruit { | ||
def tag: Int | ||
} | ||
|
||
object TaggedFruit { | ||
case object Apple extends TaggedFruit { | ||
val tag = 1 | ||
} | ||
case object Banana extends TaggedFruit { | ||
val tag = 2 | ||
} | ||
case object Orange extends TaggedFruit { | ||
val tag = 3 | ||
} | ||
|
||
def isCitrus(fruit: TaggedFruit): Boolean = | ||
(fruit.tag: @switch) match { | ||
case 3 => true | ||
case _ => false | ||
} | ||
} |
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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.
Why do we have this branch? Shouldn't it always be 1?
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.
The general idea is to limit the warnings to when
numTypes(original.cases) >= MinSwitchCases
https://github.com/lampepfl/dotty/blob/609d8d6bdbe9be33080967cdac478a6957237356/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala#L59-L60
However value classes are special cased as an attempt at "best-effort" support. Consider:
which results in
numTypes(original.cases)
2
{ IntAnyVal, Ten.type }
numTypes(resultCases)
0
We want to warn here since the
switch
was not emitted despite a sufficient number of cases, thus we use a lowercaseThreshold
for value classes.