From d1cb9deb7002bdaef988989aeed00ff5c9113286 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 26 Aug 2021 12:41:48 +0200 Subject: [PATCH] Address review comments - Move $throws to scala.runtime - Add comment --- compiler/src/dotty/tools/dotc/ast/Desugar.scala | 6 ++++-- compiler/src/dotty/tools/dotc/core/Definitions.scala | 1 + library/src-bootstrapped/scala/CanThrow.scala | 10 +--------- library/src-bootstrapped/scala/runtime/$throws.scala | 11 +++++++++++ 4 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 library/src-bootstrapped/scala/runtime/$throws.scala diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index c4761ec87c13..acd81f0ac7ae 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -1261,13 +1261,15 @@ object desugar { makeOp(left, right, Span(left.span.start, op.span.end, op.span.start)) } - /** Translate throws type `A throws E1 | ... | En` + /** Translate throws type `A throws E1 | ... | En` to + * $throws[... $throws[A, E1] ... , En]. */ def throws(tpt: Tree, op: Ident, excepts: Tree)(using Context): AppliedTypeTree = excepts match case InfixOp(l, bar @ Ident(tpnme.raw.BAR), r) => throws(throws(tpt, op, l), bar, r) case e => - AppliedTypeTree(cpy.Ident(op)(tpnme.THROWS), tpt :: excepts :: Nil) + AppliedTypeTree( + TypeTree(defn.throwsAlias.typeRef).withSpan(op.span), tpt :: excepts :: Nil) /** Translate tuple expressions of arity <= 22 * diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index e94681e7a5ef..886c9d9a5559 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -834,6 +834,7 @@ class Definitions { CanEqualClass.companionModule.requiredMethod(methodName) @tu lazy val CanThrowClass: ClassSymbol = requiredClass("scala.CanThrow") + @tu lazy val throwsAlias: Symbol = ScalaRuntimePackageVal.requiredType(tpnme.THROWS) @tu lazy val TypeBoxClass: ClassSymbol = requiredClass("scala.runtime.TypeBox") @tu lazy val TypeBox_CAP: TypeSymbol = TypeBoxClass.requiredType(tpnme.CAP) diff --git a/library/src-bootstrapped/scala/CanThrow.scala b/library/src-bootstrapped/scala/CanThrow.scala index ce518bff19ec..4da00b8a76ac 100644 --- a/library/src-bootstrapped/scala/CanThrow.scala +++ b/library/src-bootstrapped/scala/CanThrow.scala @@ -10,15 +10,7 @@ import annotation.{implicitNotFound, experimental} @implicitNotFound("The capability to throw exception ${E} is missing.\nThe capability can be provided by one of the following:\n - A using clause `(using CanThrow[${E}])`\n - A `throws` clause in a result type such as `X throws ${E}`\n - an enclosing `try` that catches ${E}") erased class CanThrow[-E <: Exception] -/** A helper type to allow syntax like - * - * def f(): T throws Ex1 | Ex2 - * - * Used in desugar.throws. - */ -@experimental -infix type $throws[R, +E <: Exception] = CanThrow[E] ?=> R - @experimental object unsafeExceptions: given canThrowAny: CanThrow[Exception] = ??? + diff --git a/library/src-bootstrapped/scala/runtime/$throws.scala b/library/src-bootstrapped/scala/runtime/$throws.scala new file mode 100644 index 000000000000..af35f7402ca3 --- /dev/null +++ b/library/src-bootstrapped/scala/runtime/$throws.scala @@ -0,0 +1,11 @@ +package scala.runtime +import annotation.experimental + +/** A helper type to allow syntax like + * + * def f(): T throws Ex1 | Ex2 + * + * Used in desugar.throws. + */ +@experimental +infix type $throws[R, +E <: Exception] = CanThrow[E] ?=> R