-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #14488: Scala.js: Add compiler support for scala.Enumeration.
This is the same logic that is used in the Scala.js compiler plugin for Scala 2. We catch ValDefs of the forms val SomeField = Value val SomeOtherField = Value(5) and rewrite them as val SomeField = Value("SomeField") val SomeOtherField = Value(5, "SomeOtherField") For calls to `Value` and `new Val` without name that we cannot rewrite, we emit warnings.
- Loading branch information
Showing
13 changed files
with
244 additions
and
21 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,60 @@ | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:6:4 ------------------------------------------------------------- | ||
6 | Value // error | ||
| ^^^^^ | ||
| Could not transform call to scala.Enumeration.Value. | ||
| The resulting program is unlikely to function properly as this operation requires reflection. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:10:9 ------------------------------------------------------------ | ||
10 | Value(4) // error | ||
| ^^^^^^^^ | ||
| Could not transform call to scala.Enumeration.Value. | ||
| The resulting program is unlikely to function properly as this operation requires reflection. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:15:15 ----------------------------------------------------------- | ||
15 | val a = Value(null) // error | ||
| ^^^^^^^^^^^ | ||
| Passing null as name to scala.Enumeration.Value requires reflection at run-time. | ||
| The resulting program is unlikely to function properly. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:16:15 ----------------------------------------------------------- | ||
16 | val b = Value(10, null) // error | ||
| ^^^^^^^^^^^^^^^ | ||
| Passing null as name to scala.Enumeration.Value requires reflection at run-time. | ||
| The resulting program is unlikely to function properly. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:20:10 ----------------------------------------------------------- | ||
20 | val a = new Val // error | ||
| ^^^^^^^ | ||
| Calls to the non-string constructors of scala.Enumeration.Val require reflection at run-time. | ||
| The resulting program is unlikely to function properly. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:21:10 ----------------------------------------------------------- | ||
21 | val b = new Val(10) // error | ||
| ^^^^^^^^^^^ | ||
| Calls to the non-string constructors of scala.Enumeration.Val require reflection at run-time. | ||
| The resulting program is unlikely to function properly. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:25:10 ----------------------------------------------------------- | ||
25 | val a = new Val(null) // error | ||
| ^^^^^^^^^^^^^ | ||
| Passing null as name to a constructor of scala.Enumeration.Val requires reflection at run-time. | ||
| The resulting program is unlikely to function properly. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:26:10 ----------------------------------------------------------- | ||
26 | val b = new Val(10, null) // error | ||
| ^^^^^^^^^^^^^^^^^ | ||
| Passing null as name to a constructor of scala.Enumeration.Val requires reflection at run-time. | ||
| The resulting program is unlikely to function properly. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:30:31 ----------------------------------------------------------- | ||
30 | protected class Val1 extends Val // error | ||
| ^^^ | ||
| Calls to the non-string constructors of scala.Enumeration.Val require reflection at run-time. | ||
| The resulting program is unlikely to function properly. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:31:31 ----------------------------------------------------------- | ||
31 | protected class Val2 extends Val(1) // error | ||
| ^^^^^^ | ||
| Calls to the non-string constructors of scala.Enumeration.Val require reflection at run-time. | ||
| The resulting program is unlikely to function properly. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:35:31 ----------------------------------------------------------- | ||
35 | protected class Val1 extends Val(null) // error | ||
| ^^^^^^^^^ | ||
| Passing null as name to a constructor of scala.Enumeration.Val requires reflection at run-time. | ||
| The resulting program is unlikely to function properly. | ||
-- Error: tests/neg-scalajs/enumeration-warnings.scala:36:31 ----------------------------------------------------------- | ||
36 | protected class Val2 extends Val(1, null) // error | ||
| ^^^^^^^^^^^^ | ||
| Passing null as name to a constructor of scala.Enumeration.Val requires reflection at run-time. | ||
| The resulting program is unlikely to function properly. |
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,37 @@ | ||
// scalac: -Xfatal-warnings | ||
|
||
class UnableToTransformValue extends Enumeration { | ||
val a = { | ||
println("oh, oh!") | ||
Value // error | ||
} | ||
val b = { | ||
println("oh, oh!") | ||
Value(4) // error | ||
} | ||
} | ||
|
||
class ValueWithNullName extends Enumeration { | ||
val a = Value(null) // error | ||
val b = Value(10, null) // error | ||
} | ||
|
||
class NewValWithNoName extends Enumeration { | ||
val a = new Val // error | ||
val b = new Val(10) // error | ||
} | ||
|
||
class NewValWithNullName extends Enumeration { | ||
val a = new Val(null) // error | ||
val b = new Val(10, null) // error | ||
} | ||
|
||
class ExtendsValWithNoName extends Enumeration { | ||
protected class Val1 extends Val // error | ||
protected class Val2 extends Val(1) // error | ||
} | ||
|
||
class ExtendsValWithNullName extends Enumeration { | ||
protected class Val1 extends Val(null) // error | ||
protected class Val2 extends Val(1, null) // error | ||
} |
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// scalajs: --skip --pending | ||
|
||
object Q extends Enumeration { | ||
val A = Value("A") | ||
val B = Value("B") | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// scalajs: --skip --pending | ||
|
||
object Test extends App { | ||
|
||
object Color extends Enumeration { | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// scalajs: --skip --pending | ||
|
||
object X extends Enumeration { | ||
val Y = Value | ||
} | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// scalajs: --skip --pending | ||
|
||
object t extends Enumeration { val a, b = Value } | ||
|
||
object Test extends App { | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// scalajs: --skip --pending | ||
// scalajs: --skip | ||
|
||
object Days extends Enumeration { | ||
type Day = DayValue | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// scalajs: --skip --pending | ||
|
||
object Test extends Enumeration { | ||
val foo = Value | ||
def bar = withName("foo") | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// scalajs: --skip --pending | ||
|
||
object L extends Enumeration { | ||
val One, Two, Three = Value | ||
} | ||
|