Skip to content

Fix #3917: Properly desugar Ident #3925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions compiler/sjs/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -850,21 +850,6 @@ class JSCodeGen()(implicit ctx: Context) {
}
} // end of genStatOrExpr()

// !!! DUPLICATE code with DottyBackendInterface
private def desugarIdent(i: Ident): Option[Select] = {
i.tpe match {
case TermRef(prefix: TermRef, name) =>
Some(tpd.ref(prefix).select(i.symbol))
case TermRef(prefix: ThisType, name) =>
Some(tpd.This(prefix.cls).select(i.symbol))
/*case TermRef(NoPrefix, name) =>
if (i.symbol is Method) Some(This(i.symbol.topLevelClass).select(i.symbol)) // workaround #342 todo: remove after fixed
else None*/
case _ =>
None
}
}

private def qualifierOf(fun: Tree): Tree = fun match {
case fun: Ident =>
fun.tpe match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
def desugarIdent(i: Ident): Option[tpd.Select] = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not get rid of the Option here since the backend expects this signature

var found = desugared.get(i.tpe)
if (found == null) {
i.tpe match {
case TermRef(prefix: TermRef, _) =>
found = tpd.ref(prefix).select(i.symbol)
case TermRef(prefix: ThisType, _) =>
found = tpd.This(prefix.cls).select(i.symbol)
case TermRef(NoPrefix, _) =>
if (i.symbol is Flags.Method) found = This(i.symbol.topLevelClass).select(i.symbol) // workaround #342 todo: remove after fixed
case _ =>
}
found = tpd.desugarIdent(i).orNull
if (found != null) desugared.put(i.tpe, found)
}
if (found == null) None else Some(found)
Expand Down
29 changes: 22 additions & 7 deletions compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -826,14 +826,13 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
*/
def becomes(rhs: Tree)(implicit ctx: Context): Tree =
if (tree.symbol is Method) {
val setr = tree match {
case Ident(_) =>
val setter = tree.symbol.setter
assert(setter.exists, tree.symbol.showLocated)
ref(tree.symbol.setter)
case Select(qual, _) => qual.select(tree.symbol.setter)
val setter = tree.symbol.setter
assert(setter.exists, tree.symbol.showLocated)
val qual = tree match {
case id: Ident => desugarIdentPrefix(id)
case Select(qual, _) => qual
}
setr.appliedTo(rhs)
qual.select(setter).appliedTo(rhs)
}
else Assign(tree, rhs)

Expand Down Expand Up @@ -1012,5 +1011,21 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
val encoding = ctx.settings.encoding.value
if (file != null && file.exists) new SourceFile(file, Codec(encoding)) else NoSource
}

/** Desugar identifier into a select node. Return None if not possible */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't usually use Option in the compiler, first, because of the allocation overhead, and second, because we usually have sentinel values that signal absence of a proper result. E.g. EmptyTree for trees. So I would just let desugarIdent return an EmptyTree if no rules apply. That's more idiomatic. Hint: orElse works on empty trees.

I see there are quite a lot of uses of desugarIdent, and have the impression that most of these uses could be improved by going to EmptyTree. The use of Option.fold in JSCodeGen is particularly opaque.

def desugarIdent(tree: Ident)(implicit ctx: Context): Option[Select] = {
val qual = desugarIdentPrefix(tree)
if (qual.isEmpty) None
else Some(qual.select(tree.symbol))
}

private def desugarIdentPrefix(tree: Ident)(implicit ctx: Context): Tree = tree.tpe match {
case TermRef(prefix: TermRef, _) =>
ref(prefix)
case TermRef(prefix: ThisType, _) =>
This(prefix.cls)
case _ =>
EmptyTree
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import core.Symbols._
import core.Types._
import typer.ConstFold
import ast.Trees._
import Simplify.desugarIdent

/** Various constant folding.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import core.Flags._
import core.TypeApplications.noBounds
import ast.Trees._
import transform.SymUtils._
import Simplify.desugarIdent
import dotty.tools.dotc.ast.tpd

/** Inline case class specific methods using desugarings assumptions.
Expand Down
10 changes: 0 additions & 10 deletions compiler/src/dotty/tools/dotc/transform/localopt/Simplify.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,6 @@ class Simplify extends MiniPhase with IdentityDenotTransformer {

object Simplify {
import tpd._
// TODO: This function is duplicated in jvm/DottyBackendInterface.scala, let's factor these out!
def desugarIdent(i: Ident)(implicit ctx: Context): Option[Select] = {
i.tpe match {
case TermRef(prefix: TermRef, _) =>
Some(ref(prefix).select(i.symbol))
case TermRef(prefix: ThisType, _) =>
Some(This(prefix.cls).select(i.symbol))
case _ => None
}
}

/** Is this tree mutable, or java.lang.System.{in, out, err}? These three
* System members are the only static final fields that are mutable.
Expand Down
27 changes: 27 additions & 0 deletions tests/pos/i3917.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class A {
var a = false
}

object B {
var b = false
}

class C {
var c = false
}

object C extends A {
def test = {
a = true
C.a = true
this.a = true
C.this.a = true

import B._
b = true

val c0 = new C
import c0._
c = true
}
}