From 22b72648dfe478e406d31fe2f9c80a86715f7fe7 Mon Sep 17 00:00:00 2001 From: Hamza REMMAL Date: Mon, 1 Jul 2024 14:27:33 +0200 Subject: [PATCH 1/3] Add tests for #17445 --- tests/pos-macros/i17445/Macro_1.scala | 21 +++++++++++++++++++++ tests/pos-macros/i17445/Test_2.scala | 9 +++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/pos-macros/i17445/Macro_1.scala create mode 100644 tests/pos-macros/i17445/Test_2.scala diff --git a/tests/pos-macros/i17445/Macro_1.scala b/tests/pos-macros/i17445/Macro_1.scala new file mode 100644 index 000000000000..5ce8bd41b731 --- /dev/null +++ b/tests/pos-macros/i17445/Macro_1.scala @@ -0,0 +1,21 @@ +package i17445 + +import scala.quoted.* +import scala.collection.* + + +object Macro { + + inline def changeIndexWhere[A](inline expr: A): A = + ${ changeIndexWhereImpl('expr) } + + def changeIndexWhereImpl[A: Type](expr: Expr[A])(using Quotes): Expr[A] = { + import quotes.reflect.* + val r0 = expr.asTerm + val checker = new TreeMap() {} + println(r0.getClass().getSimpleName()) + val r = checker.transformTerm(r0)(Symbol.spliceOwner) + r.asExprOf[A] + } + +} \ No newline at end of file diff --git a/tests/pos-macros/i17445/Test_2.scala b/tests/pos-macros/i17445/Test_2.scala new file mode 100644 index 000000000000..251549f889ba --- /dev/null +++ b/tests/pos-macros/i17445/Test_2.scala @@ -0,0 +1,9 @@ +//> using options -Ydebug +package i17445 + +def complileMe:Int = + Macro.changeIndexWhere { + val arr = Array(1, 2, 3) + val result = arr.indexWhere(_ == 2) + result + } \ No newline at end of file From 32b542f71726e84914be19463d9597f0a858d14c Mon Sep 17 00:00:00 2001 From: Hamza REMMAL Date: Fri, 2 Aug 2024 20:00:30 +0200 Subject: [PATCH 2/3] Add Select.copy with symbol in Quotes --- compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala | 3 +++ library/src/scala/quoted/Quotes.scala | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index 22be293c3562..9b8929d03560 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -542,6 +542,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler withDefaultPos(tpd.applyOverloaded(qualifier, name.toTermName, args, targs, returnType)) def copy(original: Tree)(qualifier: Term, name: String): Select = tpd.cpy.Select(original)(qualifier, name.toTermName) + + def copy(original: Tree)(qualifier: Term, sym: Symbol): Select = + tpd.cpy.Select(original)(qualifier, sym.name) def unapply(x: Select): (Term, String) = (x.qualifier, x.name.toString) end Select diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index 7a98d6f6f761..c721c86f2e30 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -996,6 +996,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => def copy(original: Tree)(qualifier: Term, name: String): Select + def copy(original: Tree)(qualifier: Term, sym: Symbol): Select + /** Matches `.` */ def unapply(x: Select): (Term, String) } @@ -5119,7 +5121,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => case Ident(name) => tree case Select(qualifier, name) => - Select.copy(tree)(transformTerm(qualifier)(owner), name) + Select.copy(tree)(transformTerm(qualifier)(owner), tree.symbol) case This(qual) => tree case Super(qual, mix) => From c6e06f12969039199eecea585a150ebc9ae8a318 Mon Sep 17 00:00:00 2001 From: Hamza Remmal Date: Tue, 19 Nov 2024 13:52:34 +0100 Subject: [PATCH 3/3] another way to highlight the problem --- compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala | 7 +++++-- library/src/scala/quoted/Quotes.scala | 4 +--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index 9b8929d03560..fec50cc9e0a9 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -540,11 +540,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler def overloaded(qualifier: Term, name: String, targs: List[TypeRepr], args: List[Term], returnType: TypeRepr): Term = withDefaultPos(tpd.applyOverloaded(qualifier, name.toTermName, args, targs, returnType)) + def copy(original: Tree)(qualifier: Term, name: String): Select = + val sym = original.symbol + if sym.name.is(NameKinds.DefaultGetterName) && sym.name.toString == name then + tpd.cpy.Select(original)(qualifier, sym.name) + else tpd.cpy.Select(original)(qualifier, name.toTermName) - def copy(original: Tree)(qualifier: Term, sym: Symbol): Select = - tpd.cpy.Select(original)(qualifier, sym.name) def unapply(x: Select): (Term, String) = (x.qualifier, x.name.toString) end Select diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index c721c86f2e30..7a98d6f6f761 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -996,8 +996,6 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => def copy(original: Tree)(qualifier: Term, name: String): Select - def copy(original: Tree)(qualifier: Term, sym: Symbol): Select - /** Matches `.` */ def unapply(x: Select): (Term, String) } @@ -5121,7 +5119,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => case Ident(name) => tree case Select(qualifier, name) => - Select.copy(tree)(transformTerm(qualifier)(owner), tree.symbol) + Select.copy(tree)(transformTerm(qualifier)(owner), name) case This(qual) => tree case Super(qual, mix) =>