Skip to content

Commit a8b0cfb

Browse files
authored
Fix issue where exhaustiveness check for pattern matching char was no… (#5744)
* Fix issue where exhaustiveness check for pattern matching char was not working Fixes #5557 Fixes #5743 Exhaustiveness check for pattern matching relies on checking that the pattern does not lead to a type error. Since char constants are represented internally as int, this causes an internal type error when processing patterns of the form 'x', where the constant is represented as `Pconst_integer` but the expected type is `char`. This PR intercepts this situation and changes the expected type to `int`. A cleaner solution would be to represent chars differently (but we can't change the AST), or make it official that char and int is the same type (and lose some abstraction). * comment
1 parent 9789cca commit a8b0cfb

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 10.1.0-rc.3
1414

15+
#### :bug: Bug Fix
16+
17+
- Fix issue where exhaustiveness check for pattern matching char was not working https://github.com/rescript-lang/rescript-compiler/issues/5557
18+
1519
# 10.1.0-rc.2
1620

1721
#### :bug: Bug Fix

jscomp/ml/typecore.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,11 @@ let partial_pred ~lev ?mode ?explode env expected_ty constrs labels p =
13501350
let state = save_state env in
13511351
try
13521352
reset_pattern None true;
1353+
let expected_ty = match p.ppat_desc, expected_ty.desc with
1354+
| Ppat_constant (Pconst_integer _ ), Tconstr (path, [], _) when Path.same path Predef.path_char ->
1355+
(* Constants such as 'x' are represented as Pconst_integer but expected to have type char *)
1356+
Predef.type_int
1357+
| _ -> expected_ty in
13531358
let typed_p =
13541359
Ctype.with_passive_variants
13551360
(type_pat ~allow_existentials:true ~lev

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40301,6 +40301,11 @@ let partial_pred ~lev ?mode ?explode env expected_ty constrs labels p =
4030140301
let state = save_state env in
4030240302
try
4030340303
reset_pattern None true;
40304+
let expected_ty = match p.ppat_desc, expected_ty.desc with
40305+
| Ppat_constant (Pconst_integer _ ), Tconstr (path, [], _) when Path.same path Predef.path_char ->
40306+
(* Constants such as 'x' are represented as Pconst_integer but expected to have type char *)
40307+
Predef.type_int
40308+
| _ -> expected_ty in
4030440309
let typed_p =
4030540310
Ctype.with_passive_variants
4030640311
(type_pat ~allow_existentials:true ~lev

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40301,6 +40301,11 @@ let partial_pred ~lev ?mode ?explode env expected_ty constrs labels p =
4030140301
let state = save_state env in
4030240302
try
4030340303
reset_pattern None true;
40304+
let expected_ty = match p.ppat_desc, expected_ty.desc with
40305+
| Ppat_constant (Pconst_integer _ ), Tconstr (path, [], _) when Path.same path Predef.path_char ->
40306+
(* Constants such as 'x' are represented as Pconst_integer but expected to have type char *)
40307+
Predef.type_int
40308+
| _ -> expected_ty in
4030440309
let typed_p =
4030540310
Ctype.with_passive_variants
4030640311
(type_pat ~allow_existentials:true ~lev

lib/4.06.1/whole_compiler.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216688,6 +216688,11 @@ let partial_pred ~lev ?mode ?explode env expected_ty constrs labels p =
216688216688
let state = save_state env in
216689216689
try
216690216690
reset_pattern None true;
216691+
let expected_ty = match p.ppat_desc, expected_ty.desc with
216692+
| Ppat_constant (Pconst_integer _ ), Tconstr (path, [], _) when Path.same path Predef.path_char ->
216693+
(* Constants such as 'x' are represented as Pconst_integer but expected to have type char *)
216694+
Predef.type_int
216695+
| _ -> expected_ty in
216691216696
let typed_p =
216692216697
Ctype.with_passive_variants
216693216698
(type_pat ~allow_existentials:true ~lev

0 commit comments

Comments
 (0)