Skip to content

Commit

Permalink
[java] don't check native signatures on extern functions
Browse files Browse the repository at this point in the history
closes #11131
  • Loading branch information
Simn authored and kLabz committed Apr 13, 2023
1 parent ee623c3 commit 0789ec4
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/typing/typeloadFields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ let init_field (ctx,cctx,fctx) f =
delay ctx PTypeField (fun() -> InheritDoc.build_class_field_doc ctx (Some c) cf);
cf

let check_overload ctx f fs =
let check_overload ctx f fs is_extern_class =
try
let f2 =
List.find (fun f2 ->
Expand All @@ -1667,15 +1667,16 @@ let check_overload ctx f fs =
display_error ~depth:1 ctx.com (compl_msg "The second field is declared here") f2.cf_pos;
false
with Not_found -> try
(* OVERLOADTODO: generalize this and respect whether or not we actually generate the functions *)
if ctx.com.platform <> Java then raise Not_found;
if ctx.com.platform <> Java || is_extern_class then raise Not_found;
let get_vmtype = ambiguate_funs in
let f2 =
List.find (fun f2 ->
f != f2 &&
Overloads.same_overload_args ~get_vmtype f.cf_type f2.cf_type f f2
) fs
in
(* Don't bother checking this on externs and assume the users know what they're doing (issue #11131) *)
if has_class_field_flag f CfExtern && has_class_field_flag f2 CfExtern then raise Not_found;
display_error ctx.com (
"Another overloaded field of similar signature was already declared : " ^
f.cf_name ^
Expand All @@ -1688,10 +1689,11 @@ let check_overload ctx f fs =

let check_overloads ctx c =
(* check if field with same signature was declared more than once *)
let is_extern = has_class_flag c CExtern in
let check_field f =
if has_class_field_flag f CfOverload then begin
let all = f :: f.cf_overloads in
ignore(List.fold_left (fun b f -> b && check_overload ctx f all) true all)
ignore(List.fold_left (fun b f -> b && check_overload ctx f all is_extern) true all)
end
in
List.iter check_field c.cl_ordered_fields;
Expand Down
38 changes: 38 additions & 0 deletions tests/misc/java/projects/Issue11131/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@:native("test.MathOperation")
extern class MathOperation {
public static inline overload function perform(op:Int->Int, value:Int) {
return op(value);
}

public static inline overload function perform(op:Int->Int->Int, value:Int) {
return op(value, value);
}
}

@:native("test.MathOperation")
class MathOperationNotExtern {
public static extern inline overload function perform(op:Int->Int, value:Int) {
return op(value);
}

public static extern inline overload function perform(op:Int->Int->Int, value:Int) {
return op(value, value);
}
}

class Main {
static function main() {
Sys.println(MathOperation.perform(double, 3));
Sys.println(MathOperation.perform(multiply, 3));
Sys.println(MathOperationNotExtern.perform(double, 3));
Sys.println(MathOperationNotExtern.perform(multiply, 3));
}

static function double(a):Int {
return a * 2;
}

static function multiply(a, b):Int {
return a * b;
}
}
25 changes: 25 additions & 0 deletions tests/misc/java/projects/Issue11131/MainFail.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@:native("test.MathOperation")
class MathOperation {
public static inline overload function perform(op:Int->Int, value:Int) {
return op(value);
}

public static inline overload function perform(op:Int->Int->Int, value:Int) {
return op(value, value);
}
}

class MainFail {
static function main() {
Sys.println(MathOperation.perform(double, 3));
Sys.println(MathOperation.perform(multiply, 3));
}

static function double(a):Int {
return a * 2;
}

static function multiply(a, b):Int {
return a * b;
}
}
3 changes: 3 additions & 0 deletions tests/misc/java/projects/Issue11131/compile-fail.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-cp src
-main MainFail
--jvm run.jar
3 changes: 3 additions & 0 deletions tests/misc/java/projects/Issue11131/compile-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MainFail.hx:3: lines 3-5 : Another overloaded field of similar signature was already declared : perform
MainFail.hx:3: lines 3-5 : ... The signatures are different in Haxe, but not in the target language
MainFail.hx:7: lines 7-9 : ... The second field is declared here
4 changes: 4 additions & 0 deletions tests/misc/java/projects/Issue11131/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-cp src
-main Main
--jvm run.jar
--cmd java -jar run.jar
4 changes: 4 additions & 0 deletions tests/misc/java/projects/Issue11131/compile.hxml.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
6
9
6
9

0 comments on commit 0789ec4

Please sign in to comment.