From 92ba998bac2609b746d63821aef0601eb335151c Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 9 Mar 2021 10:55:42 +0800 Subject: [PATCH 1/2] simplify x##property#= 3 into x#property#= 3 --- jscomp/frontend/ast_exp_apply.ml | 43 +++++++++++++++++------------- jscomp/test/hash_sugar_desugar.js | 6 +++++ jscomp/test/hash_sugar_desugar.ml | 2 ++ jscomp/test/hash_sugar_desugar.mli | 2 +- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/jscomp/frontend/ast_exp_apply.ml b/jscomp/frontend/ast_exp_apply.ml index fdc08751ae..b42ebb8563 100644 --- a/jscomp/frontend/ast_exp_apply.ml +++ b/jscomp/frontend/ast_exp_apply.ml @@ -202,25 +202,32 @@ let app_exp_mapper ]} *) | Some {op = "#="; loc; args = [obj; arg]} -> - begin match view_as_app obj ["##"] with - | Some { args = [obj; { - pexp_desc = - Pexp_ident {txt = Lident name} + let gen_assignment obj name name_loc = + sane_property_name_check name_loc name; + Exp.constraint_ ~loc + { e with + pexp_desc = + Ast_uncurry_apply.method_apply loc self obj + (name ^ Literals.setter_suffix) [Nolabel,arg] } + (Ast_literal.type_unit ~loc ()) in + begin match obj.pexp_desc with + | Pexp_send (obj , {txt = name; loc = name_loc}) + -> gen_assignment obj name name_loc + | _ -> + match view_as_app obj ["##"] with + | Some { args = [obj; { + pexp_desc = + Pexp_ident {txt = Lident name} | Pexp_constant ( - Pconst_string - (name, None)); pexp_loc - } - ] - } - -> - sane_property_name_check pexp_loc name; - Exp.constraint_ ~loc - { e with - pexp_desc = - Ast_uncurry_apply.method_apply loc self obj - (name ^ Literals.setter_suffix) [Nolabel,arg] } - (Ast_literal.type_unit ~loc ()) - | _ -> assert false + Pconst_string + (name, None)); pexp_loc = name_loc + } + ] + } + -> + gen_assignment obj name name_loc + | _ -> + Location.raise_errorf ~loc "invalid #= assignment" end | Some { op = "|."; loc; } -> Location.raise_errorf ~loc diff --git a/jscomp/test/hash_sugar_desugar.js b/jscomp/test/hash_sugar_desugar.js index e530b43572..e2ba1fe7cc 100644 --- a/jscomp/test/hash_sugar_desugar.js +++ b/jscomp/test/hash_sugar_desugar.js @@ -19,6 +19,11 @@ function h4(u) { return u.hi(1, 2); } +function g5(u) { + u.hi = 3; + +} + function h5(u) { u.hi = 3; @@ -45,6 +50,7 @@ exports.h1 = h1; exports.h2 = h2; exports.h3 = h3; exports.h4 = h4; +exports.g5 = g5; exports.h5 = h5; exports.h6 = h6; exports.h7 = h7; diff --git a/jscomp/test/hash_sugar_desugar.ml b/jscomp/test/hash_sugar_desugar.ml index 3e69ab2ac3..94dbeb7a62 100644 --- a/jscomp/test/hash_sugar_desugar.ml +++ b/jscomp/test/hash_sugar_desugar.ml @@ -29,6 +29,8 @@ let h3 u = u ["hi"] (1, 2) *) +let g5 u = + u#hi #= 3 let h5 u = u ##hi #= 3 (* assignment diff --git a/jscomp/test/hash_sugar_desugar.mli b/jscomp/test/hash_sugar_desugar.mli index 69d7fb47fe..36355cded1 100644 --- a/jscomp/test/hash_sugar_desugar.mli +++ b/jscomp/test/hash_sugar_desugar.mli @@ -4,7 +4,7 @@ val h1 : < p : 'a; .. > -> 'a val h2 : < m : (int -> int -> 'a [@bs]); .. > -> 'a val h3 : < hi : int -> int -> 'a; .. > -> 'a val h4 : < hi : (int -> int -> 'a [@bs.meth]); .. > -> 'a - +val g5 : < hi : int [@bs.set]; .. > -> unit val h5 : < hi : int [@bs.set]; .. > -> unit (* The inferred type is val h5 : < hi#= : (int -> unit [@bs.meth]); .. > -> unit From 944f3e0aade3eda92b2517c9ff7dae6bd85a36fe Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 9 Mar 2021 10:55:55 +0800 Subject: [PATCH 2/2] snapshot --- lib/4.06.1/unstable/js_compiler.ml | 43 +++++++++++++++++------------- lib/4.06.1/whole_compiler.ml | 43 +++++++++++++++++------------- 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/lib/4.06.1/unstable/js_compiler.ml b/lib/4.06.1/unstable/js_compiler.ml index 61f0f2a0b6..c29d0119dc 100644 --- a/lib/4.06.1/unstable/js_compiler.ml +++ b/lib/4.06.1/unstable/js_compiler.ml @@ -404623,25 +404623,32 @@ let app_exp_mapper ]} *) | Some {op = "#="; loc; args = [obj; arg]} -> - begin match view_as_app obj ["##"] with - | Some { args = [obj; { - pexp_desc = - Pexp_ident {txt = Lident name} + let gen_assignment obj name name_loc = + sane_property_name_check name_loc name; + Exp.constraint_ ~loc + { e with + pexp_desc = + Ast_uncurry_apply.method_apply loc self obj + (name ^ Literals.setter_suffix) [Nolabel,arg] } + (Ast_literal.type_unit ~loc ()) in + begin match obj.pexp_desc with + | Pexp_send (obj , {txt = name; loc = name_loc}) + -> gen_assignment obj name name_loc + | _ -> + match view_as_app obj ["##"] with + | Some { args = [obj; { + pexp_desc = + Pexp_ident {txt = Lident name} | Pexp_constant ( - Pconst_string - (name, None)); pexp_loc - } - ] - } - -> - sane_property_name_check pexp_loc name; - Exp.constraint_ ~loc - { e with - pexp_desc = - Ast_uncurry_apply.method_apply loc self obj - (name ^ Literals.setter_suffix) [Nolabel,arg] } - (Ast_literal.type_unit ~loc ()) - | _ -> assert false + Pconst_string + (name, None)); pexp_loc = name_loc + } + ] + } + -> + gen_assignment obj name name_loc + | _ -> + Location.raise_errorf ~loc "invalid #= assignment" end | Some { op = "|."; loc; } -> Location.raise_errorf ~loc diff --git a/lib/4.06.1/whole_compiler.ml b/lib/4.06.1/whole_compiler.ml index 51a353d5fc..f4b3e9c868 100644 --- a/lib/4.06.1/whole_compiler.ml +++ b/lib/4.06.1/whole_compiler.ml @@ -407665,25 +407665,32 @@ let app_exp_mapper ]} *) | Some {op = "#="; loc; args = [obj; arg]} -> - begin match view_as_app obj ["##"] with - | Some { args = [obj; { - pexp_desc = - Pexp_ident {txt = Lident name} + let gen_assignment obj name name_loc = + sane_property_name_check name_loc name; + Exp.constraint_ ~loc + { e with + pexp_desc = + Ast_uncurry_apply.method_apply loc self obj + (name ^ Literals.setter_suffix) [Nolabel,arg] } + (Ast_literal.type_unit ~loc ()) in + begin match obj.pexp_desc with + | Pexp_send (obj , {txt = name; loc = name_loc}) + -> gen_assignment obj name name_loc + | _ -> + match view_as_app obj ["##"] with + | Some { args = [obj; { + pexp_desc = + Pexp_ident {txt = Lident name} | Pexp_constant ( - Pconst_string - (name, None)); pexp_loc - } - ] - } - -> - sane_property_name_check pexp_loc name; - Exp.constraint_ ~loc - { e with - pexp_desc = - Ast_uncurry_apply.method_apply loc self obj - (name ^ Literals.setter_suffix) [Nolabel,arg] } - (Ast_literal.type_unit ~loc ()) - | _ -> assert false + Pconst_string + (name, None)); pexp_loc = name_loc + } + ] + } + -> + gen_assignment obj name name_loc + | _ -> + Location.raise_errorf ~loc "invalid #= assignment" end | Some { op = "|."; loc; } -> Location.raise_errorf ~loc