@@ -34,6 +34,8 @@ import Implicits._
3434import util .Stats .{track , record }
3535import config .Printers ._
3636import rewrite .Rewrites .patch
37+ import NavigateAST ._
38+ import transform .SymUtils ._
3739import language .implicitConversions
3840
3941object 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)
0 commit comments