Skip to content

Commit 4b554f2

Browse files
committed
Revert "Nothing is a special bottom type; it is a subtype of every other type. The Scala compiler loves to infer Nothing as a generic type but that is almost always incorrect. Explicit type arguments should be used instead."
This reverts commit 9622a3f.
1 parent 9622a3f commit 4b554f2

File tree

5 files changed

+98
-54
lines changed

5 files changed

+98
-54
lines changed

analyzer/src/main/scala/com/avsystem/commons/analyzer/AnalyzerPlugin.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ final class AnalyzerPlugin(val global: Global) extends Plugin { plugin =>
5656
new Any2StringAdd(global),
5757
new ThrowableObjects(global),
5858
new DiscardedMonixTask(global),
59-
new RequireExplicitNothing(global),
59+
new ThrownExceptionNotInFunction(global),
6060
new BadSingletonComponent(global),
6161
new ConstantDeclarations(global),
6262
new BasePackage(global),

analyzer/src/main/scala/com/avsystem/commons/analyzer/RequireExplicitNothing.scala

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.avsystem.commons
2+
package analyzer
3+
4+
import scala.tools.nsc.Global
5+
6+
final class ThrownExceptionNotInFunction(g: Global) extends AnalyzerRule(g, "thrownExceptionNotInFunction") {
7+
8+
import global.*
9+
10+
def analyze(unit: CompilationUnit): Unit = unit.body.foreach(analyzeTree {
11+
case t@Apply(f: TypeApply, List(Throw(_))) if definitions.isFunctionType(f.tpe.params.head.tpe) =>
12+
report(t.pos, "exception thrown in place of function definition")
13+
})
14+
}

analyzer/src/test/scala/com/avsystem/commons/analyzer/RequireExplicitNothingTest.scala

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.avsystem.commons
2+
package analyzer
3+
4+
import org.scalatest.funsuite.AnyFunSuite
5+
6+
final class ThrownExceptionNotInFunctionTest extends AnyFunSuite with AnalyzerTest {
7+
settings.pluginOptions.value ++= List("AVSystemAnalyzer:-discardedMonixTask")
8+
9+
Seq(
10+
("Option[_]", "map"),
11+
("Option[_]", "flatMap"),
12+
("List[_]", "map"),
13+
("Seq[_]", "map"),
14+
("Set[_]", "map"),
15+
("Map[_, _]", "map"),
16+
("scala.concurrent.Future[_]", "map"),
17+
("scala.concurrent.Future[_]", "flatMap"),
18+
("scala.util.Try[_]", "map"),
19+
("Either[_, _]", "map"),
20+
("monix.eval.Task[_]", "map"),
21+
("monix.eval.Task[_]", "flatMap"),
22+
("com.avsystem.commons.misc.Opt[_]", "map"),
23+
("String => Int", "andThen"),
24+
("String => Nothing", "andThen"),
25+
("Nothing => Nothing", "andThen"),
26+
("String => Int", "compose"),
27+
("Seq[_]", "foreach"),
28+
).foreach { case (tpe, function) =>
29+
test(s"Testing $function of $tpe") {
30+
assertErrors(10,
31+
//language=Scala
32+
s"""
33+
|object whatever {
34+
| implicit val ec: scala.concurrent.ExecutionContext = ??? // for Future
35+
|
36+
| def sth: $tpe = ???
37+
| def ex: Exception = ???
38+
|
39+
| // errors from these
40+
| sth.$function(throw ex)
41+
|
42+
| {
43+
| println(""); sth.$function(throw ex)
44+
| }
45+
|
46+
| if (true) sth.$function(throw ex) else sth.$function(throw ex)
47+
|
48+
| try sth.$function(throw ex) catch {
49+
| case _: Exception => sth.$function(throw ex)
50+
| } finally sth.$function(throw ex)
51+
|
52+
| Seq(1, 2, 3).foreach(_ => sth.$function(throw ex))
53+
|
54+
| while (true) sth.$function(throw ex)
55+
|
56+
| do sth.$function(throw ex) while (true)
57+
|
58+
| // no errors from these
59+
| sth.$function(_ => throw ex)
60+
|
61+
| {
62+
| println(""); sth.$function(_ => throw ex)
63+
| }
64+
|
65+
| if (true) sth.$function(_ => throw ex) else sth.$function(_ => throw ex)
66+
|
67+
| try sth.$function(_ => throw ex) catch {
68+
| case _: Exception => sth.$function(_ => throw ex)
69+
| } finally sth.$function(_ => throw ex)
70+
|
71+
| Seq(1, 2, 3).foreach(_ => sth.$function(_ => throw ex))
72+
|
73+
| while (true) sth.$function(_ => throw ex)
74+
|
75+
| do sth.$function(_ => throw ex) while (true)
76+
|}
77+
|
78+
|""".stripMargin
79+
)
80+
}
81+
}
82+
}
83+

0 commit comments

Comments
 (0)