diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala index c2bb1173aced..37d0ac38da19 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala @@ -1883,10 +1883,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend def QuotedType_unseal(self: scala.quoted.Type[?])(using ctx: Context): TypeTree = PickledQuotes.quotedTypeToTree(self) - /** Convert `Term` to an `quoted.Expr[Any]` */ - def QuotedExpr_seal(self: Term)(using ctx: Context): scala.quoted.Expr[Any] = self.tpe.widen match { - case _: Types.MethodType | _: Types.PolyType => throw new Exception("Cannot seal a partially applied Term. Try eta-expanding the term first.") - case _ => new TastyTreeExpr(self, compilerId) + def QuotedExpr_seal(self: Term)(using ctx: Context): Option[scala.quoted.Expr[Any]] = self.tpe.widen match { + case _: Types.MethodType | _: Types.PolyType => None + case _ => Some(new TastyTreeExpr(self, compilerId)) } /** Checked cast to a `quoted.Expr[U]` */ diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index d3f6e63719b7..238c38534ecc 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -619,13 +619,26 @@ class Reflection(private[scala] val internal: CompilerInterface) { self => extension TermOps on (self: Term) { - /** Convert `Term` to an `quoted.Expr[Any]` */ + /** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression or throws */ def seal(using ctx: Context): scala.quoted.Expr[Any] = + internal.QuotedExpr_seal(self).getOrElse { + throw new Exception("Cannot seal a partially applied Term. Try eta-expanding the term first.") + } + + /** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression */ + def sealOpt(using ctx: Context): Option[scala.quoted.Expr[Any]] = internal.QuotedExpr_seal(self) + /** Type of this term */ def tpe(using ctx: Context): Type = internal.Term_tpe(self) + + /** Replace Inlined nodes and InlineProxy references to underlying arguments */ def underlyingArgument(using ctx: Context): Term = internal.Term_underlyingArgument(self) + + /** Replace Ident nodes references to the underlying tree that defined them */ def underlying(using ctx: Context): Term = internal.Term_underlying(self) + + /** Converts a partally applied term into a lambda expression */ def etaExpand(using ctx: Context): Term = internal.Term_etaExpand(self) /** A unary apply node with given argument: `tree(arg)` */ diff --git a/library/src/scala/tasty/reflect/CompilerInterface.scala b/library/src/scala/tasty/reflect/CompilerInterface.scala index 55ac971e4ffb..459746698399 100644 --- a/library/src/scala/tasty/reflect/CompilerInterface.scala +++ b/library/src/scala/tasty/reflect/CompilerInterface.scala @@ -1434,9 +1434,8 @@ trait CompilerInterface { /** View this expression `quoted.Type[T]` as a `TypeTree` */ def QuotedType_unseal(self: scala.quoted.Type[_])(using ctx: Context): TypeTree - /** Convert `Term` to an `quoted.Expr[Any]` */ - def QuotedExpr_seal(self: Term)(using ctx: Context): scala.quoted.Expr[Any] - + /** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression */ + def QuotedExpr_seal(self: Term)(using ctx: Context): Option[scala.quoted.Expr[Any]] /** Convert `Type` to an `quoted.Type[_]` */ def QuotedType_seal(self: Type)(using ctx: Context): scala.quoted.Type[_]