Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove pure field from J.Str, always true #1200

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jscomp/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ and expression_desc =
it will be true when it's a method
Last param represents whether the function returns unit.
*)
| Str of { pure : bool; string : string }
| Str of string
(* A string is UTF-8 encoded, the string may contain
escape sequences.
The first argument is used to mark it is non-pure, please
Expand Down
7 changes: 2 additions & 5 deletions jscomp/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
->
no_side_effect a && no_side_effect b
| Is_null_or_undefined b -> no_side_effect b
| Str { pure = b; _ } -> b
| Str _ -> true
| Array { items = xs; mutable_flag = _mutable_flag }
| Caml_block { fields = xs; mutable_flag = _mutable_flag; _ } ->
(* create [immutable] block,
Expand Down Expand Up @@ -194,10 +194,7 @@ let rec eq_expression ({ expression_desc = x0; _ } : J.expression)
| Bin { op = op1; expr1 = a1; expr2 = b1 } ->
op0 = op1 && eq_expression a0 a1 && eq_expression b0 b1
| _ -> false)
| Str { pure = a0; string = b0 } -> (
match y0 with
| Str { pure = a1; string = b1 } -> a0 = a1 && b0 = b1
| _ -> false)
| Str a0 -> ( match y0 with Str a1 -> a0 = a1 | _ -> false)
| Unicode s0 -> ( match y0 with Unicode s1 -> s0 = s1 | _ -> false)
| Module { id = id0; dynamic_import = d0; _ } -> (
match y0 with
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ and expression_desc cxt ~(level : int) x : cxt =
string cxt s;
string cxt "\"";
cxt
| Str { string = s; _ } ->
| Str s ->
(*TODO --
when utf8-> it will not escape '\\' which is definitely not we want
*)
Expand Down Expand Up @@ -787,7 +787,7 @@ and expression_desc cxt ~(level : int) x : cxt =
(Object (List.map_combine_array fields el (fun i -> Js_op.Lit i)))
| Caml_block { fields = el; tag_info = Blk_poly_var; _ } -> (
match el with
| [ { expression_desc = Str { string = name; _ }; _ }; value ] ->
| [ { expression_desc = Str name; _ }; value ] ->
expression_desc cxt ~level
(Object
[
Expand Down
34 changes: 12 additions & 22 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ let pure_runtime_call ~module_name ~fn_name args =
args

let runtime_ref module_name fn_name = runtime_var_dot module_name fn_name

let str ?(pure = true) ?loc ?comment s : t =
make_expression ?loc ?comment (Str { pure; string = s })

let str ?loc ?comment s : t = make_expression ?loc ?comment (Str s)
let unicode ?loc ?comment s : t = make_expression ?loc ?comment (Unicode s)
let module_ ?loc ?comment id : t = make_expression ?loc ?comment (Module id)

Expand Down Expand Up @@ -428,7 +425,7 @@ let extension_access (e : t) ?name (pos : int32) : t =

let string_index ?loc ?comment (e0 : t) (e1 : t) : t =
match (e0.expression_desc, e1.expression_desc) with
| Str { string = s; _ }, Number (Int { i; _ }) ->
| Str s, Number (Int { i; _ }) ->
(* Don't optimize {j||j} *)
let i = Int32.to_int i in
if i >= 0 && i < String.length s then
Expand Down Expand Up @@ -512,7 +509,7 @@ let array_length ?loc ?comment (e : t) : t =

let string_length ?loc ?comment (e : t) : t =
match e.expression_desc with
| Str { string = v; _ } -> int ?comment (Int32.of_int (String.length v))
| Str v -> int ?comment (Int32.of_int (String.length v))
(* No optimization for {j||j}*)
| _ ->
make_expression ?loc ?comment
Expand Down Expand Up @@ -548,7 +545,7 @@ let function_length ?loc ?comment (e : t) : t =

let char_to_int ?loc ?comment (v : t) : t =
match v.expression_desc with
| Str { string = x; _ } ->
| Str x ->
(* No optimization for .. *)
assert (String.length x = 1);
int ~comment:(Printf.sprintf "%S" x) (Int32.of_int @@ Char.code x.[0])
Expand All @@ -557,22 +554,16 @@ let char_to_int ?loc ?comment (v : t) : t =

let rec string_append ?loc ?comment (e : t) (el : t) : t =
match (e.expression_desc, el.expression_desc) with
| ( Str { string = a; _ },
String_append
{ prefix = { expression_desc = Str { string = b; _ }; _ }; suffix = c }
) ->
| Str a, String_append { prefix = { expression_desc = Str b; _ }; suffix = c }
->
string_append ?comment (str (a ^ b)) c
| ( String_append
{ prefix = c; suffix = { expression_desc = Str { string = b; _ }; _ } },
Str { string = a; _ } ) ->
| String_append { prefix = c; suffix = { expression_desc = Str b; _ } }, Str a
->
string_append ?comment c (str (b ^ a))
| ( String_append
{ prefix = a; suffix = { expression_desc = Str { string = b; _ }; _ } },
String_append
{ prefix = { expression_desc = Str { string = c; _ }; _ }; suffix = d }
) ->
| ( String_append { prefix = a; suffix = { expression_desc = Str b; _ } },
String_append { prefix = { expression_desc = Str c; _ }; suffix = d } ) ->
string_append ?comment (string_append a (str (b ^ c))) d
| Str { string = a; _ }, Str { string = b; _ } -> str ?comment (a ^ b)
| Str a, Str b -> str ?comment (a ^ b)
| _, _ ->
make_expression ?loc ?comment (String_append { prefix = e; suffix = el })

Expand All @@ -595,8 +586,7 @@ let float_mod ?loc ?comment e1 e2 : J.expression =

let str_equal (str0 : J.expression_desc) (str1 : J.expression_desc) =
match (str0, str1) with
| Str { string = txt0; _ }, Str { string = txt1; _ }
| Unicode txt0, Unicode txt1 ->
| Str txt0, Str txt1 | Unicode txt0, Unicode txt1 ->
if String.equal txt0 txt1 then Some true
else if
Melange_ffi.Utf8_string.simple_comparison txt0
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ val runtime_call : module_name:string -> fn_name:string -> t list -> t
val pure_runtime_call : module_name:string -> fn_name:string -> t list -> t
val runtime_ref : string -> string -> t
val public_method_call : string -> t -> t -> Int32.t -> t list -> t
val str : ?pure:bool -> ?loc:Location.t -> ?comment:string -> string -> t
val str : ?loc:Location.t -> ?comment:string -> string -> t
val unicode : ?loc:Location.t -> ?comment:string -> string -> t
val module_ : ?loc:Location.t -> ?comment:string -> J.module_id -> t

Expand Down
6 changes: 3 additions & 3 deletions jscomp/core/js_of_lam_variant.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let eval (arg : J.expression) (dispatches : (string * string) list) : E.t =
if arg == E.undefined then E.undefined
else
match arg.expression_desc with
| Str { string = s; _ } -> E.str (List.assoc s dispatches)
| Str s -> E.str (List.assoc s dispatches)
| _ ->
E.of_block
[
Expand All @@ -58,7 +58,7 @@ let eval_as_event (arg : J.expression)
match arg.expression_desc with
| Caml_block
{
fields = [ { expression_desc = Str { string = s; _ }; _ }; cb ];
fields = [ { expression_desc = Str s; _ }; cb ];
tag_info = Blk_poly_var;
_;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ let eval_as_int (arg : J.expression) (dispatches : (string * int) list) : E.t =
if arg == E.undefined then E.undefined
else
match arg.expression_desc with
| Str { string = i; _ } -> E.int (Int32.of_int (List.assoc i dispatches))
| Str i -> E.int (Int32.of_int (List.assoc i dispatches))
| _ ->
E.of_block
[
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_stmt_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ let string_switch ?(comment : string option)
?(declaration : (J.property * Ident.t) option) ?(default : J.block option)
(e : J.expression) (clauses : J.string_clause list) : t =
match e.expression_desc with
| Str { string = txt; _ } | Unicode txt -> (
| Str txt | Unicode txt -> (
let continuation =
match
List.find_map
Expand Down