Skip to content

Conversation

@mediremi
Copy link
Member

@mediremi mediremi commented Oct 28, 2025

When editing invalid JS in an %ffi, my compiler would sometimes fail with:

FAILED: MyFile.ast

  Warning number 103
  /home/mediremi/MyFile.res:30:9-10
Fatal error: exception Invalid_argument("String.sub / Bytes.sub")

Enabling backtraces with Printexc.record_backtrace true gave me:

Fatal error: exception Invalid_argument("String.sub / Bytes.sub")
Raised at Stdlib.invalid_arg in file "stdlib.ml", line 30, characters 20-45
Called from Stdlib__String.sub in file "string.ml" (inlined), line 43, characters 2-23
Called from Code_frame.print in file "compiler/ml/code_frame.ml", lines 139-140, characters 4-57
Called from Location.print in file "compiler/ml/location.ml", lines 143-144, characters 8-57
...

This PR prevents this fatal error by guarding String.sub with a check for its pos and len arguments (in this case, start_line_line_offset and end_line_line_end_offset - start_line_line_offset).

With this fix, the compiler now successfully reports the warning triggered by my invalid JS:

rescript: [1/6] MyFile.ast

  Warning number 103
  /home/mediremi/MyFile.res:30:9-10


  FFI warning: Unexpected reserved word

@mediremi mediremi changed the title Check pos >= 0 and len >= 0 before doing String.sub in Code_frame.print Fix fatal compiler error that occurred when an %ffi extension point contained invalid JavaScript Oct 28, 2025
@mediremi mediremi marked this pull request as ready for review October 28, 2025 22:44
@mediremi mediremi requested a review from cristianoc October 28, 2025 22:44
@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 28, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7998

@rescript/darwin-arm64

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

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7998

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/runtime@7998

@rescript/win32-x64

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

commit: 52b065d

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.

1 participant