Skip to content

Commit

Permalink
Warn nopos & tests/init refactor for warn tests
Browse files Browse the repository at this point in the history
  • Loading branch information
szymon-rd committed Dec 15, 2023
1 parent b1d1fe8 commit ebc9795
Show file tree
Hide file tree
Showing 153 changed files with 199 additions and 195 deletions.
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class CompilationTests {
implicit val testGroup: TestGroup = TestGroup("checkInit")
val options = defaultOptions.and("-Ysafe-init", "-Xfatal-warnings")
compileFilesInDir("tests/init/neg", options).checkExpectedErrors()
compileFilesInDir("tests/init/warn", defaultOptions.and("-Ysafe-init")).checkWarnings()
compileFilesInDir("tests/init/pos", options).checkCompile()
compileFilesInDir("tests/init/crash", options.without("-Xfatal-warnings")).checkCompile()
// The regression test for i12128 has some atypical classpath requirements.
Expand Down
11 changes: 7 additions & 4 deletions compiler/test/dotty/tools/vulpix/ParallelTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
end maybeFailureMessage

def getWarnMapAndExpectedCount(files: Seq[JFile]): (HashMap[String, Integer], Int) =
val comment = raw"//( *)warn".r
val comment = raw"//( *)(nopos-)?warn".r
val map = new HashMap[String, Integer]()
var count = 0
def bump(key: String): Unit =
Expand All @@ -787,8 +787,11 @@ trait ParallelTesting extends RunnerOrchestration { self =>
files.filter(isSourceFile).foreach { file =>
Using(Source.fromFile(file, StandardCharsets.UTF_8.name)) { source =>
source.getLines.zipWithIndex.foreach { case (line, lineNbr) =>
comment.findAllMatchIn(line).foreach { _ =>
bump(s"${file.getPath}:${lineNbr+1}")
comment.findAllMatchIn(line).foreach { m =>
m.group(2) match
case "nopos-" =>
bump("nopos")
case _ => bump(s"${file.getPath}:${lineNbr+1}")
}
}
}.get
Expand All @@ -809,7 +812,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
val key = s"${relativize(srcpos.source.file.toString())}:${srcpos.line + 1}"
if !seenAt(key) then unexpected += key
else
unpositioned += relativize(srcpos.source.file.toString())
if(!seenAt("nopos")) unpositioned += relativize(srcpos.source.file.toString())

reporterWarnings.foreach(sawDiagnostic)

Expand Down
6 changes: 0 additions & 6 deletions tests/init/neg/early-promote2.scala

This file was deleted.

4 changes: 0 additions & 4 deletions tests/init/neg/simple1.scala

This file was deleted.

3 changes: 0 additions & 3 deletions tests/init/neg/soundness4.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ abstract class AbstractFile {
}

class RemoteFile(url: String) extends AbstractFile {
val localFile: String = s"${url.##}.tmp" // error: usage of `localFile` before it's initialized
val localFile: String = s"${url.##}.tmp" // warn: usage of `localFile` before it's initialized
def name: String = localFile
}
2 changes: 1 addition & 1 deletion tests/init/neg/alias.scala → tests/init/warn/alias.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Foo {
val self = this
val x = self.n
val n = 10 // error
val n = 10 // warn
}
6 changes: 3 additions & 3 deletions tests/init/neg/apply.scala → tests/init/warn/apply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object A:

class B:
val a = A(this)
val a2 = A.foo(this) // error
val a2 = A.foo(this) // warn
val a3 = A.bar(this)

// test receiver is ThisRef
Expand All @@ -20,7 +20,7 @@ object O:

class B:
val a = A(this)
val a2 = A.foo(this) // error
val a2 = A.foo(this) // warn
val a3 = A.bar(this)

val b = new B
Expand All @@ -38,7 +38,7 @@ class M(n: N):

class B:
val a = A(this)
val a2 = A.foo(this) // error
val a2 = A.foo(this) // warn
val a3 = A.bar(this)
end M

Expand Down
2 changes: 1 addition & 1 deletion tests/init/neg/apply2.scala → tests/init/warn/apply2.scala
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class O:
println(n)

class B:
val a = A(this) // error
val a = A(this) // warn

val b = new B
val n = 10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
final class MyAsInstanceOfClass(o: MyAsInstanceOfClass) {
val other: MyAsInstanceOfClass = {
if (o.asInstanceOf[MyAsInstanceOfClass].oRef ne null) o
else new MyAsInstanceOfClass(this) // error
else new MyAsInstanceOfClass(this) // warn
}
val oRef = o
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ object Foo {
}

class A {
val foo = implicitly[Foo] // error
val foo = implicitly[Foo] // warn
assert(foo eq foo.next)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class Outer {
}

val l: List[A] = List(new A(5), new A(10))
l.foreach(a => a.addX(this)) // error
l.foreach(a => a.addX(this)) // warn
val p = 10
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ object A:

class B {
var a = new Array[B](2)
A.foo(this, a) // error
A.foo(this, a) // warn
println(a(0).i)
val i = 99
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
case class A(b: B) {
val x1 = b.x
val x = B(this) // error
val x = B(this) // warn
val y = x.a
}

case class B(a: A) {
val x1 = a.x
val x = A(this) // error
val x = A(this) // warn
val h = x.b
}
4 changes: 2 additions & 2 deletions tests/init/neg/cycle.scala → tests/init/warn/cycle.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class A(x: B) {
println(x.b)
val a = new B(this) // error
val a = new B(this) // warn
val d = a.b
}

class B(x: A) {
println(x.a)
val b = new A(this) // error
val b = new A(this) // warn
val d = b.a
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class A {
class B extends A {
def updateThenCompare(c: Int): Boolean = {
x = c
compare() // error
compare() // warn
}
val result = updateThenCompare(5)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class A { // checking A
def c = new C
}
val b = new B()
println(b) // error: the checker simply issue warnings for objects that contain inner classes
println(b) // warn: the checker simply issue warnings for objects that contain inner classes
val af = 42
}

Expand Down
6 changes: 6 additions & 0 deletions tests/init/warn/early-promote2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class M {
println(this) // warn
foo()
private val a = 5 // warn
def foo() = a
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ abstract class A {
}

class M extends A {
def bar() = promote(this) // error
def bar() = promote(this) // warn
def promote(m: M) = m.foo()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class Outer {

class Dummy {
val m: Int = n + 4
val n: Int = 10 // error
val n: Int = 10 // warn
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class Outer {

class Container {
val o = new Outer
val m = new o.M(this) // error
val m = new o.M(this) // warn
val s = "hello"
}

class Dummy {
val m: Int = n + 4
val n: Int = 10 // error
val n: Int = 10 // warn
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Foo {
val a = Foo.bar(this) // error
val a = Foo.bar(this) // warn
val b = "hello"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ abstract class Parent {
}
class Child extends Parent {
val a = f()
val b = "hello" // error
val b = "hello" // warn
def message: String = b
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
trait TA {
val x = "world" // error
val x = "world" // warn
}

trait TB {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ class Trees {
class ValDef { counter += 1 }
class EmptyValDef extends ValDef
val theEmptyValDef = new EmptyValDef
private var counter = 0 // error
private var counter = 0 // warn
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class Test1 extends U {
}

class Test extends T {
override final val f1 = /*super.f1*/ 1 + f2 // error
override final val f2 = 2 // error
override final val f3 = {println(3); 3} // error
override val f4 = f3 + 1 // error
override final val f1 = /*super.f1*/ 1 + f2 // warn
override final val f2 = 2 // warn
override final val f3 = {println(3); 3} // warn
override val f4 = f3 + 1 // warn

def g: 3 = { println("g"); 3 }
final val x = g + 1
Expand Down
2 changes: 1 addition & 1 deletion tests/init/neg/flow2.scala → tests/init/warn/flow2.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Foo {
val len = list.size
val list = List(4, 6) // error
val list = List(4, 6) // warn

lazy val len2 = list2.size // ok
val list2 = List(4, 6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ class Foo {
val f: Int => Foo = (x: Int) => if x > 0 then f(x) else this
f(10).n

val n = 10 // error
val n = 10 // warn
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class Foo {
val fun2: Int => Int = n => 1 + n + list.size
fun2(5)

List(5, 9).map(n => 2 + n + list.size) // error
List(5, 9).map(n => 2 + n + list.size) // warn

final val list = List(1, 2, 3) // error
final val list = List(1, 2, 3) // warn

List(5, 9).map(n => 3 + n + list.size)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Base { self =>
(0 to 10).foreach { i => // error
(0 to 10).foreach { i => // warn
println(a)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
final class Capture {
private var m: Boolean = false

(0 to 10).foreach { i => // error
(0 to 10).foreach { i => // warn
f()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ final class Foo {
def fun: Int => Int = n => n + x.size
fun(5)

val x = "hello" // error
val x = "hello" // warn
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ class Y {

new C

val n = 10 // error
val n = 10 // warn
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ class Y {
val x = new X
x.b.g

val n = 10 // error
val n = 10 // warn
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class Foo {
}

val bar: Bar = new Bar
val list = List(1, 2, 3) // error
val list = List(1, 2, 3) // warn
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ class Foo {
def g = new Foo.this.Inner
}

val list = List(1, 2, 3) // error
val list = List(1, 2, 3) // warn
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ class Foo {
def g = list
}

val list = List(1, 2, 3) // error
val list = List(1, 2, 3) // warn
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class Wrap {
abstract class E[+T] { def foo: T }
object E {
final val A: E[Nothing] = new E { def foo = ref }
val ref = qux(A) // error
val ref = qux(A) // warn
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class A {
trait Bounded {
type T >: Cov[Int] <: Cov[String]
}
val t: Bounded = new Bounded { // error
val t: Bounded = new Bounded { // warn
// Note: using this instead of t produces an error (as expected)
override type T >: t.T <: t.T
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Outer:

class B() { def foo() = n + 1 }
def g(b: B): Int = b.foo()
g(new B()) // error
g(new B()) // warn
val n: Int = 10

@main def main(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class A:
// should report one error here
val b = new B(this) // error
val b = new B(this) // warn
val m = 10
val n = 20

Expand Down
Loading

0 comments on commit ebc9795

Please sign in to comment.