Skip to content

Commit 13e3d59

Browse files
committed
Fix two rewrite patches.
1. trailing `_`: `x _` is rewritten to `(() => x)` not to `x` 2. lazy vals: Rewrites are done in Typer, not LazyVals. Later on we are too much at risk to hit synthetically generated lazy vals.
1 parent 13a376c commit 13e3d59

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/dotty/tools/dotc/transform/LazyVals.scala

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import StdNames.nme
1515
import rewrite.Rewrites.patch
1616
import util.Positions.Position
1717
import dotty.tools.dotc.transform.TreeTransforms.{TransformerInfo, TreeTransformer, MiniPhaseTransform}
18-
import dotty.tools.dotc.ast.NavigateAST._
1918
import dotty.tools.dotc.ast.Trees._
2019
import dotty.tools.dotc.ast.{untpd, tpd}
2120
import dotty.tools.dotc.core.Constants.Constant
@@ -71,19 +70,14 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer with Nee
7170
val isField = sym.owner.isClass
7271
if (isField) {
7372
if (sym.isVolatile ||
74-
ctx.scala2Mode && {
75-
if (ctx.settings.rewrite.value.isDefined)
76-
patch(ctx.compilationUnit.source, Position(toUntyped(tree).envelope.start), "@volatile ")
77-
false // cannot assume volatile because of problems with compilestdlib. See #1149
78-
} ||
79-
(sym.is(Flags.Module) && !sym.is(Flags.Synthetic)))
80-
// module class is user-defined.
81-
// Should be threadsafe, to mimic safety guaranteed by global object
73+
(sym.is(Flags.Module)/* || ctx.scala2Mode*/) && !sym.is(Flags.Synthetic)) // TODO assume @voliat
74+
// module class is user-defined.
75+
// Should be threadsafe, to mimic safety guaranteed by global object
8276
transformMemberDefVolatile(tree)
83-
else if (sym.is(Flags.Module)) { // synthetic module
77+
else if (sym.is(Flags.Module)) // synthetic module
8478
transformSyntheticModule(tree)
85-
}
86-
else transformMemberDefNonVolatile(tree)
79+
else
80+
transformMemberDefNonVolatile(tree)
8781
}
8882
else transformLocalDef(tree)
8983
}

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import Implicits._
3434
import util.Stats.{track, record}
3535
import config.Printers._
3636
import rewrite.Rewrites.patch
37+
import NavigateAST._
38+
import transform.SymUtils._
3739
import language.implicitConversions
3840

3941
object Typer {
@@ -985,7 +987,18 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
985987
case rhs @ Ident(nme.WILDCARD) => rhs withType tpt1.tpe
986988
case rhs => typedExpr(rhs, tpt1.tpe)
987989
}
988-
assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
990+
val vdef1 = assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
991+
patchIfLazy(vdef1)
992+
vdef1
993+
}
994+
995+
/** Add a @volitile to lazy vals when rewriting from Scala2 */
996+
private def patchIfLazy(vdef: ValDef)(implicit ctx: Context): Unit = {
997+
val sym = vdef.symbol
998+
if (sym.is(Lazy, butNot = Deferred | Module | Synthetic) && !sym.isVolatile &&
999+
ctx.scala2Mode && ctx.settings.rewrite.value.isDefined &&
1000+
!ctx.isAfterTyper)
1001+
patch(ctx.compilationUnit.source, Position(toUntyped(vdef).envelope.start), "@volatile ")
9891002
}
9901003

9911004
def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(implicit ctx: Context) = track("typedDefDef") {
@@ -1143,8 +1156,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
11431156
if (pt1.eq(AnyFunctionProto) && !defn.isFunctionClass(res.tpe.classSymbol)) {
11441157
def msg = i"not a function: ${res.tpe}; cannot be followed by `_'"
11451158
if (ctx.scala2Mode) {
1159+
// Under -rewrite, patch `x _` to `(() => x)`
11461160
ctx.migrationWarning(msg, tree.pos)
1147-
patch(ctx.compilationUnit.source, Position(qual.pos.end, tree.pos.end), "")
1161+
patch(ctx.compilationUnit.source, Position(tree.pos.start), "(() => ")
1162+
patch(ctx.compilationUnit.source, Position(qual.pos.end, tree.pos.end), ")")
11481163
res = typed(untpd.Function(Nil, untpd.TypedSplice(res)))
11491164
}
11501165
else ctx.error(msg, tree.pos)

tests/pos-scala2/rewrites.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ object Test {
2424
@deprecated private lazy val (x2, y2) = (1, 2)
2525

2626
val yy = x1 _
27+
val zz: () => Int = yy
2728

2829
}
2930

0 commit comments

Comments
 (0)