scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

Cannot convert `scala.Boolean` to `java.lang.Boolean` automatically #12818

Open He-Pin opened 1 year ago

He-Pin commented 1 year ago

With Code when using netty 4:

        val bootstrap = new ServerBootstrap()
        bootstrap
          .option(ChannelOption.SO_REUSEADDR, !Helpers.isWindows)
          .option(ChannelOption.TCP_NODELAY, true)
          .bind(sockaddr)
          .channel()

which gives:

[error]  found   : io.netty.channel.ChannelOption[Boolean]
[error]  required: io.netty.channel.ChannelOption[Any]
[error] Note: Boolean <: Any, but Java-defined class ChannelOption is invariant in type T.
[error] You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
[error]         bootstrap.option(ChannelOption.TCP_NODELAY, true)

Which compiles if:

bootstrap
          .option(ChannelOption.SO_REUSEADDR, java.lang.Boolean.valueOf(!Helpers.isWindows))
          .option(ChannelOption.TCP_NODELAY, java.lang.Boolean.TRUE)

or

bootstrap
          .option(ChannelOption.SO_REUSEADDR, Boolean.box(!Helpers.isWindows))
          .option(ChannelOption.TCP_NODELAY, Boolean.box(true))

reproducer: https://github.com/He-Pin/scala2booleanBug master branch

som-snytt commented 12 months ago
//> using dep io.netty:netty-all:4.1.94.Final

import io.netty.bootstrap._
import io.netty.channel._

import scala.util.Properties.isWin

class C {
  val bootstrap = new ServerBootstrap()
  val sockaddr  = 42
  def f(): Unit =
    bootstrap
    .option(ChannelOption.SO_REUSEADDR, !isWin)
    .option(ChannelOption.TCP_NODELAY, true)
    .bind(sockaddr)
    .channel()
}

then

➜  t12818 scala-cli compile t12818.scala
➜  t12818 scala-cli compile --scala 2 t12818.scala
Compiling project (Scala 2.13.10, JVM)
[error] ./t12818.scala:14:13
[error] type mismatch;
[error]  found   : io.netty.channel.ChannelOption[Boolean]
[error]  required: io.netty.channel.ChannelOption[Any]
[error] Note: Boolean <: Any, but Java-defined class ChannelOption is invariant in type T.
[error] You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
[error]     .option(ChannelOption.SO_REUSEADDR, !isWin)
[error]             ^^^^^^^^^^^^^^^^^^^^^^^^^^
Error compiling project (Scala 2.13.10, JVM)
Compilation failed

or

➜  t12818 scala-cli compile t12818.scala -O -Vprint:typer
Compiling project (Scala 3.2.2, JVM)
[[syntax trees at end of                     typer]] // .../t12818/t12818.scala
package <empty> {
  import io.netty.bootstrap.*
  import io.netty.channel.*
  import scala.util.Properties.isWin
  class C() extends Object() {
    val bootstrap: io.netty.bootstrap.ServerBootstrap =
      new io.netty.bootstrap.ServerBootstrap()
    val sockaddr: Int = 42
    def f(): Unit =
      {
        this.bootstrap.option[Boolean](
          io.netty.channel.ChannelOption.SO_REUSEADDR
        , boolean2Boolean(util.Properties.isWin.unary_!)).option[Boolean](
          io.netty.channel.ChannelOption.TCP_NODELAY
        , boolean2Boolean(true)).bind(this.sockaddr).channel()
        ()
      }
  }
}

Compiled project (Scala 3.2.2, JVM)

Workaround for Scala 2 is explicit .option[java.lang.Boolean](...).

He-Pin commented 12 months ago

Thanks @som-snytt

SethTisue commented 12 months ago

@som-snytt do you see any possibility of this progressing in Scala 2? wondering if we should leave the ticket open.

He-Pin commented 12 months ago

@SethTisue I think we should leave it open, Kotlin will not require a <java.lang.Boolean> at least, it would be great getting some usability improvements back-ported.

And maybe it can labeled with java-interop too?

SethTisue commented 12 months ago

@He-Pin If you're interested in seeing it progress, the best starting point would be to have a standalone reproduction, without any external dependencies or unnecessary code. That would best help us assess whether improvement here is possible.

som-snytt commented 12 months ago

This issue was why I asked why scala-cli doesn't support -Vprint. I asked on discord, so of course I didn't get an answer. I thought I'd lodged a ticket, but now I remember I thought, What would Seth say to do? Obviously, ask in a forum first. More the fool, I.

➜  t12818 cat s.scala J.java

import java.util.Optional

class C {
  val j = new J
  val x = Optional.of(java.lang.Boolean.TRUE)

  def f = j.f(x, true)
}

import java.util.Optional;

public class J {
        public <T> int f(Optional<T> x, T t) {
                return 42;
        }
}

There are a couple of type inference tickets I was going to look into, related to applications.

I realize that the Lightbend team may not wish to get out the can opener given the unknown number of worms.

One option for ticket management is to mark won't fix because out of scope. That is, have an out-of-scope label so people can search the tickets. Better yet, milestone it for the mythical 2.14.

He-Pin commented 12 months ago

@SethTisue https://github.com/He-Pin/scala2booleanBug I have updated one

He-Pin commented 10 months ago

Where Kotlin can: image