Skip to content

IndexOutOfBoundsException on wrong number of type params of polymorphic function #20533

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

Closed
TomasMikula opened this issue Jun 7, 2024 · 1 comment · Fixed by #21248
Closed

Comments

@TomasMikula
Copy link
Contributor

Compiler version

3.4.2
3.5.1-RC1

Minimized code

enum Foo[F[_, _], A, B]:
  case Wrap(f: F[A, B])

  def mapF[G[_, _]](h: [X, Y] => F[X, Y] => G[X, Y]): Foo[G, A, B] =
    this match
      case Wrap(f) => Wrap(h(f))

def test[A, B] =
  val m = Map.empty[A, B]
  val foo = Foo.Wrap(m)
  foo.mapF[[X, Y] =>> List[Map[X, Y]]](
    [X] => List(_) // expected compilation error (the polymorphic function should have 2 parameters)

    // correct version that works:
    // [X, Y] => List(_)
  )

Output (click arrow to expand)

% ~/Downloads/scala3-3.5.0-RC1/bin/scalac test.scala

  exception occurred while typechecking test.scala

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/scala/scala3/issues/new/choose
  For non-enriched exceptions, compile with -Xno-enrich-error-messages.

     while compiling: test.scala
        during phase: typer
                mode: Mode(ImplicitsEnabled)
     library version: version 2.13.12
    compiler version: version 3.5.0-RC1
            settings: 

Exception in thread "main" java.lang.IndexOutOfBoundsException: 1
	at scala.collection.LinearSeqOps.apply(LinearSeq.scala:131)
	at scala.collection.LinearSeqOps.apply$(LinearSeq.scala:128)
	at scala.collection.immutable.List.apply(List.scala:79)
	at dotty.tools.dotc.core.Substituters$.substParams(Substituters.scala:153)
	at dotty.tools.dotc.core.Substituters$.mapArgs$2(Substituters.scala:160)
	at dotty.tools.dotc.core.Substituters$.mapArgs$2(Substituters.scala:160)
	at dotty.tools.dotc.core.Substituters$.substParams(Substituters.scala:160)
	at dotty.tools.dotc.core.Substituters$.mapArgs$2(Substituters.scala:160)
	at dotty.tools.dotc.core.Substituters$.substParams(Substituters.scala:160)
	at dotty.tools.dotc.core.Types$Type.substParams(Types.scala:1927)
	at dotty.tools.dotc.typer.Typer.$anonfun$34(Typer.scala:1897)
	at dotty.tools.dotc.typer.Namer.valOrDefDefSig(Namer.scala:1791)
	at dotty.tools.dotc.typer.Namer.defDefSig(Namer.scala:1959)
	at dotty.tools.dotc.typer.Namer$Completer.typeSig(Namer.scala:827)
	at dotty.tools.dotc.typer.Namer$Completer.completeInCreationContext(Namer.scala:974)
	at dotty.tools.dotc.typer.Namer$Completer.complete(Namer.scala:850)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:175)
	at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:190)
	at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:192)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:393)
	at dotty.tools.dotc.typer.Typer.retrieveSym(Typer.scala:3346)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3371)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3483)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3561)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3565)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3587)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3633)
	at dotty.tools.dotc.typer.Typer.typedBlockStats(Typer.scala:1377)
	at dotty.tools.dotc.typer.Typer.typedBlock(Typer.scala:1381)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3407)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3484)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3561)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3565)
	at dotty.tools.dotc.typer.Typer.typedPolyFunctionValue(Typer.scala:1901)
	at dotty.tools.dotc.typer.Typer.typedPolyFunction(Typer.scala:1869)
        ...

Expectation

The compiler should report an error, because the polymorphic function literal has wrong number of type parameters.

@TomasMikula TomasMikula added itype:bug itype:crash stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 7, 2024
@Gedochao Gedochao added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 11, 2024
@KacperFKorban KacperFKorban self-assigned this Jul 23, 2024
@KacperFKorban
Copy link
Member

Smaller minimization:

def mapF(h: [X, Y] => (X, Y) => Map[X, Y]): Unit = ???

def test =
  mapF(
    [X] => (x, y) => Map(x -> y) // error
  )
  exception occurred while typechecking ~/bugs/i20533.scala

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/scala/scala3/issues/new/choose
  For non-enriched exceptions, compile with -Xno-enrich-error-messages.

     while compiling: ~/bugs/i20533.scala
        during phase: typer
                mode: Mode(ImplicitsEnabled)
     library version: version 2.13.14
    compiler version: version 3.6.0-RC1-bin-20240719-af933c4-NIGHTLY-git-af933c4
            settings: -classpath ~/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.6.0-RC1-bin-20240719-af933c4-NIGHTLY/scala3-library_3-3.6.0-RC1-bin-20240719-af933c4-NIGHTLY.jar:~/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.14/scala-library-2.13.14.jar -d ~/bugs/.scala-build/bugs_8e61b95c48-5274d07eab/classes/main -java-output-version 17 -sourceroot ~/bugs

Exception in thread "main" java.lang.IndexOutOfBoundsException: 1
	at scala.collection.LinearSeqOps.apply(LinearSeq.scala:131)
	at scala.collection.LinearSeqOps.apply$(LinearSeq.scala:128)
	at scala.collection.immutable.List.apply(List.scala:79)
	at dotty.tools.dotc.core.Substituters$.substParams(Substituters.scala:153)
	at dotty.tools.dotc.core.Substituters$.mapArgs$2(Substituters.scala:160)
	at dotty.tools.dotc.core.Substituters$.mapArgs$2(Substituters.scala:160)
	at dotty.tools.dotc.core.Substituters$.substParams(Substituters.scala:160)
	at dotty.tools.dotc.core.Types$Type.substParams(Types.scala:1933)
	at dotty.tools.dotc.typer.Typer.$anonfun$34(Typer.scala:1936)
	at dotty.tools.dotc.typer.Namer.valOrDefDefSig(Namer.scala:1791)
	at dotty.tools.dotc.typer.Namer.defDefSig(Namer.scala:1959)
	at dotty.tools.dotc.typer.Namer$Completer.typeSig(Namer.scala:827)
	at dotty.tools.dotc.typer.Namer$Completer.completeInCreationContext(Namer.scala:974)
	at dotty.tools.dotc.typer.Namer$Completer.complete(Namer.scala:850)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:175)
	at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:190)
	at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:192)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:393)
	at dotty.tools.dotc.typer.Typer.retrieveSym(Typer.scala:3386)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3411)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3523)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3601)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3605)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3627)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3673)
	at dotty.tools.dotc.typer.Typer.typedBlockStats(Typer.scala:1412)
	at dotty.tools.dotc.typer.Typer.typedBlock(Typer.scala:1416)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3447)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3524)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3601)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3605)
	at dotty.tools.dotc.typer.Typer.typedPolyFunctionValue(Typer.scala:1940)
	at dotty.tools.dotc.typer.Typer.typedPolyFunction(Typer.scala:1908)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3450)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3524)
	at dotty.tools.dotc.typer.ProtoTypes$FunProto.$anonfun$7(ProtoTypes.scala:532)
	at dotty.tools.dotc.typer.ProtoTypes$FunProto.cacheTypedArg(ProtoTypes.scala:455)
	at dotty.tools.dotc.typer.ProtoTypes$FunProto.typedArg(ProtoTypes.scala:533)
	at dotty.tools.dotc.typer.Applications$ApplyToUntyped.typedArg(Applications.scala:1001)
	at dotty.tools.dotc.typer.Applications$ApplyToUntyped.typedArg(Applications.scala:1001)
	at dotty.tools.dotc.typer.Applications$Application.addTyped$1(Applications.scala:688)
	at dotty.tools.dotc.typer.Applications$Application.matchArgs(Applications.scala:752)
	at dotty.tools.dotc.typer.Applications$Application.init(Applications.scala:574)
	at dotty.tools.dotc.typer.Applications$TypedApply.<init>(Applications.scala:878)
	at dotty.tools.dotc.typer.Applications$ApplyToUntyped.<init>(Applications.scala:1000)
	at dotty.tools.dotc.typer.Applications.ApplyTo(Applications.scala:1262)
	at dotty.tools.dotc.typer.Applications.ApplyTo$(Applications.scala:434)
	at dotty.tools.dotc.typer.Typer.ApplyTo(Typer.scala:145)
	at dotty.tools.dotc.typer.Applications.simpleApply$1(Applications.scala:1073)
	at dotty.tools.dotc.typer.Applications.realApply$1$$anonfun$2(Applications.scala:1183)
	at dotty.tools.dotc.typer.Typer$.tryEither(Typer.scala:118)
	at dotty.tools.dotc.typer.Applications.realApply$1(Applications.scala:1198)
	at dotty.tools.dotc.typer.Applications.typedApply(Applications.scala:1236)
	at dotty.tools.dotc.typer.Applications.typedApply$(Applications.scala:434)
	at dotty.tools.dotc.typer.Typer.typedApply(Typer.scala:145)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3439)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3524)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3601)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3605)
	at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3716)
	at dotty.tools.dotc.typer.Namer.typedAheadExpr$$anonfun$1(Namer.scala:1747)
	at dotty.tools.dotc.typer.Namer.typedAhead(Namer.scala:1737)
	at dotty.tools.dotc.typer.Namer.typedAheadExpr(Namer.scala:1747)
	at dotty.tools.dotc.typer.Namer.typedAheadRhs$1$$anonfun$1(Namer.scala:2073)
	at dotty.tools.dotc.inlines.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:256)
	at dotty.tools.dotc.typer.Namer.typedAheadRhs$1(Namer.scala:2073)
	at dotty.tools.dotc.typer.Namer.rhsType$1(Namer.scala:2081)
	at dotty.tools.dotc.typer.Namer.cookedRhsType$1(Namer.scala:2100)
	at dotty.tools.dotc.typer.Namer.lhsType$1(Namer.scala:2101)
	at dotty.tools.dotc.typer.Namer.inferredResultType(Namer.scala:2112)
	at dotty.tools.dotc.typer.Namer.inferredType$1(Namer.scala:1779)
	at dotty.tools.dotc.typer.Namer.valOrDefDefSig(Namer.scala:1785)
	at dotty.tools.dotc.typer.Namer.defDefSig(Namer.scala:1959)
	at dotty.tools.dotc.typer.Namer$Completer.typeSig(Namer.scala:827)
	at dotty.tools.dotc.typer.Namer$Completer.completeInCreationContext(Namer.scala:974)
	at dotty.tools.dotc.typer.Namer$Completer.complete(Namer.scala:850)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:175)
	at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:190)
	at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:192)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:393)
	at dotty.tools.dotc.typer.Typer.retrieveSym(Typer.scala:3386)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3411)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3523)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3601)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3605)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3627)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3673)
	at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:3121)
	at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:3427)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3431)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3523)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3601)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3605)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3627)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3673)
	at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:3254)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3473)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3524)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3601)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3605)
	at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3716)
	at dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$1(TyperPhase.scala:47)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at dotty.tools.dotc.core.Phases$Phase.monitor(Phases.scala:503)
	at dotty.tools.dotc.typer.TyperPhase.typeCheck(TyperPhase.scala:53)
	at dotty.tools.dotc.typer.TyperPhase.$anonfun$4(TyperPhase.scala:99)
	at scala.collection.Iterator$$anon$6.hasNext(Iterator.scala:479)
	at scala.collection.Iterator$$anon$9.hasNext(Iterator.scala:583)
	at scala.collection.immutable.List.prependedAll(List.scala:152)
	at scala.collection.immutable.List$.from(List.scala:685)
	at scala.collection.immutable.List$.from(List.scala:682)
	at scala.collection.IterableOps$WithFilter.map(Iterable.scala:900)
	at dotty.tools.dotc.typer.TyperPhase.runOn(TyperPhase.scala:98)
	at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:343)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
	at dotty.tools.dotc.Run.runPhases$1(Run.scala:336)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:384)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:396)
	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:69)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:396)
	at dotty.tools.dotc.Run.compileSources(Run.scala:282)
	at dotty.tools.dotc.Run.compile(Run.scala:267)
	at dotty.tools.dotc.Driver.doCompile(Driver.scala:37)
	at dotty.tools.dotc.Driver.process(Driver.scala:201)
	at dotty.tools.dotc.Driver.process(Driver.scala:169)
	at dotty.tools.dotc.Driver.process(Driver.scala:181)
	at dotty.tools.dotc.Driver.main(Driver.scala:211)
	at dotty.tools.dotc.Main.main(Main.scala)
Compilation failed

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