Skip to content

Commit 5db3b72

Browse files
committed
Rename lambda to closure
1 parent e59cd6c commit 5db3b72

File tree

7 files changed

+66
-43
lines changed

7 files changed

+66
-43
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,21 +506,21 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
506506
def Inlined_copy(original: Tree)(call: Option[Term | TypeTree], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined =
507507
tpd.cpy.Inlined(original)(call.getOrElse(tpd.EmptyTree), bindings.asInstanceOf[List[tpd.MemberDef]], expansion)
508508

509-
type Lambda = tpd.Closure
509+
type Closure = tpd.Closure
510510

511-
def matchLambda(x: Term)(implicit ctx: Context): Option[Lambda] = x match {
511+
def matchClosure(x: Term)(implicit ctx: Context): Option[Closure] = x match {
512512
case x: tpd.Closure => Some(x)
513513
case _ => None
514514
}
515515

516-
def Lambda_meth(self: Lambda)(implicit ctx: Context): Term = self.meth
517-
def Lambda_tptOpt(self: Lambda)(implicit ctx: Context): Option[TypeTree] = optional(self.tpt)
516+
def Closure_meth(self: Closure)(implicit ctx: Context): Term = self.meth
517+
def Closure_tpeOpt(self: Closure)(implicit ctx: Context): Option[Type] = optional(self.tpt).map(_.tpe)
518518

519-
def Lambda_apply(meth: Term, tpt: Option[TypeTree])(implicit ctx: Context): Lambda =
520-
withDefaultPos(ctx => tpd.Closure(Nil, meth, tpt.getOrElse(tpd.EmptyTree))(ctx))
519+
def Closure_apply(meth: Term, tpe: Option[Type])(implicit ctx: Context): Closure =
520+
withDefaultPos(ctx => tpd.Closure(Nil, meth, tpe.map(tpd.TypeTree(_)).getOrElse(tpd.EmptyTree))(ctx))
521521

522-
def Lambda_copy(original: Tree)(meth: Tree, tpt: Option[TypeTree])(implicit ctx: Context): Lambda =
523-
tpd.cpy.Closure(original)(Nil, meth, tpt.getOrElse(tpd.EmptyTree))
522+
def Closure_copy(original: Tree)(meth: Tree, tpe: Option[Type])(implicit ctx: Context): Closure =
523+
tpd.cpy.Closure(original)(Nil, meth, tpe.map(tpd.TypeTree(_)).getOrElse(tpd.EmptyTree))
524524

525525
type If = tpd.If
526526

library/src-bootstrapped/scala/internal/quoted/Matcher.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ object Matcher {
207207
tpt1 =#= tpt2 &&
208208
withEnv(rhsEnv)(rhs1 =#= rhs2)
209209

210-
case (Lambda(_, tpt1), Lambda(_, tpt2)) =>
210+
case (Closure(_, tpt1), Closure(_, tpt2)) =>
211211
// TODO match tpt1 with tpt2?
212212
matched
213213

library/src/scala/tasty/reflect/Core.scala

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ package scala.tasty.reflect
2626
* | +- Typed
2727
* | +- Assign
2828
* | +- Block
29-
* | +- Lambda
29+
* | +- Closure
3030
* | +- If
3131
* | +- Match
3232
* | +- ImpliedMatch
@@ -200,8 +200,19 @@ trait Core {
200200
/** Tree representing a block `{ ... }` in the source code */
201201
type Block = kernel.Block
202202

203-
/** Tree representing a lambda `(...) => ...` in the source code */
204-
type Lambda = kernel.Lambda
203+
/** A lambda `(...) => ...` in the source code is represented as
204+
* a local method and a closure:
205+
*
206+
* {
207+
* def m(...) = ...
208+
* closure(m)
209+
* }
210+
*
211+
* While a function literal is usually a block with the two parts,
212+
* in the Dotty compiler, the two parts may locate in a flattened
213+
* block and may not be consecutive.
214+
*/
215+
type Closure = kernel.Closure
205216

206217
/** Tree representing an if/then/else `if (...) ... else ...` in the source code */
207218
type If = kernel.If

library/src/scala/tasty/reflect/Kernel.scala

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ package scala.tasty.reflect
2525
* | +- Typed
2626
* | +- Assign
2727
* | +- Block
28-
* | +- Lambda
28+
* | +- Closure
2929
* | +- If
3030
* | +- Match
3131
* | +- ImpliedMatch
@@ -436,16 +436,27 @@ trait Kernel {
436436
def Block_apply(stats: List[Statement], expr: Term)(implicit ctx: Context): Block
437437
def Block_copy(original: Tree)(stats: List[Statement], expr: Term)(implicit ctx: Context): Block
438438

439-
/** Tree representing a lambda `(...) => ...` in the source code */
440-
type Lambda <: Term
439+
/** A lambda `(...) => ...` in the source code is represented as
440+
* a local method and a closure:
441+
*
442+
* {
443+
* def m(...) = ...
444+
* closure(m)
445+
* }
446+
*
447+
* While a function literal is usually a block with the two parts,
448+
* in the Dotty compiler, the two parts may locate in a flattened
449+
* block and may not be consecutive.
450+
*/
451+
type Closure <: Term
441452

442-
def matchLambda(tree: Tree)(implicit ctx: Context): Option[Lambda]
453+
def matchClosure(tree: Tree)(implicit ctx: Context): Option[Closure]
443454

444-
def Lambda_meth(self: Lambda)(implicit ctx: Context): Term
445-
def Lambda_tptOpt(self: Lambda)(implicit ctx: Context): Option[TypeTree]
455+
def Closure_meth(self: Closure)(implicit ctx: Context): Term
456+
def Closure_tpeOpt(self: Closure)(implicit ctx: Context): Option[Type]
446457

447-
def Lambda_apply(meth: Term, tpt: Option[TypeTree])(implicit ctx: Context): Lambda
448-
def Lambda_copy(original: Tree)(meth: Tree, tpt: Option[TypeTree])(implicit ctx: Context): Lambda
458+
def Closure_apply(meth: Term, tpe: Option[Type])(implicit ctx: Context): Closure
459+
def Closure_copy(original: Tree)(meth: Tree, tpe: Option[Type])(implicit ctx: Context): Closure
449460

450461
/** Tree representing an if/then/else `if (...) ... else ...` in the source code */
451462
type If <: Term

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ trait Printers
201201
this += "Block(" ++= stats += ", " += expr += ")"
202202
case If(cond, thenp, elsep) =>
203203
this += "If(" += cond += ", " += thenp += ", " += elsep += ")"
204-
case Lambda(meth, tpt) =>
205-
this += "Lambda(" += meth += ", " += tpt += ")"
204+
case Closure(meth, tpt) =>
205+
this += "Closure(" += meth += ", " += tpt += ")"
206206
case Match(selector, cases) =>
207207
this += "Match(" += selector += ", " ++= cases += ")"
208208
case ImpliedMatch(cases) =>
@@ -406,6 +406,7 @@ trait Printers
406406

407407
private implicit class TypeOps(buff: Buffer) {
408408
def +=(x: TypeOrBounds): Buffer = { visitType(x); buff }
409+
def +=(x: Option[TypeOrBounds]): Buffer = { visitOption(x, visitType); buff }
409410
def ++=(x: List[TypeOrBounds]): Buffer = { visitList(x, visitType); buff }
410411
}
411412

@@ -911,7 +912,7 @@ trait Printers
911912
case Inlined(_, bindings, expansion) =>
912913
printFlatBlock(bindings, expansion)
913914

914-
case Lambda(meth, tpt) =>
915+
case Closure(meth, tpt) =>
915916
// Printed in by it's DefDef
916917
this
917918

@@ -1019,11 +1020,12 @@ trait Printers
10191020
val (stats1, expr1) = flatBlock(stats, expr)
10201021
// Remove Lambda nodes, lambdas are printed by their definition
10211022
val stats2 = stats1.filter {
1022-
case Lambda(_, _) => false
1023+
case Closure(_, _) => false
1024+
case IsTypeDef(tree) => !tree.symbol.annots.exists(_.symbol.owner.fullName == "scala.internal.Quoted$.quoteTypeTag")
10231025
case _ => true
10241026
}
10251027
val (stats3, expr3) = expr1 match {
1026-
case Lambda(_, _) =>
1028+
case Closure(_, _) =>
10271029
val init :+ last = stats2
10281030
(init, last)
10291031
case _ => (stats2, expr1)

library/src/scala/tasty/reflect/TreeOps.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -552,26 +552,26 @@ trait TreeOps extends Core {
552552
def expr(implicit ctx: Context): Term = kernel.Block_expr(self)
553553
}
554554

555-
object IsLambda {
556-
/** Matches any Lambda and returns it */
557-
def unapply(tree: Tree)(implicit ctx: Context): Option[Lambda] = kernel.matchLambda(tree)
555+
object IsClosure {
556+
/** Matches any Closure and returns it */
557+
def unapply(tree: Tree)(implicit ctx: Context): Option[Closure] = kernel.matchClosure(tree)
558558
}
559559

560-
object Lambda {
560+
object Closure {
561561

562-
def apply(meth: Term, tpt: Option[TypeTree])(implicit ctx: Context): Lambda =
563-
kernel.Lambda_apply(meth, tpt)
562+
def apply(meth: Term, tpt: Option[Type])(implicit ctx: Context): Closure =
563+
kernel.Closure_apply(meth, tpt)
564564

565-
def copy(original: Tree)(meth: Tree, tpt: Option[TypeTree])(implicit ctx: Context): Lambda =
566-
kernel.Lambda_copy(original)(meth, tpt)
565+
def copy(original: Tree)(meth: Tree, tpt: Option[Type])(implicit ctx: Context): Closure =
566+
kernel.Closure_copy(original)(meth, tpt)
567567

568-
def unapply(tree: Tree)(implicit ctx: Context): Option[(Term, Option[TypeTree])] =
569-
kernel.matchLambda(tree).map(x => (x.meth, x.tptOpt))
568+
def unapply(tree: Tree)(implicit ctx: Context): Option[(Term, Option[Type])] =
569+
kernel.matchClosure(tree).map(x => (x.meth, x.tpeOpt))
570570
}
571571

572-
implicit class LambdaAPI(self: Lambda) {
573-
def meth(implicit ctx: Context): Term = kernel.Lambda_meth(self)
574-
def tptOpt(implicit ctx: Context): Option[TypeTree] = kernel.Lambda_tptOpt(self)
572+
implicit class ClosureAPI(self: Closure) {
573+
def meth(implicit ctx: Context): Term = kernel.Closure_meth(self)
574+
def tpeOpt(implicit ctx: Context): Option[Type] = kernel.Closure_tpeOpt(self)
575575
}
576576

577577
object IsIf {

library/src/scala/tasty/reflect/TreeUtils.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ trait TreeUtils
4848
foldTree(foldTree(foldTree(x, cond), thenp), elsep)
4949
case While(cond, body) =>
5050
foldTree(foldTree(x, cond), body)
51-
case Lambda(meth, tpt) =>
52-
val a = foldTree(x, meth)
53-
tpt.fold(a)(b => foldTree(a, b))
51+
case Closure(meth, tpt) =>
52+
foldTree(x, meth)
5453
case Match(selector, cases) =>
5554
foldTrees(foldTree(x, selector), cases)
5655
case Return(expr) =>
@@ -193,8 +192,8 @@ trait TreeUtils
193192
Block.copy(tree)(transformStats(stats), transformTerm(expr))
194193
case If(cond, thenp, elsep) =>
195194
If.copy(tree)(transformTerm(cond), transformTerm(thenp), transformTerm(elsep))
196-
case Lambda(meth, tpt) =>
197-
Lambda.copy(tree)(transformTerm(meth), tpt.map(x => transformTypeTree(x)))
195+
case Closure(meth, tpt) =>
196+
Closure.copy(tree)(transformTerm(meth), tpt)
198197
case Match(selector, cases) =>
199198
Match.copy(tree)(transformTerm(selector), transformCaseDefs(cases))
200199
case Return(expr) =>

0 commit comments

Comments
 (0)