Skip to content

Commit b2ae38a

Browse files
authored
fixed Not_found exception raised in certain coercion cases (#6574)
* fixed Not_found exception raised in certain coercion cases * changelog * match for specific exception * add back changelog entry
1 parent 0223607 commit b2ae38a

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# 11.0.1 (Unreleased)
1414

1515
#### :bug: Bug Fix
16+
17+
- Fixed issue with coercions sometimes raising a `Not_found` instead of giving a proper error message. https://github.com/rescript-lang/rescript-compiler/pull/6574
1618
- Fix issue with recursive modules and uncurried. https://github.com/rescript-lang/rescript-compiler/pull/6575
1719

1820
# 11.0.0

jscomp/ml/ctype.ml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3904,6 +3904,11 @@ let subtypes = TypePairs.create 17
39043904
let subtype_error env trace =
39053905
raise (Subtype (expand_trace env (List.rev trace), []))
39063906

3907+
let extract_concrete_typedecl_opt env t =
3908+
match extract_concrete_typedecl env t with
3909+
| v -> Some v
3910+
| exception Not_found -> None
3911+
39073912
let rec subtype_rec env trace t1 t2 cstrs =
39083913
let t1 = repr t1 in
39093914
let t2 = repr t2 in
@@ -3960,23 +3965,23 @@ let rec subtype_rec env trace t1 t2 cstrs =
39603965
| (Tconstr(p1, [], _), Tconstr(p2, [], _)) when Path.same p1 Predef.path_int && Path.same p2 Predef.path_float ->
39613966
cstrs
39623967
| (Tconstr(path, [], _), Tconstr(_, [], _)) when Variant_coercion.can_coerce_primitive path &&
3963-
extract_concrete_typedecl env t2 |> Variant_coercion.can_try_coerce_variant_to_primitive |> Option.is_some
3968+
extract_concrete_typedecl_opt env t2 |> Variant_coercion.can_try_coerce_variant_to_primitive_opt |> Option.is_some
39643969
->
39653970
(* type coercion for primitives (int/float/string) to elgible unboxed variants:
39663971
- must be unboxed
39673972
- must have a constructor case with a supported and matching primitive payload *)
3968-
(match Variant_coercion.can_try_coerce_variant_to_primitive (extract_concrete_typedecl env t2) with
3973+
(match Variant_coercion.can_try_coerce_variant_to_primitive_opt (extract_concrete_typedecl_opt env t2) with
39693974
| Some (constructors, true) ->
39703975
if Variant_coercion.variant_has_catch_all_case constructors (fun p -> Path.same p path) then
39713976
cstrs
39723977
else
39733978
(trace, t1, t2, !univar_pairs)::cstrs
39743979
| _ -> (trace, t1, t2, !univar_pairs)::cstrs)
39753980
| (Tconstr(_, [], _), Tconstr(path, [], _)) when Variant_coercion.can_coerce_primitive path &&
3976-
extract_concrete_typedecl env t1 |> Variant_coercion.can_try_coerce_variant_to_primitive |> Option.is_some
3981+
extract_concrete_typedecl_opt env t1 |> Variant_coercion.can_try_coerce_variant_to_primitive_opt |> Option.is_some
39773982
->
39783983
(* type coercion for variants to primitives *)
3979-
(match Variant_coercion.can_try_coerce_variant_to_primitive (extract_concrete_typedecl env t1) with
3984+
(match Variant_coercion.can_try_coerce_variant_to_primitive_opt (extract_concrete_typedecl_opt env t1) with
39803985
| Some (constructors, unboxed) ->
39813986
if constructors |> Variant_coercion.variant_has_same_runtime_representation_as_target ~targetPath:path ~unboxed then
39823987
cstrs

jscomp/ml/variant_coercion.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ let can_try_coerce_variant_to_primitive
6060
Some (constructors, type_attributes |> Ast_untagged_variants.has_untagged)
6161
| _ -> None
6262

63+
let can_try_coerce_variant_to_primitive_opt p =
64+
match p with
65+
| None -> None
66+
| Some p -> can_try_coerce_variant_to_primitive p
67+
6368
let variant_representation_matches (c1_attrs : Parsetree.attributes)
6469
(c2_attrs : Parsetree.attributes) =
6570
match

0 commit comments

Comments
 (0)