Skip to content

java.lang.IndexOutOfBoundsException: 0 from subtyping of dependent function types #6503

Closed
@Blaisorblade

Description

@Blaisorblade

Variation on #6502, triggers a crash in master (0f8c950) and on 0.14.0-RC1 after recovering from a (genuine) syntax error.

trait MapImpl {
  type Key
  type Value
  type Map
  val lookup: Map => Key => Value
}
import scala.collection.immutable.HashMap

class HashMapImpl[K, V] extends MapImpl {
  type Key = K
  type Value = V
  type Map = HashMap[K, V]
  val lookup: Map => Key => Value = m => k => m(k)
}

object Foo {
  val Server0:
    (mImpl: MapImpl) => mImpl.Map => mImpl.Key => mImpl.Value
    = mImpl => mImpl.lookup
  val Client:
    (server: (mImpl: MapImpl { type Key = String; type Value = Int}) => mImpl.Map => String => Int) => Int =
    server => server(HashMapImpl[String, Int])(HashMap())("test lookup key") //works
    // server => server(??? : (HashMapImpl[String, Int]))(???)("test lookup key") //works
    // server => server(???)(???)("test lookup key") // crashes
  val Server1: (mImpl: MapImpl {type Key = String; type Value = Int}) => mImpl.Map => String => Int =
    mImpl => Server0(mImpl)
  //implicitly[(mImpl: MapImpl {type Key = String; type Value = Int}) => mImpl.Map => String => Int <:< (mImpl: MapImpl) => mImpl.Map => mImpl.Key => mImpl.Value]
  implicitly[(mImpl: MapImpl {type Key = String; type Value = Int}) => mImpl.Map => mImpl.Key => mImpl.Value <:< (mImpl: MapImpl) => mImpl.Map => mImpl.Key => mImpl.Value]

  //val Result1: Int = Client(Server0) // rejected
  val Result2: Int = Client(Server1) // accepted
  val Result3: Int = Client(mImpl => Server0(mImpl)) // accepted
}
-- [E040] Syntax Error: /Users/pgiarrusso/git/dotty-example-project/src/main/scala/foo.scala:28:119
28 |  implicitly[(mImpl: MapImpl {type Key = String; type Value = Int}) => mImpl.Map => mImpl.Key => mImpl.Value <:< (mImpl: MapImpl) => mImpl.Map => mImpl.Key => mImpl.Value]
   |                                                                                                                       ^
   |                                             ')' expected, but ':' found
exception occurred while typechecking /Users/pgiarrusso/git/dotty-example-project/src/main/scala/foo.scala
exception occurred while compiling /Users/pgiarrusso/git/dotty-example-project/src/main/scala/foo.scala
java.lang.IndexOutOfBoundsException: 0 while compiling /Users/pgiarrusso/git/dotty-example-project/src/main/scala/foo.scala
Exception in thread "main" java.lang.IndexOutOfBoundsException: 0
	at scala.collection.LinearSeqOptimized.apply(LinearSeqOptimized.scala:67)
	at scala.collection.LinearSeqOptimized.apply$(LinearSeqOptimized.scala:65)
	at scala.collection.immutable.List.apply(List.scala:89)
	at dotty.tools.dotc.typer.ErrorReporting$Errors.translate$1(ErrorReporting.scala:156)
	at dotty.tools.dotc.typer.ErrorReporting$Errors.userDefinedErrorString$$anonfun$1(ErrorReporting.scala:158)
	at scala.util.matching.Regex.$anonfun$replaceSomeIn$1$adapted(Regex.scala:535)
	at scala.collection.Iterator.foreach(Iterator.scala:941)
	at scala.collection.Iterator.foreach$(Iterator.scala:941)
	at scala.collection.AbstractIterator.foreach(Iterator.scala:1429)
	at scala.util.matching.Regex.replaceSomeIn(Regex.scala:535)
	at dotty.tools.dotc.typer.ErrorReporting$Errors.userDefinedErrorString(ErrorReporting.scala:158)
	at dotty.tools.dotc.typer.Implicits.$anonfun$1(Implicits.scala:915)
	at scala.Option.map(Option.scala:163)
	at dotty.tools.dotc.typer.Implicits.missingArgMsg(Implicits.scala:915)
	at dotty.tools.dotc.typer.Typer.missingArgMsg(Typer.scala:87)
	at dotty.tools.dotc.typer.Typer.issueErrors$1$$anonfun$1$$anonfun$1(Typer.scala:2666)
	at dotty.tools.dotc.reporting.diagnostic.MessageContainer.contained(MessageContainer.scala:59)
	at dotty.tools.dotc.reporting.diagnostic.MessageContainer.message(MessageContainer.scala:42)
	at dotty.tools.dotc.reporting.diagnostic.MessageContainer.isNonSensical(MessageContainer.scala:71)
	at dotty.tools.dotc.reporting.HideNonSensicalMessages.isHidden(HideNonSensicalMessages.scala:17)
	at dotty.tools.dotc.reporting.AbstractReporter.isHidden(AbstractReporter.scala:8)
	at dotty.tools.dotc.reporting.Reporter.report(Reporter.scala:230)
	at dotty.tools.dotc.reporting.Reporting.error(Reporter.scala:110)
	at dotty.tools.dotc.core.Contexts$Context.error(Contexts.scala:71)
	at dotty.tools.dotc.typer.Typer.issueErrors$2$$anonfun$2(Typer.scala:2665)
	at dotty.runtime.function.JProcedure3.apply(JProcedure3.java:15)
	at dotty.runtime.function.JProcedure3.apply(JProcedure3.java:10)
	at scala.runtime.Tuple3Zipped$.$anonfun$foreach$1(Tuple3Zipped.scala:124)
	at scala.collection.immutable.List.foreach(List.scala:392)
	at scala.runtime.Tuple3Zipped$.foreach$extension(Tuple3Zipped.scala:122)
	at dotty.tools.dotc.typer.Typer.issueErrors$3(Typer.scala:2669)
	at dotty.tools.dotc.typer.Typer.addImplicitArgs$4(Typer.scala:2694)
	at dotty.tools.dotc.typer.Typer.adaptNoArgsImplicitMethod$2(Typer.scala:2703)
	at dotty.tools.dotc.typer.Typer.adaptNoArgs$1(Typer.scala:2866)
	at dotty.tools.dotc.typer.Typer.adapt1(Typer.scala:3098)
	at dotty.tools.dotc.typer.Typer.adapt(Typer.scala:2520)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2222)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2234)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2277)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2299)
	at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:1694)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2124)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2187)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2222)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2234)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2253)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2299)
	at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:1813)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2164)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2188)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2222)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2234)
	at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2310)
	at dotty.tools.dotc.typer.FrontEnd.typeCheck$$anonfun$1(FrontEnd.scala:61)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions