Skip to content

Commit e85958e

Browse files
Merge pull request #8958 from dotty-staging/add-missing-optional-seal
Add documentation and missing optional seal
2 parents 492c100 + e991313 commit e85958e

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,10 +1895,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
18951895
def QuotedType_unseal(self: scala.quoted.Type[?])(using ctx: Context): TypeTree =
18961896
PickledQuotes.quotedTypeToTree(self)
18971897

1898-
/** Convert `Term` to an `quoted.Expr[Any]` */
1899-
def QuotedExpr_seal(self: Term)(using ctx: Context): scala.quoted.Expr[Any] = self.tpe.widen match {
1900-
case _: Types.MethodType | _: Types.PolyType => throw new Exception("Cannot seal a partially applied Term. Try eta-expanding the term first.")
1901-
case _ => new TastyTreeExpr(self, compilerId)
1898+
def QuotedExpr_seal(self: Term)(using ctx: Context): Option[scala.quoted.Expr[Any]] = self.tpe.widen match {
1899+
case _: Types.MethodType | _: Types.PolyType => None
1900+
case _ => Some(new TastyTreeExpr(self, compilerId))
19021901
}
19031902

19041903
/** Checked cast to a `quoted.Expr[U]` */

library/src/scala/tasty/Reflection.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,13 +619,26 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
619619

620620
extension TermOps on (self: Term) {
621621

622-
/** Convert `Term` to an `quoted.Expr[Any]` */
622+
/** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression or throws */
623623
def seal(using ctx: Context): scala.quoted.Expr[Any] =
624+
internal.QuotedExpr_seal(self).getOrElse {
625+
throw new Exception("Cannot seal a partially applied Term. Try eta-expanding the term first.")
626+
}
627+
628+
/** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression */
629+
def sealOpt(using ctx: Context): Option[scala.quoted.Expr[Any]] =
624630
internal.QuotedExpr_seal(self)
625631

632+
/** Type of this term */
626633
def tpe(using ctx: Context): Type = internal.Term_tpe(self)
634+
635+
/** Replace Inlined nodes and InlineProxy references to underlying arguments */
627636
def underlyingArgument(using ctx: Context): Term = internal.Term_underlyingArgument(self)
637+
638+
/** Replace Ident nodes references to the underlying tree that defined them */
628639
def underlying(using ctx: Context): Term = internal.Term_underlying(self)
640+
641+
/** Converts a partally applied term into a lambda expression */
629642
def etaExpand(using ctx: Context): Term = internal.Term_etaExpand(self)
630643

631644
/** A unary apply node with given argument: `tree(arg)` */

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,9 +1445,8 @@ trait CompilerInterface {
14451445
/** View this expression `quoted.Type[T]` as a `TypeTree` */
14461446
def QuotedType_unseal(self: scala.quoted.Type[_])(using ctx: Context): TypeTree
14471447

1448-
/** Convert `Term` to an `quoted.Expr[Any]` */
1449-
def QuotedExpr_seal(self: Term)(using ctx: Context): scala.quoted.Expr[Any]
1450-
1448+
/** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression */
1449+
def QuotedExpr_seal(self: Term)(using ctx: Context): Option[scala.quoted.Expr[Any]]
14511450

14521451
/** Convert `Type` to an `quoted.Type[_]` */
14531452
def QuotedType_seal(self: Type)(using ctx: Context): scala.quoted.Type[_]

0 commit comments

Comments
 (0)