Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switchable matches with string-typed scrutinee survive the pattern matcher in the same way as those on integer types do: as a series of `CaseDef`s with empty guard and literal pattern. Cleanup collates them by hash code and emits a switch on that. No sooner, so scala.js can emit a more JS-friendly implementation. Labels were used to avoid a proliferation of `throw new MatchError`. Works with nulls. Works with Unit. Enclosed "pos" test stands for positions, not positivity. Fixes scala/bug#11740
- Loading branch information
Showing
10 changed files
with
266 additions
and
15 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
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,7 @@ | ||
import annotation.switch | ||
class Switches { | ||
val cond = true | ||
def two = ("foo" : @switch) match { case "foo" => case "bar" => } | ||
def guard = ("foo" : @switch) match { case "z" => case "y" => case x if cond => } | ||
def colli = ("foo" : @switch) match { case "DB" => case "Ca" => } | ||
} |
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,16 @@ | ||
import scala.tools.partest.BytecodeTest | ||
import scala.tools.asm | ||
import scala.collection.JavaConverters._ | ||
import scala.PartialFunction.cond | ||
|
||
object Test extends BytecodeTest { | ||
def show: Unit = { | ||
val clasz = loadClassNode("Switches") | ||
List("two", "guard", "colli") foreach { meth => | ||
val mn = getMethod(clasz, meth) | ||
assert(mn.instructions.iterator.asScala.exists(isSwitchInsn), meth) | ||
} | ||
} | ||
def isSwitchInsn(insn: asm.tree.AbstractInsnNode) = | ||
cond(insn.getOpcode) { case asm.Opcodes.LOOKUPSWITCH | asm.Opcodes.TABLESWITCH => 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
[[syntax trees at end of patmat]] // newSource1.scala | ||
[6]package [6]<empty> { | ||
[6]class Switch extends [13][187]scala.AnyRef { | ||
[187]def <init>(): [13]Switch = [187]{ | ||
[187][187][187]Switch.super.<init>(); | ||
[13]() | ||
}; | ||
[21]def switch([28]s: [31]<type: [31]scala.Predef.String>, [39]cond: [45]<type: [45]scala.Boolean>): [21]Int = [56]{ | ||
[56]case <synthetic> val x1: [56]String = [56]s; | ||
[56][56]x1 match { | ||
[56]case [75]"AaAa" => [93]1 | ||
[56]case [104]"asdf" => [122]2 | ||
[133]case [133]"BbBb" => [133]if ([143]cond) | ||
[151]3 | ||
else | ||
[180]4 | ||
[56]case [56]_ => [56]throw [56][56][56]new [56]MatchError([56]x1) | ||
} | ||
} | ||
} | ||
} | ||
|
||
[[syntax trees at end of cleanup]] // newSource1.scala | ||
[6]package [6]<empty> { | ||
[6]class Switch extends [13][13]Object { | ||
[21]def switch([28]s: [31]<type: [31]scala.Predef.String>, [39]cond: [45]<type: [45]scala.Boolean>): [21]Int = [56]{ | ||
[56]case <synthetic> val x1: [56]String = [56]s; | ||
[56]{ | ||
[56][56]matchEnd1([56][56]if ([56][56]x1.eq([56]null)) | ||
[56]0 | ||
else | ||
[56][56]x1.hashCode() match { | ||
[56]case [56]2031744 => [75]if ([75][75][75]"AaAa".equals([75]x1)) | ||
[93]1 | ||
else | ||
[56][56]matchEnd2() | ||
[56]case [56]2062528 => [133]if ([133][133][133]"BbBb".equals([133]x1)) | ||
[133]if ([143]cond) | ||
[151]3 | ||
else | ||
[180]4 | ||
else | ||
[56][56]matchEnd2() | ||
[56]case [56]3003444 => [104]if ([104][104][104]"asdf".equals([104]x1)) | ||
[122]2 | ||
else | ||
[56][56]matchEnd2() | ||
[56]case [56]_ => [56][56]matchEnd2() | ||
}); | ||
[56]matchEnd2(){ | ||
[56]throw [56][56][56]new [56]MatchError([56]x1) | ||
}; | ||
[56]matchEnd1(x$1: [NoPosition]Int){ | ||
[56]x$1 | ||
} | ||
} | ||
}; | ||
[187]def <init>(): [13]Switch = [187]{ | ||
[187][187][187]Switch.super.<init>(); | ||
[13]() | ||
} | ||
} | ||
} | ||
|
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,18 @@ | ||
import scala.tools.partest._ | ||
|
||
object Test extends DirectTest { | ||
override def extraSettings: String = "-usejavacp -stop:cleanup -Vprint:patmat,cleanup -Vprint-pos" | ||
|
||
override def code = | ||
"""class Switch { | ||
| def switch(s: String, cond: Boolean) = s match { | ||
| case "AaAa" => 1 | ||
| case "asdf" => 2 | ||
| case "BbBb" if cond => 3 | ||
| case "BbBb" => 4 | ||
| } | ||
|} | ||
""".stripMargin.trim | ||
|
||
override def show(): Unit = Console.withErr(Console.out) { super.compile() } | ||
} |
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,20 @@ | ||
fido Success(dog) | ||
garfield Success(cat) | ||
wanda Success(fish) | ||
henry Success(horse) | ||
felix Failure(scala.MatchError: felix (of class java.lang.String)) | ||
deuteronomy Success(cat) | ||
===== | ||
AaAa 2031744 Success(1) | ||
BBBB 2031744 Success(2) | ||
BBAa 2031744 Failure(scala.MatchError: BBAa (of class java.lang.String)) | ||
cCCc 3015872 Success(3) | ||
ddDd 3077408 Success(4) | ||
EEee 2125120 Failure(scala.MatchError: EEee (of class java.lang.String)) | ||
===== | ||
A Success(()) | ||
X Failure(scala.MatchError: X (of class java.lang.String)) | ||
===== | ||
Success(3) | ||
null Failure(scala.MatchError: null) | ||
7 Failure(scala.MatchError: 7 (of class java.lang.String)) |
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,46 @@ | ||
// scalac: -Werror | ||
import annotation.switch | ||
import util.Try | ||
|
||
object Test extends App { | ||
|
||
def species(name: String) = (name.toLowerCase : @switch) match { | ||
case "fido" => "dog" | ||
case "garfield" | "deuteronomy" => "cat" | ||
case "wanda" => "fish" | ||
case "henry" => "horse" | ||
} | ||
List("fido", "garfield", "wanda", "henry", "felix", "deuteronomy").foreach { n => println(s"$n ${Try(species(n))}") } | ||
|
||
println("=====") | ||
|
||
def collide(in: String) = (in : @switch) match { | ||
case "AaAa" => 1 | ||
case "BBBB" => 2 | ||
case "cCCc" => 3 | ||
case x if x == "ddDd" => 4 | ||
} | ||
List("AaAa", "BBBB", "BBAa", "cCCc", "ddDd", "EEee").foreach { s => | ||
println(s"$s ${s.##} ${Try(collide(s))}") | ||
} | ||
|
||
println("=====") | ||
|
||
def unitary(in: String) = (in : @switch) match { | ||
case "A" => | ||
} | ||
List("A","X").foreach { s => | ||
println(s"$s ${Try(unitary(s))}") | ||
} | ||
|
||
println("=====") | ||
|
||
def nullFun(in: String) = (in : @switch) match { | ||
case "1" => 1 | ||
case null => 2 | ||
case "" => 3 | ||
} | ||
List("", null, "7").foreach { s => | ||
println(s"$s ${Try(nullFun(s))}") | ||
} | ||
} |