@@ -50,7 +50,7 @@ class TestBCode extends DottyBytecodeTest {
50
50
/** This test verifies that simple matches with `@switch` annotations are
51
51
* indeed transformed to a switch
52
52
*/
53
- @ Test def basicTransfromAnnotated = {
53
+ @ Test def basicSwitch = {
54
54
val source = """
55
55
|object Foo {
56
56
| import scala.annotation.switch
@@ -69,6 +69,71 @@ class TestBCode extends DottyBytecodeTest {
69
69
}
70
70
}
71
71
72
+ @ Test def switchWithAlternatives = {
73
+ val source =
74
+ """
75
+ |object Foo {
76
+ | import scala.annotation.switch
77
+ | def foo(i: Int) = (i: @switch) match {
78
+ | case 2 => println(2)
79
+ | case 1 | 3 | 5 => println(1)
80
+ | case 0 => println(0)
81
+ | }
82
+ |}
83
+ """ .stripMargin
84
+
85
+ checkBCode(source) { dir =>
86
+ val moduleIn = dir.lookupName(" Foo$.class" , directory = false )
87
+ val moduleNode = loadClassNode(moduleIn.input)
88
+ val methodNode = getMethod(moduleNode, " foo" )
89
+ assert(verifySwitch(methodNode))
90
+ }
91
+ }
92
+
93
+ @ Test def switchWithGuards = {
94
+ val source =
95
+ """
96
+ |object Foo {
97
+ | import scala.annotation.switch
98
+ | def foo(i: Int, b: Boolean) = (i: @switch) match {
99
+ | case 2 => println(3)
100
+ | case 1 if b => println(2)
101
+ | case 1 => println(1)
102
+ | case 0 => println(0)
103
+ | }
104
+ |}
105
+ """ .stripMargin
106
+
107
+ checkBCode(source) { dir =>
108
+ val moduleIn = dir.lookupName(" Foo$.class" , directory = false )
109
+ val moduleNode = loadClassNode(moduleIn.input)
110
+ val methodNode = getMethod(moduleNode, " foo" )
111
+ assert(verifySwitch(methodNode))
112
+ }
113
+ }
114
+
115
+ @ Test def matchWithDefaultNoThrowMatchError = {
116
+ val source =
117
+ """ class Test {
118
+ | def test(s: String) = s match {
119
+ | case "Hello" => 1
120
+ | case _ => 2
121
+ | }
122
+ |}
123
+ """ .stripMargin
124
+
125
+ checkBCode(source) { dir =>
126
+ val clsIn = dir.lookupName(" Test.class" , directory = false )
127
+ val clsNode = loadClassNode(clsIn.input)
128
+ val method = getMethod(clsNode, " test" )
129
+ val throwMatchError = instructionsFromMethod(method).exists {
130
+ case Op (Opcodes .ATHROW ) => true
131
+ case _ => false
132
+ }
133
+ assertFalse(throwMatchError)
134
+ }
135
+ }
136
+
72
137
@ Test def failTransform = {
73
138
val source = """
74
139
|object Foo {
0 commit comments