Skip to content

Add throw #7346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Fix Pervasive.max using boolean comparison for floats. https://github.com/rescript-lang/rescript/pull/7333
- Experimental: Support nested/inline record types - records defined inside of other records, without needing explicit separate type definitions. https://github.com/rescript-lang/rescript/pull/7241
- Add unified exponentiation (`**`) operator for numeric types using ES7 `**`. https://github.com/rescript-lang/rescript-compiler/pull/7153
- Rename `raise` to `throw` to align with JavaScript vocabulary. `raise` has been deprecated. https://github.com/rescript-lang/rescript/pull/7346

#### :boom: Breaking Change

Expand Down
8 changes: 4 additions & 4 deletions runtime/Belt_List.res
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ let head = x =>

let headExn = x =>
switch x {
| list{} => raise(Not_found)
| list{} => throw(Not_found)
| list{x, ..._} => x
}

Expand All @@ -100,7 +100,7 @@ let tail = x =>

let tailExn = x =>
switch x {
| list{} => raise(Not_found)
| list{} => throw(Not_found)
| list{_, ...t} => t
}

Expand All @@ -126,7 +126,7 @@ let rec nthAuxAssert = (x, n) =>
} else {
nthAuxAssert(t, n - 1)
}
| _ => raise(Not_found)
| _ => throw(Not_found)
}

let get = (x, n) =>
Expand All @@ -138,7 +138,7 @@ let get = (x, n) =>

let getExn = (x, n) =>
if n < 0 {
raise(Not_found)
throw(Not_found)
} else {
nthAuxAssert(x, n)
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/Belt_MutableQueue.res
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ let peekUndefined = q =>

let peekExn = q =>
switch q.first {
| None => raise(Not_found)
| None => throw(Not_found)
| Some(v) => v.content
}

Expand All @@ -95,7 +95,7 @@ let pop = q =>
let popExn = q =>
/* TO fix */
switch q.first {
| None => raise(Not_found)
| None => throw(Not_found)
| Some(x) =>
let next = x.next
if next == None {
Expand Down
2 changes: 1 addition & 1 deletion runtime/Belt_Option.res
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let forEach = (opt, f) =>
let getExn = x =>
switch x {
| Some(x) => x
| None => raise(Not_found)
| None => throw(Not_found)
}

external getUnsafe: option<'a> => 'a = "%identity"
Expand Down
2 changes: 1 addition & 1 deletion runtime/Belt_Result.res
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type t<'a, 'b> = result<'a, 'b> =
let getExn = x =>
switch x {
| Ok(x) => x
| Error(_) => raise(Not_found)
| Error(_) => throw(Not_found)
}

let mapWithDefault = (opt, default, f) =>
Expand Down
2 changes: 1 addition & 1 deletion runtime/Belt_internalAVLset.res
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ let rec getUndefined = (n: t<_>, x, ~cmp) =>

let rec getExn = (n: t<_>, x, ~cmp) =>
switch n {
| None => raise(Not_found)
| None => throw(Not_found)
| Some(t) /* Node(l, v, r, _) */ =>
let v = t.value
let c = Belt_Id.getCmpInternal(cmp)(x, v)
Expand Down
2 changes: 1 addition & 1 deletion runtime/Belt_internalAVLtree.res
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ let rec getUndefined = (n, x, ~cmp) =>

let rec getExn = (n, x, ~cmp) =>
switch n {
| None => raise(Not_found)
| None => throw(Not_found)
| Some(n) /* Node(l, v, d, r, _) */ =>
let v = n.key
let c = Belt_Id.getCmpInternal(cmp)(x, v)
Expand Down
2 changes: 1 addition & 1 deletion runtime/Belt_internalMapInt.res
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ let rec getUndefined = (n, x: key) =>

let rec getExn = (n, x: key) =>
switch n {
| None => raise(Not_found)
| None => throw(Not_found)
| Some(n) =>
let v = n.N.key
if x == v {
Expand Down
2 changes: 1 addition & 1 deletion runtime/Belt_internalMapString.res
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ let rec getUndefined = (n, x: key) =>

let rec getExn = (n, x: key) =>
switch n {
| None => raise(Not_found)
| None => throw(Not_found)
| Some(n) =>
let v = n.N.key
if x == v {
Expand Down
2 changes: 1 addition & 1 deletion runtime/Belt_internalSetInt.res
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ let rec getUndefined = (n: t, x: value) =>

let rec getExn = (n: t, x: value) =>
switch n {
| None => raise(Not_found)
| None => throw(Not_found)
| Some(t) =>
let v = t.value
if x == v {
Expand Down
2 changes: 1 addition & 1 deletion runtime/Belt_internalSetString.res
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ let rec getUndefined = (n: t, x: value) =>

let rec getExn = (n: t, x: value) =>
switch n {
| None => raise(Not_found)
| None => throw(Not_found)
| Some(t) =>
let v = t.value
if x == v {
Expand Down
33 changes: 22 additions & 11 deletions runtime/Pervasives.res
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
/**
Since [others] depend on this file, its public mli files **should not
export types** introduced here, otherwise it would cause
conflicts here.

If the type exported here is also exported in modules from others,
you will get a type not equivalent.
*/
@deprecated("Do not use. This will be removed in v13")
external /* Internal */

__unsafe_cast: 'a => 'b = "%identity"

/* Exceptions */

@deprecated(
"`raise` has been renamed to `throw` to align with JavaScript vocabulary. Please use `throw` instead"
)
external raise: exn => 'a = "%raise"

@deprecated("Use custom exception instead")
let failwith = s => raise(Failure(s))
let failwith = s => throw(Failure(s))

@deprecated("Use custom exception instead")
let invalid_arg = s => raise(Invalid_argument(s))
let invalid_arg = s => throw(Invalid_argument(s))

@deprecated("Use custom exception instead") exception Exit

/**
Raises the given exception, terminating execution unless caught by a surrounding try/catch block.

## Examples

```rescript
let error = Error.make("Everything is upside down.")

if 5 > 10 {
throw(error)
} else {
Console.log("Phew, sanity still rules.")
}
```
*/
external throw: Stdlib_Error.t => 'a = "%raise"

/* Composition operators */

external \"|>": ('a, 'a => 'b) => 'b = "%revapply"
Expand Down
2 changes: 1 addition & 1 deletion runtime/Pervasives_mini.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Exceptions */
external raise: exn => 'a = "%raise"
external throw: exn => 'a = "%raise"

/* Debugging */

Expand Down
4 changes: 2 additions & 2 deletions runtime/Primitive_array.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ let length = Primitive_array_extern.length

let get = (xs, index) =>
if index < 0 || index >= length(xs) {
raise(Invalid_argument("index out of bounds"))
throw(Invalid_argument("index out of bounds"))
} else {
xs->Primitive_array_extern.getUnsafe(index)
}

let set = (xs, index, newval) =>
if index < 0 || index >= length(xs) {
raise(Invalid_argument("index out of bounds"))
throw(Invalid_argument("index out of bounds"))
} else {
xs->Primitive_array_extern.setUnsafe(index, newval)
}
4 changes: 2 additions & 2 deletions runtime/Primitive_bigint.res
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ external div: (bigint, bigint) => bigint = "%divbigint"

let div = (x: bigint, y: bigint) =>
if y == 0n {
raise(Division_by_zero)
throw(Division_by_zero)
} else {
div(x, y)
}
Expand All @@ -34,7 +34,7 @@ external mod_: (bigint, bigint) => bigint = "%modbigint"

let mod_ = (x: bigint, y: bigint) =>
if y == 0n {
raise(Division_by_zero)
throw(Division_by_zero)
} else {
mod_(x, y)
}
4 changes: 2 additions & 2 deletions runtime/Primitive_int.res
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ let max = (x: int, y: int): int =>

let div = (x: int, y: int) =>
if y == 0 {
raise(Division_by_zero)
throw(Division_by_zero)
} else {
Primitive_int_extern.div(x, y)
}

let mod_ = (x: int, y: int) =>
if y == 0 {
raise(Division_by_zero)
throw(Division_by_zero)
} else {
Primitive_int_extern.mod_(x, y)
}
6 changes: 3 additions & 3 deletions runtime/Primitive_lazy.res
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ exception Undefined
}
)

%%private(let raise_undefined = () => raise(Undefined))
%%private(let raise_undefined = () => throw(Undefined))

/* Assume [blk] is a block with tag lazy */
%%private(
Expand All @@ -59,8 +59,8 @@ exception Undefined
blk.value = fnToVal(raise_undefined)
try forward_with_closure(blk, closure) catch {
| e =>
blk.value = fnToVal(() => raise(e))
raise(e)
blk.value = fnToVal(() => throw(e))
throw(e)
}
}
)
Expand Down
2 changes: 1 addition & 1 deletion runtime/Primitive_module.res
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module type Empty = {}
in the lambda layer
*/
let init = (loc: (string, int, int), shape: shape) => {
let undef_module = _ => raise(Undefined_recursive_module(loc))
let undef_module = _ => throw(Undefined_recursive_module(loc))
let rec loop = (shape: shape, struct_: Obj.t, idx) =>
switch shape {
| Function => Obj.setField(struct_, idx, Obj.magic(undef_module))
Expand Down
4 changes: 2 additions & 2 deletions runtime/Primitive_object.res
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ let rec compare = (a: t, b: t): int =>
| ("boolean", "boolean") => Pervasives.compare((magic(a): bool), magic(b))
| ("boolean", _) => 1
| (_, "boolean") => -1
| ("function", "function") => raise(Invalid_argument("compare: functional value"))
| ("function", "function") => throw(Invalid_argument("compare: functional value"))
| ("function", _) => 1
| (_, "function") => -1
| ("bigint", "bigint")
Expand Down Expand Up @@ -261,7 +261,7 @@ let rec equal = (a: t, b: t): bool =>
} else {
let b_type = Js.typeof(b)
if a_type == "function" || b_type == "function" {
raise(Invalid_argument("equal: functional value"))
throw(Invalid_argument("equal: functional value"))
} /* first, check using reference equality */
else if (
/* a_type = "object" || "symbol" */
Expand Down
2 changes: 1 addition & 1 deletion runtime/Primitive_string.res
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let max = (x: string, y: string): string =>

let getChar = (s, i) =>
if i >= Primitive_string_extern.length(s) || i < 0 {
raise(Invalid_argument("index out of bounds"))
throw(Invalid_argument("index out of bounds"))
} else {
Primitive_string_extern.getChar(s, i)
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/Primitive_util.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Js = Primitive_js_extern

let raiseWhenNotFound = x =>
if Js.testAny(x) {
raise(Not_found)
throw(Not_found)
} else {
x
}
2 changes: 1 addition & 1 deletion runtime/Stdlib_Bool.res
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let fromStringExn = param =>
switch param {
| "true" => true
| "false" => false
| _ => raise(Invalid_argument(`Bool.fromStringExn: value is neither "true" nor "false"`))
| _ => throw(Invalid_argument(`Bool.fromStringExn: value is neither "true" nor "false"`))
}

external compare: (bool, bool) => Stdlib_Ordering.t = "%compare"
Expand Down
2 changes: 1 addition & 1 deletion runtime/Stdlib_Char.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ external fromIntUnsafe: int => t = "%identity"

let fromIntExn = n =>
if n < 0 || n > 255 {
raise(Invalid_argument("`Char.fromIntExn` expects an integer between 0 and 255"))
throw(Invalid_argument("`Char.fromIntExn` expects an integer between 0 and 255"))
} else {
fromIntUnsafe(n)
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/Stdlib_Error.res
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ module URIError = {

external raise: t => 'a = "%raise"

external throw: t => 'a = "%raise"

let panic = msg => make(`Panic! ${msg}`)->raise

external ignore: t => unit = "%ignore"
20 changes: 20 additions & 0 deletions runtime/Stdlib_Error.resi
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,28 @@ if 5 > 10 {
}
```
*/
@deprecated(
"`raise` has been renamed to `throw` to align with JavaScript vocabulary. Please use `throw` instead"
)
external raise: t => 'a = "%raise"

/**
Raises the given exception, terminating execution unless caught by a surrounding try/catch block.

## Examples

```rescript
let error = Error.make("Everything is upside down.")

if 5 > 10 {
Error.throw(error)
} else {
Console.log("Phew, sanity still rules.")
}
```
*/
external throw: t => 'a = "%raise"

/**
Raises a panic exception with the given message.

Expand Down
Loading
Loading