-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #9809: enable static fields in Scala.js and change forwarders #9955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e181bfc
enable static fields on classes
bishabosha 240f618
fix #9809: when compiling scala.js - enum value forwarders are defs
bishabosha f0395c8
add tests for enums compiled with Scala.js
bishabosha 07de522
add pattern match to scala.js enum test
bishabosha 66eefc0
err if assign static field in other scalajs unit
bishabosha dc31c9d
add comment about special case
bishabosha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
-- Error: tests/neg-scalajs/js-enums.scala:4:5 ------------------------------------------------------------------------- | ||
4 |enum MyEnum extends js.Object: // error | ||
|^ | ||
|MyEnum extends scala.reflect.Enum which does not extend js.Any. | ||
5 | case Foo | ||
-- Error: tests/neg-scalajs/js-enums.scala:9:5 ------------------------------------------------------------------------- | ||
7 |@js.native | ||
8 |@JSGlobal | ||
9 |enum MyEnumNative extends js.Object: // error | ||
|^ | ||
|MyEnumNative extends scala.reflect.Enum which does not extend js.Any. | ||
10 | case Bar | ||
-- Error: tests/neg-scalajs/js-enums.scala:12:5 ------------------------------------------------------------------------ | ||
12 |enum MyEnumAny extends js.Any: // error | ||
|^ | ||
|Non-native JS classes and objects cannot directly extend AnyRef. They must extend a JS class (native or not). | ||
13 | case Foo | ||
-- Error: tests/neg-scalajs/js-enums.scala:17:5 ------------------------------------------------------------------------ | ||
15 |@js.native | ||
16 |@JSGlobal | ||
17 |enum MyEnumNativeAny extends js.Any: // error | ||
|^ | ||
|MyEnumNativeAny extends scala.reflect.Enum which does not extend js.Any. | ||
18 | case Bar |
This file contains hidden or 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.scalajs.js | ||
import scala.scalajs.js.annotation._ | ||
|
||
enum MyEnum extends js.Object: // error | ||
case Foo | ||
|
||
@js.native | ||
@JSGlobal | ||
enum MyEnumNative extends js.Object: // error | ||
case Bar | ||
|
||
enum MyEnumAny extends js.Any: // error | ||
case Foo | ||
|
||
@js.native | ||
@JSGlobal | ||
enum MyEnumNativeAny extends js.Any: // error | ||
case Bar |
169 changes: 169 additions & 0 deletions
169
tests/sjs-junit/test/org/scalajs/testsuite/compiler/EnumTestScala3.scala
This file contains hidden or 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,169 @@ | ||
package org.scalajs.testsuite.compiler | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
|
||
class EnumTestScala3: | ||
import EnumTestScala3._ | ||
|
||
@Test def testColor1(): Unit = | ||
import EnumTestScala3.{Color1 => Color} | ||
|
||
def code(c: Color): Character = c match | ||
case Color.Red => 'R' | ||
case Color.Green => 'G' | ||
case Color.Blue => 'B' | ||
|
||
assert(Color.Red.ordinal == 0) | ||
assert(Color.Green.ordinal == 1) | ||
assert(Color.Blue.ordinal == 2) | ||
assert(Color.Red.productPrefix == "Red") | ||
assert(Color.Green.productPrefix == "Green") | ||
assert(Color.Blue.productPrefix == "Blue") | ||
assert(Color.valueOf("Red") == Color.Red) | ||
assert(Color.valueOf("Green") == Color.Green) | ||
assert(Color.valueOf("Blue") == Color.Blue) | ||
assert(Color.valueOf("Blue") != Color.Red) | ||
assert(Color.valueOf("Blue") != Color.Green) | ||
assert(Color.values(0) == Color.Red) | ||
assert(Color.values(1) == Color.Green) | ||
assert(Color.values(2) == Color.Blue) | ||
assert(code(Color.Red) == 'R') | ||
assert(code(Color.Green) == 'G') | ||
assert(code(Color.Blue) == 'B') | ||
|
||
end testColor1 | ||
|
||
@Test def testColor2(): Unit = // copied from `color1` | ||
import EnumTestScala3.{Color2 => Color} | ||
|
||
def code(c: Color): Character = c match | ||
case Color.Red => 'R' | ||
case Color.Green => 'G' | ||
case Color.Blue => 'B' | ||
|
||
assert(Color.Red.ordinal == 0) | ||
assert(Color.Green.ordinal == 1) | ||
assert(Color.Blue.ordinal == 2) | ||
assert(Color.Red.productPrefix == "Red") | ||
assert(Color.Green.productPrefix == "Green") | ||
assert(Color.Blue.productPrefix == "Blue") | ||
assert(Color.valueOf("Red") == Color.Red) | ||
assert(Color.valueOf("Green") == Color.Green) | ||
assert(Color.valueOf("Blue") == Color.Blue) | ||
assert(Color.valueOf("Blue") != Color.Red) | ||
assert(Color.valueOf("Blue") != Color.Green) | ||
assert(Color.values(0) == Color.Red) | ||
assert(Color.values(1) == Color.Green) | ||
assert(Color.values(2) == Color.Blue) | ||
assert(code(Color.Red) == 'R') | ||
assert(code(Color.Green) == 'G') | ||
assert(code(Color.Blue) == 'B') | ||
|
||
end testColor2 | ||
|
||
@Test def testCurrency1(): Unit = | ||
import EnumTestScala3.{Currency1 => Currency} | ||
|
||
def code(c: Currency): String = c match | ||
case Currency.Dollar => "USD" | ||
case Currency.SwissFanc => "CHF" | ||
case Currency.Euro => "EUR" | ||
|
||
assert(Currency.Dollar.ordinal == 0) | ||
assert(Currency.SwissFanc.ordinal == 1) | ||
assert(Currency.Euro.ordinal == 2) | ||
assert(Currency.Dollar.productPrefix == "Dollar") | ||
assert(Currency.SwissFanc.productPrefix == "SwissFanc") | ||
assert(Currency.Euro.productPrefix == "Euro") | ||
assert(Currency.valueOf("Dollar") == Currency.Dollar) | ||
assert(Currency.valueOf("SwissFanc") == Currency.SwissFanc) | ||
assert(Currency.valueOf("Euro") == Currency.Euro) | ||
assert(Currency.valueOf("Euro") != Currency.Dollar) | ||
assert(Currency.valueOf("Euro") != Currency.SwissFanc) | ||
assert(Currency.values(0) == Currency.Dollar) | ||
assert(Currency.values(1) == Currency.SwissFanc) | ||
assert(Currency.values(2) == Currency.Euro) | ||
assert(Currency.Dollar.dollarValue == 1.00) | ||
assert(Currency.SwissFanc.dollarValue == 1.09) | ||
assert(Currency.Euro.dollarValue == 1.18) | ||
assert(code(Currency.Dollar) == "USD") | ||
assert(code(Currency.SwissFanc) == "CHF") | ||
assert(code(Currency.Euro) == "EUR") | ||
|
||
|
||
end testCurrency1 | ||
|
||
@Test def testCurrency2(): Unit = // copied from `testCurrency1` | ||
import EnumTestScala3.{Currency2 => Currency} | ||
|
||
def code(c: Currency): String = c match | ||
case Currency.Dollar => "USD" | ||
case Currency.SwissFanc => "CHF" | ||
case Currency.Euro => "EUR" | ||
|
||
assert(Currency.Dollar.ordinal == 0) | ||
assert(Currency.SwissFanc.ordinal == 1) | ||
assert(Currency.Euro.ordinal == 2) | ||
assert(Currency.Dollar.productPrefix == "Dollar") | ||
assert(Currency.SwissFanc.productPrefix == "SwissFanc") | ||
assert(Currency.Euro.productPrefix == "Euro") | ||
assert(Currency.valueOf("Dollar") == Currency.Dollar) | ||
assert(Currency.valueOf("SwissFanc") == Currency.SwissFanc) | ||
assert(Currency.valueOf("Euro") == Currency.Euro) | ||
assert(Currency.valueOf("Euro") != Currency.Dollar) | ||
assert(Currency.valueOf("Euro") != Currency.SwissFanc) | ||
assert(Currency.values(0) == Currency.Dollar) | ||
assert(Currency.values(1) == Currency.SwissFanc) | ||
assert(Currency.values(2) == Currency.Euro) | ||
assert(Currency.Dollar.dollarValue == 1.00) | ||
assert(Currency.SwissFanc.dollarValue == 1.09) | ||
assert(Currency.Euro.dollarValue == 1.18) | ||
assert(code(Currency.Dollar) == "USD") | ||
assert(code(Currency.SwissFanc) == "CHF") | ||
assert(code(Currency.Euro) == "EUR") | ||
|
||
end testCurrency2 | ||
|
||
@Test def testOpt(): Unit = | ||
|
||
def encode[T <: AnyVal](t: Opt[T]): T | Null = t match | ||
case Opt.Sm(t) => t | ||
case Opt.Nn => null | ||
|
||
assert(Opt.Sm(1).ordinal == 0) | ||
assert(Opt.Nn.ordinal == 1) | ||
assert(Opt.Sm(1).productPrefix == "Sm") | ||
assert(Opt.Nn.productPrefix == "Nn") | ||
assert(Opt.valueOf("Nn") == Opt.Nn) | ||
assert(Opt.values(0) == Opt.Nn) | ||
assert(Opt.Sm("hello").value == "hello") | ||
assert(encode(Opt.Sm(23)) == 23) | ||
assert(encode(Opt.Nn) == null) | ||
|
||
end testOpt | ||
|
||
object EnumTestScala3: | ||
|
||
enum Color1 derives Eql: | ||
case Red, Green, Blue | ||
|
||
enum Color2 extends java.lang.Enum[Color2] derives Eql: | ||
case Red, Green, Blue | ||
|
||
// test "non-simple" cases with anonymous subclasses | ||
enum Currency1(val dollarValue: Double) derives Eql: | ||
case Dollar extends Currency1(1.0) | ||
case SwissFanc extends Currency1(1.09) | ||
case Euro extends Currency1(1.18) | ||
|
||
enum Currency2(val dollarValue: Double) extends java.lang.Enum[Currency2] derives Eql: | ||
case Dollar extends Currency2(1.0) | ||
case SwissFanc extends Currency2(1.09) | ||
case Euro extends Currency2(1.18) | ||
|
||
enum Opt[+T]: | ||
case Sm[+T1](value: T1) extends Opt[T1] | ||
case Nn extends Opt[Nothing] | ||
|
||
end EnumTestScala3 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.