Skip to content

Print @as names and external strings as ReScript string literals - #9143

Open
cknitt wants to merge 1 commit into
masterfrom
as-name-escaping
Open

cknitt wants to merge 1 commit into
masterfrom
as-name-escaping

Conversation

@cknitt

@cknitt cknitt commented Oct 30, 2026

Copy link
Copy Markdown
Member

Fixes #9142.

The printer of declarations escaped strings the OCaml way: %S for the @as name of a constructor, String.escaped for the @as name of a field and for every string of a printed external (@module, @scope, inline string constants). Both write non-ASCII bytes as decimal escapes like \195\169, which ReScript reads back as other characters. A generated interface for an applied functor then named @as("prénom") as prénom at run time, and error messages and hover showed the escapes.

The strings are now encoded with String_literal.encode_js_string, which keeps valid UTF-8 and escapes only what a string literal needs, as #9136 does for @tag names.

Before:  type t = @as("\195\169") A({@as("pr\195\169nom") name: int}) | @as("a\"b") B
After:   type t = @as("é") A({@as("prénom") name: int}) | @as("a\"b") B

New tests: tests/build_tests/super_errors/fixtures/as_names_printed_as_written.res (constructor and field @as with non-ASCII letters and a quote) and external_strings_printed_as_written.res (@module and @scope with non-ASCII letters).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • 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.

Semantic Diff

1 commit · Full page

Print @as names and external strings as ReScript string literals

4676523 · cknitt + Claude · Oct 30, 2026, 16:14 UTC

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)

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
}

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
+  type t = @as("é") A({@as("prénom") name: int}) | @as("a\"b") B

Diagnostic modified (strings of an external)

module M: {
  @module("ünïcode") @scope("ëxtra")
  external f: int => int = "f"
} = {
  @module("ünïcode") @scope("ëxtra")
  external f: string => int = "f"
}

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"
+  @val @module("ünïcode") @scope("ëxtra") external f: string => int = "f"

Format modified (createInterface for an applied functor)

module Make = (X: {type t}) => {
  type record = {@as("prénom") name: X.t}
}
module Applied = Make({
  type t = string
})

rescript · Before: the field is named "prénom" when the interface is read back

 module Applied: {
-  type record = {@as("pr\195\169nom") name: string}
+  type record = {@as("prénom") name: string}
 }

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.74%. Comparing base (7249c11) to head (4676523).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9143   +/-   ##
=======================================
  Coverage   81.73%   81.74%           
=======================================
  Files         460      460           
  Lines       64936    64940    +4     
=======================================
+ Hits        53076    53082    +6     
+ Misses      11860    11858    -2     
Files with missing lines Coverage Δ
compiler/ml/printtyp.ml 72.46% <100.00%> (+0.02%) ⬆️
compiler/syntax/src/res_outcome_printer.ml 83.00% <100.00%> (+0.55%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9143

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9143

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9143

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9143

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9143

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9143

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9143

commit: 4676523

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

@codex review

@cknitt
cknitt marked this pull request as ready for review October 30, 2026 17:16
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T17:18:01.005535Z 859cef9 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 859cef9e2c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cknitt
cknitt requested a review from cristianoc October 30, 2026 17:20
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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn
Signed-off-by: Christoph Knittel <ck@cca.io>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Printed @as names of fields and constructors turn non-ASCII letters into other characters

1 participant