Skip to content

Compare two enum values as their element type - #691

Merged
ASDAlexander77 merged 1 commit into
mainfrom
enum-compare-lowering
Oct 26, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
enum-compare-lowering

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

What

A comparison whose left operand was still an enum, such as a variable or parameter of the enum type or E | undefined, had no case in the LLVM lowering (LogicOp in UnaryBinLogicalOrHelper.h, where it was commented out). It fell through to the default: a warning, "Not applicable logical operator for type: 'E'", and a constant false.

enum Button { Left, Right }
function f(a: Button, b: Button) { return a == b; }
f(Button.Right, Button.Right);   // was false

Comparing with a member (held == Button.Right) worked only because the member is a literal, which MLIRGen turns into an si32 first.

Fix

An enum operand is lowered as its element type: integers for numeric enums, a string compare for string enums. E | undefined already unwraps into this path through the optional lowering, and so does a value narrowed by !== undefined. The "doesn't narrow" symptom in the report was this same bug.

Tests

  • New: tslang/test/tester/tests/00enum_compare.ts, registered for compile and JIT runs (rc and none corpus runs pick it up too). It covers ==/!=/< on enum parameters, E | undefined vs E, E | undefined vs E | undefined, string enums, and the original held == other case from native_ts_graphics.
  • Windows Release: ctest -C Release: 100% passed, 5798/5798 (3 disabled as before).

Docs

TSLANG_ISSUES.md: items 1-19 were already fixed or answered, and 20 is fixed here. The file now says nothing is open and points to git history for the old write-ups.

🤖 Generated with Claude Code

A comparison whose left operand was still an enum (a variable or parameter of
the enum type, or `E | undefined`) had no case in the LLVM lowering: it warned
"Not applicable logical operator for type: 'E'" and was constant false, `a == a`
included. Only a comparison with a member (a literal) worked, as an integer.

TSLANG_ISSUES.md: items 1-19 were fixed or answered and 20 is fixed here, so
the file now lists nothing open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 211b664 into main Oct 26, 2026
2 of 3 checks passed
@ASDAlexander77
ASDAlexander77 deleted the enum-compare-lowering branch October 26, 2026 10:57
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