From ea6d51043489b361cc8f4a4fa179d047ba0a769c Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Thu, 8 Feb 2024 08:53:14 +0100 Subject: [PATCH 1/2] add empty enum as haxe.Unit --- std/haxe/Unit.hx | 28 ++++++++++++++++++++++++ tests/unit/src/unitstd/haxe/Unit.unit.hx | 9 ++++++++ 2 files changed, 37 insertions(+) create mode 100644 std/haxe/Unit.hx create mode 100644 tests/unit/src/unitstd/haxe/Unit.unit.hx diff --git a/std/haxe/Unit.hx b/std/haxe/Unit.hx new file mode 100644 index 00000000000..b2d1b1cf926 --- /dev/null +++ b/std/haxe/Unit.hx @@ -0,0 +1,28 @@ +/* + * Copyright (C)2005-2019 Haxe Foundation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +package haxe; + +/** + A unit type which can only be `null` at run-time. +**/ +enum Unit {} diff --git a/tests/unit/src/unitstd/haxe/Unit.unit.hx b/tests/unit/src/unitstd/haxe/Unit.unit.hx new file mode 100644 index 00000000000..e7b38f4867a --- /dev/null +++ b/tests/unit/src/unitstd/haxe/Unit.unit.hx @@ -0,0 +1,9 @@ +var u:haxe.Unit = null; +Reflect.isObject(u) == false; +Reflect.isEnumValue(u) == false; +Reflect.isFunction(u) == false; +Reflect.compare(u, null) == 0; +Reflect.compare(null, u) == 0; +Reflect.compare(u, u) == 0; +Type.getClass(u) == null; +Type.getEnum(u) == null; From c5d67c9e20f16b7e235352a9ba48dba34611ca91 Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Thu, 8 Feb 2024 10:07:55 +0100 Subject: [PATCH 2/2] add `@:nullable` and respect it for null-safety --- src-json/meta.json | 7 +++++++ src/typing/nullSafety.ml | 2 ++ std/haxe/Unit.hx | 1 + 3 files changed, 10 insertions(+) diff --git a/src-json/meta.json b/src-json/meta.json index ebbd0a5c445..52f511980ee 100644 --- a/src-json/meta.json +++ b/src-json/meta.json @@ -738,6 +738,13 @@ "platforms": ["flash"], "internal": true }, + { + "name": "Nullable", + "metadata": ":nullable", + "doc": "Explicitly admits `null` as valid value for an enum, which affects null-safety", + "targets": ["TEnum"], + "links": ["https://haxe.org/manual/types-nullability.html"] + }, { "name": "NullSafety", "metadata": ":nullSafety", diff --git a/src/typing/nullSafety.ml b/src/typing/nullSafety.ml index 63d9d0f1f78..8d7986d58b5 100644 --- a/src/typing/nullSafety.ml +++ b/src/typing/nullSafety.ml @@ -85,6 +85,8 @@ let rec is_nullable_type ?(dynamic_is_nullable=false) = function is_nullable_type (apply_typedef t tl) | (TDynamic _) as t -> dynamic_is_nullable && t == t_dynamic + | TEnum(en,_) -> + Meta.has Meta.Nullable en.e_meta | _ -> false (* diff --git a/std/haxe/Unit.hx b/std/haxe/Unit.hx index b2d1b1cf926..8b33473e714 100644 --- a/std/haxe/Unit.hx +++ b/std/haxe/Unit.hx @@ -25,4 +25,5 @@ package haxe; /** A unit type which can only be `null` at run-time. **/ +@:nullable enum Unit {}