From 5beb62403607a78d4a91045d37696a654d08ad51 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 2 Jun 2020 16:37:52 +0200 Subject: [PATCH 1/5] Add a val in Scala 2 macro bundles --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 5e56352c739f..eff1d78b587e 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3574,7 +3574,9 @@ class Typer extends Namer case Ident(name) => untpd.Ident(name.toTypeName).withSpan(tree.span) case Select(qual, name) => untpd.Select(qual, name.toTypeName).withSpan(tree.span) val bundle = untpd.Apply(untpd.Select(untpd.New(ref), nme.CONSTRUCTOR), untpd.Literal(Constant(null))).withSpan(call.span) - typedExpr(bundle, defn.AnyType) + val bundle1 = typedExpr(bundle, defn.AnyType) + val bundleVal = SyntheticValDef(NameKinds.UniqueName.fresh("bundle".toTermName), bundle1) + tpd.Block(List(bundleVal), tpd.ref(bundleVal.symbol)) } } if ctx.phase.isTyper then From 73177bcb419192bb0a46a27482a58db30ec69df7 Mon Sep 17 00:00:00 2001 From: bishabosha Date: Wed, 3 Jun 2020 15:36:00 +0200 Subject: [PATCH 2/5] splice selection into block --- .../src/dotty/tools/dotc/typer/Typer.scala | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index eff1d78b587e..ee2b5ca36af2 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3565,9 +3565,9 @@ class Typer extends Namer /** Types the body Scala 2 macro declaration `def f = macro ` */ private def typedScala2MacroBody(call: untpd.Tree)(using Context): Tree = // TODO check that call is to a method with valid signature - def typedPrefix(tree: untpd.RefTree): Tree = { + def typedPrefix(tree: untpd.RefTree)(splice: Context ?=> Tree => Tree)(using Context): Tree = { tryAlternatively { - typedExpr(tree, defn.AnyType) + splice(typedExpr(tree, defn.AnyType)) } { // Try to type as a macro bundle val ref = tree match @@ -3576,7 +3576,7 @@ class Typer extends Namer val bundle = untpd.Apply(untpd.Select(untpd.New(ref), nme.CONSTRUCTOR), untpd.Literal(Constant(null))).withSpan(call.span) val bundle1 = typedExpr(bundle, defn.AnyType) val bundleVal = SyntheticValDef(NameKinds.UniqueName.fresh("bundle".toTermName), bundle1) - tpd.Block(List(bundleVal), tpd.ref(bundleVal.symbol)) + tpd.Block(List(bundleVal), splice(tpd.ref(bundleVal.symbol))) } } if ctx.phase.isTyper then @@ -3584,11 +3584,15 @@ class Typer extends Namer case call: untpd.Ident => typedIdent(call, defn.AnyType) case untpd.Select(qual: untpd.RefTree, name) => - val call2 = untpd.Select(untpd.TypedSplice(typedPrefix(qual)), name).withSpan(call.span) - typedSelect(call2, defn.AnyType) + typedPrefix(qual) { qual => + val call2 = untpd.Select(untpd.TypedSplice(qual), name).withSpan(call.span) + typedSelect(call2, defn.AnyType) + } case untpd.TypeApply(untpd.Select(qual: untpd.RefTree, name), targs) => - val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(typedPrefix(qual)), name), targs).withSpan(call.span) - typedTypeApply(call2, defn.AnyType) + typedPrefix(qual) { qual => + val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(qual), name), targs).withSpan(call.span) + typedTypeApply(call2, defn.AnyType) + } case _ => ctx.error("Invalid Scala 2 macro " + call.show, call.sourcePos) EmptyTree From 8411fe27c4f0a194d3ce642fc897d3bf43efeba4 Mon Sep 17 00:00:00 2001 From: bishabosha Date: Wed, 3 Jun 2020 17:32:59 +0200 Subject: [PATCH 3/5] adjust spans --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index ee2b5ca36af2..9f7d137271a1 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3575,8 +3575,8 @@ class Typer extends Namer case Select(qual, name) => untpd.Select(qual, name.toTypeName).withSpan(tree.span) val bundle = untpd.Apply(untpd.Select(untpd.New(ref), nme.CONSTRUCTOR), untpd.Literal(Constant(null))).withSpan(call.span) val bundle1 = typedExpr(bundle, defn.AnyType) - val bundleVal = SyntheticValDef(NameKinds.UniqueName.fresh("bundle".toTermName), bundle1) - tpd.Block(List(bundleVal), splice(tpd.ref(bundleVal.symbol))) + val bundleVal = SyntheticValDef(NameKinds.UniqueName.fresh("bundle".toTermName), bundle1).withSpan(call.span) + tpd.Block(List(bundleVal), splice(tpd.ref(bundleVal.symbol))).withSpan(call.span) } } if ctx.phase.isTyper then @@ -3585,12 +3585,12 @@ class Typer extends Namer typedIdent(call, defn.AnyType) case untpd.Select(qual: untpd.RefTree, name) => typedPrefix(qual) { qual => - val call2 = untpd.Select(untpd.TypedSplice(qual), name).withSpan(call.span) + val call2 = untpd.Select(untpd.TypedSplice(qual).withSpan(call.span), name).withSpan(call.span) typedSelect(call2, defn.AnyType) } case untpd.TypeApply(untpd.Select(qual: untpd.RefTree, name), targs) => typedPrefix(qual) { qual => - val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(qual), name), targs).withSpan(call.span) + val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(qual).withSpan(call.span), name), targs).withSpan(call.span) typedTypeApply(call2, defn.AnyType) } case _ => From 478f35befacb2b081e55c9e3e8756290bad1d6b3 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 5 Jun 2020 14:59:35 +0200 Subject: [PATCH 4/5] Remove unnecessary withSpan --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 9f7d137271a1..7dea83005c37 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3575,7 +3575,7 @@ class Typer extends Namer case Select(qual, name) => untpd.Select(qual, name.toTypeName).withSpan(tree.span) val bundle = untpd.Apply(untpd.Select(untpd.New(ref), nme.CONSTRUCTOR), untpd.Literal(Constant(null))).withSpan(call.span) val bundle1 = typedExpr(bundle, defn.AnyType) - val bundleVal = SyntheticValDef(NameKinds.UniqueName.fresh("bundle".toTermName), bundle1).withSpan(call.span) + val bundleVal = SyntheticValDef(NameKinds.UniqueName.fresh("bundle".toTermName), bundle1) tpd.Block(List(bundleVal), splice(tpd.ref(bundleVal.symbol))).withSpan(call.span) } } @@ -3585,12 +3585,12 @@ class Typer extends Namer typedIdent(call, defn.AnyType) case untpd.Select(qual: untpd.RefTree, name) => typedPrefix(qual) { qual => - val call2 = untpd.Select(untpd.TypedSplice(qual).withSpan(call.span), name).withSpan(call.span) + val call2 = untpd.Select(untpd.TypedSplice(qual), name).withSpan(call.span) typedSelect(call2, defn.AnyType) } case untpd.TypeApply(untpd.Select(qual: untpd.RefTree, name), targs) => typedPrefix(qual) { qual => - val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(qual).withSpan(call.span), name), targs).withSpan(call.span) + val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(qual), name), targs).withSpan(call.span) typedTypeApply(call2, defn.AnyType) } case _ => From bd066e161c72c13f616c095b2014ef23e342587b Mon Sep 17 00:00:00 2001 From: bishabosha Date: Fri, 5 Jun 2020 15:30:33 +0200 Subject: [PATCH 5/5] Address review comments, adjust spans --- compiler/src/dotty/tools/dotc/core/StdNames.scala | 1 + compiler/src/dotty/tools/dotc/typer/Typer.scala | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/StdNames.scala b/compiler/src/dotty/tools/dotc/core/StdNames.scala index abbe43bf12df..80f386664972 100644 --- a/compiler/src/dotty/tools/dotc/core/StdNames.scala +++ b/compiler/src/dotty/tools/dotc/core/StdNames.scala @@ -425,6 +425,7 @@ object StdNames { val assume_ : N = "assume" val box: N = "box" val build : N = "build" + val bundle: N = "bundle" val bytes: N = "bytes" val canEqual_ : N = "canEqual" val cbnArg: N = "" diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 7dea83005c37..ef732e29662b 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3575,7 +3575,7 @@ class Typer extends Namer case Select(qual, name) => untpd.Select(qual, name.toTypeName).withSpan(tree.span) val bundle = untpd.Apply(untpd.Select(untpd.New(ref), nme.CONSTRUCTOR), untpd.Literal(Constant(null))).withSpan(call.span) val bundle1 = typedExpr(bundle, defn.AnyType) - val bundleVal = SyntheticValDef(NameKinds.UniqueName.fresh("bundle".toTermName), bundle1) + val bundleVal = SyntheticValDef(NameKinds.UniqueName.fresh(nme.bundle), bundle1).withSpan(call.span) tpd.Block(List(bundleVal), splice(tpd.ref(bundleVal.symbol))).withSpan(call.span) } } @@ -3590,7 +3590,7 @@ class Typer extends Namer } case untpd.TypeApply(untpd.Select(qual: untpd.RefTree, name), targs) => typedPrefix(qual) { qual => - val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(qual), name), targs).withSpan(call.span) + val call2 = untpd.TypeApply(untpd.Select(untpd.TypedSplice(qual), name), targs).withSpan(call.span) typedTypeApply(call2, defn.AnyType) } case _ =>