Skip to content

Replace "raise" in tests with "throw"; treat "throw" like "raise" in analysis #7521

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 5 commits into from
May 26, 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 @@
#### :bug: Bug fix

- `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497
- Treat `throw` like `raise` in analysis. https://github.com/rescript-lang/rescript/pull/7521

#### :nail_care: Polish

Expand Down
8 changes: 4 additions & 4 deletions analysis/examples/larger-project/src/arg_helper.res
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,27 @@ module Make = (
| exception Not_found =>
switch S.Value.of_string(value) {
| value => set_user_default(value, acc)
| exception exn => raise(Parse_failure(exn))
| exception exn => throw(Parse_failure(exn))
}
| equals =>
let key_value_pair = value
let length = String.length(key_value_pair)
assert (equals >= 0 && equals < length)
if equals == 0 {
raise(Parse_failure(Failure("Missing key in argument specification")))
throw(Parse_failure(Failure("Missing key in argument specification")))
}
let key = {
let key = String.sub(key_value_pair, 0, equals)
try S.Key.of_string(key) catch {
| exn => raise(Parse_failure(exn))
| exn => throw(Parse_failure(exn))
}
}

let value = {
let value = String.sub(key_value_pair, equals + 1, length - equals - 1)

try S.Value.of_string(value) catch {
| exn => raise(Parse_failure(exn))
| exn => throw(Parse_failure(exn))
}
}

Expand Down
4 changes: 2 additions & 2 deletions analysis/examples/larger-project/src/ast_helper.res
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let with_default_loc = (l, f) => {
} catch {
| exn =>
default_loc := old
raise(exn)
throw(exn)
}
}

Expand Down Expand Up @@ -94,7 +94,7 @@ module Typ = {
@raises(Error)
let check_variable = (vl, loc, v) =>
if List.mem(v, vl) {
raise({
throw({
open Syntaxerr
Error(Variable_in_scope(loc, v))
})
Expand Down
4 changes: 2 additions & 2 deletions analysis/examples/larger-project/src/exception/BsJson.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@raise(DecodeError)
@throw(DecodeError)
let testBsJson = x => Json_decode.string(x)

@raise(DecodeError)
@throw(DecodeError)
let testBsJson2 = x => Json.Decode.string(x)
2 changes: 1 addition & 1 deletion analysis/examples/larger-project/src/exception/ExnB.res
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@raises(Not_found)
let foo = () => raise(Not_found)
let foo = () => throw(Not_found)
4 changes: 2 additions & 2 deletions analysis/examples/larger-project/src/exception/Yojson.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module Basic = {
type t

@raises(Json_error)
let from_string: string => t = _ => raise(Json_error("Basic.from_string"))
let from_string: string => t = _ => throw(Json_error("Basic.from_string"))

module Util = {
exception Type_error(string, t)

@raises(Type_error)
let member: (string, t) => t = (_s, j) => raise(Type_error("Basic.Util.member", j))
let member: (string, t) => t = (_s, j) => throw(Type_error("Basic.Util.member", j))

let to_int: t => int = _ => 34

Expand Down
2 changes: 1 addition & 1 deletion analysis/examples/larger-project/src/location.res
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,5 @@ let () = register_error_of_exn(x =>
@raises(Error)
let raise_errorf = (~loc=none, ~sub=list{}, ~if_highlight="") =>
pp_ksprintf(~before=print_phanton_error_prefix, msg =>
raise(Error({loc: loc, msg: msg, sub: sub, if_highlight: if_highlight}))
throw(Error({loc: loc, msg: msg, sub: sub, if_highlight: if_highlight}))
)
34 changes: 17 additions & 17 deletions analysis/examples/larger-project/src/misc.res
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exception Fatal_error
let fatal_error = msg => {
print_string(">> Fatal error: ")
prerr_endline(msg)
raise(Fatal_error)
throw(Fatal_error)
}

@raises(Fatal_error)
Expand All @@ -36,7 +36,7 @@ let try_finally = (work, cleanup) => {
let result = try work() catch {
| e =>
cleanup()
raise(e)
throw(e)
}
cleanup()
result
Expand All @@ -56,7 +56,7 @@ let protect_refs = {
x
| exception e =>
set_refs(backup)
raise(e)
throw(e)
}
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ module Stdlib = {
let rec aux = (acc, l1, l2) =>
switch (l1, l2) {
| (list{}, _) => (List.rev(acc), l2)
| (list{_, ..._}, list{}) => raise(Invalid_argument("map2_prefix"))
| (list{_, ..._}, list{}) => throw(Invalid_argument("map2_prefix"))
| (list{h1, ...t1}, list{h2, ...t2}) =>
let h = f(h1, h2)
aux(list{h, ...acc}, t1, t2)
Expand Down Expand Up @@ -179,7 +179,7 @@ module Stdlib = {
(List.rev(acc), l)
} else {
switch l {
| list{} => raise(Invalid_argument("split_at"))
| list{} => throw(Invalid_argument("split_at"))
| list{t, ...q} => aux(n - 1, list{t, ...acc}, q)
}
}
Expand Down Expand Up @@ -254,13 +254,13 @@ let find_in_path = (path, name) =>
if Sys.file_exists(name) {
name
} else {
raise(Not_found)
throw(Not_found)
}
} else {
@raises(Not_found)
let rec try_dir = x =>
switch x {
| list{} => raise(Not_found)
| list{} => throw(Not_found)
| list{dir, ...rem} =>
let fullname = Filename.concat(dir, name)
if Sys.file_exists(fullname) {
Expand Down Expand Up @@ -290,7 +290,7 @@ let find_in_path_rel = (path, name) => {
@raises(Not_found)
let rec try_dir = x =>
switch x {
| list{} => raise(Not_found)
| list{} => throw(Not_found)
| list{dir, ...rem} =>
let fullname = simplify(Filename.concat(dir, name))
if Sys.file_exists(fullname) {
Expand All @@ -309,7 +309,7 @@ let find_in_path_uncap = (path, name) => {
@raises(Not_found)
let rec try_dir = x =>
switch x {
| list{} => raise(Not_found)
| list{} => throw(Not_found)
| list{dir, ...rem} =>
let fullname = Filename.concat(dir, name)
and ufullname = Filename.concat(dir, uname)
Expand Down Expand Up @@ -381,7 +381,7 @@ let copy_file_chunk = (ic, oc, len) => {
} else {
let r = input(ic, buff, 0, min(n, 0x1000))
if r == 0 {
raise(End_of_file)
throw(End_of_file)
} else {
output(oc, buff, 0, r)
copy(n - r)
Expand Down Expand Up @@ -435,12 +435,12 @@ let output_to_file_via_temporary = (~mode=list{Open_text}, filename, fn) => {
} catch {
| exn =>
remove_file(temp_filename)
raise(exn)
throw(exn)
}
| exception exn =>
close_out(oc)
remove_file(temp_filename)
raise(exn)
throw(exn)
}
}

Expand Down Expand Up @@ -514,7 +514,7 @@ let search_substring = (pat, str, start) => {
if j >= String.length(pat) {
i
} else if i + j >= String.length(str) {
raise(Not_found)
throw(Not_found)
} else if String.get(str, i + j) == String.get(pat, j) {
search(i, j + 1)
} else {
Expand Down Expand Up @@ -831,7 +831,7 @@ module Color = {
| "error" => cur_styles.contents.error
| "warning" => cur_styles.contents.warning
| "loc" => cur_styles.contents.loc
| _ => raise(Not_found)
| _ => throw(Not_found)
}

let color_enabled = ref(true)
Expand Down Expand Up @@ -957,15 +957,15 @@ exception HookExnWrapper({error: exn, hook_name: string, hook_info: hook_info})
exception HookExn(exn)

@raises(HookExn)
let raise_direct_hook_exn = e => raise(HookExn(e))
let raise_direct_hook_exn = e => throw(HookExn(e))

@raises([HookExnWrapper, genericException])
let fold_hooks = (list, hook_info, ast) =>
List.fold_left(
(ast, (hook_name, f)) =>
try f(hook_info, ast) catch {
| HookExn(e) => raise(e)
| error => raise(HookExnWrapper({error: error, hook_name: hook_name, hook_info: hook_info}))
| HookExn(e) => throw(e)
| error => throw(HookExnWrapper({error: error, hook_name: hook_name, hook_info: hook_info}))
},
/* when explicit reraise with backtrace will be available,
it should be used here */
Expand Down
2 changes: 1 addition & 1 deletion analysis/examples/larger-project/src/res_token.res
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ let keywordTable = x =>
| "type" => Typ
| "when" => When
| "while" => While
| _ => raise(Not_found)
| _ => throw(Not_found)
}

let isKeyword = x =>
Expand Down
2 changes: 1 addition & 1 deletion analysis/examples/larger-project/src/syntaxerr.res
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ let location_of_error = x =>
| Expecting(l, _) => l
}

let ill_formed_ast = (loc, s) => raise(Error(Ill_formed_ast(loc, s)))
let ill_formed_ast = (loc, s) => throw(Error(Ill_formed_ast(loc, s)))

6 changes: 3 additions & 3 deletions analysis/examples/larger-project/src/warnings.res
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ let mk_lazy = f => {
} catch {
| exn =>
restore(prev)
raise(exn)
throw(exn)
}
}
}
Expand All @@ -249,7 +249,7 @@ let parse_opt = (error, active, flags, s) => {
active[i] = true
error[i] = true
}
let error = () => raise(Arg.Bad("Ill-formed list of warnings"))
let error = () => throw(Arg.Bad("Ill-formed list of warnings"))
let rec get_num = (n, i) =>
if i >= String.length(s) {
(i, n)
Expand Down Expand Up @@ -601,7 +601,7 @@ let reset_fatal = () => nerrors := 0
let check_fatal = () =>
if nerrors.contents > 0 {
nerrors := 0
raise(Errors)
throw(Errors)
}

let descriptions = list{
Expand Down
2 changes: 1 addition & 1 deletion analysis/reanalyze/src/Exception.ml
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ let traverseAst () =
case.c_guard |> iterExprOpt self;
case.c_rhs |> iterExpr self)
in
let isRaise s = s = "Pervasives.raise" || s = "Pervasives.raise_notrace" in
let isRaise s = s = "Pervasives.raise" || s = "Pervasives.throw" in
let raiseArgs args =
match args with
| [(_, Some {Typedtree.exp_desc = Texp_construct ({txt}, _, _)})] ->
Expand Down
Loading