Repository navigation
An empty string is falsy - #511
Merged
Merged
Conversation
`""` was true in every boolean test of a string: `if`, `while`, `!`, `?:`, `&&` and `||`, for a plain, optional, nullable or `any` string, under every memory model (#509). The string -> boolean cast had no case of its own and fell to the generic pointer -> boolean one, which only compares with null. castStringToBool reads the first character and is true when it is not the terminator. A null string reads the terminator of a static "", so the test needs no branch and is safe in any region. An optional string selects null when it has no value before reading, as the value of an undefined optional is no pointer at all. 00string_empty_falsy.ts: each of those forms with "" and with a non-empty string, a module-level `"" ? 1 : 2`, and empty strings made at run time (concatenation, template). On main the first assertion fails. Closes #509 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
28boolcasts.ts had `if ("") { boolDie() }` commented out ("TODO: finish
it"), and 29lazyreferences.ts had `"" || ...` and `"" && ...` commented
out with "\"\" should be false for boolean". Both are right now (#509);
on main each fails its assertion.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #509.
""was true in every boolean test of a string:if,while,!,?:,&&and||, for a plain, optional, nullable oranystring, under every memory model. Soe || "x"gave""ande && "x"gave"x". The string → boolean cast had no case of its own and fell to the generic pointer → boolean one inCastLogicHelper, which only compares the pointer with null.Change
castStringToBoolreads the first character and returns true when it is not the terminator. For a null string it reads the terminator of a static""(aselectof the pointer), so the test makes no branch and works in any region.Test
00string_empty_falsy.tstests""and a non-empty string in each of those forms. It also covers a module-level"" ? 1 : 2, a global"", and empty strings made at run time (concatenation, template).??still keeps"". On main the first assertion fails.It is registered for compile and JIT and in
TSLANG_CORPUS(rc and none). It passes on the plain JIT under the default model, rc and none, with and without--opt, and as an--emit=exe. Under-mm=ownwithout the default library, a smaller program coveringif/!/?:/while/optional/literal gives the right answers.ctest -C Release: 3857 of 3864 passed in the full run. The 7 failures were this test (6 registrations plus its ownership-verifier shard): its first version calledsubstring, which the runner's--no-default-libdoes not have. With that removed, all 7 pass. gtest unittests are not built in this tree.DefaultLib
Merge ASDAlexander77/TypeScriptCompilerDefaultLib#24 with this PR. DefaultLib's
endsWith,startsWithandindexOfusedif (!searchString)as a null check. With""falsy,"abc".endsWith("")would returnfalseandindexOf("")would return-1. That PR makes the guards== null, which behaves the same under either compiler, so it can merge first. No other DefaultLib code tests a string for truthiness. No embedded TypeScript in this repo does either.Re-enabled tests
28boolcasts.ts(if ("")) and29lazyreferences.ts("" || ...,"" && ...) had these cases commented out as TODO (""" should be false for boolean"). They are switched back on. On main each fails its assertion; with this change all 12 of their registrations pass.🤖 Generated with Claude Code