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

Type computed to any, works in Scala 2 #16018

Open
He-Pin opened this issue Sep 11, 2022 · 7 comments
Open

Type computed to any, works in Scala 2 #16018

He-Pin opened this issue Sep 11, 2022 · 7 comments

Comments

@He-Pin
Copy link

He-Pin commented Sep 11, 2022

Compiler version

3.1.2

Rrror: https://github.com/akka/akka/runs/8294129202?check_suite_focus=true
From: akka/akka#31345

Minimized code

Sorry I have not minimized it :(

  def combine[T, U, M](
      sources: java.util.List[_ <: Graph[SourceShape[T], _<: M]],
      fanInGraph: Graph[UniformFanInShape[T, U], NotUsed]): Source[U, java.util.List[M]] = {
    val seq = if (sources != null) Util.immutableSeq(sources).collect {
      case source: Source[T @unchecked, M @unchecked] => source.asScala
      case other                                      => other
    } else immutable.Seq()
    import akka.util.ccompat.JavaConverters._
    new Source(scaladsl.Source.combine(seq)(fanInGraph).mapMaterializedValue(_.asJava))
  }

if I change to :

  def combine[T, U, M](
      sources: java.util.List[_ <: Graph[SourceShape[T], M]],
      fanInGraph: Graph[UniformFanInShape[T, U], NotUsed]): Source[U, java.util.List[M]] = {
    val seq = if (sources != null) Util.immutableSeq(sources).collect {
      case source: Source[T @unchecked, M @unchecked] => source.asScala
      case other                                      => other
    } else immutable.Seq()
    import akka.util.ccompat.JavaConverters._
    new Source(scaladsl.Source.combine(seq)(fanInGraph).mapMaterializedValue(_.asJava))
  }

The only difference is _<:M and M.

Output

[error] -- [E007] Type Mismatch Error: /home/runner/work/akka/akka/akka-stream/src/main/scala/akka/stream/javadsl/Sink.scala:397:76 
[error] 397 |    new Sink(scaladsl.Sink.combine(seq)(fanoutGraph).mapMaterializedValue(_.asJava))
[error]     |                                                                          ^^^^^^^^
[error]     |                                         Found:    java.util.List[Any]
[error]     |                                         Required: java.util.List[M]
[error] -- [E007] Type Mismatch Error: /home/runner/work/akka/akka/akka-stream/src/main/scala/akka/stream/javadsl/Source.scala:682:79 
[error] 682 |    new Source(scaladsl.Source.combine(seq)(fanInGraph).mapMaterializedValue(_.asJava))
[error]     |                                                                             ^^^^^^^^
[error]     |                                         Found:    java.util.List[Any]
[error]     |                                         Required: java.util.List[M]
[error] two errors found

image

Expectation

Compile as Scala 2.

I think that should be computed to java.util.List[M] not java.util.List[Any]

@He-Pin He-Pin added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 11, 2022
@mbovel
Copy link
Member

mbovel commented Sep 12, 2022

Here is an attempt to minimize:

object Test {
    class Box[T](val value: T)
    def f[T](l: List[Box[_ <: T]]): List[T] = l.map(_.value)
}

I am not sure if this captures every aspect of your problem or if this is over-simplified.

The above snippet compiles with Scala 2.12, 3.1.2 and 3.2.0 but fails with Scala 2.13, 3.1.1 and 3.1.3.

Output
➜  ~/scala-snippets scala-cli compile --server=false --scala-version=2.12 type_param_any.scala 
➜  ~/scala-snippets scala-cli compile --server=false --scala-version=2.13 type_param_any.scala 
/Users/mbovel/scala-snippets/type_param_any.scala:3: error: type mismatch;
 found   : Test.Box[_ <: T] => x$1.value.type forSome { val x$1: Test.Box[_ <: T] }
 required: Test.Box[_ <: T] => (some other)_$1(in value l)
    def f[T](l: List[Box[_ <: T]]): List[T] = l.map(_.value)
                                                      ^
1 error
Compilation failed
➜  ~/scala-snippets scala-cli compile --server=false --scala-version=3.1.1 type_param_any.scala 
-- Error: /Users/mbovel/scala-snippets/type_param_any.scala:3:52 ---------------
3 |    def f[T](l: List[Box[_ <: T]]): List[T] = l.map(_.value)
  |                                                    ^^^^^^^
  |                          return type B of lambda cannot be made hygienic;
  |                          it is not a supertype of the hygienic type Any
-- Error: /Users/mbovel/scala-snippets/type_param_any.scala:3:51 ---------------
3 |    def f[T](l: List[Box[_ <: T]]): List[T] = l.map(_.value)
  |                                                   ^
  |       Inaccessible variables captured in instantation of type variable B.
  |       _$1.T was fixed to Any,
  |       but the latter type does not conform to the upper bound T
-- Error: /Users/mbovel/scala-snippets/type_param_any.scala:3:8 ----------------
3 |    def f[T](l: List[Box[_ <: T]]): List[T] = l.map(_.value)
  |        ^
  |       Inaccessible variables captured in instantation of type variable B.
  |       _$1.T was fixed to Any,
  |       but the latter type does not conform to the upper bound T
-- Error: /Users/mbovel/scala-snippets/type_param_any.scala:1:7 ----------------
1 |object Test {
  |       ^
  |       Inaccessible variables captured in instantation of type variable B.
  |       _$1.T was fixed to Any,
  |       but the latter type does not conform to the upper bound T
-- Error: /Users/mbovel/scala-snippets/type_param_any.scala:1:0 ----------------
1 |object Test {
  |^
  |Inaccessible variables captured in instantation of type variable B.
  |_$1.T was fixed to Any,
  |but the latter type does not conform to the upper bound T
5 errors found
Compilation failed
➜  ~/scala-snippets scala-cli compile --server=false --scala-version=3.1.2 type_param_any.scala 
➜  ~/scala-snippets scala-cli compile --server=false --scala-version=3.1.3 type_param_any.scala 
-- Error: /Users/mbovel/scala-snippets/type_param_any.scala:3:52 ---------------
3 |    def f[T](l: List[Box[_ <: T]]): List[T] = l.map(_.value)
  |                                                    ^^^^^^^
  |                      return type _$1.T of lambda cannot be made hygienic;
  |                      it is not a supertype of the hygienic type Any
1 error found
Compilation failed
➜  ~/scala-snippets scala-cli compile --server=false --scala-version=3.2.0 type_param_any.scala 

I believe this is linked to type avoidance and recent related work from @smarter and @odersky; can you please confirm?

@He-Pin have you tried to compile with Scala 3.2.0? And with 2.13?

@mbovel mbovel added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 12, 2022
@He-Pin
Copy link
Author

He-Pin commented Sep 12, 2022

I just updated it, it was 3.1.2 not 3.1.3 .Akka not compiles with 3.1.3 now.
image

@odersky
Copy link
Contributor

odersky commented Sep 12, 2022

Hard to tell whether it's avoidance or not. But it works in nightly, right?

@mbovel
Copy link
Member

mbovel commented Sep 12, 2022

But it works in nightly, right?

My simplified example compiles with 3.2.0, but I am not sure for the original one.

@He-Pin could please try with 3.2.0 and with the latest nightly?

@He-Pin
Copy link
Author

He-Pin commented Sep 12, 2022

@mbovel I am trying to get Akka built at: akka/akka#31578 :) ,that will be the first to come, it currently only compiles with scala 3.1.2,

@He-Pin
Copy link
Author

He-Pin commented Sep 13, 2022

it's still the same.

[info] compiling 186 Scala sources and 3 Java sources to C:\Users\hepin\IdeaProjects\akka\akka-stream\target\scala-3.2.0\classes ...
[error] -- [E007] Type Mismatch Error: C:\Users\hepin\IdeaProjects\akka\akka-stream\src\main\scala\akka\stream\javadsl\Source.scala:682:79
[error] 682 |    new Source(scaladsl.Source.combine(seq)(fanInGraph).mapMaterializedValue(_.asJava))
[error]     |                                                                             ^^^^^^^^
[error]     |                                         Found:    java.util.List[Any]
[error]     |                                         Required: java.util.List[M]
[error]     |
[error]     | longer explanation available when compiling with `-explain`
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 58 s, completed 2022-9-13 14:41:21

I rebased akka/akka#31345 to akka/akka#31579
and change the M to _<:M
and then run project akka-stream ++ 3.2.0 compile
It shows the same

@mbovel Sorry for not minimize it, you can have a try.

@He-Pin
Copy link
Author

He-Pin commented Sep 17, 2022

Mian.

import scala.collection.immutable

object Main {

  class Source[+Out, +Mat] extends Graph[SourceShape[Out], Mat]

  class Shape

  class SourceShape[+T] extends Shape

  class Graph[+S <: Shape, +M]

  def combine[T, U, M](sources: java.util.List[_ <: Graph[SourceShape[T], _ <: M]]): Source[U, java.util.List[M]] = {
    val seq: immutable.Seq[Graph[SourceShape[T], M]] = if (sources != null) immutableSeq(sources).collect {
      case source: Source[T, M]@unchecked => source
      case other => other
    } else immutable.Seq()
    ???
  }

  def immutableSeq[T](iterable: java.lang.Iterable[T]): immutable.Seq[T] = ???
}

it gives me:

C:\Users\hepin\IdeaProjects\MtoAnyTest\src\main\scala\Main.scala:16:21
Found:    (other : ?1.CAP)
Required: Main.Source[T, M] & ?1.CAP

where:    ?1 is an unknown value of type scala.runtime.TypeBox[Nothing, Main.Graph[Main.SourceShape[T], ? <: M]]

      case other => other

@mbovel @odersky this code compiles with 2.12.16 and 2.13.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants