Skip to content

Remove unsafe use of ctx.owner in Reflection #10160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1021,12 +1021,12 @@ trait Reflection { reflection =>
trait ReturnModule { this: Return.type =>

/** Creates `return <expr: Term>` */
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 <expr: Term>` */
def unapply(x: Return): Option[Term]
/** Matches `return <expr: Term>` and extracts the expression and symbol of the method */
def unapply(x: Return): Option[(Term, Symbol)]
}

given ReturnMethods as ReturnMethods = ReturnMethodsImpl
Expand All @@ -1035,6 +1035,7 @@ trait Reflection { reflection =>
trait ReturnMethods:
extension (self: Return):
def expr: Term
def from: Symbol
end extension
end ReturnMethods

Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/tasty/reflect/ExtractorsPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/reflect/SourceCodePrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/reflect/TreeAccumulator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/tasty/reflect/TreeMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/tasty-extractors-1.check
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Test$._$_$f2>))))), 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())))
Expand Down