Skip to content

Commit fbc7de4

Browse files
cknittclaude
andauthored
Fix %todo with a local JsError module, and the location in its message (#9160)
* Expand %todo to the standard library's JsError, located like __LOC__ Why: %todo called `JsError.throwWithMessage` by name, so a user module named JsError replaced it. Its message also embedded the absolute path of the source file, and character offsets from the start of the file as columns. JS modified (%todo with a local JsError module): Example: module JsError = { let throwWithMessage = (s: string) => s } let f = (_: int): int => %todo("later") Before (diagnostic): This has type: string But it's expected to have type: int After (js): function f(param) { return Stdlib_JsError.throwWithMessage("A.res:4:25-30 - Todo: later"); } JS modified (%todo message): Example: let a = 1 let f = () => %todo Before (js): return Stdlib_JsError.throwWithMessage("/home/me/app/src/D.res:2:24-29 - Todo"); After (js): return Stdlib_JsError.throwWithMessage("D.res:2:14-19 - Todo"); Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gJFAva9685fjvkajub Signed-off-by: Christoph Knittel <ck@cca.io> * Allow the %todo warning in the tests project instead of in the test Every %todo reports warning 110, which the tests project turned into an error. Answers the Codex review on #9160: rather than disabling the warning in todo_shadowing_test.res, the project keeps 110 a warning (-warn-error A-110), and every other warning stays an error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gJFAva9685fjvkajub Signed-off-by: Christoph Knittel <ck@cca.io> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f88294f commit fbc7de4

5 files changed

Lines changed: 57 additions & 8 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Fix `%todo` calling a user-defined `JsError` module instead of the standard library one.
2+
- Fix the location in the `%todo` error message: the file's base name instead of its absolute path, and columns instead of character offsets.

‎compiler/frontend/ast_exp_extension.ml‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@ let handle_extension e (_self : Ast_mapper.mapper)
3333
| None -> None
3434
in
3535
Location.prerr_warning e.Parsetree.pexp_loc (Bs_todo todo_message);
36+
(* Like [__LOC__]: the file's base name, so the generated code does not
37+
depend on where it was built, and columns rather than offsets. *)
38+
let file, line, start_col = Location.get_pos_info loc.loc_start in
39+
let end_col = loc.loc_end.pos_cnum - loc.loc_start.pos_cnum + start_col in
3640
let pretext =
37-
loc.loc_start.pos_fname ^ ":"
38-
^ string_of_int loc.loc_start.pos_lnum
39-
^ ":"
40-
^ string_of_int loc.loc_start.pos_cnum
41-
^ "-"
42-
^ string_of_int loc.loc_end.pos_cnum
41+
Printf.sprintf "%s:%d:%d-%d" (Filename.basename file) line start_col
42+
end_col
4343
in
4444

4545
Exp.apply ~loc
46-
(Exp.ident ~loc {txt = Longident.parse "JsError.throwWithMessage"; loc})
46+
(* The compilation unit, not [JsError]: a user module of that name
47+
would shadow the [Stdlib] alias. *)
48+
(Exp.ident ~loc
49+
{txt = Ldot (Lident "Stdlib_JsError", "throwWithMessage"); loc})
4750
[
4851
( Nolabel,
4952
Exp.constant ~loc

‎tests/tests/rescript.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
"compiler-flags": [
1616
"-check-lam",
1717
"-w -3-6-26-27-29-30-32..40-44-45-52-60-9-106+104",
18-
"-warn-error A"
18+
"-warn-error A-110"
1919
]
2020
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Mocha from "mocha";
4+
import * as Test_utils from "./test_utils.mjs";
5+
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.mjs";
6+
7+
function throwWithMessage(s) {
8+
return s;
9+
}
10+
11+
let JsError = {
12+
throwWithMessage: throwWithMessage
13+
};
14+
15+
function notImplemented(param) {
16+
return Stdlib_JsError.throwWithMessage("todo_shadowing_test.res:9:38-43 - Todo: later");
17+
}
18+
19+
Mocha.describe("Todo_shadowing_test", () => {
20+
Mocha.test("%todo throws even with a local JsError module", () => Test_utils.throws("File \"todo_shadowing_test.res\", line 13, characters 11-18", () => {
21+
Stdlib_JsError.throwWithMessage("todo_shadowing_test.res:9:38-43 - Todo: later");
22+
}));
23+
});
24+
25+
export {
26+
JsError,
27+
notImplemented,
28+
}
29+
/* Not a pure module */
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
open Mocha
2+
open Test_utils
3+
4+
// A local module named JsError must not change what %todo expands to.
5+
module JsError = {
6+
let throwWithMessage = (s: string) => s
7+
}
8+
9+
let notImplemented = (_: int): int => %todo("later")
10+
11+
describe(__MODULE__, () => {
12+
test("%todo throws even with a local JsError module", () => {
13+
throws(__LOC__, () => ignore(notImplemented(1)))
14+
})
15+
})

0 commit comments

Comments
 (0)