Skip to content

Illegal cyclic reference when creating a wrapper method of min/max for java.util.Collections #12404

@kynthus

Description

@kynthus

reproduction steps

using Scala 2.13.6, on Oracle Java SE Development Kit 8u202.

I have created a method that wraps the min and max methods of java.util.Collections.
Since generics are bounded, Scala's type parameters are bound as well.

// java/util/Collections.java

public class Collections {
    /* ... */
    public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) { /* ... */ }
    public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) { /* ... */ }
    /* ... */
}
// CollectionsWrapper.scala

object CollectionsWrapper {
  def minWrapper[T <: java.lang.Object with java.lang.Comparable[_ >: T]](coll: java.util.Collection[_ <: T]): T = java.util.Collections.min(coll)
  def maxWrapper[T <: java.lang.Object with java.lang.Comparable[_ >: T]](coll: java.util.Collection[_ <: T]): T = java.util.Collections.max(coll)
}

problem

Failed to compile, and message says illegal cyclic reference.

# scalac -version
Scala compiler version 2.13.6 -- Copyright 2002-2021, LAMP/EPFL and Lightbend, Inc.

# scalac CollectionsWrapper.scala
CollectionsWrapper.scala:4: error: illegal cyclic reference involving type T
  def minWrapper[T <: java.lang.Object with java.lang.Comparable[_ >: T]](coll: java.util.Collection[_ <: T]): T = java.util.Collections.min(coll)
                                                                 ^
CollectionsWrapper.scala:4: error: inferred type arguments [_$2] do not conform to method min's type parameter bounds [T <: Comparable[_ >: T]]
  def minWrapper[T <: java.lang.Object with java.lang.Comparable[_ >: T]](coll: java.util.Collection[_ <: T]): T = java.util.Collections.min(coll)
                                                                                                                                         ^
CollectionsWrapper.scala:4: error: type mismatch;
 found   : java.util.Collection[_$2] where type _$2 <: T
 required: java.util.Collection[_ <: T]
  def minWrapper[T <: java.lang.Object with java.lang.Comparable[_ >: T]](coll: java.util.Collection[_ <: T]): T = java.util.Collections.min(coll)
                                                                                                                                             ^
CollectionsWrapper.scala:5: error: illegal cyclic reference involving type T
  def maxWrapper[T <: java.lang.Object with java.lang.Comparable[_ >: T]](coll: java.util.Collection[_ <: T]): T = java.util.Collections.max(coll)
                                                                 ^
CollectionsWrapper.scala:5: error: inferred type arguments [_$4] do not conform to method max's type parameter bounds [T <: Comparable[_ >: T]]
  def maxWrapper[T <: java.lang.Object with java.lang.Comparable[_ >: T]](coll: java.util.Collection[_ <: T]): T = java.util.Collections.max(coll)
                                                                                                                                         ^
CollectionsWrapper.scala:5: error: type mismatch;
 found   : java.util.Collection[_$4] where type _$4 <: T
 required: java.util.Collection[_ <: T]
  def maxWrapper[T <: java.lang.Object with java.lang.Comparable[_ >: T]](coll: java.util.Collection[_ <: T]): T = java.util.Collections.max(coll)
                                                                                                                                             ^
6 errors

In Scala 3, compile successfully.

# scalac -version
Scala compiler version 3.0.0 -- Copyright 2002-2021, LAMP/EPFL

# scalac CollectionsWrapper.scala # <-success

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)java interoptyper

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions