Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
 - Move $throws to scala.runtime
 - Add comment
  • Loading branch information
odersky committed Aug 26, 2021
1 parent af9b940 commit d1cb9de
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 1 addition & 9 deletions library/src-bootstrapped/scala/CanThrow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] = ???

11 changes: 11 additions & 0 deletions library/src-bootstrapped/scala/runtime/$throws.scala
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d1cb9de

Please sign in to comment.