Skip to content
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

Find more looping implicits #15481

Merged
merged 3 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@ import MegaPhase.MiniPhase
import Contexts.*, Types.*, Symbols.*, SymDenotations.*, Flags.*
import ast.*
import Decorators.*

import StdNames.*

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._
Expand Down Expand Up @@ -60,6 +75,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) =>
Expand All @@ -84,7 +102,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)
Copy link
Contributor

@julienrf julienrf Jun 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment to explain the intent behind the condition on the right side of the ||? I wonder if this condition is too strong, but I am not sure. Do we have existing “pos” tests (the PR only contains two “neg” tests)?

then
checkNotLooping(mdef.rhs)
mdef
end transform
Expand Down
16 changes: 16 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i15474.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import scala.language.implicitConversions

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

object Price{
given Ordering[Price] = summon[Ordering[BigDecimal]] // error
}
}
132 changes: 132 additions & 0 deletions tests/neg/i13044.check
Original file line number Diff line number Diff line change
@@ -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
| ^^^^^^^^^^
Expand Down