Skip to content

Commit 9f43ede

Browse files
Merge pull request #9119 from dotty-staging/fix-implicit-scope-2
Fix #9103: Fix implicit scopes
2 parents 8554cfb + e124d19 commit 9f43ede

File tree

16 files changed

+311
-190
lines changed

16 files changed

+311
-190
lines changed

compiler/src/dotty/tools/dotc/ast/Positioned.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import core.Contexts.Context
88
import core.Decorators._
99
import core.Flags.{JavaDefined, Extension}
1010
import core.StdNames.nme
11+
import ast.Trees.mods
1112
import annotation.constructorOnly
1213
import annotation.internal.sharable
1314
import reporting.Reporter

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ object Trees {
333333
def namedType: NamedType = tpe.asInstanceOf[NamedType]
334334
}
335335

336+
def (mdef: untpd.DefTree).mods: untpd.Modifiers = mdef.rawMods
337+
336338
abstract class NamedDefTree[-T >: Untyped](implicit @constructorOnly src: SourceFile) extends NameTree[T] with DefTree[T] {
337339
type ThisTree[-T >: Untyped] <: NamedDefTree[T]
338340

@@ -1538,6 +1540,7 @@ object Trees {
15381540
receiver: tpd.Tree, method: TermName, args: List[Tree], targs: List[Type],
15391541
expectedType: Type)(using parentCtx: Context): tpd.Tree = {
15401542
given ctx as Context = parentCtx.retractMode(Mode.ImplicitsEnabled)
1543+
import dotty.tools.dotc.ast.tpd.TreeOps
15411544

15421545
val typer = ctx.typer
15431546
val proto = FunProto(args, expectedType)

compiler/src/dotty/tools/dotc/ast/untpd.scala

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
510510
/** A repeated argument such as `arg: _*` */
511511
def repeated(arg: Tree)(implicit ctx: Context): Typed = Typed(arg, Ident(tpnme.WILDCARD_STAR))
512512

513-
// ----- Accessing modifiers ----------------------------------------------------
514-
515-
abstract class ModsDecorator { def mods: Modifiers }
516-
517-
implicit class modsDeco(val mdef: DefTree)(implicit ctx: Context) {
518-
def mods: Modifiers = mdef.rawMods
519-
}
520513

521514
// --------- Copier/Transformer/Accumulator classes for untyped trees -----
522515

compiler/src/dotty/tools/dotc/core/TypeApplications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ class TypeApplications(val self: Type) extends AnyVal {
337337
case dealiased: TypeBounds =>
338338
dealiased.derivedTypeBounds(dealiased.lo.appliedTo(args), dealiased.hi.appliedTo(args))
339339
case dealiased: LazyRef =>
340-
LazyRef(c => dealiased.ref(c).appliedTo(args))
340+
LazyRef(c => dealiased.ref(c).appliedTo(args)(using c))
341341
case dealiased: WildcardType =>
342342
WildcardType(dealiased.optBounds.orElse(TypeBounds.empty).appliedTo(args).bounds)
343343
case dealiased: TypeRef if dealiased.symbol == defn.NothingClass =>

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4862,7 +4862,7 @@ object Types {
48624862
}
48634863
}
48644864

4865-
abstract class TypeMap(implicit protected val mapCtx: Context)
4865+
abstract class TypeMap(implicit protected var mapCtx: Context)
48664866
extends VariantTraversal with (Type => Type) { thisMap =>
48674867

48684868
protected def stopAtStatic: Boolean = true
@@ -4979,7 +4979,16 @@ object Types {
49794979
derivedSuperType(tp, this(thistp), this(supertp))
49804980

49814981
case tp: LazyRef =>
4982-
LazyRef(_ => this(tp.ref))
4982+
LazyRef { c =>
4983+
val ref1 = tp.ref(using c)
4984+
if c.runId == mapCtx.runId then this(ref1)
4985+
else // splice in new run into map context
4986+
val saved = mapCtx
4987+
mapCtx = mapCtx.fresh
4988+
.setPeriod(Period(c.runId, mapCtx.phaseId))
4989+
.setRun(c.run)
4990+
try this(ref1) finally mapCtx = saved
4991+
}
49834992

49844993
case tp: ClassInfo =>
49854994
mapClassInfo(tp)
@@ -5453,14 +5462,15 @@ object Types {
54535462
def apply(x: Unit, tp: Type): Unit = foldOver(p(tp), tp)
54545463
}
54555464

5465+
class TypeHashSet extends util.HashSet[Type](64):
5466+
override def hash(x: Type): Int = System.identityHashCode(x)
5467+
override def isEqual(x: Type, y: Type) = x.eq(y)
5468+
54565469
class NamedPartsAccumulator(p: NamedType => Boolean, excludeLowerBounds: Boolean = false)
54575470
(implicit ctx: Context) extends TypeAccumulator[mutable.Set[NamedType]] {
54585471
override def stopAtStatic: Boolean = false
54595472
def maybeAdd(x: mutable.Set[NamedType], tp: NamedType): mutable.Set[NamedType] = if (p(tp)) x += tp else x
5460-
val seen: util.HashSet[Type] = new util.HashSet[Type](64) {
5461-
override def hash(x: Type): Int = System.identityHashCode(x)
5462-
override def isEqual(x: Type, y: Type) = x.eq(y)
5463-
}
5473+
val seen = TypeHashSet()
54645474
def apply(x: mutable.Set[NamedType], tp: Type): mutable.Set[NamedType] =
54655475
if (seen contains tp) x
54665476
else {

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
277277
}
278278

279279
protected def toTextCore[T >: Untyped](tree: Tree[T]): Text = {
280-
import untpd.{modsDeco => _, _}
280+
import untpd._
281281

282282
def isLocalThis(tree: Tree) = tree.typeOpt match {
283283
case tp: ThisType => tp.cls == ctx.owner.enclosingClass
@@ -645,7 +645,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
645645
}
646646

647647
override def toText[T >: Untyped](tree: Tree[T]): Text = controlled {
648-
import untpd.{modsDeco => _, _}
648+
import untpd._
649649

650650
var txt = toTextCore(tree)
651651

@@ -720,11 +720,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
720720
}
721721
}
722722

723-
/** Print modifiers from symbols if tree has type, overriding the untpd behavior. */
724-
private implicit def modsDeco(mdef: untpd.DefTree): untpd.ModsDecorator =
725-
new untpd.ModsDecorator {
726-
def mods = if (mdef.hasType) Modifiers(mdef.symbol) else mdef.rawMods
727-
}
723+
/** Print modifiers from symbols if tree has type, overriding the behavior in Trees. */
724+
def (mdef: untpd.DefTree).mods: untpd.Modifiers =
725+
if mdef.hasType then Modifiers(mdef.symbol) else mdef.rawMods
728726

729727
private def Modifiers(sym: Symbol): Modifiers = untpd.Modifiers(
730728
sym.flags & (if (sym.isType) ModifierFlags | VarianceFlags else ModifierFlags),
@@ -768,7 +766,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
768766
vparamss.foldLeft(leading)((txt, params) => txt ~ paramsText(params))
769767

770768
protected def valDefToText[T >: Untyped](tree: ValDef[T]): Text = {
771-
import untpd.{modsDeco => _}
769+
import untpd._
772770
dclTextOr(tree) {
773771
modText(tree.mods, tree.symbol, keywordStr(if (tree.mods.is(Mutable)) "var" else "val"), isType = false) ~~
774772
valDefText(nameIdText(tree)) ~ optAscription(tree.tpt) ~
@@ -782,7 +780,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
782780
~ toText(params, ", ") ~ ")"
783781

784782
protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
785-
import untpd.{modsDeco => _}
783+
import untpd._
786784
dclTextOr(tree) {
787785
val defKeyword = modText(tree.mods, tree.symbol, keywordStr("def"), isType = false)
788786
val isExtension = tree.hasType && tree.symbol.is(Extension)

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package semanticdb
55
import core._
66
import Phases._
77
import ast.tpd._
8+
import ast.Trees.mods
89
import Contexts._
910
import Symbols._
1011
import Flags._

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,6 @@ trait Checking {
11111111
* 2. Check that case class `enum` cases do not extend java.lang.Enum.
11121112
*/
11131113
def checkEnum(cdef: untpd.TypeDef, cls: Symbol, firstParent: Symbol)(using Context): Unit = {
1114-
import untpd.modsDeco
11151114
def isEnumAnonCls =
11161115
cls.isAnonymousClass &&
11171116
cls.owner.isTerm &&

0 commit comments

Comments
 (0)