Skip to content

Bootstrap compiler with -Ysafe-init #15722

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

Merged
merged 8 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/init/Checker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ class Checker extends Phase {

override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
val checkCtx = ctx.fresh.setPhase(this.start)
Semantic.withInitialState {
Semantic.checkTasks(using checkCtx) {
val traverser = new InitTreeTraverser()
units.foreach { unit => traverser.traverse(unit.tpdTree) }
given Context = checkCtx
Semantic.check()
super.runOn(units)
}
units

def run(using Context): Unit = {
// ignore, we already called `Semantic.check()` in `runOn`
Expand Down
33 changes: 14 additions & 19 deletions compiler/src/dotty/tools/dotc/transform/init/Semantic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1131,11 +1131,6 @@ object Semantic:
*
*/
def tryPromote(msg: String): Contextual[List[Error]] = log("promote " + warm.show + ", promoted = " + promoted, printer) {
val classRef = warm.klass.appliedRef
val hasInnerClass = classRef.memberClasses.filter(_.symbol.hasSource).nonEmpty
if hasInnerClass then
return PromoteError(msg + "Promotion cancelled as the value contains inner classes.", trace.toVector) :: Nil

val obj = warm.objekt

def doPromote(klass: ClassSymbol, subClass: ClassSymbol, subClassSegmentHot: Boolean)(using Reporter): Unit =
Expand All @@ -1148,7 +1143,9 @@ object Semantic:
params.forall(param => obj.field(param).isHot)
}

// check invariant: subClassSegmentHot => isHotSegment
// Check invariant: subClassSegmentHot ==> isHotSegment
//
// This invariant holds because of the Scala/Java/JVM restriction that we cannot use `this` in super constructor calls.
if subClassSegmentHot && !isHotSegment then
report.error("[Internal error] Expect current segment to hot in promotion, current klass = " + klass.show +
", subclass = " + subClass.show + Trace.show, Trace.position)
Expand All @@ -1158,7 +1155,10 @@ object Semantic:
// those methods are checked as part of the check for the class where they are defined.
if !isHotSegment then
for member <- klass.info.decls do
if !member.isType && !member.isConstructor && member.hasSource && !member.is(Flags.Deferred) then
if member.isClass then
val error = PromoteError("Promotion cancelled as the value contains inner " + member.show + ".", Vector.empty)
reporter.report(error)
else if !member.isType && !member.isConstructor && !member.is(Flags.Deferred) then
given Trace = Trace.empty
if member.is(Flags.Method, butNot = Flags.Accessor) then
val args = member.info.paramInfoss.flatten.map(_ => ArgInfo(Hot, Trace.empty))
Expand Down Expand Up @@ -1261,22 +1261,17 @@ object Semantic:
/** Add a checking task to the work list */
def addTask(thisRef: ThisRef)(using WorkList) = workList.addTask(Task(thisRef))

/** Perform check on the work list until it becomes empty
*
* Should only be called once from the checker.
*/
def check()(using Cache, WorkList, Context) = workList.work()

/** Perform actions with initial checking state.
/** Check the specified tasks
*
* Semantic.withInitialState {
* Semantic.checkTasks {
* Semantic.addTask(...)
* ...
* Semantic.check()
* }
*/
def withInitialState[T](work: (Cache, WorkList) ?=> T): T =
work(using new Cache, new WorkList)
def checkTasks(using Context)(taskBuilder: WorkList ?=> Unit): Unit =
val workList = new WorkList
val cache = new Cache
taskBuilder(using workList)
workList.work()(using cache, ctx)

// ----- Semantic definition --------------------------------

Expand Down
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ object Build {
},

// Note: bench/profiles/projects.yml should be updated accordingly.
Compile / scalacOptions ++= Seq("-Yexplicit-nulls"),
Compile / scalacOptions ++= Seq("-Yexplicit-nulls", "-Ysafe-init"),

repl := (Compile / console).value,
Compile / console / scalacOptions := Nil, // reset so that we get stock REPL behaviour! E.g. avoid -unchecked being enabled
Expand Down
12 changes: 12 additions & 0 deletions tests/init/neg/promotion-segment3.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Error: tests/init/neg/promotion-segment3.scala:9:6 ------------------------------------------------------------------
9 | bar(new B) // error
| ^^^^^
| Cannot prove the method argument is hot. Only hot values are safe to leak.
| Found = Warm[class B] { outer = ThisRef[class A] }. Calling trace:
| -> class A: [ promotion-segment3.scala:2 ]
| ^
| -> bar(new B) // error [ promotion-segment3.scala:9 ]
| ^^^^^
|
| Promoting the value to hot failed due to the following problem:
| Promotion cancelled as the value contains inner class C.
11 changes: 11 additions & 0 deletions tests/init/neg/promotion-segment3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

class A:
class B:
class C:
def foo(): Int = m

def bar(b: B) = new b.C().foo()

bar(new B) // error

val m = 10
58 changes: 58 additions & 0 deletions tests/init/neg/super-resolution3.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-- Error: tests/init/neg/super-resolution3.scala:27:6 ------------------------------------------------------------------
27 | val n = 40 // error
| ^
| Access non-initialized value n. Calling trace:
| -> class C extends A with M with N: [ super-resolution3.scala:22 ]
| ^
| -> new Inner() [ super-resolution3.scala:23 ]
| ^^^^^^^^^^^
| -> class Inner: [ super-resolution3.scala:17 ]
| ^
| -> N.super[A].foo() [ super-resolution3.scala:18 ]
| ^^^^^^^^^^^^^^^^
| -> def foo(): Int = n [ super-resolution3.scala:3 ]
| ^
-- Error: tests/init/neg/super-resolution3.scala:26:6 ------------------------------------------------------------------
26 | val m = 30 // error
| ^
| Access non-initialized value m. Calling trace:
| -> class C extends A with M with N: [ super-resolution3.scala:22 ]
| ^
| -> new Inner() [ super-resolution3.scala:23 ]
| ^^^^^^^^^^^
| -> class Inner: [ super-resolution3.scala:17 ]
| ^
| -> N.super.foo() [ super-resolution3.scala:19 ]
| ^^^^^^^^^^^^^
| -> override def foo(): Int = a + super.foo() [ super-resolution3.scala:11 ]
| ^^^^^^^^^^^
| -> def foo(): Int = m [ super-resolution3.scala:7 ]
| ^
-- Error: tests/init/neg/super-resolution3.scala:24:6 ------------------------------------------------------------------
24 | val a = 10 // error
| ^
| Access non-initialized value a. Calling trace:
| -> class C extends A with M with N: [ super-resolution3.scala:22 ]
| ^
| -> new Inner() [ super-resolution3.scala:23 ]
| ^^^^^^^^^^^
| -> class Inner: [ super-resolution3.scala:17 ]
| ^
| -> N.super.foo() [ super-resolution3.scala:19 ]
| ^^^^^^^^^^^^^
| -> override def foo(): Int = a + super.foo() [ super-resolution3.scala:11 ]
| ^
-- Error: tests/init/neg/super-resolution3.scala:25:6 ------------------------------------------------------------------
25 | val b = 20 // error
| ^
| Access non-initialized value b. Calling trace:
| -> class C extends A with M with N: [ super-resolution3.scala:22 ]
| ^
| -> new Inner() [ super-resolution3.scala:23 ]
| ^^^^^^^^^^^
| -> class Inner: [ super-resolution3.scala:17 ]
| ^
| -> foo() [ super-resolution3.scala:20 ]
| ^^^^^
| -> override def foo(): Int = b * super.foo() [ super-resolution3.scala:15 ]
| ^
27 changes: 27 additions & 0 deletions tests/init/neg/super-resolution3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
abstract class A:
val n: Int
def foo(): Int = n

trait B:
val m: Int
def foo(): Int = m

trait M extends A with B:
val a: Int
override def foo(): Int = a + super.foo()

trait N extends A with B:
val b: Int
override def foo(): Int = b * super.foo()

class Inner:
N.super[A].foo()
N.super.foo()
foo()

class C extends A with M with N:
new Inner()
val a = 10 // error
val b = 20 // error
val m = 30 // error
val n = 40 // error
8 changes: 8 additions & 0 deletions tests/init/pos/DottyTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ContextBase:
class Inner:
def foo(): ContextBase = ContextBase.this

class Outer:
val ctx = new ContextBase {}
println(ctx)
val n = 10