Skip to content

Commit f951c6b

Browse files
committed
Rename transparent methods -> rewrite methods
Also implements rewrite matches and rewrite ifs. Keeps transparent as a separate modifier with meaning: "inline if called from a rewrite context".
1 parent 1abb405 commit f951c6b

File tree

254 files changed

+788
-851
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

254 files changed

+788
-851
lines changed

bench/tests/power-macro/PowerMacro.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted.Expr
22

33
object PowerMacro {
44

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

77
def powerCode(n: Long, x: Expr[Double]): Expr[Double] =
88
if (n == 0) '(1.0)

compiler/src/dotty/tools/dotc/CompilationUnit.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import core.Types.Type // Do not remove me #3383
55
import util.SourceFile
66
import ast.{tpd, untpd}
77
import tpd.{ Tree, TreeTraverser }
8-
import typer.PrepareTransparent.InlineAccessors
8+
import typer.PrepareInlineable.InlineAccessors
99
import dotty.tools.dotc.core.Contexts.Context
1010
import dotty.tools.dotc.core.SymDenotations.ClassDenotation
1111
import dotty.tools.dotc.core.Symbols._

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import scala.io.Codec
1616
import util.{Set => _, _}
1717
import reporting.Reporter
1818
import transform.TreeChecker
19-
import rewrite.Rewrites
19+
import rewrites.Rewrites
2020
import java.io.{BufferedWriter, OutputStreamWriter}
2121

2222
import profile.Profiler

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
437437
def refPurity(tree: Tree)(implicit ctx: Context): PurityLevel = {
438438
val sym = tree.symbol
439439
if (!tree.hasType) Impure
440-
else if (!tree.tpe.widen.isParameterless || sym.is(Erased)) SimplyPure
440+
else if (!tree.tpe.widen.isParameterless || sym.isEffectivelyErased) SimplyPure
441441
else if (!sym.isStable) Impure
442442
else if (sym.is(Module))
443443
if (sym.moduleClass.isNoInitsClass) Pure else Idempotent

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ object Trees {
497497
extends TermTree[T] {
498498
type ThisTree[-T >: Untyped] = If[T]
499499
}
500+
class RewriteIf[T >: Untyped] private[ast] (cond: Tree[T], thenp: Tree[T], elsep: Tree[T])
501+
extends If(cond, thenp, elsep) {
502+
override def toString = s"RewriteIf($cond, $thenp, $elsep)"
503+
}
500504

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

521529
/** case pat if guard => body; only appears as child of a Match */
522530
case class CaseDef[-T >: Untyped] private[ast] (pat: Tree[T], guard: Tree[T], body: Tree[T])
@@ -883,8 +891,10 @@ object Trees {
883891
type Assign = Trees.Assign[T]
884892
type Block = Trees.Block[T]
885893
type If = Trees.If[T]
894+
type RewriteIf = Trees.RewriteIf[T]
886895
type Closure = Trees.Closure[T]
887896
type Match = Trees.Match[T]
897+
type RewriteMatch = Trees.RewriteMatch[T]
888898
type CaseDef = Trees.CaseDef[T]
889899
type Return = Trees.Return[T]
890900
type Try = Trees.Try[T]
@@ -1013,6 +1023,9 @@ object Trees {
10131023
case _ => finalize(tree, untpd.Block(stats, expr))
10141024
}
10151025
def If(tree: Tree)(cond: Tree, thenp: Tree, elsep: Tree)(implicit ctx: Context): If = tree match {
1026+
case tree: RewriteIf =>
1027+
if ((cond eq tree.cond) && (thenp eq tree.thenp) && (elsep eq tree.elsep)) tree
1028+
else finalize(tree, untpd.RewriteIf(cond, thenp, elsep))
10161029
case tree: If if (cond eq tree.cond) && (thenp eq tree.thenp) && (elsep eq tree.elsep) => tree
10171030
case _ => finalize(tree, untpd.If(cond, thenp, elsep))
10181031
}
@@ -1021,6 +1034,9 @@ object Trees {
10211034
case _ => finalize(tree, untpd.Closure(env, meth, tpt))
10221035
}
10231036
def Match(tree: Tree)(selector: Tree, cases: List[CaseDef])(implicit ctx: Context): Match = tree match {
1037+
case tree: RewriteMatch =>
1038+
if ((selector eq tree.selector) && (cases eq tree.cases)) tree
1039+
else finalize(tree, untpd.RewriteMatch(selector, cases))
10241040
case tree: Match if (selector eq tree.selector) && (cases eq tree.cases) => tree
10251041
case _ => finalize(tree, untpd.Match(selector, cases))
10261042
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
132132

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

135+
case class Rewrite() extends Mod(Flags.Rewrite)
136+
135137
case class Transparent() extends Mod(Flags.Transparent)
136138

137139
case class Enum() extends Mod(Flags.Enum)
@@ -273,8 +275,10 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
273275
def Assign(lhs: Tree, rhs: Tree): Assign = new Assign(lhs, rhs)
274276
def Block(stats: List[Tree], expr: Tree): Block = new Block(stats, expr)
275277
def If(cond: Tree, thenp: Tree, elsep: Tree): If = new If(cond, thenp, elsep)
278+
def RewriteIf(cond: Tree, thenp: Tree, elsep: Tree): If = new RewriteIf(cond, thenp, elsep)
276279
def Closure(env: List[Tree], meth: Tree, tpt: Tree): Closure = new Closure(env, meth, tpt)
277280
def Match(selector: Tree, cases: List[CaseDef]): Match = new Match(selector, cases)
281+
def RewriteMatch(selector: Tree, cases: List[CaseDef]): Match = new RewriteMatch(selector, cases)
278282
def CaseDef(pat: Tree, guard: Tree, body: Tree): CaseDef = new CaseDef(pat, guard, body)
279283
def Return(expr: Tree, from: Tree): Return = new Return(expr, from)
280284
def Try(expr: Tree, cases: List[CaseDef], finalizer: Tree): Try = new Try(expr, cases, finalizer)
@@ -310,7 +314,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
310314
* where `Ts` are the class type arguments of `T` or its class type alias.
311315
* Note: we also keep any type arguments as parts of `T`. This is necessary to allow
312316
* navigation into these arguments from the IDE, and to do the right thing in
313-
* PrepareTransparent.
317+
* PrepareInlineable.
314318
*/
315319
def New(tpt: Tree, argss: List[List[Tree]])(implicit ctx: Context): Tree = {
316320
val (tycon, targs) = tpt match {

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import java.io.File
55
import dotty.tools.io.{ Directory, PlainDirectory }
66

77
import PathResolver.Defaults
8-
import rewrite.Rewrites
8+
import rewrites.Rewrites
99

1010
class ScalaSettings extends Settings.SettingGroup {
1111

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object Annotations {
5757
}
5858

5959
/** An annotation indicating the body of a right-hand side,
60-
* typically of a transparent method. Treated specially in
60+
* typically of a rewrite or transparent method. Treated specially in
6161
* pickling/unpickling and TypeTreeMaps
6262
*/
6363
abstract class BodyAnnotation extends Annotation {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ object Contexts {
307307
def isNonEmptyScopeContext: Boolean =
308308
(this.scope ne outer.scope) && !this.scope.isEmpty
309309

310+
/** Is this a context for typechecking an inlined body? */
311+
def isInlineContext: Boolean =
312+
typer.isInstanceOf[Inliner#InlineTyper]
313+
310314
/** The next outer context whose tree is a template or package definition
311315
* Note: Currently unused
312316
def enclTemplate: Context = {

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ object Flags {
283283
*/
284284
final val Synthetic = commonFlag(18, "<synthetic>")
285285

286+
/** Labelled with `rewrite` modifier */
287+
final val Rewrite = commonFlag(19, "rewrite")
288+
286289
/** A covariant type variable / an outer accessor */
287290
final val CovariantOrOuter = commonFlag(20, "")
288291
final val Covariant = typeFlag(20, "<covariant>")
@@ -433,7 +436,7 @@ object Flags {
433436

434437
/** Flags representing source modifiers */
435438
final val SourceModifierFlags =
436-
commonFlags(Private, Protected, Abstract, Final, Transparent,
439+
commonFlags(Private, Protected, Abstract, Final, Rewrite | Transparent,
437440
Sealed, Case, Implicit, Override, AbsOverride, Lazy, JavaStatic, Erased)
438441

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

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

551-
/** Labeled `private`, `final`, or `transparent` */
552-
final val EffectivelyFinal = Private | Final | Transparent
554+
/** Labeled `private`, `final`, `rewrite` or `transparent` */
555+
final val EffectivelyFinal = Private | Final | Rewrite | Transparent
553556

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

563-
/** A transparent implicit method */
564-
final val TransparentImplicitMethod = allOf(Transparent, Implicit, Method)
566+
/** A rewrite method */
567+
final val RewriteMethod = allOf(Rewrite, Method)
568+
569+
/** An implicit rewrite method */
570+
final val ImplicitRewriteMethod = allOf(Rewrite, Implicit, Method)
565571

566572
/** A transparent parameter */
567573
final val TransparentParam = allOf(Transparent, Param)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ object Mode {
9595
/** We are in the IDE */
9696
val Interactive = newMode(20, "Interactive")
9797

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

101101
/** Read comments from definitions when unpickling from TASTY */
102102
val ReadComments = newMode(22, "ReadComments")

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object NameKinds {
4545
}
4646

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

300300
/** A kind of unique extension methods; Unlike other unique names, these can be
301301
* unmangled.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ object NameOps {
123123

124124
/** Revert the expanded name. */
125125
def unexpandedName: N = likeSpacedN {
126-
name.rewrite { case ExpandedName(_, unexp) => unexp }
126+
name.replace { case ExpandedName(_, unexp) => unexp }
127127
}
128128

129129
/** Remove the variance from the name. */
130130
def invariantName: N = likeSpacedN {
131-
name.rewrite { case VariantName(invariant, _) => invariant }
131+
name.replace { case VariantName(invariant, _) => invariant }
132132
}
133133

134134
def implClassName: N = likeSpacedN(name ++ tpnme.IMPL_CLASS_SUFFIX)
@@ -254,11 +254,11 @@ object NameOps {
254254
}
255255

256256
def unmangle(kind: NameKind): N = likeSpacedN {
257-
name rewrite {
257+
name replace {
258258
case unmangled: SimpleName =>
259259
kind.unmangle(unmangled)
260260
case ExpandedName(prefix, last) =>
261-
kind.unmangle(last) rewrite {
261+
kind.unmangle(last) replace {
262262
case kernel: SimpleName =>
263263
ExpandedName(prefix, kernel)
264264
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ object Names {
8383
* Stops at derived names whose kind has `definesNewName = true`.
8484
* If `f` does not apply to any part, return name unchanged.
8585
*/
86-
def rewrite(f: PartialFunction[Name, Name]): ThisName
86+
def replace(f: PartialFunction[Name, Name]): ThisName
8787

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

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

450-
override def rewrite(f: PartialFunction[Name, Name]): ThisName = toTermName.rewrite(f).toTypeName
450+
override def replace(f: PartialFunction[Name, Name]): ThisName = toTermName.replace(f).toTypeName
451451
override def collect[T](f: PartialFunction[Name, T]): Option[T] = toTermName.collect(f)
452452
override def mapLast(f: SimpleName => SimpleName) = toTermName.mapLast(f).toTypeName
453453
override def mapParts(f: SimpleName => SimpleName) = toTermName.mapParts(f).toTypeName
@@ -480,11 +480,11 @@ object Names {
480480
override def toSimpleName = termName(toString)
481481
override final def mangle = encode.toSimpleName
482482

483-
override def rewrite(f: PartialFunction[Name, Name]): ThisName =
483+
override def replace(f: PartialFunction[Name, Name]): ThisName =
484484
if (f.isDefinedAt(this)) likeSpaced(f(this))
485485
else info match {
486486
case qual: QualifiedInfo => this
487-
case _ => underlying.rewrite(f).derived(info)
487+
case _ => underlying.replace(f).derived(info)
488488
}
489489

490490
override def collect[T](f: PartialFunction[Name, T]): Option[T] =

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ object SymDenotations {
409409
prefix = prefix.exclude(ModuleClassName)
410410
def qualify(n: SimpleName) =
411411
kind(prefix.toTermName, if (filler.isEmpty) n else termName(filler + n))
412-
val fn = name rewrite {
412+
val fn = name replace {
413413
case name: SimpleName => qualify(name)
414414
case name @ AnyQualifiedName(_, _) => qualify(name.mangled.toSimpleName)
415415
}
@@ -781,11 +781,24 @@ object SymDenotations {
781781
def isTransparentMethod(implicit ctx: Context): Boolean =
782782
is(TransparentMethod, butNot = AccessorOrSynthetic)
783783

784-
/** A transparent method that is not nested inside another transparent method.
785-
* Nested transparents are not inlineable yet, only their inlined copies are.
784+
def isRewriteMethod(implicit ctx: Context): Boolean =
785+
is(RewriteMethod, butNot = AccessorOrSynthetic)
786+
787+
/** A transparent or rewrite method */
788+
def isInlineable(implicit ctx: Context): Boolean =
789+
is(TransparentMethod) || is(RewriteMethod)
790+
791+
/** An erased value or a rewrite method, excluding @forceInline annotated methods.
792+
* The latter have to be kept around to get to parity with Scala.
793+
* This is necessary at least until we have full bootstrap. Right now
794+
* dotty-bootstrapped involves running the Dotty compiler compiled with Scala 2 with
795+
* a Dotty runtime library compiled with Dotty. If we erase @forceInline annotated
796+
* methods, this means that the support methods in dotty.runtime.LazyVals vanish.
797+
* But they are needed for running the lazy val implementations in the Scala-2 compiled compiler.
786798
*/
787-
def isTransparentInlineable(implicit ctx: Context): Boolean =
788-
isTransparentMethod && !owner.ownersIterator.exists(_.is(TransparentMethod))
799+
def isEffectivelyErased(implicit ctx: Context): Boolean =
800+
is(Erased) ||
801+
isRewriteMethod && unforcedAnnotation(defn.ForceInlineAnnot).isEmpty
789802

790803
/** ()T and => T types should be treated as equivalent for this symbol.
791804
* Note: For the moment, we treat Scala-2 compiled symbols as loose matching,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
273273
violations.toList
274274
}
275275

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

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

323-
def testScala2Mode(msg: => Message, pos: Position, rewrite: => Unit = ()) = {
323+
def testScala2Mode(msg: => Message, pos: Position, replace: => Unit = ()) = {
324324
if (scala2Mode) {
325325
migrationWarning(msg, pos)
326-
rewrite
326+
replace
327327
}
328328
scala2Mode
329329
}

0 commit comments

Comments
 (0)