Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Matching strings makes switches (#8451)
Matching strings makes switches
- Loading branch information
Showing
10 changed files
with
352 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,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]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,29 @@ | ||
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) | ||
===== | ||
Ea 2236 Success(1) | ||
FB 2236 Success(2) | ||
cC 3136 Success(3) | ||
xx 3840 Success(4) | ||
null 0 Success(4) |
Oops, something went wrong.