From a008ca2779205c7db998970a337f0b64039f0bc9 Mon Sep 17 00:00:00 2001 From: odersky Date: Sun, 19 Jun 2022 23:18:45 +0200 Subject: [PATCH 1/3] Find more looping implicits 1. Also check apply methods of companions of implicit or given classes. Specifically apply methods of implicit Conversions. 2. Look inside Inlined nodes to detect loops. Fixes #15474 Fixes #10947 --- .../tools/dotc/transform/CheckLoopingImplicits.scala | 9 +++++++-- tests/neg-custom-args/fatal-warnings/i15474.scala | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/neg-custom-args/fatal-warnings/i15474.scala diff --git a/compiler/src/dotty/tools/dotc/transform/CheckLoopingImplicits.scala b/compiler/src/dotty/tools/dotc/transform/CheckLoopingImplicits.scala index 758e5d7f5cc6..4a8aefc2ddab 100644 --- a/compiler/src/dotty/tools/dotc/transform/CheckLoopingImplicits.scala +++ b/compiler/src/dotty/tools/dotc/transform/CheckLoopingImplicits.scala @@ -6,7 +6,7 @@ import MegaPhase.MiniPhase import Contexts.*, Types.*, Symbols.*, SymDenotations.*, Flags.* import ast.* import Decorators.* - +import StdNames.* object CheckLoopingImplicits: val name: String = "checkLoopingImplicits" @@ -60,6 +60,9 @@ class CheckLoopingImplicits extends MiniPhase: case Block(stats, expr) => stats.foreach(checkNotLooping) checkNotLooping(expr) + case Inlined(_, bindings, expr) => + bindings.foreach(checkNotLooping) + checkNotLooping(expr) case Typed(expr, _) => checkNotLooping(expr) case Assign(lhs, rhs) => @@ -84,7 +87,9 @@ class CheckLoopingImplicits extends MiniPhase: checkNotLooping(t.rhs) case _ => - if sym.isOneOf(GivenOrImplicit | Lazy | ExtensionMethod) then + if sym.isOneOf(GivenOrImplicit | Lazy | ExtensionMethod) + || sym.name == nme.apply && sym.owner.is(Module) && sym.owner.sourceModule.isOneOf(GivenOrImplicit) + then checkNotLooping(mdef.rhs) mdef end transform diff --git a/tests/neg-custom-args/fatal-warnings/i15474.scala b/tests/neg-custom-args/fatal-warnings/i15474.scala new file mode 100644 index 000000000000..fdbcfda60c38 --- /dev/null +++ b/tests/neg-custom-args/fatal-warnings/i15474.scala @@ -0,0 +1,12 @@ +import scala.language.implicitConversions + +given Conversion[ String, Int ] with + def apply(from: String): Int = from.toInt // error + +object Prices { + opaque type Price = BigDecimal + + object Price{ + given Ordering[Price] = summon[Ordering[BigDecimal]] // error + } +} \ No newline at end of file From cbdf40ac89d6c31007b15bc934554daed9be3167 Mon Sep 17 00:00:00 2001 From: odersky Date: Mon, 20 Jun 2022 00:02:06 +0200 Subject: [PATCH 2/3] Update check file --- tests/neg/i13044.check | 132 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/tests/neg/i13044.check b/tests/neg/i13044.check index c5584aadf767..437e84c0f480 100644 --- a/tests/neg/i13044.check +++ b/tests/neg/i13044.check @@ -1,3 +1,135 @@ +-- Warning: tests/neg/i13044.scala:50:40 ------------------------------------------------------------------------------- +50 | implicit def typeSchema: Schema[A] = Schema.gen // error // error + | ^^^^^^^^^^ + |Infinite loop in function body + |{ + | val SchemaDerivation_this: Schema.type = Schema + | { + | val SchemaDerivation_this: (SchemaDerivation_this : Schema.type) = SchemaDerivation_this + | { + | val $scrutinee1: + | scala.deriving.Mirror.Product{ + | MirroredMonoType = A; MirroredType = A; MirroredLabel = ("A" : String); MirroredElemTypes = (A, B); + | MirroredElemLabels = (("a" : String), ("b" : String)) + | } + | = + | A.$asInstanceOf[ + | scala.deriving.Mirror.Product{ + | MirroredMonoType = A; MirroredType = A; MirroredLabel = ("A" : String); MirroredElemTypes = (A, B); + | MirroredElemLabels = (("a" : String), ("b" : String)) + | } + | ] + | val m: + | scala.deriving.Mirror.Product{ + | MirroredMonoType = A; MirroredType = A; MirroredLabel = ("A" : String); MirroredElemTypes = (A, B); + | MirroredElemLabels = (("a" : String), ("b" : String)) + | } + | = $scrutinee1 + | lazy val fields: List[Schema[Any]] = + | { + | val SchemaDerivation_this: (SchemaDerivation_this : (SchemaDerivation_this : Schema.type)) = + | SchemaDerivation_this + | { + | val builder: Schema[Any] = TestApp.typeSchema.asInstanceOf[Schema[Any]] + | { + | val SchemaDerivation_this: + | (SchemaDerivation_this : (SchemaDerivation_this : (SchemaDerivation_this : Schema.type))) + | = SchemaDerivation_this + | ( + | { + | val builder: Schema[Any] = + | { + | val SchemaDerivation_this: Schema.type = Schema + | ( + | { + | val SchemaDerivation_this: (SchemaDerivation_this : Schema.type) = SchemaDerivation_this + | { + | val $scrutinee4: + | scala.deriving.Mirror.Product{ + | MirroredMonoType = B; MirroredType = B; MirroredLabel = ("B" : String); + | MirroredElemTypes = C *: EmptyTuple.type + | ; MirroredElemLabels = ("c" : String) *: EmptyTuple.type + | } + | = + | B.$asInstanceOf[ + | scala.deriving.Mirror.Product{ + | MirroredMonoType = B; MirroredType = B; MirroredLabel = ("B" : String); + | MirroredElemTypes = C *: EmptyTuple.type + | ; MirroredElemLabels = ("c" : String) *: EmptyTuple.type + | } + | ] + | val m: + | scala.deriving.Mirror.Product{ + | MirroredMonoType = B; MirroredType = B; MirroredLabel = ("B" : String); + | MirroredElemTypes = C *: EmptyTuple.type + | ; MirroredElemLabels = ("c" : String) *: EmptyTuple.type + | } + | = $scrutinee4 + | lazy val fields: List[Schema[Any]] = + | { + | val SchemaDerivation_this: + | (SchemaDerivation_this : (SchemaDerivation_this : Schema.type)) + | = SchemaDerivation_this + | { + | val builder: Schema[Any] = + | { + | val SchemaDerivation_this: Schema.type = Schema + | ( + | { + | val SchemaDerivation_this: (...SchemaDerivation_this : ....type) = + | SchemaDerivation_this + | { + | val $scrutinee6: + | ...{ + | MirroredMonoType...; MirroredType...; MirroredLabel...; + | MirroredElemTypes... + | ; MirroredElemLabels... + | } + | = ....$asInstanceOf[...] + | val m: ... = ...$scrutinee6 + | lazy val fields: ... = + | { + | val SchemaDerivation_this: ... = ... + | ...:... + | } + | { + | final class $anon() extends ...(), ... { + | def build: ... = ... + | } + | ...():... + | } + | }:...[...] + | } + | :Schema[C]) + | }.asInstanceOf[Schema[Any]] + | SchemaDerivation_this.recurse[EmptyTuple.type].::[Schema[Any]](builder) + | }:List[Schema[Any]] + | } + | { + | final class $anon() extends Object(), Schema[B] { + | def build: B = ??? + | } + | new Object with Schema[B] {...}():Schema[B] + | } + | }:Schema[B] + | } + | :Schema[B]) + | }.asInstanceOf[Schema[Any]] + | SchemaDerivation_this.recurse[EmptyTuple.type].::[Schema[Any]](builder) + | } + | :List[Schema[Any]]) + | }.::[Schema[Any]](builder) + | }:List[Schema[Any]] + | } + | { + | final class $anon() extends Object(), Schema[A] { + | def build: A = ??? + | } + | new Object with Schema[A] {...}():Schema[A] + | } + | }:Schema[A] + | }:Schema[A] + |} -- Error: tests/neg/i13044.scala:50:40 --------------------------------------------------------------------------------- 50 | implicit def typeSchema: Schema[A] = Schema.gen // error // error | ^^^^^^^^^^ From af4c365a8f296645c799e7d3a8c3379ea6fde57c Mon Sep 17 00:00:00 2001 From: odersky Date: Mon, 20 Jun 2022 14:42:49 +0200 Subject: [PATCH 3/3] Add doc comment that explains what checkLooingImplicits does --- .../dotc/transform/CheckLoopingImplicits.scala | 17 ++++++++++++++++- .../neg-custom-args/fatal-warnings/i15474.scala | 8 ++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/transform/CheckLoopingImplicits.scala b/compiler/src/dotty/tools/dotc/transform/CheckLoopingImplicits.scala index 4a8aefc2ddab..47f65f04cff7 100644 --- a/compiler/src/dotty/tools/dotc/transform/CheckLoopingImplicits.scala +++ b/compiler/src/dotty/tools/dotc/transform/CheckLoopingImplicits.scala @@ -12,7 +12,22 @@ object CheckLoopingImplicits: val name: String = "checkLoopingImplicits" val description: String = "check that implicit defs do not call themselves in an infinite loop" -/** Checks that implicit defs do not call themselves in an infinite loop */ +/** Checks that some definitions do not call themselves in an infinite loop + * This is an incomplete check, designed to catch some likely bugs instead + * of being exhaustive. The situations where infinite loops are diagnosed are + * 1. A given method should not directly call itself + * 2. An apply method in a given object should not directly call itself + * 3. A lazy val should not directly force itself + * 4. An extension method should not directly call itself + * + * In all these cases, there are some situations which would not lead to + * an infinite loop at runtime. For instance, the call could go at runtime to an + * overriding version of the method or val which breaks the loop. That's why + * this phase only issues warnings, not errors, and also why we restrict + * checks to the 4 cases above, where a recursion is somewhat hidden. + * There are also other more complicated calling patterns that could also + * be diagnosed as loops with more effort. This could be improved in the future. + */ class CheckLoopingImplicits extends MiniPhase: thisPhase => import tpd._ diff --git a/tests/neg-custom-args/fatal-warnings/i15474.scala b/tests/neg-custom-args/fatal-warnings/i15474.scala index fdbcfda60c38..86b01eb28ce6 100644 --- a/tests/neg-custom-args/fatal-warnings/i15474.scala +++ b/tests/neg-custom-args/fatal-warnings/i15474.scala @@ -1,7 +1,11 @@ import scala.language.implicitConversions -given Conversion[ String, Int ] with - def apply(from: String): Int = from.toInt // error +object Test1: + given c: Conversion[ String, Int ] with + def apply(from: String): Int = from.toInt // error + +object Test2: + given c: Conversion[ String, Int ] = _.toInt // loop not detected, could be used as a fallback to avoid the warning. object Prices { opaque type Price = BigDecimal