Răsfoiți Sursa

Support overloading true extern constructors. (#11979)

* Support overloading true extern constructors.

* Fix expected error message in extern-overloads/not-on-constructor test.
Zeta 5 luni în urmă
părinte
comite
cff64495aa

+ 10 - 6
src/typing/typeloadFields.ml

@@ -1289,7 +1289,10 @@ let create_method (ctx,cctx,fctx) c f cf fd p =
 		if ctx.com.config.pf_overload then
 			add_class_field_flag cf CfOverload
 		else if fctx.field_kind = CfrConstructor then
-			invalid_modifier ctx.com fctx "overload" "constructor" p
+			if (has_class_flag c CExtern || fctx.is_extern) && not fctx.is_inline then
+				add_class_field_flag cf CfOverload
+			else
+				invalid_modifier_only ctx.com fctx "overload" "on extern non-inline constructors" p
 		else begin
 			add_class_field_flag cf CfOverload;
 			if not (has_class_flag c CExtern || fctx.is_extern) then
@@ -1681,12 +1684,13 @@ let init_class ctx_c cctx c p herits fields =
 				begin match c.cl_constructor with
 				| None ->
 					c.cl_constructor <- Some cf
-				| Some ctor when ctx.com.config.pf_overload ->
-					if has_class_field_flag cf CfOverload && has_class_field_flag ctor CfOverload then
-						ctor.cf_overloads <- cf :: ctor.cf_overloads
-					else
-						display_error ctx.com ("If using overloaded constructors, all constructors must be declared with 'overload'") (if has_class_field_flag cf CfOverload then ctor.cf_pos else cf.cf_pos)
 				| Some ctor ->
+					if ctx.com.config.pf_overload || ((has_class_flag c CExtern || fctx.is_extern) && not fctx.is_inline) then
+						if has_class_field_flag cf CfOverload && has_class_field_flag ctor CfOverload then
+							ctor.cf_overloads <- cf :: ctor.cf_overloads
+						else
+							display_error ctx.com ("If using overloaded constructors, all constructors must be declared with 'overload'") (if has_class_field_flag cf CfOverload then ctor.cf_pos else cf.cf_pos)
+					else
 						display_error ctx.com "Duplicate constructor" p
 				end
 			| CfrInit ->

+ 11 - 0
tests/misc/projects/extern-overloads/extern-constructor/Main.hx

@@ -0,0 +1,11 @@
+function main() {
+	new Foo();
+	new Foo("hello");
+	new Foo(0);
+}
+
+extern class Foo {
+	overload function new():Void;
+	overload function new(s:String):Void;
+	overload function new(i:Int):Void;
+}

+ 3 - 0
tests/misc/projects/extern-overloads/extern-constructor/compile.hxml

@@ -0,0 +1,3 @@
+-m Main
+-js out.js
+--no-output

+ 4 - 0
tests/misc/projects/extern-overloads/not-on-constructor/Main.hx

@@ -1,3 +1,7 @@
 class Main {
 	overload function new() {}
+}
+
+class Foo {
+	overload extern inline function new() {}
 }

+ 2 - 1
tests/misc/projects/extern-overloads/not-on-constructor/compile-fail.hxml.stderr

@@ -1 +1,2 @@
-Main.hx:2: characters 2-10 : Invalid modifier: overload on constructor
+Main.hx:6: characters 2-10 : Invalid modifier: overload is only supported on extern non-inline constructors
+Main.hx:2: characters 2-10 : Invalid modifier: overload is only supported on extern non-inline constructors