Skip to content

Commit b65958f

Browse files
committed
Rename capability -> ability
1 parent a1d5e9a commit b65958f

File tree

8 files changed

+278
-234
lines changed

8 files changed

+278
-234
lines changed

compiler/src/dotty/tools/dotc/transform/TypeUtils.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ object TypeUtils {
2424
def isErasedClass(using Context): Boolean =
2525
self.underlyingClassRef(refinementOK = true).typeSymbol.is(Flags.Erased)
2626

27-
/** Is this type a checked exception? This is the case of the type
27+
/** Is this type a checked exception? This is the case if the type
2828
* derives from Exception but not from RuntimeException. According to
2929
* that definition Throwable is unchecked. That makes sense since you should
3030
* neither throw nor catch `Throwable` anyway, so we should not define
31-
* a capability to do so.
31+
* an ability to do so.
3232
*/
3333
def isCheckedException(using Context): Boolean =
3434
self.derivesFrom(defn.ExceptionClass)

docs/docs/reference/experimental/canthrow.md

Lines changed: 241 additions & 0 deletions
Large diffs are not rendered by default.

docs/docs/reference/metaprogramming/canthrow.md

Lines changed: 0 additions & 217 deletions
This file was deleted.

library/src-bootstrapped/scala/CanThrow.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package scala
22
import language.experimental.erasedTerms
33
import annotation.implicitNotFound
44

5-
/** A capability class that allows to throw exception `E`. When used with the
6-
* experimental.saferThrows feature, a `throw Ex` expression will require
5+
/** A ability class that allows to throw exception `E`. When used with the
6+
* experimental.saferExceptions feature, a `throw Ex()` expression will require
77
* a given of class `CanThrow[Ex]` to be available.
88
*/
9-
@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 `canThrow` clause in a result type such as `X canThrow ${E}`\n - an enclosing `try` that catches ${E}")
9+
@implicitNotFound("The ability to throw exception ${E} is missing.\nThe ability can be provided by one of the following:\n - A using clause `(using CanThrow[${E}])`\n - A `canThrow` clause in a result type such as `X canThrow ${E}`\n - an enclosing `try` that catches ${E}")
1010
erased class CanThrow[-E <: Exception]
1111

1212
/** A helper type to allow syntax like
1313
*
14-
* def f(): T throws Ex
14+
* def f(): T canThrow Ex
1515
*/
1616
infix type canThrow[R, +E <: Exception] = CanThrow[E] ?=> R
1717

1818
object unsafeExceptions:
19-
given canThrowAny[E <: Exception]: CanThrow[E] = ???
19+
given canThrowAny: CanThrow[Exception] = ???

library/src/scala/runtime/stdLibPatches/language.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ object language:
4343
*/
4444
object erasedTerms
4545

46-
/** Experimental support for typechecked exception capabilities */
46+
/** Experimental support for typechecked exception capabilities
47+
*
48+
* @see [[https://dotty.epfl.ch/docs/reference/experimental/canthrow]]
49+
*/
4750
object saferExceptions
4851

4952
end experimental

tests/neg/saferExceptions.check

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
-- Error: tests/neg/saferExceptions.scala:12:16 ------------------------------------------------------------------------
2-
12 | case 4 => throw Exception() // error
1+
-- Error: tests/neg/saferExceptions.scala:14:16 ------------------------------------------------------------------------
2+
14 | case 4 => throw Exception() // error
33
| ^^^^^^^^^^^^^^^^^
4-
| The capability to throw exception Exception is missing.
5-
| The capability can be provided by one of the following:
4+
| The ability to throw exception Exception is missing.
5+
| The ability can be provided by one of the following:
66
| - A using clause `(using CanThrow[Exception])`
77
| - A `canThrow` clause in a result type such as `X canThrow Exception`
88
| - an enclosing `try` that catches Exception
@@ -11,11 +11,11 @@
1111
|
1212
| import unsafeExceptions.canThrowAny
1313
|
14-
-- Error: tests/neg/saferExceptions.scala:17:48 ------------------------------------------------------------------------
15-
17 | def baz(x: Int): Int canThrow Failure = bar(x) // error
14+
-- Error: tests/neg/saferExceptions.scala:19:48 ------------------------------------------------------------------------
15+
19 | def baz(x: Int): Int canThrow Failure = bar(x) // error
1616
| ^
17-
| The capability to throw exception java.io.IOException is missing.
18-
| The capability can be provided by one of the following:
17+
| The ability to throw exception java.io.IOException is missing.
18+
| The ability can be provided by one of the following:
1919
| - A using clause `(using CanThrow[java.io.IOException])`
2020
| - A `canThrow` clause in a result type such as `X canThrow java.io.IOException`
2121
| - an enclosing `try` that catches java.io.IOException

tests/neg/saferExceptions.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ object test:
44

55
class Failure extends Exception
66

7-
def bar(x: Int): Int canThrow Failure canThrow IOException =
7+
def bar(x: Int): Int
8+
`canThrow` Failure
9+
`canThrow` IOException =
810
x match
911
case 1 => throw AssertionError()
1012
case 2 => throw Failure() // ok
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import language.experimental.saferExceptions
2+
3+
4+
class LimitExceeded extends Exception
5+
6+
val limit = 10e9
7+
8+
def f(x: Double): Double canThrow LimitExceeded =
9+
if x < limit then x * x else throw LimitExceeded()
10+
11+
@main def test(xs: Double*) =
12+
try println(xs.map(f).sum)
13+
catch case ex: LimitExceeded => println("too large")
14+
15+

0 commit comments

Comments
 (0)