From f54e91a95b8cf59dcf0c5f20552821e4bf69cb1d Mon Sep 17 00:00:00 2001 From: Zeta <53486764+Apprentice-Alchemist@users.noreply.github.com> Date: Thu, 9 May 2024 13:59:17 +0200 Subject: [PATCH] [struct-init] Ignore non-physical fields. (#11662) --- src/typing/typeloadFields.ml | 6 ++--- tests/misc/projects/Issue11661/Main.hx | 27 +++++++++++++++++++++ tests/misc/projects/Issue11661/compile.hxml | 2 ++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 tests/misc/projects/Issue11661/Main.hx create mode 100644 tests/misc/projects/Issue11661/compile.hxml diff --git a/src/typing/typeloadFields.ml b/src/typing/typeloadFields.ml index 175608066b3..11533c92015 100644 --- a/src/typing/typeloadFields.ml +++ b/src/typing/typeloadFields.ml @@ -209,9 +209,7 @@ let ensure_struct_init_constructor ctx c ast_fields p = let params = extract_param_types c.cl_params in let ethis = mk (TConst TThis) (TInst(c,params)) p in let doc_buf = Buffer.create 0 in - let args,el,tl = List.fold_left (fun (args,el,tl) cf -> match cf.cf_kind with - | Var { v_write = AccNever } -> args,el,tl - | Var _ -> + let args,el,tl = List.fold_left (fun (args,el,tl) cf -> if is_physical_var_field cf then let has_default_expr = field_has_default_expr cf.cf_name in let opt = has_default_expr || (Meta.has Meta.Optional cf.cf_meta) in let t = if opt then ctx.t.tnull cf.cf_type else cf.cf_type in @@ -245,7 +243,7 @@ let ensure_struct_init_constructor ctx c ast_fields p = Buffer.add_string doc_buf "\n"; end; (v,None) :: args,e :: el,(cf.cf_name,opt,t) :: tl - | Method _ -> + else args,el,tl ) ([],[],[]) (List.rev c.cl_ordered_fields) in let el = match super_expr with Some e -> e :: el | None -> el in diff --git a/tests/misc/projects/Issue11661/Main.hx b/tests/misc/projects/Issue11661/Main.hx new file mode 100644 index 00000000000..3d5d2043240 --- /dev/null +++ b/tests/misc/projects/Issue11661/Main.hx @@ -0,0 +1,27 @@ +@:structInit +class Foo { + @:isVar public var real(get, set):String; + public var foo(get, never):String; + public var bar(get, set):String; + + function get_real() + return real; + + function set_real(v) + return real = v; + + function get_foo() + return "foo"; + + function get_bar() + return "bar"; + + function set_bar(v) + return v; +} + +function main() { + var foo:Foo = { + real: "real" + }; +} diff --git a/tests/misc/projects/Issue11661/compile.hxml b/tests/misc/projects/Issue11661/compile.hxml new file mode 100644 index 00000000000..5f82c470c12 --- /dev/null +++ b/tests/misc/projects/Issue11661/compile.hxml @@ -0,0 +1,2 @@ +-m Main +--interp \ No newline at end of file