Skip to content

Commit

Permalink
[typer] allow type parameters in rvalue functions
Browse files Browse the repository at this point in the history
see #11513
  • Loading branch information
Simn committed Jan 30, 2024
1 parent 19a490c commit ac57059
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/typing/typer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,13 @@ and type_local_function ctx kind f with_type p =
let params = TypeloadFunction.type_function_params ctx f TPHLocal (match name with None -> "localfun" | Some (n,_) -> n) p in
if params <> [] then begin
if name = None then display_error ctx.com "Type parameters not supported in unnamed local functions" p;
if with_type <> WithType.NoValue then raise_typing_error "Type parameters are not supported for rvalue functions" p
begin match ctx.com.platform with
| Java | Cs ->
(* gencommon just can't *)
if with_type <> WithType.NoValue then raise_typing_error "Type parameters are not supported for rvalue functions" p
| _ ->
()
end
end;
let v,pname = (match name with
| None -> None,p
Expand Down Expand Up @@ -1323,6 +1329,13 @@ and type_local_function ctx kind f with_type p =
| _ ->
());
let ft = TFun (targs,rt) in
let ft = match with_type with
| WithType.NoValue ->
ft
| _ ->
(* We want to apply params as if we accessed the function by name (see type_ident_raise). *)
apply_params params (Monomorph.spawn_constrained_monos (fun t -> t) params) ft
in
let v = (match v with
| None -> None
| Some v ->
Expand Down Expand Up @@ -1364,7 +1377,7 @@ and type_local_function ctx kind f with_type p =
else []
in
let exprs =
if is_rec then begin
if is_rec || (params <> [] && with_type <> WithType.NoValue) then begin
if inline then display_error ctx.com "Inline function cannot be recursive" e.epos;
(mk (TVar (v,Some (mk (TConst TNull) ft p))) ctx.t.tvoid p) ::
(mk (TBinop (OpAssign,mk (TLocal v) ft p,e)) ft p) ::
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/src/unit/issues/Issue11513.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package unit.issues;

private enum abstract Message<TBody>(Int) {
final move_to:Message<{x:Int, y:Int}>;
}

private typedef Listener = {
function send<TBody>(name:Message<TBody>, body:TBody):Void;
}

private final listener:Listener = {
send: function send<T>(message:Message<T>, body:T) {
switch (message) {
case move_to:
}
}
}

class Issue11513 extends Test {
function test() {
listener.send(move_to, {x: 1, y: 2});
noAssert();
}
}

0 comments on commit ac57059

Please sign in to comment.