Skip to content
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

Trait parameters are not abstract when Scala.js inspects them #12621

Closed
bishabosha opened this issue May 27, 2021 · 5 comments
Closed

Trait parameters are not abstract when Scala.js inspects them #12621

bishabosha opened this issue May 27, 2021 · 5 comments
Assignees
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc area:scala.js itype:bug

Comments

@bishabosha
Copy link
Member

Compiler version

3.0.0

Minimized code

import scala.scalajs.js
import scala.scalajs.js.annotation.*

object A {

  @js.native
  trait Bag extends js.Any {
    val str: String
  }

  trait NonNativeBagHolderTrait(val bag: Bag) extends js.Any // error

}

Output

-- Error: tests/neg-scalajs/js-native-exports.scala:11:36 --------------------------------------------------------------
11 |  trait NonNativeBagHolderTrait(val bag: Bag) extends js.Any // error
                                ^^^^^^^^^^^^
       Members of non-native JS traits must either be abstract, or their right-hand-side must be `js.undefined`.

Expectation

no error, like if val bag: Bag is moved from trait params to the body

@sjrd
Copy link
Member

sjrd commented May 27, 2021

It's not the same in terms of compilation if they are moved to the body, though, is it? I'm not sure it would make sense from the perspective of semantics of JS types. JS traits are supposed to be pure interfaces without any implementation.

@anatoliykmetyuk
Copy link
Contributor

Does it introduce a regression compared to Scala 2 Scala.js? @sjrd does your comment mean we can close this?

@sjrd
Copy link
Member

sjrd commented Jul 20, 2021

Scala 2 doesn't have trait parameters, so definitely not a regression.

Reporting an error is the correct thing to do. That said, the error message could be improved, saying something like "JS traits cannot have parameters" or something like that.

@bishabosha
Copy link
Member Author

if I compile this similar example

object A {

  trait Bag extends Any {
    val str: String
  }

  trait TraitWithParam(val bag: Bag) extends Any


  trait StdTrait extends Any {
    val bag: Bag
  }

  val b: Bag = new { val str = "hello" }

  class Foo extends TraitWithParam(b)
  class Bar extends StdTrait {
    val bag = b
  }

}

then both Foo and Bar look identical after mixin phase - so is the issue that effectively you must provide an implementation for the bag field?

@bishabosha
Copy link
Member Author

conceptually, parameters are concrete members so they should be disallowed, but we can special case the error message here

@odersky odersky added area:reporting Error reporting including formatting, implicit suggestions, etc and removed discussion labels Apr 7, 2022
bishabosha added a commit that referenced this issue Feb 2, 2023
…16811)

JS traits cannot have constructor params any more than they can have
concrete term members. This improves the error message in that case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc area:scala.js itype:bug
Projects
None yet
Development

No branches or pull requests

4 participants