Skip to content

Commit

Permalink
Add helpers ignoredType and ignoredSymbol
Browse files Browse the repository at this point in the history
These helpers are only used in `Dependency` for now, but I feel that
they could become handy in other Zinc scalac phases and remove the
overplaty way of guarding against inexistent or useless symbols and
types.
  • Loading branch information
jvican committed Feb 6, 2017
1 parent 511ad6c commit c53439a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
12 changes: 3 additions & 9 deletions internal/compiler-bridge/src/main/scala/xsbt/Dependency.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,10 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
}
}

@inline
def ignoreType(tpe: Type) =
tpe == null ||
tpe == NoType ||
tpe.typeSymbol == EmptyPackageClass

private def addTreeDependency(tree: Tree): Unit = {
addDependency(tree.symbol)
val tpe = tree.tpe
if (!ignoreType(tpe))
if (!ignoredType(tpe))
foreachSymbolInType(tpe)(addDependency)
()
}
Expand Down Expand Up @@ -276,12 +270,12 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
traverseTrees(body)

// In some cases (eg. macro annotations), `typeTree.tpe` may be null. See sbt/sbt#1593 and sbt/sbt#1655.
case typeTree: TypeTree if !ignoreType(typeTree.tpe) =>
case typeTree: TypeTree if !ignoredType(typeTree.tpe) =>
symbolsInType(typeTree.tpe) foreach addDependency
case m @ MacroExpansionOf(original) if inspectedOriginalTrees.add(original) =>
traverse(original)
super.traverse(m)
case _: ClassDef | _: ModuleDef if tree.symbol != null && tree.symbol != NoSymbol =>
case _: ClassDef | _: ModuleDef if !ignoredSymbol(tree.symbol) =>
// make sure we cache lookups for all classes declared in the compilation unit; the recorded information
// will be used in Analyzer phase
val sym = if (tree.symbol.isModule) tree.symbol.moduleClass else tree.symbol
Expand Down
17 changes: 16 additions & 1 deletion internal/compiler-bridge/src/main/scala/xsbt/GlobalHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@ trait GlobalHelpers {
val global: Global
import global._

/** Return true if type shall be ignored, false otherwise. */
@inline def ignoredType(tpe: Type) = {
tpe == null ||
tpe == NoType ||
tpe.typeSymbol == EmptyPackageClass
}

/** Return true if symbol shall be ignored, false otherwise. */
@inline def ignoredSymbol(symbol: Symbol) = {
symbol == null ||
symbol == NoSymbol ||
symbol == EmptyPackageClass
}

def symbolsInType(tp: Type): Set[Symbol] = {
val typeSymbolCollector =
new CollectTypeCollector({
case tpe if (tpe != null) && !tpe.typeSymbolDirect.hasPackageFlag => tpe.typeSymbolDirect
case tpe if !ignoredType(tpe) && !tpe.typeSymbolDirect.hasPackageFlag =>
tpe.typeSymbolDirect
})

typeSymbolCollector.collect(tp).toSet
Expand Down

0 comments on commit c53439a

Please sign in to comment.