Permalink
Browse files

SI-8035 Deprecate automatic () insertion in argument lists

This promotes the () insertion warning from -Ywarn-adapted-args to a
deprecation warning. -Xfuture tunrs it into a compiler error.

Auto tupling remains unchanged for now.

The tests have been fixed the following way:
- Warnings caused by general sloppiness (Try(), Future(), ...) have been
  fixed.
- Warnings which raise interesting questions (x == (), ...) received an
  updated checkfile for now.
  • Loading branch information...
1 parent ada8d91 commit c5567e2700dfe6c19d968c2285821ef4ab8a8e6c @soc soc committed Dec 10, 2013
@@ -43,11 +43,11 @@ trait Adaptations {
def givenString = if (args.isEmpty) "<none>" else args.mkString(", ")
def adaptedArgs = if (args.isEmpty) "(): Unit" else args.mkString("(", ", ", "): " + applyArg.tpe)
- def adaptWarning(msg: String) = context.warning(t.pos, msg +
+ def adaptWarningMessage(msg: String, showAdaptation: Boolean = true) = msg +
"\n signature: " + sigString +
"\n given arguments: " + givenString +
- "\n after adaptation: " + callString + "(" + adaptedArgs + ")"
- )
+ (if (showAdaptation) "\n after adaptation: " + callString + "(" + adaptedArgs + ")" else "")
+
// A one-argument method accepting Object (which may look like "Any"
// at this point if the class is java defined) is a "leaky target" for
// which we should be especially reluctant to insert () or auto-tuple.
@@ -69,17 +69,20 @@ trait Adaptations {
}
if (settings.noAdaptedArgs)
- adaptWarning("No automatic adaptation here: use explicit parentheses.")
- else if (settings.warnAdaptedArgs)
- adaptWarning(
- if (args.isEmpty) "Adapting argument list by inserting (): " + (
- if (isLeakyTarget) "leaky (Object-receiving) target makes this especially dangerous."
- else "this is unlikely to be what you want."
- )
- else "Adapting argument list by creating a " + args.size + "-tuple: this may not be what you want."
- )
+ context.warning(t.pos, adaptWarningMessage("No automatic adaptation here: use explicit parentheses."))
+ else if (args.isEmpty) {
+ if (settings.future)
+ context.error(t.pos, adaptWarningMessage("Adaptation of argument list by inserting () has been removed.", showAdaptation = false))
+ else {
+ val msg = "Adaptation of argument list by inserting () has been deprecated: " + (
+ if (isLeakyTarget) "leaky (Object-receiving) target makes this especially dangerous."
+ else "this is unlikely to be what you want.")
+ context.unit.deprecationWarning(t.pos, adaptWarningMessage(msg))
+ }
+ } else if (settings.warnAdaptedArgs)
+ context.warning(t.pos, adaptWarningMessage(s"Adapting argument list by creating a ${args.size}-tuple: this may not be what you want."))
- !settings.noAdaptedArgs
+ !settings.noAdaptedArgs || !(args.isEmpty && settings.future)
}
}
}
@@ -11,7 +11,7 @@ object Test {
react {
case 'hello =>
println("MSG1")
- reply()
+ reply(())
react {
case 'bye =>
println("done")
@@ -1,6 +1,3 @@
-
-
-
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.duration.Duration.Inf
@@ -518,7 +515,7 @@ class FutureTests extends MinimalScalaTest {
}
"should not deadlock with nested await (ticket 1313)" in {
- val simple = Future() map {
+ val simple = Future(()) map {
_ =>
val unit = Future(())
val umap = unit map { _ => () }
@@ -527,7 +524,7 @@ class FutureTests extends MinimalScalaTest {
Await.ready(simple, Inf).isCompleted mustBe (true)
val l1, l2 = new TestLatch
- val complex = Future() map {
+ val complex = Future(()) map {
_ =>
blocking {
val nested = Future(())
@@ -549,5 +546,3 @@ class FutureTests extends MinimalScalaTest {
}
}
-
-
@@ -58,7 +58,7 @@ t1513: Array[Null] = Array(null)
scala> // ambiguous toString problem from #547
-scala> val atom = new scala.xml.Atom()
+scala> val atom = new scala.xml.Atom(())
atom: scala.xml.Atom[Unit] = ()
scala> // overriding toString problem from #1404
@@ -25,7 +25,7 @@ println("hello")
// ticket #1513
val t1513 = Array(null)
// ambiguous toString problem from #547
-val atom = new scala.xml.Atom()
+val atom = new scala.xml.Atom(())
// overriding toString problem from #1404
class S(override val toString : String)
val fish = new S("fish")
@@ -3,139 +3,139 @@ import scala.util.{Try, Success, Failure}
// tests the basic combinators on Try
trait TryStandard {
- def testForeachSuccess(): Unit = {
- val t = Success(1)
- var res = 0
- t.foreach(x => res = x * 10)
- assert(res == 10)
- }
-
- def testForeachFailure(): Unit = {
- val t = Failure(new Exception("foo"))
- t.foreach(x => assert(false))
- }
-
- def testFlatMapSuccess(): Unit = {
- val t = Success(1)
- val n = t.flatMap(x => Try(x * 10))
- assert(n.get == 10)
- }
-
- def testFlatMapFailure(): Unit = {
- val t = Failure(new Exception("foo"))
- val n = t.flatMap{ x => assert(false); Try() }
- }
-
- def testMapSuccess(): Unit = {
- val t = Success(1)
- val n = t.map(x => x * 10)
- assert(n.get == 10)
- }
-
- def testMapFailure(): Unit = {
- val t = Failure(new Exception("foo"))
- val n = t.map(x => assert(false))
- }
-
- def testFilterSuccessTrue(): Unit = {
- val t = Success(1)
- val n = t.filter(x => x > 0)
- assert(n.get == 1)
- }
-
- def testFilterSuccessFalse(): Unit = {
- val t = Success(1)
- val n = t.filter(x => x < 0)
- n match {
- case Success(v) => assert(false)
- case Failure(e: NoSuchElementException) => assert(true)
+ def testForeachSuccess(): Unit = {
+ val t = Success(1)
+ var res = 0
+ t.foreach(x => res = x * 10)
+ assert(res == 10)
+ }
+
+ def testForeachFailure(): Unit = {
+ val t = Failure(new Exception("foo"))
+ t.foreach(x => assert(false))
+ }
+
+ def testFlatMapSuccess(): Unit = {
+ val t = Success(1)
+ val n = t.flatMap(x => Try(x * 10))
+ assert(n.get == 10)
+ }
+
+ def testFlatMapFailure(): Unit = {
+ val t = Failure(new Exception("foo"))
+ val n = t.flatMap{ x => assert(false); Try(()) }
+ }
+
+ def testMapSuccess(): Unit = {
+ val t = Success(1)
+ val n = t.map(x => x * 10)
+ assert(n.get == 10)
+ }
+
+ def testMapFailure(): Unit = {
+ val t = Failure(new Exception("foo"))
+ val n = t.map(x => assert(false))
+ }
+
+ def testFilterSuccessTrue(): Unit = {
+ val t = Success(1)
+ val n = t.filter(x => x > 0)
+ assert(n.get == 1)
+ }
+
+ def testFilterSuccessFalse(): Unit = {
+ val t = Success(1)
+ val n = t.filter(x => x < 0)
+ n match {
+ case Success(v) => assert(false)
+ case Failure(e: NoSuchElementException) => assert(true)
case _ => assert(false)
- }
- }
-
- def testFilterFailure(): Unit = {
- val t = Failure(new Exception("foo"))
- val n = t.filter{ x => assert(false); true }
- }
-
- def testRescueSuccess(): Unit = {
- val t = Success(1)
- t.recoverWith{ case x => assert(false); Try() }
- }
-
- def testRescueFailure(): Unit = {
- val t = Failure(new Exception("foo"))
- val n = t.recoverWith{ case x => Try(1) }
- assert(n.get == 1)
- }
-
- def testRecoverSuccess(): Unit = {
- val t = Success(1)
- t.recover{ case x => assert(false); 99 }
- }
-
- def testRecoverFailure(): Unit = {
- val t = Failure(new Exception("foo"))
- val n = t.recover{ case x => 1 }
- assert(n.get == 1)
- }
-
- def testFlattenSuccess(): Unit = {
- val f = Failure(new Exception("foo"))
- val t = Success(f)
- assert(t.flatten == f)
- }
-
- def testFailedSuccess(): Unit = {
- val t = Success(1)
- val n = t.failed
- n match {
- case Failure(e: UnsupportedOperationException) => assert(true)
- case _ => assert(false)
- }
- }
-
- def testFailedFailure(): Unit = {
- val t = Failure(new Exception("foo"))
- val n = t.failed
- n match {
- case Success(e: Exception) => assert(true)
- case _ => assert(false)
- }
- }
-
- def testSuccessTransform(): Unit = {
- val s = Success(1)
- val succ = (x: Int) => Success(x * 10)
- val fail = (x: Throwable) => Success(0)
- assert(s.transform(succ, fail).get == 10)
- }
-
- def testFailureTransform(): Unit = {
- val f = Failure(new Exception("foo"))
- val succ = (x: Int) => Success(x * 10)
- val fail = (x: Throwable) => Success(0)
- assert(f.transform(succ, fail).get == 0)
- }
-
- testForeachSuccess()
- testForeachFailure()
- testFlatMapSuccess()
- testFlatMapFailure()
- testMapSuccess()
- testMapFailure()
- testFilterSuccessTrue()
- testFilterSuccessFalse()
- testFilterFailure()
- testRescueSuccess()
- testRescueFailure()
- testRecoverSuccess()
- testRecoverFailure()
- testFlattenSuccess()
- testFailedSuccess()
- testFailedFailure()
- testSuccessTransform()
- testFailureTransform()
+ }
+ }
+
+ def testFilterFailure(): Unit = {
+ val t = Failure(new Exception("foo"))
+ val n = t.filter{ x => assert(false); true }
+ }
+
+ def testRescueSuccess(): Unit = {
+ val t = Success(1)
+ t.recoverWith{ case x => assert(false); Try(()) }
+ }
+
+ def testRescueFailure(): Unit = {
+ val t = Failure(new Exception("foo"))
+ val n = t.recoverWith{ case x => Try(1) }
+ assert(n.get == 1)
+ }
+
+ def testRecoverSuccess(): Unit = {
+ val t = Success(1)
+ t.recover{ case x => assert(false); 99 }
+ }
+
+ def testRecoverFailure(): Unit = {
+ val t = Failure(new Exception("foo"))
+ val n = t.recover{ case x => 1 }
+ assert(n.get == 1)
+ }
+
+ def testFlattenSuccess(): Unit = {
+ val f = Failure(new Exception("foo"))
+ val t = Success(f)
+ assert(t.flatten == f)
+ }
+
+ def testFailedSuccess(): Unit = {
+ val t = Success(1)
+ val n = t.failed
+ n match {
+ case Failure(e: UnsupportedOperationException) => assert(true)
+ case _ => assert(false)
+ }
+ }
+
+ def testFailedFailure(): Unit = {
+ val t = Failure(new Exception("foo"))
+ val n = t.failed
+ n match {
+ case Success(e: Exception) => assert(true)
+ case _ => assert(false)
+ }
+ }
+
+ def testSuccessTransform(): Unit = {
+ val s = Success(1)
+ val succ = (x: Int) => Success(x * 10)
+ val fail = (x: Throwable) => Success(0)
+ assert(s.transform(succ, fail).get == 10)
+ }
+
+ def testFailureTransform(): Unit = {
+ val f = Failure(new Exception("foo"))
+ val succ = (x: Int) => Success(x * 10)
+ val fail = (x: Throwable) => Success(0)
+ assert(f.transform(succ, fail).get == 0)
+ }
+
+ testForeachSuccess()
+ testForeachFailure()
+ testFlatMapSuccess()
+ testFlatMapFailure()
+ testMapSuccess()
+ testMapFailure()
+ testFilterSuccessTrue()
+ testFilterSuccessFalse()
+ testFilterFailure()
+ testRescueSuccess()
+ testRescueFailure()
+ testRecoverSuccess()
+ testRecoverFailure()
+ testFlattenSuccess()
+ testFailedSuccess()
+ testFailedFailure()
+ testSuccessTransform()
+ testFailureTransform()
}
object Test
Oops, something went wrong.

0 comments on commit c5567e2

Please sign in to comment.