scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.84k stars 1.05k forks source link

`inline` methods can leak references to private classes #8194

Open LPTK opened 4 years ago

LPTK commented 4 years ago

minimized code

object Test {
  private class A() { def test = 42 }
  inline def foo: Int = A().test
}
@main def main = Test.foo

https://scastie.scala-lang.org/or0Eg6u9Tky1ImaUNEdOug

Compilation output

class A in object Test cannot be accessed as a member of (Test : Test.type) from module class main$package$.

expectation

This should compile, or at least report an error at the definition site of the inline method (similarly to how errors are reported when classes leak private val references), not at call sites.

odersky commented 4 years ago

The fine print of this is actually very tricky. The problem in general is that an inline method could be called in a contect where a type it refers to is not accessible. This looks very hard to implement without getting false negatives. Even the limited case of just flagging accesses to private types looks non-trivial. It requires a complete refactoring of the code in checkNoPrivateLeaks which itself is quite tricky. So, this will be time consuming to do, and the benefits are not that great. I think this is a case where outside contributions would be welcome and not much will happen without them.