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 8 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 @@ -20,6 +20,7 @@
- Make reanalyze exception tracking work with the new stdlib. https://github.com/rescript-lang/rescript/pull/7328
- 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
- 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
27 changes: 23 additions & 4 deletions runtime/Pervasives.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
Since [others] depend on this file, its public mli files **should not
export types** introduced here, otherwise it would cause
export types** introduced here, otherwise it would cause
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole comment is obsolete and should be removed as there is no others folder anymore (leftover from the old stdlib build procedure).

Or actually, there should be a module-level doc comment (/***) instead explaining what the Pervasives module is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment removed a36b71d

conflicts here.

If the type exported here is also exported in modules from others,
Expand All @@ -12,17 +12,36 @@ external /* Internal */
__unsafe_cast: 'a => 'b = "%identity"

/* Exceptions */

@deprecated(
"`raise` has been renamed to `throw` to align with JavaScript vocabulary. Please use `throw`"
)
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

/**
Throw and exception which will stop execution.

## Examples

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

if 5 > 10 {
error->throw
} 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: 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`"
)
external raise: t => 'a = "%raise"

/**
Throw and exception provided `Error.t`, which will stop execution.

## Examples

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

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

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

Expand Down
14 changes: 7 additions & 7 deletions runtime/Stdlib_Exn.res
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add @@deprecated to this module?

It has the same content as https://github.com/rescript-lang/rescript/blob/master/runtime/Stdlib_Error.res

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't have exactly the same content though, Error is missing the definition of a JS exception that today is catched with Exn.Error(obj), but I agree the current situation is not ideal, maybe we could merge the two?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will explore this in another PR

Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,38 @@ type error

external anyToExnInternal: 'a => exn = "%wrap_exn"

let raiseError = str => raise((Obj.magic((makeError(str): error)): exn))
let raiseError = str => throw((Obj.magic((makeError(str): error)): exn))

type eval_error
@new external makeEvalError: string => eval_error = "EvalError"

let raiseEvalError = str => raise((Obj.magic((makeEvalError(str): eval_error)): exn))
let raiseEvalError = str => throw((Obj.magic((makeEvalError(str): eval_error)): exn))

type range_error
@new external makeRangeError: string => range_error = "RangeError"

let raiseRangeError = str => raise((Obj.magic((makeRangeError(str): range_error)): exn))
let raiseRangeError = str => throw((Obj.magic((makeRangeError(str): range_error)): exn))

type reference_error

@new external makeReferenceError: string => reference_error = "ReferenceError"

let raiseReferenceError = str => raise(Obj.magic(makeReferenceError(str)))
let raiseReferenceError = str => throw(Obj.magic(makeReferenceError(str)))

type syntax_error
@new external makeSyntaxError: string => syntax_error = "SyntaxError"

let raiseSyntaxError = str => raise(Obj.magic(makeSyntaxError(str)))
let raiseSyntaxError = str => throw(Obj.magic(makeSyntaxError(str)))

type type_error
@new external makeTypeError: string => type_error = "TypeError"

let raiseTypeError = str => raise(Obj.magic(makeTypeError(str)))
let raiseTypeError = str => throw(Obj.magic(makeTypeError(str)))

type uri_error
@new external makeURIError: string => uri_error = "URIError"

let raiseUriError = str => raise(Obj.magic(makeURIError(str)))
let raiseUriError = str => throw(Obj.magic(makeURIError(str)))

/* TODO add predicate to tell which error is which " */

Expand Down
2 changes: 1 addition & 1 deletion runtime/Stdlib_Int.res
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ let range = (start, end, ~options: rangeOptions={}) => {
let step = switch options.step {
| None => isInverted ? -1 : 1
| Some(0) if start !== end =>
Stdlib_Error.raise(Stdlib_Error.RangeError.make("Incorrect range arguments"))
Stdlib_Error.throw(Stdlib_Error.RangeError.make("Incorrect range arguments"))
| Some(n) => n
}

Expand Down
Loading