From 2cc44881f746fe0be039ffc6e4f1a681d96403e2 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 28 Apr 2021 13:03:48 +0200 Subject: [PATCH] Add regression test Closes #12253 --- tests/pos-macros/i12253/Macro_1.scala | 24 ++++++++++++++++++++++++ tests/pos-macros/i12253/Test_2.scala | 4 ++++ 2 files changed, 28 insertions(+) create mode 100644 tests/pos-macros/i12253/Macro_1.scala create mode 100644 tests/pos-macros/i12253/Test_2.scala diff --git a/tests/pos-macros/i12253/Macro_1.scala b/tests/pos-macros/i12253/Macro_1.scala new file mode 100644 index 000000000000..43eb73542c33 --- /dev/null +++ b/tests/pos-macros/i12253/Macro_1.scala @@ -0,0 +1,24 @@ + +import scala.quoted.* +import deriving.*, compiletime.* + +object MacroUtils: + transparent inline def extractNameFromSelector[To, T](inline code: To => T) = ${extractNameFromSelectorImpl('code)} + + def extractNameFromSelectorImpl[To: Type, T: Type](code: Expr[To => T])(using Quotes): Expr[String] = + import quotes.reflect.* + code.asTerm match + case InlinedLambda(_, Select(_, name)) => Expr(name) + case t => Expr("") + + object InlinedLambda: + def unapply(using Quotes)(arg: quotes.reflect.Term): Option[(List[quotes.reflect.ValDef], quotes.reflect.Term)] = + import quotes.reflect.* + arg match + case Inlined(_, _, Lambda(vals, term)) => Some((vals, term)) + case Inlined(_, _, nested) => InlinedLambda.unapply(nested) + case t => None + end InlinedLambda + +end MacroUtils + diff --git a/tests/pos-macros/i12253/Test_2.scala b/tests/pos-macros/i12253/Test_2.scala new file mode 100644 index 000000000000..61aae41d1ee0 --- /dev/null +++ b/tests/pos-macros/i12253/Test_2.scala @@ -0,0 +1,4 @@ +object Usage: + case class Bar(x: Int, y: String, z: (Double, Double)) + MacroUtils.extractNameFromSelector[Bar, String](_.y + "abc") +end Usage