Skip to content

Commit c4e2b80

Browse files
authored
Merge pull request rescript-lang#4887 from rescript-lang/syntax_check
Syntax check: not allowing literal with suffix n
2 parents 90d5ae4 + 1ce75bf commit c4e2b80

29 files changed

+3340
-3687
lines changed

jscomp/syntax/bs_ast_invariant.ml

+41-35
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ let mark_used_bs_attribute ((x,_) : Parsetree.attribute) =
5757
if not x.loc.loc_ghost then
5858
Hash_set_poly.add used_attributes x
5959

60-
let dummy_unused_attribute : Warnings.t = (Bs_unused_attribute "")
61-
62-
6360

6461
let warn_unused_attribute
6562
(({txt; loc} as sloc, _) : Parsetree.attribute) =
@@ -81,6 +78,38 @@ let warn_discarded_unused_attributes (attrs : Parsetree.attributes) =
8178

8279
type iterator = Ast_iterator.iterator
8380
let default_iterator = Ast_iterator.default_iterator
81+
82+
let check_constant loc kind (const : Parsetree.constant) =
83+
match const with
84+
| Pconst_string
85+
(_, Some s) ->
86+
begin match kind with
87+
| `expr ->
88+
(if Ast_utf8_string_interp.is_unescaped s then
89+
Bs_warnings.error_unescaped_delimiter loc s)
90+
| `pat ->
91+
if s = "j" then
92+
Location.raise_errorf ~loc "Unicode string is not allowed in pattern match"
93+
end
94+
| Pconst_integer(s,None) ->
95+
(* range check using int32
96+
It is better to give a warning instead of error to avoid make people unhappy.
97+
It also has restrictions in which platform bsc is running on since it will
98+
affect int ranges
99+
*)
100+
(
101+
try
102+
ignore (
103+
if String.length s = 0 || s.[0] = '-' then
104+
Int32.of_string s
105+
else Int32.of_string ("-" ^ s))
106+
with _ ->
107+
Bs_warnings.warn_literal_overflow loc
108+
)
109+
| Pconst_integer(_, Some 'n')
110+
-> Location.raise_errorf ~loc "literal with `n` suffix is not supported"
111+
| _ -> ()
112+
84113
(* Note we only used Bs_ast_iterator here, we can reuse compiler-libs instead of
85114
rolling our own*)
86115
let emit_external_warnings : iterator=
@@ -96,27 +125,8 @@ let emit_external_warnings : iterator=
96125
| _ -> default_iterator.structure_item self str_item
97126
);
98127
expr = (fun self a ->
99-
match a.pexp_desc with
100-
| Pexp_constant (
101-
Pconst_string
102-
(_, Some s))
103-
when Ast_utf8_string_interp.is_unescaped s ->
104-
Bs_warnings.error_unescaped_delimiter a.pexp_loc s
105-
| Pexp_constant(Pconst_integer(s,None)) ->
106-
(* range check using int32
107-
It is better to give a warning instead of error to avoid make people unhappy.
108-
It also has restrictions in which platform bsc is running on since it will
109-
affect int ranges
110-
*)
111-
(
112-
try
113-
ignore (
114-
if String.length s = 0 || s.[0] = '-' then
115-
Int32.of_string s
116-
else Int32.of_string ("-" ^ s))
117-
with _ ->
118-
Bs_warnings.warn_literal_overflow a.pexp_loc
119-
)
128+
match a.pexp_desc with
129+
| Pexp_constant(const) -> check_constant a.pexp_loc `expr const
120130
| _ -> default_iterator.expr self a
121131
);
122132
label_declaration = (fun self lbl ->
@@ -156,14 +166,12 @@ let emit_external_warnings : iterator=
156166
| _ ->
157167
default_iterator.value_description self v
158168
);
159-
pat = begin fun self (pat : Parsetree.pattern) ->
160-
match pat.ppat_desc with
161-
| Ppat_constant(
162-
Pconst_string
163-
(_, Some "j")) ->
164-
Location.raise_errorf ~loc:pat.ppat_loc "Unicode string is not allowed in pattern match"
169+
pat = begin fun self (pat : Parsetree.pattern) ->
170+
match pat.ppat_desc with
171+
| Ppat_constant(constant) ->
172+
check_constant pat.ppat_loc `pat constant
165173
| _ -> default_iterator.pat self pat
166-
end
174+
end
167175
}
168176

169177
let rec iter_warnings_on_stru (stru : Parsetree.structure) =
@@ -190,9 +198,7 @@ let rec iter_warnings_on_sigi (stru : Parsetree.signature) =
190198

191199

192200
let emit_external_warnings_on_structure (stru : Parsetree.structure) =
193-
if Warnings.is_active dummy_unused_attribute then
194-
emit_external_warnings.structure emit_external_warnings stru
201+
emit_external_warnings.structure emit_external_warnings stru
195202

196203
let emit_external_warnings_on_signature (sigi : Parsetree.signature) =
197-
if Warnings.is_active dummy_unused_attribute then
198-
emit_external_warnings.signature emit_external_warnings sigi
204+
emit_external_warnings.signature emit_external_warnings sigi

jscomp/test/build.ninja

+4-7
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ o test/gpr_858_test.cmi test/gpr_858_test.cmj : cc test/gpr_858_test.ml | $stdli
337337
o test/gpr_858_unit2_test.cmi test/gpr_858_unit2_test.cmj : cc test/gpr_858_unit2_test.ml | $stdlib
338338
o test/gpr_904_test.cmi test/gpr_904_test.cmj : cc test/gpr_904_test.ml | test/mt.cmj $stdlib
339339
o test/gpr_974_test.cmi test/gpr_974_test.cmj : cc test/gpr_974_test.ml | $stdlib
340-
o test/gpr_977_test.cmi test/gpr_977_test.cmj : cc test/gpr_977_test.ml | test/mt.cmj test/nativeint.cmj $stdlib
340+
o test/gpr_977_test.cmi test/gpr_977_test.cmj : cc test/gpr_977_test.ml | test/mt.cmj $stdlib
341341
o test/gpr_return_type_unused_attribute.cmi test/gpr_return_type_unused_attribute.cmj : cc test/gpr_return_type_unused_attribute.ml | $stdlib
342342
o test/gray_code_test.cmi test/gray_code_test.cmj : cc test/gray_code_test.ml | $stdlib
343343
o test/guide_for_ext.cmi test/guide_for_ext.cmj : cc test/guide_for_ext.ml | $stdlib
@@ -375,7 +375,7 @@ o test/int32_test.cmi test/int32_test.cmj : cc test/int32_test.ml | test/ext_arr
375375
o test/int64_mul_div_test.cmi test/int64_mul_div_test.cmj : cc test/int64_mul_div_test.ml | test/mt.cmj $stdlib
376376
o test/int64_string_bench.cmi test/int64_string_bench.cmj : cc test/int64_string_bench.ml | $stdlib
377377
o test/int64_string_test.cmi test/int64_string_test.cmj : cc test/int64_string_test.ml | test/mt.cmj $stdlib
378-
o test/int64_test.cmi test/int64_test.cmj : cc test/int64_test.ml | test/ext_array_test.cmj test/mt.cmj test/nativeint.cmj $stdlib
378+
o test/int64_test.cmi test/int64_test.cmj : cc test/int64_test.ml | test/ext_array_test.cmj test/mt.cmj $stdlib
379379
o test/int_hashtbl_test.cmi test/int_hashtbl_test.cmj : cc test/int_hashtbl_test.ml | test/mt.cmj $stdlib
380380
o test/int_map.cmi test/int_map.cmj : cc test/int_map.ml | $stdlib
381381
o test/int_overflow_test.cmi test/int_overflow_test.cmj : cc test/int_overflow_test.ml | test/mt.cmj $stdlib
@@ -454,9 +454,7 @@ o test/mutable_obj_test.cmi test/mutable_obj_test.cmj : cc test/mutable_obj_test
454454
o test/mutable_uncurry_test.cmi test/mutable_uncurry_test.cmj : cc test/mutable_uncurry_test.ml | test/mt.cmj $stdlib
455455
o test/mutual_non_recursive_type.cmi test/mutual_non_recursive_type.cmj : cc test/mutual_non_recursive_type.ml | $stdlib
456456
o test/name_mangle_test.cmi test/name_mangle_test.cmj : cc test/name_mangle_test.ml | test/mt.cmj $stdlib
457-
o test/native_pattern.cmi test/native_pattern.cmj : cc test/native_pattern.ml | test/nativeint.cmj $stdlib
458-
o test/nativeint.cmj : cc_cmi test/nativeint.ml | test/nativeint.cmi $stdlib
459-
o test/nativeint.cmi : cc test/nativeint.mli | $stdlib
457+
o test/nativeint.cmi test/nativeint.cmj : cc test/nativeint.ml | $stdlib
460458
o test/nested_include.cmi test/nested_include.cmj : cc test/nested_include.ml | $stdlib
461459
o test/nested_module_alias.cmi test/nested_module_alias.cmj : cc test/nested_module_alias.ml | $stdlib
462460
o test/nested_obj_literal.cmi test/nested_obj_literal.cmj : cc test/nested_obj_literal.ml | $stdlib
@@ -568,7 +566,6 @@ o test/sexp.cmi : cc test/sexp.mli | $stdlib
568566
o test/sexpm.cmj : cc_cmi test/sexpm.ml | test/sexpm.cmi $stdlib
569567
o test/sexpm.cmi : cc test/sexpm.mli | $stdlib
570568
o test/sexpm_test.cmi test/sexpm_test.cmj : cc test/sexpm_test.ml | test/mt.cmj test/sexpm.cmj $stdlib
571-
o test/shift_test.cmi test/shift_test.cmj : cc test/shift_test.ml | test/nativeint.cmj $stdlib
572569
o test/side_effect.cmi test/side_effect.cmj : cc test/side_effect.ml | $stdlib
573570
o test/side_effect_free.cmi test/side_effect_free.cmj : cc test/side_effect_free.ml | $stdlib
574571
o test/simple_derive_test.cmj : cc_cmi test/simple_derive_test.ml | test/simple_derive_test.cmi $stdlib
@@ -720,7 +717,7 @@ o test/ticker.cmi test/ticker.cmj : cc test/ticker.ml | $stdlib
720717
o test/to_string_test.cmi test/to_string_test.cmj : cc test/to_string_test.ml | test/mt.cmj $stdlib
721718
o test/topsort_test.cmi test/topsort_test.cmj : cc test/topsort_test.ml | $stdlib
722719
o test/tramp_fib.cmi test/tramp_fib.cmj : cc test/tramp_fib.ml | test/mt.cmj $stdlib
723-
o test/tscanf_test.cmi test/tscanf_test.cmj : cc test/tscanf_test.ml | test/mt.cmj test/mt_global.cmj test/nativeint.cmj test/testing.cmj $stdlib
720+
o test/tscanf_test.cmi test/tscanf_test.cmj : cc test/tscanf_test.ml | test/mt.cmj test/mt_global.cmj test/testing.cmj $stdlib
724721
o test/tuple_alloc.cmi test/tuple_alloc.cmj : cc test/tuple_alloc.ml | $stdlib
725722
o test/type_disambiguate.cmi test/type_disambiguate.cmj : cc test/type_disambiguate.ml | $stdlib
726723
o test/typeof_test.cmi test/typeof_test.cmj : cc test/typeof_test.ml | test/mt.cmj $stdlib

0 commit comments

Comments
 (0)