Skip to content

Commit

Permalink
enable imports for path-dependent types
Browse files Browse the repository at this point in the history
Remove related TODO as we do want to favor `NestedObj.T4` reference
rather than `T4` when the parent is an object, or `Obj.NestedObj.T4`
if the grand parent is also an object.

For example, we want Term.Name (with import scala.meta.Term) rather
than Name (with import scala.meta.Term.Name).
  • Loading branch information
github-brice-jaglin committed Nov 9, 2020
1 parent 5b2eb94 commit 51a14aa
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,21 @@ class ScalafixGlobal(
// "scala.Seq[T]" even when it's needed.
loop(ThisType(sym.owner), name)
} else if (sym.hasPackageFlag || sym.isPackageObjectOrClass) {
if (history.tryShortenName(name)) NoPrefix
val dotSyntaxFriendlyName = name.map { name0 =>
if (name0.symbol.isStatic) name0
else {
// Use the prefix rather than the real owner to maximize the
// chances of shortening the reference: when `name` is directly
// nested in a non-statically addressable type (class or trait),
// its original owner is that type (requiring a type projection
// to reference it) while the prefix is its concrete owner value
// (for which the dot syntax works).
// https://docs.scala-lang.org/tour/inner-classes.html
// https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-13-path-dependent-types/
ShortName(name0.symbol.cloneSymbol(sym))
}
}
if (history.tryShortenName(dotSyntaxFriendlyName)) NoPrefix
else tpe
} else {
if (history.isSymbolInScope(sym, pre)) SingleType(NoPrefix, sym)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ class Clazz {
package object PackageObject extends Trait

package pkg {
object Obj extends Clazz
abstract class AbstractClazz {
trait T4
}
object Obj extends Clazz {
object NestedObj extends AbstractClazz
}
}

object ExplicitResultTypesPathDependent {
Expand All @@ -46,4 +51,7 @@ object ExplicitResultTypesPathDependent {

def t3: pkg.Obj.T3 = ???
val t3Ref = t3

def t4: pkg.Obj.NestedObj.T4 = ???
val t4Ref = t4
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ object ExplicitResultTypesImports {

val timezone = null.asInstanceOf[java.util.TimeZone]

// TODO: Is this desirable behavior?
val inner = null.asInstanceOf[scala.collection.Searching.SearchResult]

final val javaEnum = java.util.Locale.Category.DISPLAY
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package test.explicitResultTypes

import test.explicitResultTypes.PackageObject.{ Nested, T1 }
import test.explicitResultTypes.pkg.Obj
// like https://github.com/tpolecat/doobie/blob/c2e044/modules/core/src/main/scala/doobie/free/Aliases.scala#L10
trait Trait {
Expand All @@ -18,7 +19,12 @@ class Clazz {
package object PackageObject extends Trait

package pkg {
object Obj extends Clazz
abstract class AbstractClazz {
trait T4
}
object Obj extends Clazz {
object NestedObj extends AbstractClazz
}
}

object ExplicitResultTypesPathDependent {
Expand All @@ -37,11 +43,14 @@ object ExplicitResultTypesPathDependent {

// like https://github.com/tpolecat/doobie/blob/c2e0445/modules/core/src/main/scala/doobie/util/query.scala#L163
def t1: PackageObject.T1 = ???
val t1Ref: test.explicitResultTypes.PackageObject.T1 = t1
val t1Ref: T1 = t1

def t2: PackageObject.Nested.T2 = ???
val t2Ref: test.explicitResultTypes.PackageObject.Nested.T2 = t2
val t2Ref: Nested.T2 = t2

def t3: pkg.Obj.T3 = ???
val t3Ref: Obj.T3 = t3

def t4: pkg.Obj.NestedObj.T4 = ???
val t4Ref: Obj.NestedObj.T4 = t4
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ object ExplicitResultTypesImports {

val timezone: ju.TimeZone = null.asInstanceOf[java.util.TimeZone]

// TODO: Is this desirable behavior?
val inner: Searching.SearchResult = null.asInstanceOf[scala.collection.Searching.SearchResult]

final val javaEnum: Category = java.util.Locale.Category.DISPLAY
Expand Down

0 comments on commit 51a14aa

Please sign in to comment.