Skip to content

Commit 0800cb4

Browse files
cknittclaude
andauthored
Print @as names and external strings as ReScript string literals (#9143)
Why: The printer wrote the @as name of a constructor with OCaml's %S and the @as name of a field and the strings of an external with String.escaped. Both turn non-ASCII bytes into decimal escapes such as \195\169, which ReScript reads back as other characters: an interface written by createInterface for an applied functor then gave its fields and constructors other runtime names than the implementation (#9142). They are now encoded with String_literal.encode_js_string, which keeps valid UTF-8 and escapes only what a string literal needs. Diagnostic modified (@as names of a constructor and a field): Example: module M: { type t = | @as("é") A({@as("prénom") name: string}) | @as("a\"b") B } = { type t = | @as("é") A({@as("prénom") name: int}) | @as("a\"b") B } Before (diagnostic): Actual declaration (as_names_printed_as_written.res:6:3-66): type t = @as("\195\169") A({@as("pr\195\169nom") name: int}) | @as("a\"b") B After (diagnostic): Actual declaration (as_names_printed_as_written.res:6:3-66): type t = @as("é") A({@as("prénom") name: int}) | @as("a\"b") B Diagnostic modified (strings of an external): Example: module M: { @module("ünïcode") @scope("ëxtra") external f: int => int = "f" } = { @module("ünïcode") @scope("ëxtra") external f: string => int = "f" } Before (diagnostic): Actual declaration (external_strings_printed_as_written.res:6:3-7:33): @Val @module("\195\188n\195\175code") @scope("\195\171xtra") external f: string => int = "f" After (diagnostic): Actual declaration (external_strings_printed_as_written.res:6:3-7:33): @Val @module("ünïcode") @scope("ëxtra") external f: string => int = "f" Format modified (createInterface for an applied functor): Example: module Make = (X: {type t}) => { type record = {@as("prénom") name: X.t} } module Applied = Make({ type t = string }) Before (rescript): the field is named "prénom" when the interface is read back module Applied: { type record = {@as("pr\195\169nom") name: string} } After (rescript): module Applied: { type record = {@as("prénom") name: string} } Fixes: #9142 Claude-Session: https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8e26a9c commit 0800cb4

7 files changed

Lines changed: 67 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Print the `@as` names of fields and constructors and the strings of externals as they are written, so that non-ASCII letters are no longer turned into escapes that read back as other characters in error messages, hover and generated interfaces.

‎compiler/ml/printtyp.ml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,8 @@ and tree_of_constructor ?printing_context ~layout ~position cd =
945945
let as_annotation = function
946946
| Variant_runtime.Null -> "@as(null)"
947947
| Undefined -> "@as(undefined)"
948-
| String s -> Printf.sprintf "@as(%S)" s
948+
| String s ->
949+
Printf.sprintf "@as(\"%s\")" (String_literal.encode_js_string s)
949950
| Int i -> Printf.sprintf "@as(%d)" i
950951
| Float f -> Printf.sprintf "@as(%s)" f
951952
| Bool b -> Printf.sprintf "@as(%b)" b

‎compiler/syntax/src/res_outcome_printer.ml‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ let collect_arrow_args (out_type : Outcometree.out_type) =
4141
| Otyp_arrow (args, ret) -> (args, ret)
4242
| _ -> ([], out_type)
4343

44+
let print_string_literal_doc s =
45+
Doc.text ("\"" ^ String_literal.encode_js_string s ^ "\"")
46+
4447
let rec collect_functor_args (out_module_type : Outcometree.out_module_type)
4548
args =
4649
match out_module_type with
@@ -394,7 +397,12 @@ and print_record_decl_row_doc (name, runtime_name, mut, opt, arg) =
394397
[
395398
(match runtime_name with
396399
| Some runtime_name ->
397-
Doc.text ("@as(\"" ^ String.escaped runtime_name ^ "\") ")
400+
Doc.concat
401+
[
402+
Doc.text "@as(";
403+
print_string_literal_doc runtime_name;
404+
Doc.text ") ";
405+
]
398406
| None -> Doc.nil);
399407
(if mut then Doc.text "mutable " else Doc.nil);
400408
Printer.print_ident_like ~allow_uident:false name;
@@ -450,7 +458,6 @@ let print_type_parameter_doc (typ, (co, cn)) =
450458

451459
(* Print a digested FFI declaration as the surface attributes the user
452460
wrote; the stored form is the declaration itself. *)
453-
let print_string_literal_doc s = Doc.text ("\"" ^ String.escaped s ^ "\"")
454461

455462
let print_inline_const_doc (c : External_ffi_types.inline_const) =
456463
match c with
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/as_names_printed_as_written.res:5:5-7:1
4+
5+
3 │ module M: {
6+
4 │ type t = | @as("é") A({@as("prénom") name: string}) | @as("a\"b") B
7+
5 │ } = {
8+
6 │  type t = | @as("é") A({@as("prénom") name: int}) | @as("a\"b") B
9+
7 │ }
10+
8 │
11+
12+
Signature mismatch:
13+
14+
Actual declaration (/.../fixtures/as_names_printed_as_written.res:6:3-66):
15+
type t = @as("é") A({@as("prénom") name: int}) | @as("a\"b") B
16+
17+
Expected declaration (/.../fixtures/as_names_printed_as_written.res:4:3-69):
18+
type t = @as("é") A({@as("prénom") name: string}) | @as("a\"b") B
19+
20+
The records of constructor A differ:
21+
The types for field name are not equal.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/external_strings_printed_as_written.res:5:5-8:1
4+
5+
3 │ @module("ünïcode") @scope("ëxtra")
6+
4 │ external f: int => int = "f"
7+
5 │ } = {
8+
6 │  @module("ünïcode") @scope("ëxtra")
9+
7 │  external f: string => int = "f"
10+
8 │ }
11+
9 │
12+
13+
Signature mismatch:
14+
15+
Actual declaration (/.../fixtures/external_strings_printed_as_written.res:6:3-7:33):
16+
@val @module("ünïcode") @scope("ëxtra") external f: string => int = "f"
17+
18+
Expected declaration (/.../fixtures/external_strings_printed_as_written.res:3:3-4:30):
19+
@val @module("ünïcode") @scope("ëxtra") external f: int => int = "f"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// The @as names of constructors and fields print as they are written in
2+
// source, non-ASCII letters and quotes included
3+
module M: {
4+
type t = | @as("é") A({@as("prénom") name: string}) | @as("a\"b") B
5+
} = {
6+
type t = | @as("é") A({@as("prénom") name: int}) | @as("a\"b") B
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// The strings of a printed external print as they are written in source
2+
module M: {
3+
@module("ünïcode") @scope("ëxtra")
4+
external f: int => int = "f"
5+
} = {
6+
@module("ünïcode") @scope("ëxtra")
7+
external f: string => int = "f"
8+
}

0 commit comments

Comments
 (0)