Skip to content

Syntax check: not allowing literal with suffix n #4887

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 3 commits into from
Dec 29, 2020
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
76 changes: 41 additions & 35 deletions jscomp/syntax/bs_ast_invariant.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ let mark_used_bs_attribute ((x,_) : Parsetree.attribute) =
if not x.loc.loc_ghost then
Hash_set_poly.add used_attributes x

let dummy_unused_attribute : Warnings.t = (Bs_unused_attribute "")



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

type iterator = Ast_iterator.iterator
let default_iterator = Ast_iterator.default_iterator

let check_constant loc kind (const : Parsetree.constant) =
match const with
| Pconst_string
(_, Some s) ->
begin match kind with
| `expr ->
(if Ast_utf8_string_interp.is_unescaped s then
Bs_warnings.error_unescaped_delimiter loc s)
| `pat ->
if s = "j" then
Location.raise_errorf ~loc "Unicode string is not allowed in pattern match"
end
| Pconst_integer(s,None) ->
(* range check using int32
It is better to give a warning instead of error to avoid make people unhappy.
It also has restrictions in which platform bsc is running on since it will
affect int ranges
*)
(
try
ignore (
if String.length s = 0 || s.[0] = '-' then
Int32.of_string s
else Int32.of_string ("-" ^ s))
with _ ->
Bs_warnings.warn_literal_overflow loc
)
| Pconst_integer(_, Some 'n')
-> Location.raise_errorf ~loc "literal with `n` suffix is not supported"
| _ -> ()

(* Note we only used Bs_ast_iterator here, we can reuse compiler-libs instead of
rolling our own*)
let emit_external_warnings : iterator=
Expand All @@ -96,27 +125,8 @@ let emit_external_warnings : iterator=
| _ -> default_iterator.structure_item self str_item
);
expr = (fun self a ->
match a.pexp_desc with
| Pexp_constant (
Pconst_string
(_, Some s))
when Ast_utf8_string_interp.is_unescaped s ->
Bs_warnings.error_unescaped_delimiter a.pexp_loc s
| Pexp_constant(Pconst_integer(s,None)) ->
(* range check using int32
It is better to give a warning instead of error to avoid make people unhappy.
It also has restrictions in which platform bsc is running on since it will
affect int ranges
*)
(
try
ignore (
if String.length s = 0 || s.[0] = '-' then
Int32.of_string s
else Int32.of_string ("-" ^ s))
with _ ->
Bs_warnings.warn_literal_overflow a.pexp_loc
)
match a.pexp_desc with
| Pexp_constant(const) -> check_constant a.pexp_loc `expr const
| _ -> default_iterator.expr self a
);
label_declaration = (fun self lbl ->
Expand Down Expand Up @@ -156,14 +166,12 @@ let emit_external_warnings : iterator=
| _ ->
default_iterator.value_description self v
);
pat = begin fun self (pat : Parsetree.pattern) ->
match pat.ppat_desc with
| Ppat_constant(
Pconst_string
(_, Some "j")) ->
Location.raise_errorf ~loc:pat.ppat_loc "Unicode string is not allowed in pattern match"
pat = begin fun self (pat : Parsetree.pattern) ->
match pat.ppat_desc with
| Ppat_constant(constant) ->
check_constant pat.ppat_loc `pat constant
| _ -> default_iterator.pat self pat
end
end
}

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


let emit_external_warnings_on_structure (stru : Parsetree.structure) =
if Warnings.is_active dummy_unused_attribute then
emit_external_warnings.structure emit_external_warnings stru
emit_external_warnings.structure emit_external_warnings stru

let emit_external_warnings_on_signature (sigi : Parsetree.signature) =
if Warnings.is_active dummy_unused_attribute then
emit_external_warnings.signature emit_external_warnings sigi
emit_external_warnings.signature emit_external_warnings sigi
11 changes: 4 additions & 7 deletions jscomp/test/build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ o test/gpr_858_test.cmi test/gpr_858_test.cmj : cc test/gpr_858_test.ml | $stdli
o test/gpr_858_unit2_test.cmi test/gpr_858_unit2_test.cmj : cc test/gpr_858_unit2_test.ml | $stdlib
o test/gpr_904_test.cmi test/gpr_904_test.cmj : cc test/gpr_904_test.ml | test/mt.cmj $stdlib
o test/gpr_974_test.cmi test/gpr_974_test.cmj : cc test/gpr_974_test.ml | $stdlib
o test/gpr_977_test.cmi test/gpr_977_test.cmj : cc test/gpr_977_test.ml | test/mt.cmj test/nativeint.cmj $stdlib
o test/gpr_977_test.cmi test/gpr_977_test.cmj : cc test/gpr_977_test.ml | test/mt.cmj $stdlib
o test/gpr_return_type_unused_attribute.cmi test/gpr_return_type_unused_attribute.cmj : cc test/gpr_return_type_unused_attribute.ml | $stdlib
o test/gray_code_test.cmi test/gray_code_test.cmj : cc test/gray_code_test.ml | $stdlib
o test/guide_for_ext.cmi test/guide_for_ext.cmj : cc test/guide_for_ext.ml | $stdlib
Expand Down Expand Up @@ -375,7 +375,7 @@ o test/int32_test.cmi test/int32_test.cmj : cc test/int32_test.ml | test/ext_arr
o test/int64_mul_div_test.cmi test/int64_mul_div_test.cmj : cc test/int64_mul_div_test.ml | test/mt.cmj $stdlib
o test/int64_string_bench.cmi test/int64_string_bench.cmj : cc test/int64_string_bench.ml | $stdlib
o test/int64_string_test.cmi test/int64_string_test.cmj : cc test/int64_string_test.ml | test/mt.cmj $stdlib
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
o test/int64_test.cmi test/int64_test.cmj : cc test/int64_test.ml | test/ext_array_test.cmj test/mt.cmj $stdlib
o test/int_hashtbl_test.cmi test/int_hashtbl_test.cmj : cc test/int_hashtbl_test.ml | test/mt.cmj $stdlib
o test/int_map.cmi test/int_map.cmj : cc test/int_map.ml | $stdlib
o test/int_overflow_test.cmi test/int_overflow_test.cmj : cc test/int_overflow_test.ml | test/mt.cmj $stdlib
Expand Down Expand Up @@ -454,9 +454,7 @@ o test/mutable_obj_test.cmi test/mutable_obj_test.cmj : cc test/mutable_obj_test
o test/mutable_uncurry_test.cmi test/mutable_uncurry_test.cmj : cc test/mutable_uncurry_test.ml | test/mt.cmj $stdlib
o test/mutual_non_recursive_type.cmi test/mutual_non_recursive_type.cmj : cc test/mutual_non_recursive_type.ml | $stdlib
o test/name_mangle_test.cmi test/name_mangle_test.cmj : cc test/name_mangle_test.ml | test/mt.cmj $stdlib
o test/native_pattern.cmi test/native_pattern.cmj : cc test/native_pattern.ml | test/nativeint.cmj $stdlib
o test/nativeint.cmj : cc_cmi test/nativeint.ml | test/nativeint.cmi $stdlib
o test/nativeint.cmi : cc test/nativeint.mli | $stdlib
o test/nativeint.cmi test/nativeint.cmj : cc test/nativeint.ml | $stdlib
o test/nested_include.cmi test/nested_include.cmj : cc test/nested_include.ml | $stdlib
o test/nested_module_alias.cmi test/nested_module_alias.cmj : cc test/nested_module_alias.ml | $stdlib
o test/nested_obj_literal.cmi test/nested_obj_literal.cmj : cc test/nested_obj_literal.ml | $stdlib
Expand Down Expand Up @@ -568,7 +566,6 @@ o test/sexp.cmi : cc test/sexp.mli | $stdlib
o test/sexpm.cmj : cc_cmi test/sexpm.ml | test/sexpm.cmi $stdlib
o test/sexpm.cmi : cc test/sexpm.mli | $stdlib
o test/sexpm_test.cmi test/sexpm_test.cmj : cc test/sexpm_test.ml | test/mt.cmj test/sexpm.cmj $stdlib
o test/shift_test.cmi test/shift_test.cmj : cc test/shift_test.ml | test/nativeint.cmj $stdlib
o test/side_effect.cmi test/side_effect.cmj : cc test/side_effect.ml | $stdlib
o test/side_effect_free.cmi test/side_effect_free.cmj : cc test/side_effect_free.ml | $stdlib
o test/simple_derive_test.cmj : cc_cmi test/simple_derive_test.ml | test/simple_derive_test.cmi $stdlib
Expand Down Expand Up @@ -720,7 +717,7 @@ o test/ticker.cmi test/ticker.cmj : cc test/ticker.ml | $stdlib
o test/to_string_test.cmi test/to_string_test.cmj : cc test/to_string_test.ml | test/mt.cmj $stdlib
o test/topsort_test.cmi test/topsort_test.cmj : cc test/topsort_test.ml | $stdlib
o test/tramp_fib.cmi test/tramp_fib.cmj : cc test/tramp_fib.ml | test/mt.cmj $stdlib
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
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
o test/tuple_alloc.cmi test/tuple_alloc.cmj : cc test/tuple_alloc.ml | $stdlib
o test/type_disambiguate.cmi test/type_disambiguate.cmj : cc test/type_disambiguate.ml | $stdlib
o test/typeof_test.cmi test/typeof_test.cmj : cc test/typeof_test.ml | test/mt.cmj $stdlib
Expand Down
Loading