Skip to content

Commit 2d71030

Browse files
committed
Make desugarIdent return a Tree
1 parent 469fefe commit 2d71030

File tree

8 files changed

+32
-36
lines changed

8 files changed

+32
-36
lines changed

compiler/sjs/backend/sjs/JSCodeGen.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ class JSCodeGen()(implicit ctx: Context) {
793793
throw new FatalError(s"Assignment to static member ${sym.fullName} not supported")
794794
val genRhs = genExpr(rhs)
795795
val lhs = lhs0 match {
796-
case lhs: Ident => desugarIdent(lhs).getOrElse(lhs)
796+
case lhs: Ident => desugarIdent(lhs)
797797
case lhs => lhs
798798
}
799799
lhs match {
@@ -892,7 +892,7 @@ class JSCodeGen()(implicit ctx: Context) {
892892
val sym = tree.fun.symbol
893893

894894
val fun = tree.fun match {
895-
case fun: Ident => desugarIdent(fun).getOrElse(fun)
895+
case fun: Ident => desugarIdent(fun)
896896
case fun => fun
897897
}
898898

@@ -1556,7 +1556,7 @@ class JSCodeGen()(implicit ctx: Context) {
15561556
implicit val pos = tree.pos
15571557

15581558
val fun = tree.fun match {
1559-
case fun: Ident => desugarIdent(fun).get
1559+
case fun: Ident => desugarIdent(fun).asInstanceOf[Select]
15601560
case fun: Select => fun
15611561
}
15621562
val receiver = fun.qualifier

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,12 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
444444
def desugarIdent(i: Ident): Option[tpd.Select] = {
445445
var found = desugared.get(i.tpe)
446446
if (found == null) {
447-
found = tpd.desugarIdent(i).orNull
448-
if (found != null) desugared.put(i.tpe, found)
447+
tpd.desugarIdent(i) match {
448+
case sel: tpd.Select =>
449+
desugared.put(i.tpe, sel)
450+
found = sel
451+
case _ =>
452+
}
449453
}
450454
if (found == null) None else Some(found)
451455
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,14 +1012,15 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
10121012
if (file != null && file.exists) new SourceFile(file, Codec(encoding)) else NoSource
10131013
}
10141014

1015-
/** Desugar identifier into a select node. Return None if not possible */
1016-
def desugarIdent(tree: Ident)(implicit ctx: Context): Option[Select] = {
1015+
/** Desugar identifier into a select node. Return the tree itself if not possible */
1016+
def desugarIdent(tree: Ident)(implicit ctx: Context): Tree = {
10171017
val qual = desugarIdentPrefix(tree)
1018-
if (qual.isEmpty) None
1019-
else Some(qual.select(tree.symbol))
1018+
if (qual.isEmpty) tree
1019+
else qual.select(tree.symbol)
10201020
}
10211021

1022-
private def desugarIdentPrefix(tree: Ident)(implicit ctx: Context): Tree = tree.tpe match {
1022+
/** Recover identifier prefix (e.g. this) if it exists */
1023+
def desugarIdentPrefix(tree: Ident)(implicit ctx: Context): Tree = tree.tpe match {
10231024
case TermRef(prefix: TermRef, _) =>
10241025
ref(prefix)
10251026
case TermRef(prefix: ThisType, _) =>

compiler/src/dotty/tools/dotc/transform/localopt/ConstantFold.scala

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,19 @@ import ast.Trees._
181181
case _ => false
182182
}
183183
case t1: Ident =>
184-
desugarIdent(t1) match {
185-
case Some(t) =>
186-
val t2i = t2 match {
187-
case t2: Ident => desugarIdent(t2).getOrElse(t2)
188-
case _ => t2
189-
}
190-
isSimilar(t, t2i)
191-
case None => t1.symbol eq t2.symbol
184+
t2 match {
185+
case t2: Ident =>
186+
t1.symbol eq t2.symbol
187+
case _ => // Select
188+
isSimilar(t2, t1)
192189
}
193190
case t1: Select => t2 match {
194191
case t2: Select =>
195192
(t1.symbol eq t2.symbol) &&
196193
isSimilar(t1.qualifier, t2.qualifier)
197194
case t2: Ident => desugarIdent(t2) match {
198-
case Some(t2) => isSimilar(t1, t2)
199-
case None => false
195+
case t2: Select => isSimilar(t1, t2)
196+
case _ => false
200197
}
201198
case _ => false
202199
}

compiler/src/dotty/tools/dotc/transform/localopt/Devalify.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Devalify extends Optimisation {
194194
readingOnlyVals(qual)
195195

196196
case t: Ident if !t.symbol.is(Mutable | Method) && !t.symbol.info.dealias.isInstanceOf[ExprType] =>
197-
desugarIdent(t).forall(readingOnlyVals)
197+
desugarIdent(t) match { case s: Select => readingOnlyVals(s); case _ => true }
198198

199199
case t: This => true
200200
// null => false, or the following fails devalify:

compiler/src/dotty/tools/dotc/transform/localopt/DropNoEffects.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,9 @@ class DropNoEffects(val simplifyPhase: Simplify) extends Optimisation {
100100
cpy.Block(bl)(stats2, expr2)
101101

102102
case t: Ident if !t.symbol.is(Method | Lazy) && !t.symbol.info.isInstanceOf[ExprType] || effectsDontEscape(t) =>
103-
desugarIdent(t) match {
104-
case Some(t) if !(t.qualifier.symbol.is(JavaDefined) && t.qualifier.symbol.is(Package)) => t
105-
case _ => EmptyTree
106-
}
103+
val prefix = desugarIdentPrefix(t)
104+
if (!prefix.isEmpty && !prefix.symbol.is(allOf(JavaDefined, Package))) t
105+
else EmptyTree
107106

108107
case app: Apply if app.fun.symbol.is(Label) && !app.tpe.finalResultType.derivesFrom(defn.UnitClass) =>
109108
// This is "the scary hack". It changes the return type to Unit, then

compiler/src/dotty/tools/dotc/transform/localopt/InlineCaseIntrinsics.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ class InlineCaseIntrinsics(val simplifyPhase: Simplify) extends Optimisation {
108108
def receiver(t: Tree): Type = t match {
109109
case t: Apply => receiver(t.fun)
110110
case t: TypeApply => receiver(t.fun)
111-
case t: Ident => desugarIdent(t) match {
112-
case Some(t) => receiver(t)
113-
case _ => NoType
114-
}
111+
case t: Ident =>
112+
val prefix = desugarIdentPrefix(t)
113+
prefix.tpe.widenDealias
115114
case t: Select => t.qualifier.tpe.widenDealias
116115
}
117116

compiler/src/dotty/tools/dotc/transform/localopt/Simplify.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,10 @@ object Simplify {
154154
* System members are the only static final fields that are mutable.
155155
* See https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.5.4
156156
*/
157-
@tailrec def isEffectivelyMutable(t: Tree)(implicit ctx: Context): Boolean = t match {
157+
def isEffectivelyMutable(t: Tree)(implicit ctx: Context): Boolean = t match {
158158
case _ if t.symbol.is(Mutable) => true
159-
case s: Select => s.symbol.owner == defn.SystemModule
160-
case i: Ident =>
161-
desugarIdent(i) match {
162-
case Some(ident) => isEffectivelyMutable(ident)
163-
case None => false
164-
}
159+
case _: Select | _: Ident =>
160+
t.symbol.owner == defn.SystemModule
165161
case _ => false
166162
}
167163

0 commit comments

Comments
 (0)