Skip to content

Commit

Permalink
allow @:using with Class and Enum (#11553)
Browse files Browse the repository at this point in the history
closes #10106
  • Loading branch information
Simn authored Feb 7, 2024
1 parent aaf9b43 commit a45316c
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/context/display/displayFields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,31 @@ let collect_static_extensions ctx items e p =
| _ ->
let items = loop items ctx.m.module_using in
let items = loop items ctx.g.global_using in
let items = try
let mt = module_type_of_type e.etype in
loop items (t_infos mt).mt_using
with Exit ->
items
let rec loop_module_using items t = match follow_without_type t with
| TInst(c,_) ->
loop items c.cl_using
| TEnum(en,_) ->
loop items en.e_using
| TType(td,tl) ->
let items = loop items td.t_using in
loop_module_using items (apply_typedef td tl)
| TAbstract(a,_) ->
loop items a.a_using
| TAnon an ->
begin match !(an.a_status) with
| ClassStatics c ->
loop items c.cl_using
| EnumStatics en ->
loop items en.e_using
| AbstractStatics a ->
loop items a.a_using
| _ ->
items
end
| _ ->
items
in
let items = loop_module_using items e.etype in
items

let collect ctx e_ast e dk with_type p =
Expand Down
7 changes: 7 additions & 0 deletions src/core/tFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,13 @@ let rec module_type_of_type = function
(match r.tm_type with
| Some t -> module_type_of_type t
| _ -> raise Exit)
| TAnon an ->
begin match !(an.a_status) with
| ClassStatics c -> TClassDecl c
| EnumStatics en -> TEnumDecl en
| AbstractStatics a -> TAbstractDecl a
| _ -> raise Exit
end
| _ ->
raise Exit

Expand Down
57 changes: 57 additions & 0 deletions tests/display/src/cases/Issue10106.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cases;

class Issue10106 extends DisplayTestCase {
/**
class CExtension {
public static function toS(c: C): String {
return 'c';
}
public static function fromS(cls: Class<C>, s: String):C {
return new C();
}
}
@:using(cases.Issue10106.CExtension)
class C {
public function new(){}
}
class Main {
static public function main() {
C.{-1-}
}
}
**/
function testClass() {
eq(true, hasField(fields(pos(1)), "fromS", "(s : String) -> cases.C"));
}

/**
class EnExtension {
public static function toS(e:En):String {
return '${e}';
}
public static function fromS(en:Enum<En>, s:String):En {
return A;
}
}
@:using(cases.Issue10106.EnExtension)
enum En {
A;
B;
}
class Main {
static public function main() {
En.{-1-}
}
}
**/
function testEnum() {
eq(true, hasField(fields(pos(1)), "fromS", "(s : String) -> cases.En"));
}
}
35 changes: 35 additions & 0 deletions tests/unit/src/unit/issues/Issue10106.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package unit.issues;

private class CExtension {
public static function fromS(cls:Class<C>, s:String) {
return new C(s);
}
}

@:using(unit.issues.Issue10106.CExtension)
private class C {
public final s:String;

public function new(s:String) {
this.s = s;
}
}

private class EnExtension {
public static function fromS(en:Enum<En>, st:String):En {
return A;
}
}

@:using(unit.issues.Issue10106.EnExtension)
private enum En {
A;
B;
}

class Issue10106 extends Test {
function test() {
eq(A, En.fromS("A"));
eq("foo", C.fromS("foo").s);
}
}

0 comments on commit a45316c

Please sign in to comment.