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

Orphan parameter while Ychecking with CC library #19661

Closed
nicolasstucki opened this issue Feb 9, 2024 · 1 comment · Fixed by #19684
Closed

Orphan parameter while Ychecking with CC library #19661

nicolasstucki opened this issue Feb 9, 2024 · 1 comment · Fixed by #19684
Assignees
Labels
area:experimental:cc Capture checking related itype:bug
Milestone

Comments

@nicolasstucki
Copy link
Contributor

Minimization of tests/pos/t6925.scala

class Test:
  def f(xs: Set[Int]) = xs collect { case x => x }
  def g(xs: Set[Int]): Set[Int] = f(xs)
sbt> set ThisBuild/Build.scala2Library := Build.Scala2LibraryCCTasty
sbt> scala3-bootstrapped/scalac Test.scala -Ycheck:all
checking tests/pos/t6925.scala after phase typer

  exception while retyping def g(xs: Set[Int]): Set[Int] = this.f(xs) of class DefDef # -1

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

     while compiling: tests/pos/t6925.scala
        during phase: Ycheck
                mode: Mode(ImplicitsEnabled)
     library version: version 2.13.12
    compiler version: version 3.4.1-RC1-bin-SNAPSHOT-git-7bcfce2
            settings: -Vprint List(typer) -Ycheck List(all) -classpath /Users/nicolasstucki/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.12/scala-library-2.13.12.jar:/Users/nicolasstucki/Github/dotty/library/../out/bootstrap/scala3-library-bootstrapped/scala-3.4.1-RC1-bin-SNAPSHOT-nonbootstrapped/scala3-library_3-3.4.1-RC1-bin-SNAPSHOT.jar:/Users/nicolasstucki/Github/dotty/scala2-library-cc-tasty/../out/bootstrap/scala2-library-cc-tasty/scala-3.4.1-RC1-bin-SNAPSHOT-nonbootstrapped/scala2-library-cc-tasty_3-3.4.1-RC1-bin-SNAPSHOT.jar -d /

*** error while checking tests/pos/t6925.scala after phase typer ***
Exception in thread "main" java.lang.AssertionError: assertion failed: orphan param: xs.type, hash of binder = 1664998412, tree = <empty>, type = AnnotatedType(AppliedType(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class collection)),object immutable),trait Set),List(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int))),ConcreteAnnotation(Apply(Select(New(Select(Select(Select(Ident(_root_),scala),annotation),retains)),<init>),List(Typed(SeqLiteral(List(This(Ident()), Ident(pf)),TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),Any)]),TypeTree[AppliedType(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),<repeated>),List(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),Any)))])))))
        at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
        at dotty.tools.dotc.transform.TreeChecker$$anon$2.apply(TreeChecker.scala:172)
        at dotty.tools.dotc.core.Annotations$Annotation$$anon$1.apply(Annotations.scala:64)
        ...

Originally posted by @nicolasstucki in #19652 (comment)

@nicolasstucki nicolasstucki added itype:bug cc-experiment Intended to be merged with cc-experiment branch on origin area:experimental:cc Capture checking related and removed cc-experiment Intended to be merged with cc-experiment branch on origin labels Feb 9, 2024
@nicolasstucki nicolasstucki changed the title Orphan parameter while Ychecking with CC library. Orphan parameter while Ychecking with CC library Feb 9, 2024
@Linyxus
Copy link
Contributor

Linyxus commented Feb 11, 2024

A minimisation that is independent of the capture checked library:

import language.experimental.captureChecking

trait MySet[A]:
  def collect[B](pf: PartialFunction[A, B]^): MySet[B]^{this, pf}

class Test:
  def f(xs: MySet[Int]) = xs collect { case x => x }
  def g(xs: MySet[Int]): MySet[Int] = f(xs)

odersky added a commit that referenced this issue Feb 15, 2024
…19684)

Fixes #19661.

## Cause of the issue

As reported in #19661, the following code triggers an assertation
failure during Ycheck:
```scala
import language.experimental.captureChecking

trait MySet[A]:
  def collect[B](pf: PartialFunction[A, B]^): MySet[B]^{this, pf}

class Test:
  def f(xs: MySet[Int]) = xs collect { case x => x }
  def g(xs: MySet[Int]): MySet[Int] = f(xs)
```

The failure happens when checking the tree `f(xs)`, whose type is
`MySet[Int]^{this, PartialFunction[Int, Int]}`. The `checkNoOrphans`
function is invoked on `this`, whose type turns out to be an orphan
parameter reference (`xs`).

We first inspect the tree outputed by `typer`:
```scala
class Test() extends Object() {
  def f(xs: MySet[Int]): MySet[Int]^{this, PartialFunction[Int, Int]} =
    xs.collect[Int](
      {
        def $anonfun(x$1: Int): Int =
          x$1 match
            {
              case x @ _ =>
                x:Int
            }
        closure($anonfun:PartialFunction[Int, Int])
      }
    )
  def g(xs: MySet[Int]): MySet[Int] = this.f(xs)
}
```

The problem roots in the signature of the method `f`: in the capture set
of its result type, the `this` reference is dangling.

How come? It turns out that the `asSeenFrom` map is not working
correctly for the typing of `xs.collect`:
```
(xs.collect : [B](pf: PartialFunction[Int, B]^): MySet[B]^{this, pf})
```
Instead of replacing `this` with `xs`, `asSeenFrom` keeps `this`
untouched. This is what happened:
- When mapping `asSeenFrom` on the method type, the `asSeenFrom` map
recurses and applies on the annotated type.
- When mapping the annotation (`@retains(this, pf)`), the `asSeenFrom`
map derives a `TreeTypeMap` from itself and uses it to map the `tree` of
the annotation.
- During that, the type of `this` is properly mapped to `xs.type` but
the tree `this` is never changed (since the `TreeTypeMap` is an identity
on the structure of trees).

To solve this issue, there are (at least) two possibilities:
- Refactor the `TypeMap` machineries on annotations to enable it to
properly handle these cases. But it is hard: when mapping the capture
annotation, we are at a pre-CC phase, so tools for manipulating capture
sets are not available. And it is unnecessary: even if we compute these
references properly, it gets discarded during CC.
- During Ycheck, ignore orphan parameter references inside a normal
`@retains` annotation (as opposed to an optimised `CaptureAnnotation`).
This feels like a dangerous fix but these `@retains` annotations, even
if they are ill-formed, is already treated as being unreliable in CC and
get rechecked. Also, CC turns these concrete annotations into optimised
`CaptureAnnotation`s, which are not ignored by Ycheck.

## Fix

So this PR implements the second option:
- Ignore orphan parameter errors inside a normal `@retains` annotation
during Ycheck.
- The check for `CaptureAnnotation`s will not be bypassed.
@Kordyjan Kordyjan added this to the 3.4.2 milestone Mar 28, 2024
@Kordyjan Kordyjan modified the milestones: 3.4.2, 3.5.0 May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:experimental:cc Capture checking related itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants