Skip to content

Commit d30e213

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 d76d643 commit d30e213

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
@@ -14,7 +14,6 @@ import StdNames.nme
1414
import rewrite.Rewrites.patch
1515
import util.Positions.Position
1616
import dotty.tools.dotc.transform.TreeTransforms.{TransformerInfo, TreeTransformer, MiniPhaseTransform}
17-
import dotty.tools.dotc.ast.NavigateAST._
1817
import dotty.tools.dotc.ast.Trees._
1918
import dotty.tools.dotc.ast.{untpd, tpd}
2019
import dotty.tools.dotc.core.Constants.Constant
@@ -70,19 +69,14 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer with Nee
7069
val isField = sym.owner.isClass
7170
if (isField) {
7271
if (sym.isVolatile ||
73-
ctx.scala2Mode && {
74-
if (ctx.settings.rewrite.value.isDefined)
75-
patch(ctx.compilationUnit.source, Position(toUntyped(tree).envelope.start), "@volatile ")
76-
false // cannot assume volatile because of problems with compilestdlib. See #1149
77-
} ||
78-
(sym.is(Flags.Module) && !sym.is(Flags.Synthetic)))
79-
// module class is user-defined.
80-
// Should be threadsafe, to mimic safety guaranteed by global object
72+
(sym.is(Flags.Module)/* || ctx.scala2Mode*/) && !sym.is(Flags.Synthetic)) // TODO assume @voliat
73+
// module class is user-defined.
74+
// Should be threadsafe, to mimic safety guaranteed by global object
8175
transformMemberDefVolatile(tree)
82-
else if (sym.is(Flags.Module)) { // synthetic module
76+
else if (sym.is(Flags.Module)) // synthetic module
8377
transformSyntheticModule(tree)
84-
}
85-
else transformMemberDefNonVolatile(tree)
78+
else
79+
transformMemberDefNonVolatile(tree)
8680
}
8781
else transformLocalDef(tree)
8882
}

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

9901003
def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(implicit ctx: Context) = track("typedDefDef") {
@@ -1142,8 +1155,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
11421155
if (pt1.eq(AnyFunctionProto) && !defn.isFunctionClass(res.tpe.classSymbol)) {
11431156
def msg = i"not a function: ${res.tpe}; cannot be followed by `_'"
11441157
if (ctx.scala2Mode) {
1158+
// Under -rewrite, patch `x _` to `(() => x)`
11451159
ctx.migrationWarning(msg, tree.pos)
1146-
patch(ctx.compilationUnit.source, Position(qual.pos.end, tree.pos.end), "")
1160+
patch(ctx.compilationUnit.source, Position(tree.pos.start), "(() => ")
1161+
patch(ctx.compilationUnit.source, Position(qual.pos.end, tree.pos.end), ")")
11471162
res = typed(untpd.Function(Nil, untpd.TypedSplice(res)))
11481163
}
11491164
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)