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

lint trivially self-recursive extension methods #13709

Merged
merged 1 commit into from
Nov 3, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class CheckLoopingImplicits extends MiniPhase:
case Apply(fn, args) =>
checkNotLooping(fn)
fn.tpe.widen match
case mt: MethodType =>
case mt: MethodType
// Boolean && and || aren't defined with by-name parameters
// and therefore their type isn't an ExprType, so we exempt them by symbol name
if t.symbol != defn.Boolean_&& && t.symbol != defn.Boolean_|| =>
args.lazyZip(mt.paramInfos).foreach { (arg, pinfo) =>
if !pinfo.isInstanceOf[ExprType] then checkNotLooping(arg)
}
Expand Down Expand Up @@ -80,8 +83,8 @@ class CheckLoopingImplicits extends MiniPhase:
checkNotLooping(t.rhs)
case _ =>

if sym.isOneOf(GivenOrImplicit | Lazy) then
if sym.isOneOf(GivenOrImplicit | Lazy | ExtensionMethod) then
checkNotLooping(mdef.rhs)
mdef
end transform
end CheckLoopingImplicits
end CheckLoopingImplicits
30 changes: 30 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i9880.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
opaque type Bytes = Array[Byte]
object Bytes:
extension (self: Bytes)
def size: Int = (self: Array[Byte]).size // error

//

object Module1:
opaque type State[S, +A] = S => (S, A)
object State:
extension [S, A](self: State[S, A])
def map[B](f: A => B): State[S, B] =
s => { val (s2, a) = self(s); (s2, f(a)) }
object Module2:
import Module1.State
trait RNG
opaque type Gen[+A] = State[RNG, A]
object Gen:
extension [A](self: Gen[A])
def map[B](f: A => B): Gen[B] =
self.map(f) // error

//

class Sym(val owner: Sym)

extension (sym: Sym)
def isSomething: Boolean = false
def isFoo: Boolean = sym.isSomething && sym.owner.isFoo // was: Infinite loop in function body
def isBar: Boolean = sym.isSomething || sym.owner.isBar // was: Infinite loop in function body