From 8f686f75c4b9fc54ddc289e3d12a30a801944ec8 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 28 Sep 2018 22:09:37 -0500 Subject: [PATCH 1/2] Allow None and Some to be inline parameters --- .../dotty/tools/dotc/transform/Splicer.scala | 40 ++++++++++++++----- .../src/dotty/tools/dotc/typer/Checking.scala | 17 +++++++- .../test/dotc/run-test-pickling.blacklist | 1 + tests/neg/inline-option/Macro_1.scala | 9 +++++ tests/neg/inline-option/Main_2.scala | 15 +++++++ tests/run/inline-option.check | 9 +++++ tests/run/inline-option/Macro_1.scala | 13 ++++++ tests/run/inline-option/Main_2.scala | 28 +++++++++++++ 8 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 tests/neg/inline-option/Macro_1.scala create mode 100644 tests/neg/inline-option/Main_2.scala create mode 100644 tests/run/inline-option.check create mode 100644 tests/run/inline-option/Macro_1.scala create mode 100644 tests/run/inline-option/Main_2.scala diff --git a/compiler/src/dotty/tools/dotc/transform/Splicer.scala b/compiler/src/dotty/tools/dotc/transform/Splicer.scala index a6bd284da409..5725d554b815 100644 --- a/compiler/src/dotty/tools/dotc/transform/Splicer.scala +++ b/compiler/src/dotty/tools/dotc/transform/Splicer.scala @@ -111,9 +111,20 @@ object Splicer { } protected def interpretStaticMethodCall(fn: Tree, args: => List[Object])(implicit env: Env): Object = { - val (clazz, instance) = loadModule(fn.symbol.owner) - val method = getMethod(clazz, fn.symbol.name, paramsSig(fn.symbol)) - stopIfRuntimeException(method.invoke(instance, args: _*)) + if (fn.symbol == defn.NoneModuleRef.termSymbol) { + // TODO generalize + None + } else { + val (clazz, instance) = loadModule(fn.symbol.owner) + val method = getMethod(clazz, fn.symbol.name, paramsSig(fn.symbol)) + stopIfRuntimeException(method.invoke(instance, args: _*)) + } + } + + protected def interpretNew(fn: RefTree, args: => List[Result])(implicit env: Env): Object = { + val clazz = loadClass(fn.symbol.owner.fullName) + val constr = clazz.getConstructor(paramsSig(fn.symbol): _*) + constr.newInstance(args: _*).asInstanceOf[Object] } protected def unexpectedTree(tree: Tree)(implicit env: Env): Object = @@ -240,12 +251,13 @@ object Splicer { def apply(tree: Tree): Boolean = interpretTree(tree)(Map.empty) - def interpretQuote(tree: tpd.Tree)(implicit env: Env): Boolean = true - def interpretTypeQuote(tree: tpd.Tree)(implicit env: Env): Boolean = true - def interpretLiteral(value: Any)(implicit env: Env): Boolean = true - def interpretVarargs(args: List[Boolean])(implicit env: Env): Boolean = args.forall(identity) - def interpretTastyContext()(implicit env: Env): Boolean = true - def interpretStaticMethodCall(fn: tpd.Tree, args: => List[Boolean])(implicit env: Env): Boolean = args.forall(identity) + protected def interpretQuote(tree: tpd.Tree)(implicit env: Env): Boolean = true + protected def interpretTypeQuote(tree: tpd.Tree)(implicit env: Env): Boolean = true + protected def interpretLiteral(value: Any)(implicit env: Env): Boolean = true + protected def interpretVarargs(args: List[Boolean])(implicit env: Env): Boolean = args.forall(identity) + protected def interpretTastyContext()(implicit env: Env): Boolean = true + protected def interpretStaticMethodCall(fn: tpd.Tree, args: => List[Boolean])(implicit env: Env): Boolean = args.forall(identity) + protected def interpretNew(fn: RefTree, args: => List[Result])(implicit env: Env): Boolean = args.forall(identity) def unexpectedTree(tree: tpd.Tree)(implicit env: Env): Boolean = { // Assuming that top-level splices can only be in inline methods @@ -266,6 +278,7 @@ object Splicer { protected def interpretVarargs(args: List[Result])(implicit env: Env): Result protected def interpretTastyContext()(implicit env: Env): Result protected def interpretStaticMethodCall(fn: Tree, args: => List[Result])(implicit env: Env): Result + protected def interpretNew(fn: RefTree, args: => List[Result])(implicit env: Env): Result protected def unexpectedTree(tree: Tree)(implicit env: Env): Result protected final def interpretTree(tree: Tree)(implicit env: Env): Result = tree match { @@ -298,7 +311,14 @@ object Splicer { case Inlined(EmptyTree, Nil, expansion) => interpretTree(expansion) - case Typed(SeqLiteral(elems, _), _) => interpretVarargs(elems.map(e => interpretTree(e))) + case Apply(TypeApply(fun: RefTree, _), args) if fun.symbol.isConstructor && fun.symbol.owner.owner.is(Package) => + interpretNew(fun, args.map(interpretTree)) + + case Apply(fun: RefTree, args) if fun.symbol.isConstructor && fun.symbol.owner.owner.is(Package)=> + interpretNew(fun, args.map(interpretTree)) + + case Typed(SeqLiteral(elems, _), _) => + interpretVarargs(elems.map(e => interpretTree(e))) case _ => unexpectedTree(tree) diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 695b47bf9a5c..42a48f09c451 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -678,9 +678,22 @@ trait Checking { val purityLevel = if (isFinal) Idempotent else Pure tree.tpe.widenTermRefExpr match { case tp: ConstantType if exprPurity(tree) >= purityLevel => // ok - case _ => - val allow = ctx.erasedTypes || ctx.inInlineMethod + case tp => + val allow = + ctx.erasedTypes || + ctx.inInlineMethod || + // TODO: Make None and Some constant types? + tree.symbol.eq(defn.NoneModuleRef.termSymbol) || + tree.symbol.eq(defn.SomeClass.primaryConstructor) || + (tree.symbol.name == nme.apply && tree.symbol.owner == defn.SomeClass.companionModule.moduleClass) if (!allow) ctx.error(em"$what must be a constant expression", tree.pos) + else tree match { + // TODO: add cases for type apply and multiple applies + case Apply(_, args) => + for (arg <- args) + checkInlineConformant(arg, isFinal, what) + case _ => + } } } diff --git a/compiler/test/dotc/run-test-pickling.blacklist b/compiler/test/dotc/run-test-pickling.blacklist index 39680daf65f8..6ccfdb431c33 100644 --- a/compiler/test/dotc/run-test-pickling.blacklist +++ b/compiler/test/dotc/run-test-pickling.blacklist @@ -24,6 +24,7 @@ i5119 i5119b inline-varargs-1 implicitShortcut +inline-option lazy-implicit-lists.scala lazy-implicit-nums.scala lazy-traits.scala diff --git a/tests/neg/inline-option/Macro_1.scala b/tests/neg/inline-option/Macro_1.scala new file mode 100644 index 000000000000..e78b1987f58c --- /dev/null +++ b/tests/neg/inline-option/Macro_1.scala @@ -0,0 +1,9 @@ + +import scala.quoted._ + +object Macro { + def impl(opt: Option[Int]): Expr[Int] = opt match { + case Some(i) => i.toExpr + case None => '(-1) + } +} \ No newline at end of file diff --git a/tests/neg/inline-option/Main_2.scala b/tests/neg/inline-option/Main_2.scala new file mode 100644 index 000000000000..7ae506282eba --- /dev/null +++ b/tests/neg/inline-option/Main_2.scala @@ -0,0 +1,15 @@ + +object Main { + val a: Int = 3 + size(Some(a)) // error + + val b: Option[Int] = Some(4) + size(b) // error + + inline def size(inline opt: Option[Int]): Int = ~Macro.impl(opt) + + inline def size2(inline i: Int): Int = ~Macro.impl(None) + + inline def size3(inline i: Int): Int = ~Macro.impl(Some(i)) + +} \ No newline at end of file diff --git a/tests/run/inline-option.check b/tests/run/inline-option.check new file mode 100644 index 000000000000..73ea67fc001e --- /dev/null +++ b/tests/run/inline-option.check @@ -0,0 +1,9 @@ +-1 +1 +2 +-1 +4 +5 +-1 +-1 +6 diff --git a/tests/run/inline-option/Macro_1.scala b/tests/run/inline-option/Macro_1.scala new file mode 100644 index 000000000000..6f278bcf5403 --- /dev/null +++ b/tests/run/inline-option/Macro_1.scala @@ -0,0 +1,13 @@ + +import scala.quoted._ + +object Macros { + + def impl(opt: Option[Int]): Expr[Int] = opt match { + case Some(i) => i.toExpr + case None => '(-1) + } + + def impl2(opt: Option[Option[Int]]): Expr[Int] = impl(opt.flatten) + +} diff --git a/tests/run/inline-option/Main_2.scala b/tests/run/inline-option/Main_2.scala new file mode 100644 index 000000000000..0050e2bdf89e --- /dev/null +++ b/tests/run/inline-option/Main_2.scala @@ -0,0 +1,28 @@ + +object Test { + + def main(args: Array[String]): Unit = { + println(size(None)) + println(size(Some(1))) + println(size(new Some(2))) + + println(size2(3)) + println(size3(4)) + println(size4(5)) + + println(size5(None)) + println(size5(Some(None))) + println(size5(Some(Some(6)))) + } + + inline def size(inline opt: Option[Int]): Int = ~Macros.impl(opt) + + inline def size2(inline i: Int): Int = ~Macros.impl(None) + + inline def size3(inline i: Int): Int = ~Macros.impl(Some(i)) + + inline def size4(inline i: Int): Int = ~Macros.impl2(Some(Some(i))) + + inline def size5(inline opt: Option[Option[Int]]): Int = ~Macros.impl2(opt) + +} From 3cfb4e994394a1029af85dbd31190a40173aa777 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 1 Oct 2018 21:11:30 +0200 Subject: [PATCH 2/2] Allow TupleN to be inline parameters --- .../src/dotty/tools/dotc/typer/Checking.scala | 23 +- .../test/dotc/run-test-pickling.blacklist | 2 + tests/neg/inline-tuples-1/Macro_1.scala | 27 ++ tests/neg/inline-tuples-1/Main_2.scala | 373 ++++++++++++++++++ tests/run/inline-tuples-1.check | 22 ++ tests/run/inline-tuples-1/Macro_1.scala | 27 ++ tests/run/inline-tuples-1/Main_2.scala | 52 +++ tests/run/inline-tuples-2.check | 5 + tests/run/inline-tuples-2/Macro_1.scala | 10 + tests/run/inline-tuples-2/Main_2.scala | 22 ++ 10 files changed, 559 insertions(+), 4 deletions(-) create mode 100644 tests/neg/inline-tuples-1/Macro_1.scala create mode 100644 tests/neg/inline-tuples-1/Main_2.scala create mode 100644 tests/run/inline-tuples-1.check create mode 100644 tests/run/inline-tuples-1/Macro_1.scala create mode 100644 tests/run/inline-tuples-1/Main_2.scala create mode 100644 tests/run/inline-tuples-2.check create mode 100644 tests/run/inline-tuples-2/Macro_1.scala create mode 100644 tests/run/inline-tuples-2/Main_2.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 42a48f09c451..5db1f1d04b4c 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -679,13 +679,28 @@ trait Checking { tree.tpe.widenTermRefExpr match { case tp: ConstantType if exprPurity(tree) >= purityLevel => // ok case tp => + // TODO: Make None and Some constant types? + def isCaseClassApply(sym: Symbol): Boolean = { + sym.name == nme.apply && ( + tree.symbol.owner == defn.SomeClass.companionModule.moduleClass || + defn.isTupleClass(tree.symbol.owner.companionClass) + ) + } + def isCaseClassNew(sym: Symbol): Boolean = { + sym.isPrimaryConstructor && ( + sym.eq(defn.SomeClass.primaryConstructor) || + defn.isTupleClass(tree.symbol.owner) + ) + } + def isCaseObject(sym: Symbol): Boolean = { + tree.symbol.eq(defn.NoneModuleRef.termSymbol) + } val allow = ctx.erasedTypes || ctx.inInlineMethod || - // TODO: Make None and Some constant types? - tree.symbol.eq(defn.NoneModuleRef.termSymbol) || - tree.symbol.eq(defn.SomeClass.primaryConstructor) || - (tree.symbol.name == nme.apply && tree.symbol.owner == defn.SomeClass.companionModule.moduleClass) + isCaseClassApply(tree.symbol) || + isCaseClassNew(tree.symbol) || + isCaseObject(tree.symbol) if (!allow) ctx.error(em"$what must be a constant expression", tree.pos) else tree match { // TODO: add cases for type apply and multiple applies diff --git a/compiler/test/dotc/run-test-pickling.blacklist b/compiler/test/dotc/run-test-pickling.blacklist index 6ccfdb431c33..7943a1db9d47 100644 --- a/compiler/test/dotc/run-test-pickling.blacklist +++ b/compiler/test/dotc/run-test-pickling.blacklist @@ -25,6 +25,8 @@ i5119b inline-varargs-1 implicitShortcut inline-option +inline-tuples-1 +inline-tuples-2 lazy-implicit-lists.scala lazy-implicit-nums.scala lazy-traits.scala diff --git a/tests/neg/inline-tuples-1/Macro_1.scala b/tests/neg/inline-tuples-1/Macro_1.scala new file mode 100644 index 000000000000..0d45729b963f --- /dev/null +++ b/tests/neg/inline-tuples-1/Macro_1.scala @@ -0,0 +1,27 @@ + +import scala.quoted._ + +object Macros { + def tup1(tup: Tuple1[Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup2(tup: Tuple2[Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup3(tup: Tuple3[Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup4(tup: Tuple4[Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup5(tup: Tuple5[Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup6(tup: Tuple6[Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup7(tup: Tuple7[Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup8(tup: Tuple8[Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup9(tup: Tuple9[Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup10(tup: Tuple10[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup11(tup: Tuple11[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup12(tup: Tuple12[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup13(tup: Tuple13[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup14(tup: Tuple14[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup15(tup: Tuple15[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup16(tup: Tuple16[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup17(tup: Tuple17[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup18(tup: Tuple18[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup19(tup: Tuple19[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup20(tup: Tuple20[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup21(tup: Tuple21[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup22(tup: Tuple22[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr +} diff --git a/tests/neg/inline-tuples-1/Main_2.scala b/tests/neg/inline-tuples-1/Main_2.scala new file mode 100644 index 000000000000..199c0fbdcbb6 --- /dev/null +++ b/tests/neg/inline-tuples-1/Main_2.scala @@ -0,0 +1,373 @@ + +object Test { + + def main(args: Array[String]): Unit = { + val t1 = Tuple1(1) + val t2 = Tuple2(1, 2) + val t3 = Tuple3(1, 2, 3) + val t4 = Tuple4(1, 2, 3, 4) + val t5 = Tuple5(1, 2, 3, 4, 5) + val t6 = Tuple6(1, 2, 3, 4, 5, 6) + val t7 = Tuple7(1, 2, 3, 4, 5, 6, 7) + val t8 = Tuple8(1, 2, 3, 4, 5, 6, 7, 8) + val t9 = Tuple9(1, 2, 3, 4, 5, 6, 7, 8, 9) + val t10 = Tuple10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + val t11 = Tuple11(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) + val t12 = Tuple12(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) + val t13 = Tuple13(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13) + val t14 = Tuple14(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) + val t15 = Tuple15(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15) + val t16 = Tuple16(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) + val t17 = Tuple17(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17) + val t18 = Tuple18(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) + val t19 = Tuple19(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19) + val t20 = Tuple20(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) + val t21 = Tuple21(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21) + val t22 = Tuple22(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22) + println(sum(t1)) // error + println(sum(t2)) // error + println(sum(t3)) // error + println(sum(t4)) // error + println(sum(t5)) // error + println(sum(t6)) // error + println(sum(t7)) // error + println(sum(t8)) // error + println(sum(t9)) // error + println(sum(t10)) // error + println(sum(t11)) // error + println(sum(t12)) // error + println(sum(t13)) // error + println(sum(t14)) // error + println(sum(t15)) // error + println(sum(t16)) // error + println(sum(t17)) // error + println(sum(t18)) // error + println(sum(t19)) // error + println(sum(t20)) // error + println(sum(t21)) // error + println(sum(t22)) // error + + val a: Int = 1 + println(sum(Tuple1( + a // error + ))) + println(sum(Tuple2( + a, // error + a // error + ))) + println(sum(Tuple3( + a, // error + a, // error + a // error + ))) + println(sum(Tuple4( + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple5( + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple6( + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple7( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple8( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple9( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple10( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple11( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple12( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple13( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple14( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple15( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple16( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple17( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple18( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple19( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple20( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple21( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + println(sum(Tuple22( + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a, // error + a // error + ))) + } + + inline def sum(inline tup: Tuple1[Int]): Int = ~Macros.tup1(tup) + inline def sum(inline tup: Tuple2[Int, Int]): Int = ~Macros.tup2(tup) + inline def sum(inline tup: Tuple3[Int, Int, Int]): Int = ~Macros.tup3(tup) + inline def sum(inline tup: Tuple4[Int, Int, Int, Int]): Int = ~Macros.tup4(tup) + inline def sum(inline tup: Tuple5[Int, Int, Int, Int, Int]): Int = ~Macros.tup5(tup) + inline def sum(inline tup: Tuple6[Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup6(tup) + inline def sum(inline tup: Tuple7[Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup7(tup) + inline def sum(inline tup: Tuple8[Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup8(tup) + inline def sum(inline tup: Tuple9[Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup9(tup) + inline def sum(inline tup: Tuple10[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup10(tup) + inline def sum(inline tup: Tuple11[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup11(tup) + inline def sum(inline tup: Tuple12[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup12(tup) + inline def sum(inline tup: Tuple13[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup13(tup) + inline def sum(inline tup: Tuple14[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup14(tup) + inline def sum(inline tup: Tuple15[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup15(tup) + inline def sum(inline tup: Tuple16[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup16(tup) + inline def sum(inline tup: Tuple17[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup17(tup) + inline def sum(inline tup: Tuple18[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup18(tup) + inline def sum(inline tup: Tuple19[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup19(tup) + inline def sum(inline tup: Tuple20[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup20(tup) + inline def sum(inline tup: Tuple21[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup21(tup) + inline def sum(inline tup: Tuple22[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup22(tup) + +} diff --git a/tests/run/inline-tuples-1.check b/tests/run/inline-tuples-1.check new file mode 100644 index 000000000000..277b4807807a --- /dev/null +++ b/tests/run/inline-tuples-1.check @@ -0,0 +1,22 @@ +1 +3 +6 +10 +15 +21 +28 +36 +45 +55 +66 +78 +91 +105 +120 +136 +153 +171 +190 +210 +231 +253 diff --git a/tests/run/inline-tuples-1/Macro_1.scala b/tests/run/inline-tuples-1/Macro_1.scala new file mode 100644 index 000000000000..0d45729b963f --- /dev/null +++ b/tests/run/inline-tuples-1/Macro_1.scala @@ -0,0 +1,27 @@ + +import scala.quoted._ + +object Macros { + def tup1(tup: Tuple1[Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup2(tup: Tuple2[Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup3(tup: Tuple3[Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup4(tup: Tuple4[Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup5(tup: Tuple5[Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup6(tup: Tuple6[Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup7(tup: Tuple7[Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup8(tup: Tuple8[Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup9(tup: Tuple9[Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup10(tup: Tuple10[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup11(tup: Tuple11[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup12(tup: Tuple12[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup13(tup: Tuple13[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup14(tup: Tuple14[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup15(tup: Tuple15[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup16(tup: Tuple16[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup17(tup: Tuple17[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup18(tup: Tuple18[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup19(tup: Tuple19[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup20(tup: Tuple20[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup21(tup: Tuple21[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr + def tup22(tup: Tuple22[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Expr[Int] = tup.productIterator.map(_.asInstanceOf[Int]).sum.toExpr +} diff --git a/tests/run/inline-tuples-1/Main_2.scala b/tests/run/inline-tuples-1/Main_2.scala new file mode 100644 index 000000000000..54971154d29e --- /dev/null +++ b/tests/run/inline-tuples-1/Main_2.scala @@ -0,0 +1,52 @@ + +object Test { + + def main(args: Array[String]): Unit = { + println(sum(Tuple1(1))) + println(sum(Tuple2(1, 2))) + println(sum(Tuple3(1, 2, 3))) + println(sum(Tuple4(1, 2, 3, 4))) + println(sum(Tuple5(1, 2, 3, 4, 5))) + println(sum(Tuple6(1, 2, 3, 4, 5, 6))) + println(sum(Tuple7(1, 2, 3, 4, 5, 6, 7))) + println(sum(Tuple8(1, 2, 3, 4, 5, 6, 7, 8))) + println(sum(Tuple9(1, 2, 3, 4, 5, 6, 7, 8, 9))) + println(sum(Tuple10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))) + println(sum(Tuple11(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11))) + println(sum(Tuple12(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))) + println(sum(Tuple13(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13))) + println(sum(Tuple14(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14))) + println(sum(Tuple15(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15))) + println(sum(Tuple16(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16))) + println(sum(Tuple17(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17))) + println(sum(Tuple18(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18))) + println(sum(Tuple19(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19))) + println(sum(Tuple20(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20))) + println(sum(Tuple21(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21))) + println(sum(Tuple22(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22))) + } + + inline def sum(inline tup: Tuple1[Int]): Int = ~Macros.tup1(tup) + inline def sum(inline tup: Tuple2[Int, Int]): Int = ~Macros.tup2(tup) + inline def sum(inline tup: Tuple3[Int, Int, Int]): Int = ~Macros.tup3(tup) + inline def sum(inline tup: Tuple4[Int, Int, Int, Int]): Int = ~Macros.tup4(tup) + inline def sum(inline tup: Tuple5[Int, Int, Int, Int, Int]): Int = ~Macros.tup5(tup) + inline def sum(inline tup: Tuple6[Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup6(tup) + inline def sum(inline tup: Tuple7[Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup7(tup) + inline def sum(inline tup: Tuple8[Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup8(tup) + inline def sum(inline tup: Tuple9[Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup9(tup) + inline def sum(inline tup: Tuple10[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup10(tup) + inline def sum(inline tup: Tuple11[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup11(tup) + inline def sum(inline tup: Tuple12[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup12(tup) + inline def sum(inline tup: Tuple13[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup13(tup) + inline def sum(inline tup: Tuple14[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup14(tup) + inline def sum(inline tup: Tuple15[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup15(tup) + inline def sum(inline tup: Tuple16[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup16(tup) + inline def sum(inline tup: Tuple17[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup17(tup) + inline def sum(inline tup: Tuple18[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup18(tup) + inline def sum(inline tup: Tuple19[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup19(tup) + inline def sum(inline tup: Tuple20[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup20(tup) + inline def sum(inline tup: Tuple21[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup21(tup) + inline def sum(inline tup: Tuple22[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]): Int = ~Macros.tup22(tup) + +} diff --git a/tests/run/inline-tuples-2.check b/tests/run/inline-tuples-2.check new file mode 100644 index 000000000000..de506527864a --- /dev/null +++ b/tests/run/inline-tuples-2.check @@ -0,0 +1,5 @@ +1 +2 +4 +5 +6 diff --git a/tests/run/inline-tuples-2/Macro_1.scala b/tests/run/inline-tuples-2/Macro_1.scala new file mode 100644 index 000000000000..e33f2022adc0 --- /dev/null +++ b/tests/run/inline-tuples-2/Macro_1.scala @@ -0,0 +1,10 @@ + +import scala.quoted._ + +object Macros { + + def impl(tup: Tuple1[Int]): Expr[Int] = tup._1.toExpr + + def impl2(tup: Tuple1[Tuple1[Int]]): Expr[Int] = impl(tup._1) + +} diff --git a/tests/run/inline-tuples-2/Main_2.scala b/tests/run/inline-tuples-2/Main_2.scala new file mode 100644 index 000000000000..d250d919f941 --- /dev/null +++ b/tests/run/inline-tuples-2/Main_2.scala @@ -0,0 +1,22 @@ + +object Test { + + def main(args: Array[String]): Unit = { + println(get1(Tuple1(1))) + println(get1(new Tuple1(2))) + + println(get2(4)) + println(get3(5)) + + println(get4(Tuple1(Tuple1(6)))) + } + + inline def get1(inline tup: Tuple1[Int]): Int = ~Macros.impl(tup) + + inline def get2(inline i: Int): Int = ~Macros.impl(Tuple1(i)) + + inline def get3(inline i: Int): Int = ~Macros.impl2(Tuple1(Tuple1(i))) + + inline def get4(inline tup: Tuple1[Tuple1[Int]]): Int = ~Macros.impl2(tup) + +}