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 5 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
4 changes: 2 additions & 2 deletions bench/profiles/projects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ charts:

scripts:
dotty:
- measure -feature -deprecation -unchecked -Xfatal-warnings -encoding UTF8 -language:implicitConversions -Yexplicit-nulls -with-compiler $(find $PROG_HOME/dotty/compiler/src/dotty -name *.scala -o -name *.java)
- measure -feature -deprecation -unchecked -Xfatal-warnings -encoding UTF8 -language:implicitConversions -Yexplicit-nulls -Ysafe-init -with-compiler $(find $PROG_HOME/dotty/compiler/src/dotty -name *.scala -o -name *.java)

re2s:
- measure $(find $PROG_HOME/tests/re2s/src -name *.scala)
- measure -Ysafe-init $(find $PROG_HOME/tests/re2s/src -name *.scala)

# scalapb:
# - source $PROG_HOME/dotty/bench/scripts/scalapb
Expand Down
2 changes: 1 addition & 1 deletion bench/scripts/scalap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ set -e

path=$(cs fetch -p org.scala-lang:scala-compiler:2.13.0)

measure -source 3.0-migration -classpath "$path" $(find $PROG_HOME/dotty/community-build/community-projects/scalap/src/scalap -name "*.scala")
measure -source 3.0-migration -classpath "$path" -Ysafe-init $(find $PROG_HOME/dotty/community-build/community-projects/scalap/src/scalap -name "*.scala")
2 changes: 1 addition & 1 deletion bench/scripts/stdlib213
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ set -e
pattern="! -name AnyVal.scala ! -name language.scala -name *.scala -o -name *.java"
stdlib213=$(find $PROG_HOME/dotty/community-build/community-projects/stdLib213/src/library/scala $pattern)
cd $PROG_HOME/dotty
measure -language:implicitConversions $stdlib213
measure -language:implicitConversions -Ysafe-init $stdlib213

13 changes: 7 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/init/Semantic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1132,10 +1132,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 +1144,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 +1156,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
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