Sfoglia il codice sorgente

Add haxe.Constraints.NotVoid (#8357)

Rudy Ges 5 anni fa
parent
commit
967bd500a6

+ 4 - 1
src/core/tUnification.ml

@@ -454,6 +454,9 @@ let rec unify a b =
 	| TAbstract ({a_path=[],"Void"},_) , _
 	| _ , TAbstract ({a_path=[],"Void"},_) ->
 		error [cannot_unify a b]
+	| TAbstract ({ a_path = ["haxe"],"NotVoid" },[]), _
+	| _, TAbstract ({ a_path = ["haxe"],"NotVoid" },[]) ->
+		()
 	| TAbstract (a1,tl1) , TAbstract (a2,tl2) ->
 		unify_abstracts a b a1 tl1 a2 tl2
 	| TInst (c1,tl1) , TInst (c2,tl2) ->
@@ -849,4 +852,4 @@ let does_unify a b =
 		unify a b;
 		true
 	with Unify_error _ ->
-		false
+		false

+ 8 - 0
std/haxe/Constraints.hx

@@ -40,6 +40,14 @@ abstract Function(Dynamic) {}
 **/
 abstract FlatEnum(Dynamic) {}
 
+/**
+	This type unifies with anything but `Void`.
+
+	It is intended to be used as a type parameter constraint. If used as a real
+	type, the underlying type will be `Dynamic`.
+**/
+abstract NotVoid(Dynamic) { }
+
 /**
 	This type unifies with any instance of classes that have a constructor
 	which

+ 14 - 0
tests/misc/projects/Issue6810/Fail.hx

@@ -0,0 +1,14 @@
+import haxe.Constraints.NotVoid;
+
+typedef FakeVoid = Void;
+
+class Fail {
+	public static function main() {
+		test(void);
+		test(fakeVoid);
+	}
+
+	static function void():Void {}
+	static function fakeVoid():FakeVoid {}
+	static function test<T:NotVoid>(f:Void->T):T return f();
+}

+ 11 - 0
tests/misc/projects/Issue6810/Main.hx

@@ -0,0 +1,11 @@
+import haxe.Constraints.NotVoid;
+
+class Main {
+	public static function main() {
+		test(function() return 42);
+		test(function() return "test");
+	}
+
+	static function test<T:NotVoid>(f:Void->T):T return f();
+}
+

+ 1 - 0
tests/misc/projects/Issue6810/compile-fail.hxml

@@ -0,0 +1 @@
+Fail

+ 4 - 0
tests/misc/projects/Issue6810/compile-fail.hxml.stderr

@@ -0,0 +1,4 @@
+Fail.hx:8: characters 3-7 : Constraint check failure for test.T
+Fail.hx:8: characters 3-7 : FakeVoid should be haxe.NotVoid
+Fail.hx:7: characters 3-7 : Constraint check failure for test.T
+Fail.hx:7: characters 3-7 : Void should be haxe.NotVoid

+ 1 - 0
tests/misc/projects/Issue6810/compile.hxml

@@ -0,0 +1 @@
+-main Main