Skip to content

String.match returns string[] | null; lib.ts compiles under strict null - #98

Merged
ASDAlexander77 merged 2 commits into
mainfrom
string-match-nullable
Nov 14, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
string-match-nullable

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

The last two items left out of #96.

What

  • String.match returns string[] | null, in lib.ts (the __String function and the String class method) and in lib.d.ts. It returned null through a result declared string[].
  • // @strict-null false is removed from lib.ts, so the library itself now compiles under the strict null check.

Behaviour

  • What match returns at run time is unchanged: null when a pattern without the g flag does not match, and an empty array when a global one finds nothing. That second case differs from JavaScript, which gives null there too; it is kept so a global match can always be walked or counted.
  • Compile-time change: a caller that used the result without a null check no longer compiles (for example <string>found or found.length). if (match) { ... } still compiles unchanged.

What the stricter compile needed

  • The compiler rejected the null passed to three C functions. Their parameters are now declared nullable in lib.native.d.ts: the end pointer of strtol and strtod, and the results argument and the result of regexp_exec.
  • -mm=own rejected return [...result] with the new return type ("a second reference"), so match puts the array in a local first, as matchAll does before its yield.

The searchString == null guards in the string helpers stay: they compile under strict null and still catch a null string from a caller.

Testing

  • tests/string_match.ts and tests/string_match_lastIndex.ts check for null before using the result, and string_match.ts asserts null for no match without g and an empty array with it.
  • Release, Windows x64: the whole suite passes for gc, rc, none and own, compiled and under the JIT.
  • scripts/check_declarations.ps1 passes.
  • Not run locally: debug builds, Linux, x86 and wasm. lib.linux.ts and lib.wasm.ts are now compiled under strict null as well; the only literal null in them is in a declaration that is already nullable.

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits November 14, 2026 14:05
match returned null through a result declared string[]. It is declared
string[] | null now, in lib.ts and lib.d.ts, and a global match that finds
nothing returns null too, as in JavaScript, where it returned an empty array.
A caller that used the result without a null check no longer compiles:
string_match.ts and string_match_lastIndex.ts check for null first.

With that, `// @strict-null false` is gone from lib.ts. The compiler then
rejected the null passed to three C functions, whose parameters are declared
nullable in lib.native.d.ts: the end pointer of strtol and strtod, and the
results argument (and the result) of regexp_exec.

-mm=own: match puts `[...result]` in a local before returning it; returned
directly as string[] | null it was "a second reference".

Tested in release on Windows x64 for gc, rc, none and own, compiled and under
the JIT; debug, Linux, x86 and wasm were not built locally.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The commit before made it null, as JavaScript does. It goes back to the empty
array it always was here, so a global match can be walked or counted without
a check; only a pattern without the g flag gives null. string_match.ts
asserts both.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 54b4e5f into main Nov 14, 2026
10 checks passed
@ASDAlexander77
ASDAlexander77 deleted the string-match-nullable branch November 14, 2026 14:31
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