From b9a02cdceb432b92eb9ba2082fec5b03c10f6cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Doeraene?= Date: Thu, 2 Feb 2023 15:02:50 +0100 Subject: [PATCH] Fix #12621: Scala.js: Better error message for JS trait ctor param. JS traits cannot have constructor params any more than they can have concrete term members. This improves the error message in that case. --- .../dotty/tools/dotc/transform/sjs/PrepJSInterop.scala | 3 +++ tests/neg-scalajs/js-trait-ctor-param.check | 4 ++++ tests/neg-scalajs/js-trait-ctor-param.scala | 9 +++++++++ 3 files changed, 16 insertions(+) create mode 100644 tests/neg-scalajs/js-trait-ctor-param.check create mode 100644 tests/neg-scalajs/js-trait-ctor-param.scala diff --git a/compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala b/compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala index d934dc179989..8a430991e378 100644 --- a/compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala +++ b/compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala @@ -888,6 +888,9 @@ class PrepJSInterop extends MacroTransform with IdentityDenotTransformer { thisP report.error("A non-native JS trait cannot contain private members", tree) } else if (sym.is(Lazy)) { report.error("A non-native JS trait cannot contain lazy vals", tree) + } else if (sym.is(ParamAccessor)) { + // #12621 + report.error("A non-native JS trait cannot have constructor parameters", tree) } else if (!sym.is(Deferred)) { /* Tell the back-end not to emit this thing. In fact, this only * matters for mixed-in members created from this member. diff --git a/tests/neg-scalajs/js-trait-ctor-param.check b/tests/neg-scalajs/js-trait-ctor-param.check new file mode 100644 index 000000000000..bc5296b3c76f --- /dev/null +++ b/tests/neg-scalajs/js-trait-ctor-param.check @@ -0,0 +1,4 @@ +-- Error: tests/neg-scalajs/js-trait-ctor-param.scala:9:34 ------------------------------------------------------------- +9 |trait NonNativeBagHolderTrait(val bag: Bag) extends js.Any // error + | ^^^^^^^^^^^^ + | A non-native JS trait cannot have constructor parameters diff --git a/tests/neg-scalajs/js-trait-ctor-param.scala b/tests/neg-scalajs/js-trait-ctor-param.scala new file mode 100644 index 000000000000..c907b0d9b606 --- /dev/null +++ b/tests/neg-scalajs/js-trait-ctor-param.scala @@ -0,0 +1,9 @@ +import scala.scalajs.js +import scala.scalajs.js.annotation.* + +@js.native +trait Bag extends js.Any { + val str: String +} + +trait NonNativeBagHolderTrait(val bag: Bag) extends js.Any // error