Skip to content

Commit cc827cd

Browse files
cknittclaude
andauthored
Represent %todo with a Pexp_todo parsetree node (#9161)
Why: %todo is a language feature selected by an extension name, one of the items #8624 lists before the parsetree can be stamped as v1. The node holds the message as written; PPXs still see the extension. AST modified (%todo): Example: let f = () => %todo("later") Before (parse): expression Pexp_extension "todo" [ structure_item Pstr_eval expression Pexp_constant PConst_string (source="later", semantic="later") ] After (parse): expression Pexp_todo Some expression Pexp_constant PConst_string (source="later", semantic="later") Review: Which payloads become a node? None, a string literal, or a template without interpolations, without attributes. Any other payload, such as `%todo(1)`, stays an extension and is lowered as before, ignoring it. Review: Why an expression for the message rather than a string? It keeps the spelling (quotes or backquotes, escapes) and the comments inside the payload for the printer, as the extension did. Claude-Session: https://claude.ai/code/session_01Kbf3gJFAva9685fjvkajub Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fbc7de4 commit cc827cd

30 files changed

Lines changed: 270 additions & 37 deletions

‎analysis/src/completion_front_end.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@ let completion_with_parser1 ~debug ~offset ~pos_cursor ~kind_file
13021302
| Pexp_extension ({txt}, _) -> set_result (CextensionNode txt)
13031303
| Pexp_raw {kind} ->
13041304
set_result (CextensionNode (Ast_raw.extension_name kind))
1305+
| Pexp_todo _ -> set_result (CextensionNode Ast_todo.name)
13051306
| Pexp_constant _ -> set_result Cnone
13061307
| Pexp_ident lid ->
13071308
let lid_path = flatten_lid_check_dot lid in

‎analysis/src/utils.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ let identify_pexp pexp =
117117
| Pexp_pack _ -> "Pexp_pack"
118118
| Pexp_extension _ -> "Pexp_extension"
119119
| Pexp_raw _ -> "Pexp_raw"
120+
| Pexp_todo _ -> "Pexp_todo"
120121
| Pexp_open _ -> "Pexp_open"
121122
| Pexp_await _ -> "Pexp_await"
122123
| Pexp_jsx_element _ -> "Pexp_jsx_element"

‎changes/todo-node.internal.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Represent `%todo` with a `Pexp_todo` parsetree node. PPXs still see the extension.

‎compiler/frontend/ast_exp_extension.ml‎

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,41 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424
open Ast_helper
2525

26+
(* [%todo]: warns, and throws at runtime with the location and message. [loc]
27+
is the location of [%todo], [expr_loc] that of the whole expression. *)
28+
let todo ~loc ~expr_loc (message : string option) =
29+
Location.prerr_warning expr_loc (Bs_todo message);
30+
(* Like [__LOC__]: the file's base name, so the generated code does not
31+
depend on where it was built, and columns rather than offsets. *)
32+
let file, line, start_col = Location.get_pos_info loc.Location.loc_start in
33+
let end_col = loc.loc_end.pos_cnum - loc.loc_start.pos_cnum + start_col in
34+
let pretext =
35+
Printf.sprintf "%s:%d:%d-%d" (Filename.basename file) line start_col end_col
36+
in
37+
Exp.apply ~loc
38+
(* The compilation unit, not [JsError]: a user module of that name would
39+
shadow the [Stdlib] alias. *)
40+
(Exp.ident ~loc
41+
{txt = Ldot (Lident "Stdlib_JsError", "throwWithMessage"); loc})
42+
[
43+
( Nolabel,
44+
Exp.constant ~loc
45+
(Ast_helper.Const.string
46+
(pretext
47+
^
48+
match message with
49+
| None -> " - Todo"
50+
| Some msg -> " - Todo: " ^ msg)) );
51+
]
52+
2653
let handle_extension e (_self : Ast_mapper.mapper)
2754
(({txt; loc}, payload) : Parsetree.extension) =
2855
match txt with
2956
| "todo" ->
30-
let todo_message =
31-
match Ast_payload.semantic_string_of_payload payload with
32-
| Some s -> Some s
33-
| None -> None
34-
in
35-
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
40-
let pretext =
41-
Printf.sprintf "%s:%d:%d-%d" (Filename.basename file) line start_col
42-
end_col
43-
in
44-
45-
Exp.apply ~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})
50-
[
51-
( Nolabel,
52-
Exp.constant ~loc
53-
(Ast_helper.Const.string
54-
(pretext
55-
^
56-
match todo_message with
57-
| None -> " - Todo"
58-
| Some msg -> " - Todo: " ^ msg)) );
59-
]
57+
(* A payload the parser did not turn into [Pexp_todo], or one from a v0
58+
PPX. A payload other than a string gives no message. *)
59+
todo ~loc ~expr_loc:e.Parsetree.pexp_loc
60+
(Ast_payload.semantic_string_of_payload payload)
6061
| "ffi" -> Ast_exp_handle_external.handle_ffi ~loc ~payload
6162
| "raw" -> Ast_exp_handle_external.handle_raw loc payload
6263
| _ -> e

‎compiler/frontend/ast_exp_extension.mli‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
val todo :
26+
loc:Location.t -> expr_loc:Location.t -> string option -> Parsetree.expression
27+
(** The expansion of [%todo] with the given message. *)
28+
2529
val handle_extension :
2630
Parsetree.expression ->
2731
Ast_mapper.mapper ->

‎compiler/frontend/bs_builtin_ppx.ml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
142142
Ast_exp_extension.handle_extension e self extension
143143
| Pexp_raw {kind; name_loc; source} ->
144144
Ast_exp_handle_external.handle_raw_node ~loc:name_loc kind source
145+
| Pexp_todo {name_loc; message} ->
146+
Ast_exp_extension.todo ~loc:name_loc ~expr_loc:e.pexp_loc
147+
(Option.bind message Ast_payload.semantic_string_of_expression)
145148
| Pexp_constant (Pconst_integer (s, Some 'l')) ->
146149
{e with pexp_desc = Pexp_constant (Pconst_integer (s, None))}
147150
(* End rewriting *)

‎compiler/ml/ast_helper.ml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ module Exp = struct
197197
let extension ?loc ?attrs a = mk ?loc ?attrs (Pexp_extension a)
198198
let raw ?loc ?attrs ~name_loc kind source =
199199
mk ?loc ?attrs (Pexp_raw {kind; name_loc; source})
200+
let todo ?loc ?attrs ~name_loc message =
201+
mk ?loc ?attrs (Pexp_todo {name_loc; message})
200202
let regexp ?loc ?attrs pattern flags =
201203
mk ?loc ?attrs (Pexp_regexp {pattern; flags})
202204
let template ?loc ?attrs source_segments values =

‎compiler/ml/ast_helper.mli‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ module Exp : sig
253253
raw_kind ->
254254
raw_source ->
255255
expression
256+
val todo :
257+
?loc:loc -> ?attrs:attrs -> name_loc:loc -> expression option -> expression
256258
val tagged_template :
257259
?loc:loc ->
258260
?attrs:attrs ->

‎compiler/ml/ast_iterator.ml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ module E = struct
429429
| Pexp_raw {name_loc; source} ->
430430
sub.location sub name_loc;
431431
sub.location sub (Ast_raw.source_loc source)
432+
| Pexp_todo {name_loc; message} ->
433+
sub.location sub name_loc;
434+
Option.iter (sub.expr sub) message
432435
| Pexp_regexp _ -> ()
433436
| Pexp_template {values} -> List.iter (sub.expr sub) values
434437
| Pexp_tagged_template {tag; values} ->

‎compiler/ml/ast_mapper.ml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ module E = struct
442442
~name_loc:(sub.location sub name_loc)
443443
kind
444444
(map_raw_source sub source)
445+
| Pexp_todo {name_loc; message} ->
446+
todo ~loc ~attrs
447+
~name_loc:(sub.location sub name_loc)
448+
(Option.map (sub.expr sub) message)
445449
| Pexp_await e -> await ~loc ~attrs (sub.expr sub e)
446450
| Pexp_jsx_element
447451
(Jsx_fragment

0 commit comments

Comments
 (0)