forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In the spirit of scala/scala#4418, which fixed SI-8731. The consensus seems to be that the intent of the @switch annotation is to warn when the generated bytecode may be poor, not merely if the compiler elects to not emit a tableswitch/lookupswitch. Note that the case threshold for determining whether a switch is emitted is implementation dependent, and currently varies between scalac and dotc. Also note that this implementation will not warn in instances where a switch will never be emitted (e.g. because the match is on a non-integral type) but the number of cases is below the warning threshold. This behavior is consistent with scalac, but may be surprising to the user if another case is added later and a warning suddenly appears. Not all spurious @switch warnings are addressed by this commit, see issue scala#5070 for an example.
- Loading branch information
Showing
12 changed files
with
109 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 | ||
} | ||
} |
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.