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

Fix #1180 - ExplicitResultTypes for implicit members of local traits #1181

Merged
merged 4 commits into from
Jul 2, 2020
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 @@ -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