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

Any capturing function type immediately after a type abstraction crashes the compiler #18518

Closed
Linyxus opened this issue Sep 5, 2023 · 1 comment · Fixed by #18520
Closed
Labels
cc-experiment Intended to be merged with cc-experiment branch on origin itype:bug

Comments

@Linyxus
Copy link
Contributor

Linyxus commented Sep 5, 2023

Compiler version

main

Minimized code

import language.experimental.captureChecking
type Foo1 = [R] -> (x: Unit) ->{} Unit  // crashes
// Or the following variants ...
// type Foo2 = [R] -> (x: Unit) ->{cap} Unit
// type Foo3 = (c: Int^) -> [R] -> (x: Unit) ->{c} Unit

// but the following works
type Foo4 = (c: Int^) -> [R] -> (x0: Unit) -> (x: Unit) ->{c} Unit  // ok

Output (click arrow to expand)

  exception occurred while typechecking issues/cc-lambda-types.scala

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/lampepfl/dotty/issues/new/choose

     while compiling: issues/cc-lambda-types.scala
        during phase: typer
                mode: Mode(ImplicitsEnabled)
     library version: version 2.13.10
    compiler version: version 3.4.0-RC1-bin-SNAPSHOT-nonbootstrapped-git-6dc3737
            settings: -classpath /Users/linyxus/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.10/scala-library-2.13.10.jar:/Users/linyxus/Workspace/dotty/library/../out/bootstrap/scala3-library-bootstrapped/scala-3.4.0-RC1-bin-SNAPSHOT-nonbootstrapped/scala3-library_3-3.4.0-RC1-bin-SNAPSHOT.jar -color never

                tree: EmptyTree
       tree position: :<unknown>
           tree type: <notype>
              symbol: val <none>
           call site: package <root> in module class <root>

  == Source file context for tree position ==


Exception in thread "main" scala.MatchError: CapturesAndResult(List(),Ident(Unit)) (of class dotty.tools.dotc.ast.untpd$CapturesAndResult)
	at dotty.tools.dotc.ast.desugar$.apply(Desugar.scala:1873)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3161)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
	at dotty.tools.dotc.typer.Typer.typedType(Typer.scala:3374)
	at dotty.tools.dotc.typer.Namer.typedAheadType$$anonfun$1(Namer.scala:1655)
	at dotty.tools.dotc.typer.Namer.typedAhead(Namer.scala:1648)
	at dotty.tools.dotc.typer.Namer.typedAheadType(Namer.scala:1655)
	at dotty.tools.dotc.typer.Namer.valOrDefDefSig(Namer.scala:1749)
	at dotty.tools.dotc.typer.Namer.defDefSig(Namer.scala:1800)
	at dotty.tools.dotc.typer.Namer$Completer.typeSig(Namer.scala:793)
	at dotty.tools.dotc.typer.Namer$Completer.completeInCreationContext(Namer.scala:936)
	at dotty.tools.dotc.typer.Namer$Completer.complete(Namer.scala:816)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:174)
	at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:187)
	at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:189)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:393)
	at dotty.tools.dotc.typer.Typer.retrieveSym(Typer.scala:3052)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3077)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3178)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3281)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3327)
	at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2738)
	at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:3099)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3103)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3178)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
	at dotty.tools.dotc.typer.Typer.typedRefinedTypeTree(Typer.scala:2149)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3137)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
	at dotty.tools.dotc.typer.Typer.typedPolyFunction(Typer.scala:1648)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3122)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
	at dotty.tools.dotc.typer.Typer.typedType(Typer.scala:3374)
	at dotty.tools.dotc.typer.Namer.typedAheadType$$anonfun$1(Namer.scala:1655)
	at dotty.tools.dotc.typer.Namer.typedAhead(Namer.scala:1648)
	at dotty.tools.dotc.typer.Namer.typedAheadType(Namer.scala:1655)
	at dotty.tools.dotc.typer.Namer$TypeDefCompleter.typeSig(Namer.scala:1035)
	at dotty.tools.dotc.typer.Namer$Completer.completeInCreationContext(Namer.scala:936)
	at dotty.tools.dotc.typer.Namer$Completer.complete(Namer.scala:816)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:174)
	at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:187)
	at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:189)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:393)
	at dotty.tools.dotc.typer.Typer.retrieveSym(Typer.scala:3052)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3077)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3178)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3281)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3327)
	at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2738)
	at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:3099)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3103)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3178)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3281)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3327)
	at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2873)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3145)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
	at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3371)
	at dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$1(TyperPhase.scala:44)
	at dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$adapted$1(TyperPhase.scala:50)
	at scala.Function0.apply$mcV$sp(Function0.scala:42)
	at dotty.tools.dotc.core.Phases$Phase.monitor(Phases.scala:440)
	at dotty.tools.dotc.typer.TyperPhase.typeCheck(TyperPhase.scala:50)
	at dotty.tools.dotc.typer.TyperPhase.runOn$$anonfun$3(TyperPhase.scala:84)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.immutable.List.foreach(List.scala:333)
	at dotty.tools.dotc.typer.TyperPhase.runOn(TyperPhase.scala:84)
	at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:246)
	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:1321)
	at dotty.tools.dotc.Run.runPhases$1(Run.scala:262)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:270)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:279)
	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:71)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:279)
	at dotty.tools.dotc.Run.compileSources(Run.scala:194)
	at dotty.tools.dotc.Run.compile(Run.scala:179)
	at dotty.tools.dotc.Driver.doCompile(Driver.scala:37)
	at dotty.tools.dotc.Driver.process(Driver.scala:197)
	at dotty.tools.dotc.Driver.process(Driver.scala:165)
	at dotty.tools.dotc.Driver.process(Driver.scala:177)
	at dotty.tools.dotc.Driver.main(Driver.scala:207)
	at dotty.tools.dotc.Main.main(Main.scala)
[error] Nonzero exit code returned from runner: 1
[error] (scala3-compiler / Compile / runMain) Nonzero exit code returned from runner: 1
[error] Total time: 1 s, completed Sep 6, 2023, 1:46:51 AM
@Linyxus Linyxus added itype:bug itype:crash stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 5, 2023
@Linyxus
Copy link
Contributor Author

Linyxus commented Sep 6, 2023

In the example above, the type [R] -> (x: Unit) ->{cap} Unit is desugared into PolyFunction { def apply[R](x: Unit): ^{cap} Unit } where ^{cap} Unit is the temporary tree CapturesAndResult produced by the parser. So the crash seems to be an implementation restriction imposed by our encoding of polyfunctions.

@Linyxus Linyxus added cc-experiment Intended to be merged with cc-experiment branch on origin and removed itype:crash stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 6, 2023
@Linyxus Linyxus mentioned this issue Sep 6, 2023
odersky added a commit that referenced this issue Oct 1, 2023
Fix #18518.

It is an implementation restriction that a value parameter has to follow
a type abstraction. So `[X] -> T ->{x} U` should be rejected as a
capturing type follows the type abstraction,but it wasn't. This PR
refines the check in `Parsers.scala` to reject this case properly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cc-experiment Intended to be merged with cc-experiment branch on origin itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant