Skip to content

Fix issue with comparison of string constants #6065

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

Merged
merged 1 commit into from
Mar 11, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Fix implementation of directives https://github.com/rescript-lang/rescript-compiler/pull/6052
- Fix issue if the `lib` dir is included in the sources of bsconfig.json https://github.com/rescript-lang/rescript-compiler/pull/6055
- Fix issue with string escape in pattern match https://github.com/rescript-lang/rescript-compiler/pull/6062
- Fix issue with literal comparison of string constants https://github.com/rescript-lang/rescript-compiler/pull/6065

#### :rocket: New Feature
- Add support for toplevel `await` https://github.com/rescript-lang/rescript-compiler/pull/6054
Expand Down
31 changes: 21 additions & 10 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,15 @@ let zero_float_lit : t =
let float_mod ?comment e1 e2 : J.expression =
{ comment; expression_desc = Bin (Mod, e1, e2) }


let str_equal (txt0:string) (delim0:External_arg_spec.delim) txt1 delim1 =
if delim0 = delim1 then
if Ext_string.equal txt0 txt1 then Some true
else if Ast_utf8_string.simple_comparison txt0 && Ast_utf8_string.simple_comparison txt1
then Some false
else None
else None

let rec triple_equal ?comment (e0 : t) (e1 : t) : t =
match (e0.expression_desc, e1.expression_desc) with
| ( (Null | Undefined),
Expand All @@ -566,9 +575,6 @@ let rec triple_equal ?comment (e0 : t) (e1 : t) : t =
(Null | Undefined) )
when no_side_effect e0 ->
false_
| Str { txt = x }, Str { txt = y } ->
(* CF*)
bool (Ext_string.equal x y)
| Number (Int { i = i0; _ }), Number (Int { i = i1; _ }) -> bool (i0 = i1)
| Optional_block (a, _), Optional_block (b, _) -> triple_equal ?comment a b
| Undefined, Optional_block _
Expand Down Expand Up @@ -743,9 +749,13 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t =
let int_equal = float_equal

let string_equal ?comment (e0 : t) (e1 : t) : t =
let default () : t = { expression_desc = Bin (EqEqEq, e0, e1); comment } in
match (e0.expression_desc, e1.expression_desc) with
| Str { txt = a0 }, Str { txt = b0 } -> bool (Ext_string.equal a0 b0)
| _, _ -> { expression_desc = Bin (EqEqEq, e0, e1); comment }
| Str { txt = a0; delim = d0 }, Str { txt = a1; delim = d1 } when d0 = d1 ->
(match str_equal a0 d0 a1 d1 with
| Some b -> bool b
| None -> default ())
| _, _ -> default ()

let is_type_number ?comment (e : t) : t =
string_equal ?comment (typeof e) (str "number")
Expand Down Expand Up @@ -827,11 +837,12 @@ let uint32 ?comment n : J.expression =

let string_comp (cmp : J.binop) ?comment (e0 : t) (e1 : t) =
match (e0.expression_desc, e1.expression_desc) with
| Str { txt = a0 }, Str { txt = b0 } -> (
match cmp with
| EqEqEq -> bool (a0 = b0)
| NotEqEq -> bool (a0 <> b0)
| _ -> bin ?comment cmp e0 e1)
| Str { txt = a0; delim = d0 }, Str { txt = a1; delim = d1 } -> (
match cmp, str_equal a0 d0 a1 d1 with
| EqEqEq, Some b -> bool b
| NotEqEq, Some b -> bool (b = false)
| _ ->
bin ?comment cmp e0 e1)
| _ -> bin ?comment cmp e0 e1

let obj_length ?comment e : t =
Expand Down
12 changes: 12 additions & 0 deletions jscomp/frontend/ast_utf8_string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,15 @@ let transform loc s =
Buffer.contents buf
with Error (offset, error) ->
Location.raise_errorf ~loc "Offset: %d, %a" offset pp_error error

let rec check_no_escapes_or_unicode (s : string) (byte_offset : int) (s_len : int) =
if byte_offset = s_len then true
else
let current_char = s.[byte_offset] in
match Ext_utf8.classify current_char with
| Single 92 (* '\\' *) -> false
| Single _ -> check_no_escapes_or_unicode s (byte_offset + 1) s_len
| Invalid | Cont _ | Leading _ -> false

let simple_comparison s =
check_no_escapes_or_unicode s 0 (String.length s)
3 changes: 3 additions & 0 deletions jscomp/frontend/ast_utf8_string.mli
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ val pp_error : Format.formatter -> error -> unit
val transform_test : string -> string

val transform : Location.t -> string -> string

(* Check if the string is only == to itself (no unicode or escape tricks) *)
val simple_comparison : string -> bool
2 changes: 2 additions & 0 deletions jscomp/frontend/external_arg_spec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

(** type definitions for arguments to a function declared external *)

type delim = J.delim

type cst =
| Arg_int_lit of int
| Arg_string_lit of string * J.delim
Expand Down
2 changes: 2 additions & 0 deletions jscomp/frontend/external_arg_spec.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

type delim = J.delim

type cst = private
| Arg_int_lit of int
| Arg_string_lit of string * J.delim
Expand Down
3 changes: 2 additions & 1 deletion jscomp/test/build.ninja

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions jscomp/test/string_constant_compare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';


var a1 = true;

var a2 = false;

var a3 = "'" === "\'";

var a4 = "'" !== "\'";

exports.a1 = a1;
exports.a2 = a2;
exports.a3 = a3;
exports.a4 = a4;
/* a1 Not a pure module */
4 changes: 4 additions & 0 deletions jscomp/test/string_constant_compare.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let a1 = "'" == "'"
let a2 = "'" != "'"
let a3 = "'" == "\'"
let a4 = "'" != "\'"
13 changes: 13 additions & 0 deletions lib/4.06.1/unstable/all_ounit_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39458,6 +39458,8 @@ val transform_test : string -> string

val transform : Location.t -> string -> string

(* Check if the string is only == to itself (no unicode or escape tricks) *)
val simple_comparison : string -> bool
end = struct
#1 "ast_utf8_string.ml"
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
Expand Down Expand Up @@ -39658,6 +39660,17 @@ let transform loc s =
with Error (offset, error) ->
Location.raise_errorf ~loc "Offset: %d, %a" offset pp_error error

let rec check_no_escapes_or_unicode (s : string) (byte_offset : int) (s_len : int) =
if byte_offset = s_len then true
else
let current_char = s.[byte_offset] in
match Ext_utf8.classify current_char with
| Single 92 (* '\\' *) -> false
| Single _ -> check_no_escapes_or_unicode s (byte_offset + 1) s_len
| Invalid | Cont _ | Leading _ -> false

let simple_comparison s =
check_no_escapes_or_unicode s 0 (String.length s)
end
module Ast_compatible : sig
#1 "ast_compatible.mli"
Expand Down
Loading