ScalaPB icon indicating copy to clipboard operation
ScalaPB copied to clipboard

Support new Scala 3 syntax

Open Lasering opened this issue 3 years ago • 4 comments

scalapb generated code fails to compile in Scala 3 under these scalac flags:

  • -new-syntax - Require then and do in control expressions.
  • -source:future - Deprecates: private[this], _ for wildcard arguments, some other things
  • -Werror - Fail the compilation if there are any warnings.

Would it be possible to have a new code generator under Scala 3 which would fix these problems (some of them will have to be fixed sooner or later)? I can make a PR if it helps (I'll probably need a little guidance.)

Lasering avatar Jan 31 '22 23:01 Lasering

I assume the reason this change is needed since you have a single project that includes both your code and generated code (correct me if I'm wrong). A possible solution which I highly recommend, regardless of this problem, is to move the code generation in a dedicated sub-project within the same SBT build, and have the main project depend on it. Currently, the generator aims to generate code that works for both Scala 2.x and 3.x. This reduces the effort needed to keep the code updated. I would defer on satisfying the above flags until they are required by default by scalac.

thesamet avatar Jan 31 '22 23:01 thesamet

You are correct. Another sub-project would solve it.

According to Wildcard Arguments in Types:

In Scala 3.2, the meaning of _ changes from wildcard to placeholder for type parameter.

We are already at Scala 3.1.1 so it won't take long (assuming they stick to that schedule) for these changes to be necessary.

Lasering avatar Feb 01 '22 00:02 Lasering

Yes, maybe having the code generator have cases for different language versions is going to be inevitable one day. Is there an instance where the generated code is impacted by wildcard arguments in types?

thesamet avatar Feb 01 '22 01:02 thesamet

Using the Address Book example from the docs and the -source:future, -Werror flags the following errors are reported regarding the wildcard arguments:

[error] -- Error: (...)/modules/shared/.jvm/target/scala-3.1.1/src_managed/main/scalapb/tutorial/addressbook/AddressBook.scala:98:107 
[error] 98 |  def messageCompanionForFieldNumber(__number: _root_.scala.Int): _root_.scalapb.GeneratedMessageCompanion[_] = {
[error]    |                                                                                                           ^
[error]    |      `_` is deprecated for wildcard arguments of types: use `?` instead
[error] -- Error: (...)/modules/shared/.jvm/target/scala-3.1.1/src_managed/main/scalapb/tutorial/addressbook/AddressBook.scala:99:56 
[error] 99 |    var __out: _root_.scalapb.GeneratedMessageCompanion[_] = null
[error]    |                                                        ^
[error]    |      `_` is deprecated for wildcard arguments of types: use `?` instead
[error] -- Error: (...)/modules/shared/.jvm/target/scala-3.1.1/src_managed/main/scalapb/tutorial/addressbook/AddressBook.scala:105:82 
[error] 105 |  lazy val nestedMessagesCompanions: Seq[_root_.scalapb.GeneratedMessageCompanion[_ <: _root_.scalapb.GeneratedMessage]] = Seq.empty
[error]     |                                                                                  ^
[error]     |    `_` is deprecated for wildcard arguments of types: use `?` instead
[error] -- Error: (...)/modules/shared/.jvm/target/scala-3.1.1/src_managed/main/scalapb/tutorial/addressbook/AddressBook.scala:106:106 
[error] 106 |  def enumCompanionForFieldNumber(__fieldNumber: _root_.scala.Int): _root_.scalapb.GeneratedEnumCompanion[_] = throw new MatchError(__fieldNumber)
[error]     |                                                                                                          ^
[error]     |    `_` is deprecated for wildcard arguments of types: use `?` instead

With the same repeated for the other classes.

I have some experience with Scala 3 new features I can help if needed.

Lasering avatar Feb 01 '22 15:02 Lasering

With the recent release of Scala 3.3.0, it's probably time to revisit this issue. I can work on a PR if it is something @thesamet agrees with.

ngbinh avatar Aug 28 '23 07:08 ngbinh

Sounds like it's time for this future. I am not sure how much different the code should be : is it just the wildcard change? If the changes are fairly small, maybe a generator parameter for Scala 3 syntax would be sufficient.

thesamet avatar Aug 28 '23 13:08 thesamet

Yes, there are only a handful of small changes.

ngbinh avatar Aug 29 '23 06:08 ngbinh

@Lasering @thesamet Would be great if you can take a look at https://github.com/scalapb/ScalaPB/pull/1565

ngbinh avatar Sep 04 '23 10:09 ngbinh

Will be released with 0.11.14.

thesamet avatar Oct 12 '23 13:10 thesamet