Skip to content

Commit

Permalink
Check scope type
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Oct 2, 2024
1 parent 7d94be9 commit daeeb4b
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
// ========== MiniPhase Transform ==========

override def transformBlock(tree: tpd.Block)(using Context): tpd.Tree =
popOutBlockTemplatePackageDef()
popOutBlockTemplatePackageDef(tree)
tree

override def transformTemplate(tree: tpd.Template)(using Context): tpd.Tree =
popOutBlockTemplatePackageDef()
popOutBlockTemplatePackageDef(tree)
tree

override def transformPackageDef(tree: tpd.PackageDef)(using Context): tpd.Tree =
popOutBlockTemplatePackageDef()
popOutBlockTemplatePackageDef(tree)
tree

override def transformValDef(tree: tpd.ValDef)(using Context): tpd.Tree =
Expand All @@ -189,9 +189,9 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
preparing:
ud.pushScope(UnusedData.ScopeType.fromTree(tree))

private def popOutBlockTemplatePackageDef()(using Context): Context =
private def popOutBlockTemplatePackageDef(tree: tpd.Block | tpd.Template | tpd.PackageDef)(using Context): Context =
preparing:
ud.popScope()
ud.popScope(UnusedData.ScopeType.fromTree(tree))

/**
* This traverse is the **main** component of this phase
Expand All @@ -200,8 +200,6 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
* corresponding context property
*/
private def traverser = new TreeTraverser:
import tpd.*
import UnusedData.ScopeType

// Register every import, definition and usage
override def traverse(tree: tpd.Tree)(using Context): Unit =
Expand All @@ -214,17 +212,17 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
case untpd.TypedSplice(tree1) => tree1
}.foreach(traverse(_)(using newCtx))
traverseChildren(tree)(using newCtx)
case ident: Ident =>
case ident: tpd.Ident =>
prepareForIdent(ident)
traverseChildren(tree)(using newCtx)
case sel: Select =>
case sel: tpd.Select =>
prepareForSelect(sel)
traverseChildren(tree)(using newCtx)
case tree: (tpd.Block | tpd.Template | tpd.PackageDef) =>
//! DIFFERS FROM MINIPHASE
pushInBlockTemplatePackageDef(tree)
traverseChildren(tree)(using newCtx)
popOutBlockTemplatePackageDef()
popOutBlockTemplatePackageDef(tree)
case t: tpd.ValDef =>
prepareForValDef(t)
traverseChildren(tree)(using newCtx)
Expand Down Expand Up @@ -477,8 +475,8 @@ object CheckUnused:
*
* - If there are imports in this scope check for unused ones
*/
def popScope()(using Context): Unit =
currScopeType.pop()
def popScope(scopeType: ScopeType)(using Context): Unit =
assert(currScopeType.pop() == scopeType)
val usedInfos = usedInScope.pop()
val selDatas = impInScope.pop()

Expand All @@ -501,7 +499,7 @@ object CheckUnused:
/** Leave the scope and return a result set of warnings.
*/
def getUnused(using Context): UnusedResult =
popScope()
popScope(ScopeType.Other) // sentinel

def isUsedInPosition(name: Name, span: Span): Boolean =
usedInPosition.get(name) match
Expand Down

0 comments on commit daeeb4b

Please sign in to comment.