Skip to content

@cpp class | null tested for truth with a boolean (h && cond): no instanceof - #765

Open
ASDAlexander77 wants to merge 1 commit into
mainfrom
cpp-union-truthiness
Open

ASDAlexander77 wants to merge 1 commit into
mainfrom
cpp-union-truthiness

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Fixes TSLANG_ISSUES 23: if (h && n == 3) with h a @cpp class | null failed with "instanceof can't be used with a @cpp class" and "'___cast' not all generic types can be identified".

h && n == 3 is typed Handle | null | boolean; making it a boolean goes through castFromUnion, whose generated ___cast tests every class member with instanceof, which a @cpp class refuses (its objects are C++ objects with no type information).

  • The class tests are now one writeClassTests lambda (used by the null-target and default branches).
  • When the union has a @cpp class and the class need not be told apart (the only class of the union, or tested for truth), it emits return t;: typeof has narrowed t enough, and truthiness is the null test.
  • Otherwise instanceof is emitted only for the non-@cpp classes.

Test: tests/00cpp_union_truth.ts (hand-made C++ object): h && n == 3, enabled && h with a handle and null, and two @cpp classes in a union with a boolean tested for truth. Registered compile/JIT default + own, and in the rc/none corpus. Full release ctest passes.

Issue 22 (one-field @value T | null) was already fixed by #763; checked with the current build.

🤖 Generated with Claude Code

…nger asks instanceof

`h && n == 3` with h a `Handle | null` (@cpp) is a union of the class, null
and boolean; castFromUnion's ___cast helper tested each class member with
instanceof, which a @cpp class refuses (its objects carry no type info).
When the class need not be told apart - the only class of the union, or the
union tested for truth - typeof has narrowed t enough: `return t`. Otherwise
only the non-@cpp classes are tested. TSLANG_ISSUES 23.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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.

1 participant