diff --git a/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala b/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala index 9e65455d052e..bb314a573d87 100644 --- a/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala +++ b/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala @@ -866,16 +866,18 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext: end ReturnTypeTest object Return extends ReturnModule: - def apply(expr: Term): Return = - withDefaultPos(tpd.Return(expr, ctx.owner)) - def copy(original: Tree)(expr: Term): Return = - tpd.cpy.Return(original)(expr, tpd.ref(ctx.owner)) - def unapply(x: Return): Option[Term] = Some(x.expr) + def apply(expr: Term, from: Symbol): Return = + withDefaultPos(tpd.Return(expr, from)) + def copy(original: Tree)(expr: Term, from: Symbol): Return = + tpd.cpy.Return(original)(expr, tpd.ref(from)) + def unapply(x: Return): Option[(Term, Symbol)] = + Some((x.expr, x.from.symbol)) end Return object ReturnMethodsImpl extends ReturnMethods: extension (self: Return): def expr: Term = self.expr + def from: Symbol = self.from.symbol end extension end ReturnMethodsImpl diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index f6dc337ce18f..5274f5cc3af4 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -1021,12 +1021,12 @@ trait Reflection { reflection => trait ReturnModule { this: Return.type => /** Creates `return ` */ - def apply(expr: Term): Return + def apply(expr: Term, from: Symbol): Return - def copy(original: Tree)(expr: Term): Return + def copy(original: Tree)(expr: Term, from: Symbol): Return - /** Matches `return ` */ - def unapply(x: Return): Option[Term] + /** Matches `return ` and extracts the expression and symbol of the method */ + def unapply(x: Return): Option[(Term, Symbol)] } given ReturnMethods as ReturnMethods = ReturnMethodsImpl @@ -1035,6 +1035,7 @@ trait Reflection { reflection => trait ReturnMethods: extension (self: Return): def expr: Term + def from: Symbol end extension end ReturnMethods diff --git a/library/src/scala/tasty/reflect/ExtractorsPrinter.scala b/library/src/scala/tasty/reflect/ExtractorsPrinter.scala index b83cb0d43e3c..222e53d90927 100644 --- a/library/src/scala/tasty/reflect/ExtractorsPrinter.scala +++ b/library/src/scala/tasty/reflect/ExtractorsPrinter.scala @@ -94,8 +94,8 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val reflect: R) extends Pri this += "Match(" += selector += ", " ++= cases += ")" case GivenMatch(cases) => this += "GivenMatch(" ++= cases += ")" - case Return(expr) => - this += "Return(" += expr += ")" + case Return(expr, from) => + this += "Return(" += expr += ", " += from += ")" case While(cond, body) => this += "While(" += cond += ", " += body += ")" case Try(block, handlers, finalizer) => diff --git a/library/src/scala/tasty/reflect/SourceCodePrinter.scala b/library/src/scala/tasty/reflect/SourceCodePrinter.scala index 078bc21c604f..518d5d5e4bec 100644 --- a/library/src/scala/tasty/reflect/SourceCodePrinter.scala +++ b/library/src/scala/tasty/reflect/SourceCodePrinter.scala @@ -505,7 +505,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val reflect: R)(syntaxHighl this } - case Return(expr) => + case Return(expr, from) => this += "return " printTree(expr) diff --git a/library/src/scala/tasty/reflect/TreeAccumulator.scala b/library/src/scala/tasty/reflect/TreeAccumulator.scala index ab79995f835c..ec2a509b658d 100644 --- a/library/src/scala/tasty/reflect/TreeAccumulator.scala +++ b/library/src/scala/tasty/reflect/TreeAccumulator.scala @@ -57,7 +57,7 @@ trait TreeAccumulator[X] { foldTree(x, meth) case Match(selector, cases) => foldTrees(foldTree(x, selector), cases) - case Return(expr) => + case Return(expr, _) => foldTree(x, expr) case Try(block, handler, finalizer) => foldTrees(foldTrees(foldTree(x, block), handler), finalizer) diff --git a/library/src/scala/tasty/reflect/TreeMap.scala b/library/src/scala/tasty/reflect/TreeMap.scala index bd33acdfe2cf..50db1f5fd5d7 100644 --- a/library/src/scala/tasty/reflect/TreeMap.scala +++ b/library/src/scala/tasty/reflect/TreeMap.scala @@ -99,8 +99,8 @@ trait TreeMap { Closure.copy(tree)(transformTerm(meth), tpt) case Match(selector, cases) => Match.copy(tree)(transformTerm(selector), transformCaseDefs(cases)) - case Return(expr) => - Return.copy(tree)(transformTerm(expr)) + case Return(expr, from) => + Return.copy(tree)(transformTerm(expr), from) case While(cond, body) => While.copy(tree)(transformTerm(cond), transformTerm(body)) case Try(block, cases, finalizer) => diff --git a/tests/run-macros/tasty-extractors-1.check b/tests/run-macros/tasty-extractors-1.check index 4f6d55b9e07e..2c11ae106d01 100644 --- a/tests/run-macros/tasty-extractors-1.check +++ b/tests/run-macros/tasty-extractors-1.check @@ -94,7 +94,7 @@ TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit") Inlined(None, Nil, Block(List(DefDef("f1", Nil, Nil, TypeIdent("Int"), Some(Literal(Constant.Int(3))))), Literal(Constant.Unit()))) TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit") -Inlined(None, Nil, Block(List(DefDef("f2", Nil, Nil, TypeIdent("Int"), Some(Return(Literal(Constant.Int(4)))))), Literal(Constant.Unit()))) +Inlined(None, Nil, Block(List(DefDef("f2", Nil, Nil, TypeIdent("Int"), Some(Return(Literal(Constant.Int(4)), IsDefDefSymbol())))), Literal(Constant.Unit()))) TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit") Inlined(None, Nil, Block(List(DefDef("f3", Nil, List(List(ValDef("i", TypeIdent("Int"), None))), TypeIdent("Int"), Some(Ident("i")))), Literal(Constant.Unit())))