Skip to content

Commit 9747f6b

Browse files
committed
Centralize methods for local context creation
Collect them all in NamerContextOps. Previously some were also in Typer and in Context itself.
1 parent 7d86966 commit 9747f6b

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ trait NamerContextOps { this: Context =>
9292
.dropWhile(_.owner == sym)
9393
.next()
9494

95+
/** A fresh local context with given tree and owner.
96+
* Owner might not exist (can happen for self valdefs), in which case
97+
* no owner is set in result context
98+
*/
99+
def localContext(tree: untpd.Tree, owner: Symbol): FreshContext = {
100+
val freshCtx = fresh.setTree(tree)
101+
if (owner.exists) freshCtx.setOwner(owner) else freshCtx
102+
}
103+
104+
/** A new context for the interior of a class */
105+
def inClassContext(selfInfo: DotClass /* Should be Type | Symbol*/): Context = {
106+
val localCtx: Context = ctx.fresh.setNewScope
107+
selfInfo match {
108+
case sym: Symbol if sym.exists && sym.name != nme.WILDCARD => localCtx.scope.openForMutations.enter(sym)
109+
case _ =>
110+
}
111+
localCtx
112+
}
113+
114+
def packageContext(tree: untpd.PackageDef, pkg: Symbol): Context =
115+
if (pkg is Package) ctx.fresh.setOwner(pkg.moduleClass).setTree(tree)
116+
else ctx
117+
95118
/** The given type, unless `sym` is a constructor, in which case the
96119
* type of the constructed instance is returned
97120
*/
@@ -405,17 +428,6 @@ class Namer { typer: Typer =>
405428
case _ => tree
406429
}
407430

408-
/** A new context for the interior of a class */
409-
def inClassContext(selfInfo: DotClass /* Should be Type | Symbol*/)(implicit ctx: Context): Context = {
410-
val localCtx: Context = ctx.fresh.setNewScope
411-
selfInfo match {
412-
case sym: Symbol if sym.exists && sym.name != nme.WILDCARD =>
413-
localCtx.scope.openForMutations.enter(sym)
414-
case _ =>
415-
}
416-
localCtx
417-
}
418-
419431
/** For all class definitions `stat` in `xstats`: If the companion class if
420432
* not also defined in `xstats`, invalidate it by setting its info to
421433
* NoType.
@@ -953,7 +965,7 @@ class Namer { typer: Typer =>
953965
index(constr)
954966
annotate(constr :: params)
955967

956-
indexAndAnnotate(rest)(inClassContext(selfInfo))
968+
indexAndAnnotate(rest)(ctx.inClassContext(selfInfo))
957969
symbolOfTree(constr).ensureCompleted()
958970

959971
val parentTypes = ensureFirstIsClass(parents.map(checkedParentType(_)), cls.pos)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,12 +1496,12 @@ class Typer extends Namer
14961496
cdef.withType(UnspecifiedErrorType)
14971497
} else {
14981498
val dummy = localDummy(cls, impl)
1499-
val body1 = typedStats(impl.body, dummy)(inClassContext(self1.symbol))
1499+
val body1 = typedStats(impl.body, dummy)(ctx.inClassContext(self1.symbol))
15001500
if (!ctx.isAfterTyper)
15011501
cls.setNoInitsFlags((NoInitsInterface /: body1) ((fs, stat) => fs & defKind(stat)))
15021502

15031503
// Expand comments and type usecases
1504-
cookComments(body1.map(_.symbol), self1.symbol)(localContext(cdef, cls).setNewScope)
1504+
cookComments(body1.map(_.symbol), self1.symbol)(ctx.localContext(cdef, cls).setNewScope)
15051505

15061506
checkNoDoubleDefs(cls)
15071507
val impl1 = cpy.Template(impl)(constr1, parents1, self1, body1)
@@ -1604,15 +1604,12 @@ class Typer extends Namer
16041604
// Package will not exist if a duplicate type has already been entered, see
16051605
// `tests/neg/1708.scala`, else branch's error message should be supressed
16061606
if (pkg.exists) {
1607-
val packageContext =
1608-
if (pkg is Package) ctx.fresh.setOwner(pkg.moduleClass).setTree(tree)
1609-
else {
1610-
ctx.error(PackageNameAlreadyDefined(pkg), tree.pos)
1611-
ctx
1612-
}
1613-
val stats1 = typedStats(tree.stats, pkg.moduleClass)(packageContext)
1607+
if (!pkg.is(Package)) ctx.error(PackageNameAlreadyDefined(pkg), tree.pos)
1608+
val packageCtx = ctx.packageContext(tree, pkg)
1609+
val stats1 = typedStats(tree.stats, pkg.moduleClass)(packageCtx)
16141610
cpy.PackageDef(tree)(pid1.asInstanceOf[RefTree], stats1) withType pkg.termRef
1615-
} else errorTree(tree, i"package ${tree.pid.name} does not exist")
1611+
}
1612+
else errorTree(tree, i"package ${tree.pid.name} does not exist")
16161613
}
16171614

16181615
def typedAnnotated(tree: untpd.Annotated, pt: Type)(implicit ctx: Context): Tree = track("typedAnnotated") {
@@ -1708,15 +1705,6 @@ class Typer extends Namer
17081705
NoSymbol
17091706
}
17101707

1711-
/** A fresh local context with given tree and owner.
1712-
* Owner might not exist (can happen for self valdefs), in which case
1713-
* no owner is set in result context
1714-
*/
1715-
protected def localContext(tree: untpd.Tree, owner: Symbol)(implicit ctx: Context): FreshContext = {
1716-
val freshCtx = ctx.fresh.setTree(tree)
1717-
if (owner.exists) freshCtx.setOwner(owner) else freshCtx
1718-
}
1719-
17201708
protected def localTyper(sym: Symbol): Typer = nestedTyper.remove(sym).get
17211709

17221710
def typedUnadapted(initTree: untpd.Tree, pt: Type = WildcardType)(implicit ctx: Context): Tree = {
@@ -1734,15 +1722,15 @@ class Typer extends Namer
17341722
case tree: untpd.Bind => typedBind(tree, pt)
17351723
case tree: untpd.ValDef =>
17361724
if (tree.isEmpty) tpd.EmptyValDef
1737-
else typedValDef(tree, sym)(localContext(tree, sym).setNewScope)
1725+
else typedValDef(tree, sym)(ctx.localContext(tree, sym).setNewScope)
17381726
case tree: untpd.DefDef =>
17391727
val typer1 = localTyper(sym)
1740-
typer1.typedDefDef(tree, sym)(localContext(tree, sym).setTyper(typer1))
1728+
typer1.typedDefDef(tree, sym)(ctx.localContext(tree, sym).setTyper(typer1))
17411729
case tree: untpd.TypeDef =>
17421730
if (tree.isClassDef)
1743-
typedClassDef(tree, sym.asClass)(localContext(tree, sym).setMode(ctx.mode &~ Mode.InSuperCall))
1731+
typedClassDef(tree, sym.asClass)(ctx.localContext(tree, sym).setMode(ctx.mode &~ Mode.InSuperCall))
17441732
else
1745-
typedTypeDef(tree, sym)(localContext(tree, sym).setNewScope)
1733+
typedTypeDef(tree, sym)(ctx.localContext(tree, sym).setNewScope)
17461734
case _ => typedUnadapted(desugar(tree), pt)
17471735
}
17481736
}
@@ -1776,7 +1764,7 @@ class Typer extends Namer
17761764
case tree: untpd.OrTypeTree => typedOrTypeTree(tree)
17771765
case tree: untpd.RefinedTypeTree => typedRefinedTypeTree(tree)
17781766
case tree: untpd.AppliedTypeTree => typedAppliedTypeTree(tree)
1779-
case tree: untpd.LambdaTypeTree => typedLambdaTypeTree(tree)(localContext(tree, NoSymbol).setNewScope)
1767+
case tree: untpd.LambdaTypeTree => typedLambdaTypeTree(tree)(ctx.localContext(tree, NoSymbol).setNewScope)
17801768
case tree: untpd.ByNameTypeTree => typedByNameTypeTree(tree)
17811769
case tree: untpd.TypeBoundsTree => typedTypeBoundsTree(tree, pt)
17821770
case tree: untpd.Alternative => typedAlternative(tree, pt)

0 commit comments

Comments
 (0)