forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 1
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 Co-Authored-By: "Jason Zaugg" <jzaugg@gmail.com>
- Loading branch information
Showing
11 changed files
with
336 additions
and
14 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,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,2 @@ | ||
2 | ||
-1 |
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 annotation.switch | ||
|
||
object Test { | ||
def test(s: String): Int = { | ||
(s : @switch) match { | ||
case "1" => 0 | ||
case null => -1 | ||
case _ => s.toInt | ||
} | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
println(test("2")) | ||
println(test(null)) | ||
} | ||
} |
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,73 @@ | ||
[[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)) | ||
[75][75]case1() | ||
else | ||
[56][56]matchEnd2() | ||
[56]case [56]2062528 => [133]if ([133][133][133]"BbBb".equals([133]x1)) | ||
[133][133]case3() | ||
else | ||
[56][56]matchEnd2() | ||
[56]case [56]3003444 => [104]if ([104][104][104]"asdf".equals([104]x1)) | ||
[104][104]case2() | ||
else | ||
[56][56]matchEnd2() | ||
[56]case [56]_ => [56][56]matchEnd2() | ||
}); | ||
[56]case1(){ | ||
[56][56]matchEnd1([93]1) | ||
}; | ||
[56]case2(){ | ||
[56][56]matchEnd1([122]2) | ||
}; | ||
[56]case3(){ | ||
[56][56]matchEnd1([133]if ([143]cond) | ||
[151]3 | ||
else | ||
[180]4) | ||
}; | ||
[56]matchEnd2(){ | ||
[56][56]matchEnd1([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,23 @@ | ||
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 Success(2) | ||
7 Failure(scala.MatchError: 7 (of class java.lang.String)) | ||
===== | ||
pig Success(1) | ||
dog Success(2) |
Oops, something went wrong.