From ac570598b4bfda48c43fae6337d9cc61d2f71f0f Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Tue, 30 Jan 2024 10:21:56 +0100 Subject: [PATCH] [typer] allow type parameters in rvalue functions see #11513 --- src/typing/typer.ml | 17 +++++++++++++++-- tests/unit/src/unit/issues/Issue11513.hx | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 tests/unit/src/unit/issues/Issue11513.hx diff --git a/src/typing/typer.ml b/src/typing/typer.ml index 6fad5ed24a5..811455ff3b9 100644 --- a/src/typing/typer.ml +++ b/src/typing/typer.ml @@ -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 @@ -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 -> @@ -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) :: diff --git a/tests/unit/src/unit/issues/Issue11513.hx b/tests/unit/src/unit/issues/Issue11513.hx new file mode 100644 index 00000000000..67c46b51532 --- /dev/null +++ b/tests/unit/src/unit/issues/Issue11513.hx @@ -0,0 +1,24 @@ +package unit.issues; + +private enum abstract Message(Int) { + final move_to:Message<{x:Int, y:Int}>; +} + +private typedef Listener = { + function send(name:Message, body:TBody):Void; +} + +private final listener:Listener = { + send: function send(message:Message, body:T) { + switch (message) { + case move_to: + } + } +} + +class Issue11513 extends Test { + function test() { + listener.send(move_to, {x: 1, y: 2}); + noAssert(); + } +}