Skip to content

Commit 8bb8575

Browse files
Merge pull request #10160 from dotty-staging/remove-unsafe-uses-of-ctx.owner
Remove unsafe use of ctx.owner in Reflection
2 parents f36c1f2 + 84a55e3 commit 8bb8575

File tree

7 files changed

+19
-16
lines changed

7 files changed

+19
-16
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -866,16 +866,18 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
866866
end ReturnTypeTest
867867

868868
object Return extends ReturnModule:
869-
def apply(expr: Term): Return =
870-
withDefaultPos(tpd.Return(expr, ctx.owner))
871-
def copy(original: Tree)(expr: Term): Return =
872-
tpd.cpy.Return(original)(expr, tpd.ref(ctx.owner))
873-
def unapply(x: Return): Option[Term] = Some(x.expr)
869+
def apply(expr: Term, from: Symbol): Return =
870+
withDefaultPos(tpd.Return(expr, from))
871+
def copy(original: Tree)(expr: Term, from: Symbol): Return =
872+
tpd.cpy.Return(original)(expr, tpd.ref(from))
873+
def unapply(x: Return): Option[(Term, Symbol)] =
874+
Some((x.expr, x.from.symbol))
874875
end Return
875876

876877
object ReturnMethodsImpl extends ReturnMethods:
877878
extension (self: Return):
878879
def expr: Term = self.expr
880+
def from: Symbol = self.from.symbol
879881
end extension
880882
end ReturnMethodsImpl
881883

library/src/scala/tasty/Reflection.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,12 +1021,12 @@ trait Reflection { reflection =>
10211021
trait ReturnModule { this: Return.type =>
10221022

10231023
/** Creates `return <expr: Term>` */
1024-
def apply(expr: Term): Return
1024+
def apply(expr: Term, from: Symbol): Return
10251025

1026-
def copy(original: Tree)(expr: Term): Return
1026+
def copy(original: Tree)(expr: Term, from: Symbol): Return
10271027

1028-
/** Matches `return <expr: Term>` */
1029-
def unapply(x: Return): Option[Term]
1028+
/** Matches `return <expr: Term>` and extracts the expression and symbol of the method */
1029+
def unapply(x: Return): Option[(Term, Symbol)]
10301030
}
10311031

10321032
given ReturnMethods as ReturnMethods = ReturnMethodsImpl
@@ -1035,6 +1035,7 @@ trait Reflection { reflection =>
10351035
trait ReturnMethods:
10361036
extension (self: Return):
10371037
def expr: Term
1038+
def from: Symbol
10381039
end extension
10391040
end ReturnMethods
10401041

library/src/scala/tasty/reflect/ExtractorsPrinter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val reflect: R) extends Pri
9494
this += "Match(" += selector += ", " ++= cases += ")"
9595
case GivenMatch(cases) =>
9696
this += "GivenMatch(" ++= cases += ")"
97-
case Return(expr) =>
98-
this += "Return(" += expr += ")"
97+
case Return(expr, from) =>
98+
this += "Return(" += expr += ", " += from += ")"
9999
case While(cond, body) =>
100100
this += "While(" += cond += ", " += body += ")"
101101
case Try(block, handlers, finalizer) =>

library/src/scala/tasty/reflect/SourceCodePrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val reflect: R)(syntaxHighl
505505
this
506506
}
507507

508-
case Return(expr) =>
508+
case Return(expr, from) =>
509509
this += "return "
510510
printTree(expr)
511511

library/src/scala/tasty/reflect/TreeAccumulator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ trait TreeAccumulator[X] {
5757
foldTree(x, meth)
5858
case Match(selector, cases) =>
5959
foldTrees(foldTree(x, selector), cases)
60-
case Return(expr) =>
60+
case Return(expr, _) =>
6161
foldTree(x, expr)
6262
case Try(block, handler, finalizer) =>
6363
foldTrees(foldTrees(foldTree(x, block), handler), finalizer)

library/src/scala/tasty/reflect/TreeMap.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ trait TreeMap {
9999
Closure.copy(tree)(transformTerm(meth), tpt)
100100
case Match(selector, cases) =>
101101
Match.copy(tree)(transformTerm(selector), transformCaseDefs(cases))
102-
case Return(expr) =>
103-
Return.copy(tree)(transformTerm(expr))
102+
case Return(expr, from) =>
103+
Return.copy(tree)(transformTerm(expr), from)
104104
case While(cond, body) =>
105105
While.copy(tree)(transformTerm(cond), transformTerm(body))
106106
case Try(block, cases, finalizer) =>

tests/run-macros/tasty-extractors-1.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")
9494
Inlined(None, Nil, Block(List(DefDef("f1", Nil, Nil, TypeIdent("Int"), Some(Literal(Constant.Int(3))))), Literal(Constant.Unit())))
9595
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")
9696

97-
Inlined(None, Nil, Block(List(DefDef("f2", Nil, Nil, TypeIdent("Int"), Some(Return(Literal(Constant.Int(4)))))), Literal(Constant.Unit())))
97+
Inlined(None, Nil, Block(List(DefDef("f2", Nil, Nil, TypeIdent("Int"), Some(Return(Literal(Constant.Int(4)), IsDefDefSymbol(<Test$._$_$f2>))))), Literal(Constant.Unit())))
9898
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")
9999

100100
Inlined(None, Nil, Block(List(DefDef("f3", Nil, List(List(ValDef("i", TypeIdent("Int"), None))), TypeIdent("Int"), Some(Ident("i")))), Literal(Constant.Unit())))

0 commit comments

Comments
 (0)