Skip to content

Remove PreName add (almost) all other implicit conversions #4077

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

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ object desugar {
@sharable private val synthetic = Modifiers(Synthetic)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this commit should not be merged. There is a reason for rawMods/modsDeco: mods should not be called on a typed tree because there mods is typically outdated - one should consult symbol.flags instead. The setup here makes sure that in code working with typed trees using a

import tpd._

import, mods is not visible, because modsDeco is not in scope. So this is a safety measure, not a convenience implicit.

private def toDefParam(tparam: TypeDef): TypeDef =
tparam.withMods(tparam.rawMods & EmptyFlags | Param)
tparam.withMods(tparam.mods & EmptyFlags | Param)
private def toDefParam(vparam: ValDef): ValDef =
vparam.withMods(vparam.rawMods & (Implicit | Erased) | Param)
vparam.withMods(vparam.mods & (Implicit | Erased) | Param)

/** The expansion of a class definition. See inline comments for what is involved */
def classDef(cdef: TypeDef)(implicit ctx: Context): Tree = {
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ object Trees {

private[this] var myMods: untpd.Modifiers = null

private[dotc] def rawMods: untpd.Modifiers =
def mods: untpd.Modifiers =
if (myMods == null) untpd.EmptyModifiers else myMods

def rawComment: Option[Comment] = getAttachment(DocComment)
Expand Down Expand Up @@ -338,7 +338,7 @@ object Trees {
*/
def namePos =
if (pos.exists)
if (rawMods.is(Synthetic)) Position(pos.point, pos.point)
if (mods.is(Synthetic)) Position(pos.point, pos.point)
else Position(pos.point, pos.point + name.stripModuleClassSuffix.lastPart.length, pos.point)
else pos
}
Expand Down
10 changes: 1 addition & 9 deletions compiler/src/dotty/tools/dotc/ast/untpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
/** A repeated argument such as `arg: _*` */
def repeated(arg: Tree)(implicit ctx: Context) = Typed(arg, Ident(tpnme.WILDCARD_STAR))

// ----- Accessing modifiers ----------------------------------------------------

abstract class ModsDecorator { def mods: Modifiers }

implicit class modsDeco(val mdef: MemberDef)(implicit ctx: Context) {
def mods = mdef.rawMods
}

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

override val cpy: UntypedTreeCopier = new UntypedTreeCopier
Expand All @@ -400,7 +392,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {

def postProcess(tree: Tree, copied: MemberDef): copied.ThisTree[Untyped] = {
tree match {
case tree: MemberDef => copied.withMods(tree.rawMods)
case tree: MemberDef => copied.withMods(tree.mods)
case _ => copied
}
}.asInstanceOf[copied.ThisTree[Untyped]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {

override protected def templateText(tree: TypeDef, impl: Template): Text = {
val decl =
if (!tree.mods.is(Module)) modText(tree.mods, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
else modText(tree.mods &~ (Final | Module), keywordStr("object"))
if (!modifiers(tree).is(Module)) modText(modifiers(tree), keywordStr(if (modifiers(tree) is Trait) "trait" else "class"))
else modText(modifiers(tree) &~ (Final | Module), keywordStr("object"))
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ ""
}

override protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
import untpd.{modsDeco => _, _}
dclTextOr(tree) {
val printLambda = tree.symbol.isAnonymousFunction
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree)) provided (!printLambda)
val prefix = modText(modifiers(tree), keywordStr("def")) ~~ valDefText(nameIdText(tree)) provided (!printLambda)
withEnclosingDef(tree) {
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt).provided(!printLambda) ~
optText(tree.rhs)((if (printLambda) " => " else " = ") ~ _)
Expand Down
39 changes: 20 additions & 19 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
("{" ~ toText(trees, "\n") ~ "}").close

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

def isLocalThis(tree: Tree) = tree.typeOpt match {
case tp: ThisType => tp.cls == ctx.owner.enclosingClass
Expand Down Expand Up @@ -403,7 +403,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
case tree @ TypeDef(name, rhs) =>
def typeDefText(tparamsText: => Text, rhsText: => Text) =
dclTextOr(tree) {
modText(tree.mods, keywordStr("type")) ~~ (varianceText(tree.mods) ~ typeText(nameIdText(tree))) ~
modText(modifiers(tree), keywordStr("type")) ~~ (varianceText(modifiers(tree)) ~ typeText(nameIdText(tree))) ~
withEnclosingDef(tree) { tparamsText ~ rhsText }
}
def recur(rhs: Tree, tparamsTxt: => Text): Text = rhs match {
Expand Down Expand Up @@ -441,7 +441,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
toText(t)
case tree @ ModuleDef(name, impl) =>
withEnclosingDef(tree) {
modText(tree.mods, keywordStr("object")) ~~ nameIdText(tree) ~ toTextTemplate(impl)
modText(modifiers(tree), keywordStr("object")) ~~ nameIdText(tree) ~ toTextTemplate(impl)
}
case SymbolLit(str) =>
"'" + str
Expand All @@ -457,7 +457,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
def argToText(arg: Tree) = arg match {
case arg @ ValDef(name, tpt, _) =>
val implicitText =
if ((arg.mods is Implicit) && !implicitSeen) { implicitSeen = true; keywordStr("implicit ") }
if ((modifiers(arg) is Implicit) && !implicitSeen) { implicitSeen = true; keywordStr("implicit ") }
else ""
implicitText ~ toText(name) ~ optAscription(tpt)
case _ =>
Expand Down Expand Up @@ -560,15 +560,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
}

/** Print modifiers from symbols if tree has type, overriding the untpd behavior. */
implicit def modsDeco(mdef: untpd.MemberDef)(implicit ctx: Context): untpd.ModsDecorator =
new untpd.ModsDecorator {
def mods = if (mdef.hasType) Modifiers(mdef.symbol) else mdef.rawMods
def modifiers(mdef: untpd.MemberDef)(implicit ctx: Context): Modifiers = {
if (mdef.hasType) {
val sym = mdef.symbol
untpd.Modifiers(
sym.flags & (if (sym.isType) ModifierFlags | VarianceFlags else ModifierFlags),
if (sym.privateWithin.exists) sym.privateWithin.asType.name else tpnme.EMPTY,
sym.annotations map (_.tree)
)
}

def Modifiers(sym: Symbol)(implicit ctx: Context): Modifiers = untpd.Modifiers(
sym.flags & (if (sym.isType) ModifierFlags | VarianceFlags else ModifierFlags),
if (sym.privateWithin.exists) sym.privateWithin.asType.name else tpnme.EMPTY,
sym.annotations map (_.tree))
else
mdef.mods
}

protected def optAscription[T >: Untyped](tpt: Tree[T]) = optText(tpt)(": " ~ _)

Expand Down Expand Up @@ -604,18 +607,16 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
(txt /: vparamss)((txt, vparams) => txt ~ "(" ~ toText(vparams, ", ") ~ ")")

protected def valDefToText[T >: Untyped](tree: ValDef[T]): Text = {
import untpd.{modsDeco => _, _}
dclTextOr(tree) {
modText(tree.mods, keywordStr(if (tree.mods is Mutable) "var" else "val")) ~~
modText(modifiers(tree), keywordStr(if (modifiers(tree) is Mutable) "var" else "val")) ~~
valDefText(nameIdText(tree)) ~ optAscription(tree.tpt) ~
withEnclosingDef(tree) { optText(tree.rhs)(" = " ~ _) }
}
}

protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
import untpd.{modsDeco => _, _}
dclTextOr(tree) {
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree))
val prefix = modText(modifiers(tree), keywordStr("def")) ~~ valDefText(nameIdText(tree))
withEnclosingDef(tree) {
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt) ~
optText(tree.rhs)(" = " ~ _)
Expand All @@ -630,9 +631,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
val prefix: Text =
if (vparamss.isEmpty || primaryConstrs.nonEmpty) tparamsTxt
else {
var modsText = modText(constr.mods, "")
var modsText = modText(modifiers(constr), "")
if (!modsText.isEmpty) modsText = " " ~ modsText
if (constr.mods.hasAnnotations && !constr.mods.hasFlags) modsText = modsText ~~ " this"
if (modifiers(constr).hasAnnotations && !modifiers(constr).hasFlags) modsText = modsText ~~ " this"
withEnclosingDef(constr) { addVparamssText(tparamsTxt ~~ modsText, vparamss) }
}
val parentsText = Text(parents map constrText, keywordStr(" with "))
Expand All @@ -658,7 +659,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
}

protected def templateText(tree: TypeDef, impl: Template): Text = {
val decl = modText(tree.mods, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
val decl = modText(modifiers(tree), keywordStr(if (modifiers(tree) is Trait) "trait" else "class"))
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
(if (tree.hasType && ctx.settings.verbose.value) i"[decls = ${tree.symbol.info.decls}]" else "")
}
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,6 @@ trait Checking {

/** Check that all case classes that extend `scala.Enum` are `enum` cases */
def checkEnum(cdef: untpd.TypeDef, cls: Symbol)(implicit ctx: Context): Unit = {
import untpd.modsDeco
def isEnumAnonCls =
cls.isAnonymousClass &&
cls.owner.isTerm &&
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ class Namer { typer: Typer =>
while (c.owner != target) c = c.outer
c
}
for (annotTree <- untpd.modsDeco(stat).mods.annotations) {
for (annotTree <- stat.mods.annotations) {
val cls = typedAheadAnnotation(annotTree)(annotCtx)
if (sym.unforcedAnnotation(cls).isEmpty) {
val ann = Annotation.deferred(cls, implicit ctx => typedAnnotation(annotTree))
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ class Typer extends Namer
}
}
// necessary in order to mark the typed ahead annotations as definitely typed:
untpd.modsDeco(mdef).mods.annotations.foreach(typedAnnotation(_)(annotCtx))
mdef.mods.annotations.foreach(typedAnnotation(_)(annotCtx))
}

def typedAnnotation(annot: untpd.Tree)(implicit ctx: Context): Tree = track("typedAnnotation") {
Expand Down Expand Up @@ -1861,7 +1861,6 @@ class Typer extends Namer
case mdef1: DefDef if Inliner.hasBodyToInline(mdef1.symbol) =>
buf ++= inlineExpansion(mdef1)
case mdef1 =>
import untpd.modsDeco
mdef match {
case mdef: untpd.TypeDef if mdef.mods.hasMod[untpd.Mod.Enum] =>
enumContexts(mdef1.symbol) = ctx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package parsing
import org.junit.Test
import org.junit.Assert._

import ast.untpd.modsDeco
import ast.untpd._
import ast.{ Trees => d }
import Parsers.Parser
Expand Down