Skip to content

Commit

Permalink
ExplicitResultTypes: Add (Skip)LocalImplicits tests. Ref scalacenter#…
Browse files Browse the repository at this point in the history
  • Loading branch information
giabao authored and mlachkar committed Jul 2, 2020
1 parent 37287b3 commit 1c55726
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ class CompilerTypePrinter(g: ScalafixGlobal, config: ExplicitResultTypesConfig)(
space: String
): Option[v1.Patch] = {
val gpos = unit.position(pos.start)
GlobalProxy.typedTreeAt(g, gpos)
val inverseSemanticdbSymbol = g
.inverseSemanticdbSymbols(sym.value)
.find(s => g.semanticdbSymbol(s) == sym.value)
.getOrElse(g.NoSymbol)
val tpe = GlobalProxy.typedTreeAt(g, gpos)
val inverseSemanticdbSymbol =
if (sym.isLocal) tpe.symbol
else
g.inverseSemanticdbSymbols(sym.value)
.find(s => g.semanticdbSymbol(s) == sym.value)
.getOrElse(g.NoSymbol)
val hasNothing = inverseSemanticdbSymbol.info.exists {
case g.definitions.NothingTpe => true
case _ => false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ final class ExplicitResultTypes(
case _: Defn.Var => MemberKind.Var
}

def isRuleCandidate[D <: Defn](
defn: D,
def isRuleCandidate(
defn: Defn,
nm: Name,
mods: Iterable[Mod],
body: Term
)(implicit ev: Extract[D, Mod], ctx: SemanticDocument): Boolean = {
)(implicit ctx: SemanticDocument): Boolean = {
import config._

def matchesMemberVisibility(): Boolean =
Expand All @@ -161,18 +161,18 @@ final class ExplicitResultTypes(
config.skipSimpleDefinitions.isSimpleDefinition(body)

def isImplicit: Boolean =
defn.hasMod(mod"implicit") && !isImplicitly(body)
mods.exists(_.is[Mod.Implicit]) && !isImplicitly(body)

def hasParentWihTemplate: Boolean =
defn.parent.exists(_.is[Template])

def isLocal: Boolean =
if (config.skipLocalImplicits) nm.symbol.isLocal
if (config.skipLocalImplicits) nm.symbol.isLocal && !hasParentWihTemplate
else false

isImplicit && !isFinalLiteralVal && !isLocal || {
hasParentWihTemplate &&
!defn.hasMod(mod"implicit") &&
!mods.exists(_.is[Mod.Implicit]) &&
!matchesSimpleDefinition() &&
matchesMemberKind() &&
matchesMemberVisibility()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
rule = ExplicitResultTypes
ExplicitResultTypes.skipLocalImplicits = false
*/
package test.explicitResultTypes

class LocalImplicits {
trait T
def f(): T = new T {
implicit val i = 1
}
def g(): Unit = {
class C {
implicit val i = 2
}
}
def h(): Unit = {
implicit val i = 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
rule = ExplicitResultTypes
*/
package test.explicitResultTypes

class SkipLocalImplicits {
trait T
def f(): T = new T {
implicit val i = 1
}
def g(): Unit = {
class C {
implicit val i = 2
}
}
def h(): Unit = {
implicit val i = 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test.explicitResultTypes

class LocalImplicits {
trait T
def f(): T = new T {
implicit val i: Int = 1
}
def g(): Unit = {
class C {
implicit val i: Int = 2
}
}
def h(): Unit = {
implicit val i: Int = 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test.explicitResultTypes

class SkipLocalImplicits {
trait T
def f(): T = new T {
implicit val i: Int = 1
}
def g(): Unit = {
class C {
implicit val i: Int = 2
}
}
def h(): Unit = {
implicit val i = 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object ExplicitResultTypesRefinement {
}
trait Chars { def chars: CharSequence }
val chars: Chars = new Chars {
val chars = 42.toString()
val chars: String = 42.toString()
}
def app(): Unit = {
println(field.results)
Expand Down
2 changes: 1 addition & 1 deletion scalafix-tests/output/src/main/scala/tests/RscCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ object RscCompat_Test {
val more2: more2 = new more2
class more2 extends { def foo(implicit x: Int, y: Int) = 42 }
val more3: more3 = new more3
class more3 extends { implicit def bar = 42 }
class more3 extends { implicit def bar: Int = 42 }

implicit val crazy1 = implicitly[Int]
implicit val crazy2: Bijection[In,Out1] = Bijection.connect[In, Out1]
Expand Down

0 comments on commit 1c55726

Please sign in to comment.