HaxeFoundation / HaxeFoundation/haxe

`?.` Null-Safety Fails with `match` Method on Enums

Open
#11,893 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

### Description of the Issue

The `null-safety` behavior of the `?.` operator does not work correctly when calling the `match` method on a `null` value. This results in a `TypeError: v is null`, even though the `?.` operator is expected to prevent method calls on `null`.

---

### Minimal Reproducible Example ([see here](https://try.haxe.org/#14D8cc45))

```haxe
class Test {
static function main() {
var v:TestEnum = Value1;
trace(v?.getName()); // output: Value1
trace(v?.match(Value1)); // output: true

v = Value2;
trace(v?.getName()); // output: Value2
trace(v?.match(Value1)); // output: false

v = null;
trace(v?.getName()); // output:
trace(v?.match(Value1)); // Null Pointer Exception occurs here
}
}

enum TestEnum {
Value1;
Value2;
}
```

---

### Expected Behavior

The `?.` operator should properly handle `null` values and avoid calling the `match` method when the value is `null`. For example, if `v = null`, the statement `trace(v?.match(Value1));` should evaluate to `null` without throwing an error.

---

### Actual Behavior

When `v?.match(Value1)` is called and `v` is `null`, a `TypeError: v is null` is thrown, breaking the expected `null-safety` behavior of the `?.` operator.

---

### Environment
- **Haxe Version:** 4.3.6
- **Target Platform:** html5 (other not checked)

---

### Additional Information

This issue violates the `null-safety` guarantees of the `?.` operator, leading to unexpected runtime errors when relying on this functionality in code.

Contributor guide

Open the contributing guide

Research direction

Start by running the minimal reproducible example with Haxe 4.3.6 targeting html5, then inspect how the `?.` operator handles enum `match` calls. Done means `v?.match(Value1)` evaluates to null when `v` is null without a TypeError, with regression coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.