Skip to content

Change in terminology for typelevel methods #4927

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 11 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion bench/tests/power-macro/PowerMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import scala.quoted.Expr

object PowerMacro {

transparent def power(transparent n: Long, x: Double) = ~powerCode(n, '(x))
rewrite def power(transparent n: Long, x: Double) = ~powerCode(n, '(x))

def powerCode(n: Long, x: Expr[Double]): Expr[Double] =
if (n == 0) '(1.0)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/CompilationUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package dotc
import util.SourceFile
import ast.{tpd, untpd}
import tpd.{ Tree, TreeTraverser }
import typer.PrepareTransparent.InlineAccessors
import typer.PrepareInlineable.InlineAccessors
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.SymDenotations.ClassDenotation
import dotty.tools.dotc.core.Symbols._
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import scala.io.Codec
import util.{Set => _, _}
import reporting.Reporter
import transform.TreeChecker
import rewrite.Rewrites
import rewrites.Rewrites
import java.io.{BufferedWriter, OutputStreamWriter}

import profile.Profiler
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
def refPurity(tree: Tree)(implicit ctx: Context): PurityLevel = {
val sym = tree.symbol
if (!tree.hasType) Impure
else if (!tree.tpe.widen.isParameterless || sym.is(Erased)) SimplyPure
else if (!tree.tpe.widen.isParameterless || sym.isEffectivelyErased) SimplyPure
else if (!sym.isStable) Impure
else if (sym.is(Module))
if (sym.moduleClass.isNoInitsClass) Pure else Idempotent
Expand Down
16 changes: 16 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ object Trees {
extends TermTree[T] {
type ThisTree[-T >: Untyped] = If[T]
}
class RewriteIf[T >: Untyped] private[ast] (cond: Tree[T], thenp: Tree[T], elsep: Tree[T])
extends If(cond, thenp, elsep) {
override def toString = s"RewriteIf($cond, $thenp, $elsep)"
}

/** A closure with an environment and a reference to a method.
* @param env The captured parameters of the closure
Expand All @@ -517,6 +521,10 @@ object Trees {
extends TermTree[T] {
type ThisTree[-T >: Untyped] = Match[T]
}
class RewriteMatch[T >: Untyped] private[ast] (selector: Tree[T], cases: List[CaseDef[T]])
extends Match(selector, cases) {
override def toString = s"RewriteMatch($selector, $cases)"
}

/** case pat if guard => body; only appears as child of a Match */
case class CaseDef[-T >: Untyped] private[ast] (pat: Tree[T], guard: Tree[T], body: Tree[T])
Expand Down Expand Up @@ -883,8 +891,10 @@ object Trees {
type Assign = Trees.Assign[T]
type Block = Trees.Block[T]
type If = Trees.If[T]
type RewriteIf = Trees.RewriteIf[T]
type Closure = Trees.Closure[T]
type Match = Trees.Match[T]
type RewriteMatch = Trees.RewriteMatch[T]
type CaseDef = Trees.CaseDef[T]
type Return = Trees.Return[T]
type Try = Trees.Try[T]
Expand Down Expand Up @@ -1013,6 +1023,9 @@ object Trees {
case _ => finalize(tree, untpd.Block(stats, expr))
}
def If(tree: Tree)(cond: Tree, thenp: Tree, elsep: Tree)(implicit ctx: Context): If = tree match {
case tree: RewriteIf =>
if ((cond eq tree.cond) && (thenp eq tree.thenp) && (elsep eq tree.elsep)) tree
else finalize(tree, untpd.RewriteIf(cond, thenp, elsep))
case tree: If if (cond eq tree.cond) && (thenp eq tree.thenp) && (elsep eq tree.elsep) => tree
case _ => finalize(tree, untpd.If(cond, thenp, elsep))
}
Expand All @@ -1021,6 +1034,9 @@ object Trees {
case _ => finalize(tree, untpd.Closure(env, meth, tpt))
}
def Match(tree: Tree)(selector: Tree, cases: List[CaseDef])(implicit ctx: Context): Match = tree match {
case tree: RewriteMatch =>
if ((selector eq tree.selector) && (cases eq tree.cases)) tree
else finalize(tree, untpd.RewriteMatch(selector, cases))
case tree: Match if (selector eq tree.selector) && (cases eq tree.cases) => tree
case _ => finalize(tree, untpd.Match(selector, cases))
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/untpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {

case class Lazy() extends Mod(Flags.Lazy)

case class Rewrite() extends Mod(Flags.Rewrite)

case class Transparent() extends Mod(Flags.Transparent)

case class Enum() extends Mod(Flags.Enum)
Expand Down Expand Up @@ -273,8 +275,10 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
def Assign(lhs: Tree, rhs: Tree): Assign = new Assign(lhs, rhs)
def Block(stats: List[Tree], expr: Tree): Block = new Block(stats, expr)
def If(cond: Tree, thenp: Tree, elsep: Tree): If = new If(cond, thenp, elsep)
def RewriteIf(cond: Tree, thenp: Tree, elsep: Tree): If = new RewriteIf(cond, thenp, elsep)
def Closure(env: List[Tree], meth: Tree, tpt: Tree): Closure = new Closure(env, meth, tpt)
def Match(selector: Tree, cases: List[CaseDef]): Match = new Match(selector, cases)
def RewriteMatch(selector: Tree, cases: List[CaseDef]): Match = new RewriteMatch(selector, cases)
def CaseDef(pat: Tree, guard: Tree, body: Tree): CaseDef = new CaseDef(pat, guard, body)
def Return(expr: Tree, from: Tree): Return = new Return(expr, from)
def Try(expr: Tree, cases: List[CaseDef], finalizer: Tree): Try = new Try(expr, cases, finalizer)
Expand Down Expand Up @@ -310,7 +314,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
* where `Ts` are the class type arguments of `T` or its class type alias.
* Note: we also keep any type arguments as parts of `T`. This is necessary to allow
* navigation into these arguments from the IDE, and to do the right thing in
* PrepareTransparent.
* PrepareInlineable.
*/
def New(tpt: Tree, argss: List[List[Tree]])(implicit ctx: Context): Tree = {
val (tycon, targs) = tpt match {
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.io.File
import dotty.tools.io.{ Directory, PlainDirectory }

import PathResolver.Defaults
import rewrite.Rewrites
import rewrites.Rewrites

class ScalaSettings extends Settings.SettingGroup {

Expand Down Expand Up @@ -43,7 +43,7 @@ class ScalaSettings extends Settings.SettingGroup {
val pageWidth = IntSetting("-pagewidth", "Set page width", 80)
val strict = BooleanSetting("-strict", "Use strict type rules, which means some formerly legal code does not typecheck anymore.")
val language = MultiStringSetting("-language", "feature", "Enable one or more language features.")
val rewrite = OptionSetting[Rewrites]("-rewrite", "When used in conjunction with -language:Scala2 rewrites sources to migrate to new syntax")
val `rewrite` = OptionSetting[Rewrites]("-rewrite", "When used in conjunction with -language:Scala2 rewrites sources to migrate to new syntax")
val silentWarnings = BooleanSetting("-nowarn", "Silence all warnings.")
val fromTasty = BooleanSetting("-from-tasty", "Compile classes from tasty in classpath. The arguments are used as class names.")

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object Annotations {
}

/** An annotation indicating the body of a right-hand side,
* typically of a transparent method. Treated specially in
* typically of a rewrite or transparent method. Treated specially in
* pickling/unpickling and TypeTreeMaps
*/
abstract class BodyAnnotation extends Annotation {
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ object Contexts {
def isNonEmptyScopeContext: Boolean =
(this.scope ne outer.scope) && !this.scope.isEmpty

/** Is this a context for typechecking an inlined body? */
def isInlineContext: Boolean =
typer.isInstanceOf[Inliner#InlineTyper]

/** The next outer context whose tree is a template or package definition
* Note: Currently unused
def enclTemplate: Context = {
Expand Down
18 changes: 12 additions & 6 deletions compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ object Flags {
*/
final val Synthetic = commonFlag(18, "<synthetic>")

/** Labelled with `rewrite` modifier */
final val Rewrite = commonFlag(19, "rewrite")

/** A covariant type variable / an outer accessor */
final val CovariantOrOuter = commonFlag(20, "")
final val Covariant = typeFlag(20, "<covariant>")
Expand Down Expand Up @@ -433,7 +436,7 @@ object Flags {

/** Flags representing source modifiers */
final val SourceModifierFlags =
commonFlags(Private, Protected, Abstract, Final, Transparent,
commonFlags(Private, Protected, Abstract, Final, Rewrite | Transparent,
Sealed, Case, Implicit, Override, AbsOverride, Lazy, JavaStatic, Erased)

/** Flags representing modifiers that can appear in trees */
Expand All @@ -454,7 +457,7 @@ object Flags {
Scala2ExistentialCommon | Mutable.toCommonFlags | Touched | JavaStatic |
CovariantOrOuter | ContravariantOrLabel | CaseAccessor.toCommonFlags |
NonMember | ImplicitCommon | Permanent | Synthetic |
SuperAccessorOrScala2x | Transparent
SuperAccessorOrScala2x | Rewrite | Transparent

/** Flags that are not (re)set when completing the denotation, or, if symbol is
* a top-level class or object, when completing the denotation once the class
Expand Down Expand Up @@ -548,8 +551,8 @@ object Flags {
/** Assumed to be pure */
final val StableOrErased = Stable | Erased

/** Labeled `private`, `final`, or `transparent` */
final val EffectivelyFinal = Private | Final | Transparent
/** Labeled `private`, `final`, `rewrite` or `transparent` */
final val EffectivelyFinal = Private | Final | Rewrite | Transparent

/** A private method */
final val PrivateMethod = allOf(Private, Method)
Expand All @@ -560,8 +563,11 @@ object Flags {
/** A transparent method */
final val TransparentMethod = allOf(Transparent, Method)

/** A transparent implicit method */
final val TransparentImplicitMethod = allOf(Transparent, Implicit, Method)
/** A rewrite method */
final val RewriteMethod = allOf(Rewrite, Method)

/** An implicit rewrite method */
final val ImplicitRewriteMethod = allOf(Rewrite, Implicit, Method)

/** A transparent parameter */
final val TransparentParam = allOf(Transparent, Param)
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Mode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ object Mode {
/** We are in the IDE */
val Interactive = newMode(20, "Interactive")

/** We are typing the body of a transparent method */
val TransparentBody = newMode(21, "TransparentBody")
/** We are typing the body of a transparent or rewrite method */
val InlineableBody = newMode(21, "InlineableBody")

/** Read comments from definitions when unpickling from TASTY */
val ReadComments = newMode(22, "ReadComments")
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/core/NameKinds.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object NameKinds {
}

/** Does this kind define logically a new name (respectively qualified name)?
* Tested by the `rewrite` and `collect` combinators of class `Name`.
* Tested by the `replace` and `collect` combinators of class `Name`.
*/
def definesNewName = false
def definesQualifiedName = false
Expand Down Expand Up @@ -135,7 +135,7 @@ object NameKinds {
* Needed because the suffix of an expanded name may itself be expanded.
* For example, look at javap of scala.App.initCode
*/
def apply(qual: TermName, name: TermName): TermName = name rewrite {
def apply(qual: TermName, name: TermName): TermName = name replace {
case name: SimpleName => apply(qual, name)
case AnyQualifiedName(_, _) => apply(qual, name.toSimpleName)
}
Expand Down Expand Up @@ -294,8 +294,8 @@ object NameKinds {
val SuperArgName = new UniqueNameKind("$superArg$")
val DocArtifactName = new UniqueNameKind("$doc")
val UniqueInlineName = new UniqueNameKind("$i")
val TransparentScrutineeName = new UniqueNameKind("$scrutinee")
val TransparentBinderName = new UniqueNameKind("$elem")
val RewriteScrutineeName = new UniqueNameKind("$scrutinee")
val RewriteBinderName = new UniqueNameKind("$elem")

/** A kind of unique extension methods; Unlike other unique names, these can be
* unmangled.
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/core/NameOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ object NameOps {

/** Revert the expanded name. */
def unexpandedName: N = likeSpacedN {
name.rewrite { case ExpandedName(_, unexp) => unexp }
name.replace { case ExpandedName(_, unexp) => unexp }
}

/** Remove the variance from the name. */
def invariantName: N = likeSpacedN {
name.rewrite { case VariantName(invariant, _) => invariant }
name.replace { case VariantName(invariant, _) => invariant }
}

def implClassName: N = likeSpacedN(name ++ tpnme.IMPL_CLASS_SUFFIX)
Expand Down Expand Up @@ -254,11 +254,11 @@ object NameOps {
}

def unmangle(kind: NameKind): N = likeSpacedN {
name rewrite {
name replace {
case unmangled: SimpleName =>
kind.unmangle(unmangled)
case ExpandedName(prefix, last) =>
kind.unmangle(last) rewrite {
kind.unmangle(last) replace {
case kernel: SimpleName =>
ExpandedName(prefix, kernel)
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/core/Names.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ object Names {
* Stops at derived names whose kind has `definesNewName = true`.
* If `f` does not apply to any part, return name unchanged.
*/
def rewrite(f: PartialFunction[Name, Name]): ThisName
def replace(f: PartialFunction[Name, Name]): ThisName

/** If partial function `f` is defined for some part of this name, apply it
* in a Some, otherwise None.
Expand Down Expand Up @@ -348,7 +348,7 @@ object Names {
override def toSimpleName = this
override final def mangle = encode

override def rewrite(f: PartialFunction[Name, Name]): ThisName =
override def replace(f: PartialFunction[Name, Name]): ThisName =
if (f.isDefinedAt(this)) likeSpaced(f(this)) else this
override def collect[T](f: PartialFunction[Name, T]): Option[T] = f.lift(this)
override def mapLast(f: SimpleName => SimpleName) = f(this)
Expand Down Expand Up @@ -447,7 +447,7 @@ object Names {
override def mangled = toTermName.mangled.toTypeName
override def mangledString = toTermName.mangledString

override def rewrite(f: PartialFunction[Name, Name]): ThisName = toTermName.rewrite(f).toTypeName
override def replace(f: PartialFunction[Name, Name]): ThisName = toTermName.replace(f).toTypeName
override def collect[T](f: PartialFunction[Name, T]): Option[T] = toTermName.collect(f)
override def mapLast(f: SimpleName => SimpleName) = toTermName.mapLast(f).toTypeName
override def mapParts(f: SimpleName => SimpleName) = toTermName.mapParts(f).toTypeName
Expand Down Expand Up @@ -480,11 +480,11 @@ object Names {
override def toSimpleName = termName(toString)
override final def mangle = encode.toSimpleName

override def rewrite(f: PartialFunction[Name, Name]): ThisName =
override def replace(f: PartialFunction[Name, Name]): ThisName =
if (f.isDefinedAt(this)) likeSpaced(f(this))
else info match {
case qual: QualifiedInfo => this
case _ => underlying.rewrite(f).derived(info)
case _ => underlying.replace(f).derived(info)
}

override def collect[T](f: PartialFunction[Name, T]): Option[T] =
Expand Down
23 changes: 18 additions & 5 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ object SymDenotations {
prefix = prefix.exclude(ModuleClassName)
def qualify(n: SimpleName) =
kind(prefix.toTermName, if (filler.isEmpty) n else termName(filler + n))
val fn = name rewrite {
val fn = name replace {
case name: SimpleName => qualify(name)
case name @ AnyQualifiedName(_, _) => qualify(name.mangled.toSimpleName)
}
Expand Down Expand Up @@ -781,11 +781,24 @@ object SymDenotations {
def isTransparentMethod(implicit ctx: Context): Boolean =
is(TransparentMethod, butNot = AccessorOrSynthetic)

/** A transparent method that is not nested inside another transparent method.
* Nested transparents are not inlineable yet, only their inlined copies are.
def isRewriteMethod(implicit ctx: Context): Boolean =
is(RewriteMethod, butNot = AccessorOrSynthetic)

/** A transparent or rewrite method */
def isInlineable(implicit ctx: Context): Boolean =
is(TransparentMethod) || is(RewriteMethod)

/** An erased value or a rewrite method, excluding @forceInline annotated methods.
* The latter have to be kept around to get to parity with Scala.
* This is necessary at least until we have full bootstrap. Right now
* dotty-bootstrapped involves running the Dotty compiler compiled with Scala 2 with
* a Dotty runtime library compiled with Dotty. If we erase @forceInline annotated
* methods, this means that the support methods in dotty.runtime.LazyVals vanish.
* But they are needed for running the lazy val implementations in the Scala-2 compiled compiler.
*/
def isTransparentInlineable(implicit ctx: Context): Boolean =
isTransparentMethod && !owner.ownersIterator.exists(_.is(TransparentMethod))
def isEffectivelyErased(implicit ctx: Context): Boolean =
is(Erased) ||
isRewriteMethod && unforcedAnnotation(defn.ForceInlineAnnot).isEmpty

/** ()T and => T types should be treated as equivalent for this symbol.
* Note: For the moment, we treat Scala-2 compiled symbols as loose matching,
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
violations.toList
}

/** Are we in a transparent method body? */
def inTransparentMethod = owner.ownersIterator.exists(_.isTransparentMethod)
/** Are we in a rewrite method body? */
def inRewriteMethod = owner.ownersIterator.exists(_.isRewriteMethod)

/** Is `feature` enabled in class `owner`?
* This is the case if one of the following two alternatives holds:
Expand Down Expand Up @@ -320,10 +320,10 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
def dynamicsEnabled =
featureEnabled(defn.LanguageModuleClass, nme.dynamics)

def testScala2Mode(msg: => Message, pos: Position, rewrite: => Unit = ()) = {
def testScala2Mode(msg: => Message, pos: Position, replace: => Unit = ()) = {
if (scala2Mode) {
migrationWarning(msg, pos)
rewrite
replace
}
scala2Mode
}
Expand Down
Loading