@@ -1329,9 +1329,53 @@ let make_constr_matching p def ctx = function
13291329 pat = normalize_pat p;
13301330 }
13311331
1332+ (* Whether the payload pattern [p] of [Some] can't match the object that
1333+ represents [Some] of a value like [None] or undefined (see
1334+ [Primitive_option.some]): it doesn't bind the payload itself, and tests it
1335+ with a literal comparison or with a type test that an object fails. An
1336+ untagged case binds its argument only once its type test has passed. *)
1337+ let rec rejects_nested_some p =
1338+ match p.pat_desc with
1339+ | Tpat_any | Tpat_constant _ -> true
1340+ | Tpat_or (p1 , p2 , _ ) -> rejects_nested_some p1 && rejects_nested_some p2
1341+ | Tpat_construct (_ , cstr , _ ) -> (
1342+ match cstr.cstr_kind with
1343+ | Ordinary_constructor representation
1344+ when not (Datarepr. constructor_has_optional_shape cstr) -> (
1345+ match Variant_runtime. representation representation with
1346+ | Constant {literal} -> literal <> Some Variant_runtime. Undefined
1347+ | Block (Untagged {block_type = ObjectType | UnknownType } ) -> false
1348+ | Block (Untagged _ ) -> true
1349+ | Block (Tagged _ ) -> false )
1350+ | Ordinary_constructor _ | Extension_constructor _ -> false )
1351+ | _ -> false
1352+
1353+ (* A [Some] payload whose patterns all reject the representation of a nested
1354+ [Some] is matched as it is, without checking for that representation *)
1355+ let skip_nested_some_check (cell : cell ) =
1356+ match cell.pm.args with
1357+ | (Lprim {primitive = Pval_from_option ; args = [arg]; loc}, str) :: argl
1358+ when List. for_all
1359+ (function
1360+ | p :: _ , _ -> rejects_nested_some p
1361+ | [] , _ -> false )
1362+ cell.pm.cases ->
1363+ {
1364+ cell with
1365+ pm =
1366+ {
1367+ cell.pm with
1368+ args =
1369+ (prim ~primitive: Pval_from_option_not_nest ~args: [arg] loc, str)
1370+ :: argl;
1371+ };
1372+ }
1373+ | _ -> cell
1374+
13321375let divide_constructor ctx pm =
13331376 divide make_constr_matching Types. same_constructor get_key_constr
13341377 get_args_constr ctx pm
1378+ |> List. map (fun (cstr , cell ) -> (cstr, skip_nested_some_check cell))
13351379
13361380(* Matching against a variant *)
13371381
@@ -2054,8 +2098,87 @@ type constructor_matching_plan =
20542098 | Switch_on_constructors of Lambda .lambda_switch
20552099 (* * General nominal and untagged variant matching. *)
20562100
2101+ (* Whether [present], the action for a value of the option [arg], starts with a
2102+ test of that value that takes [absent] for undefined. The presence test of
2103+ [arg] is then redundant: undefined reaches [absent] either way. *)
2104+ let rec value_test_defaults_on_undefined ~arg ~absent present =
2105+ let is_value = function
2106+ | Lprim {primitive = Pval_from_option_not_nest ; args = [value]} ->
2107+ Lambda. eq_approx arg value
2108+ | e -> Lambda. eq_approx arg e
2109+ in
2110+ let is_equality = function
2111+ | Pboolcomp c
2112+ | Pintcomp c
2113+ | Pfloatcomp c
2114+ | Pbigintcomp c
2115+ | Pstringcomp c
2116+ | Pjscomp c ->
2117+ Some c
2118+ | _ -> None
2119+ in
2120+ (* A constant that undefined is unequal to, even loosely *)
2121+ let is_defined_constant = function
2122+ | Lconst
2123+ ( Const_int _ | Const_char _ | Const_string _ | Const_float _
2124+ | Const_bigint _ | Const_js_false | Const_js_true ) ->
2125+ true
2126+ | _ -> false
2127+ in
2128+ let compares_value = function
2129+ | [a; b] ->
2130+ (is_value a && is_defined_constant b)
2131+ || (is_defined_constant a && is_value b)
2132+ | _ -> false
2133+ in
2134+ match present with
2135+ | Llet
2136+ ( Alias ,
2137+ id,
2138+ (Lprim {primitive = Pval_from_option_not_nest ; args = [value]} as def),
2139+ body )
2140+ when Lambda. eq_approx arg value ->
2141+ (* The value bound to a name *)
2142+ value_test_defaults_on_undefined ~arg: (Lambda. var id) ~absent body
2143+ || value_test_defaults_on_undefined ~arg: def ~absent body
2144+ | Lswitch (scrutinee , ({sw_failaction = Some fail } as sw )) ->
2145+ is_value scrutinee
2146+ && Lambda. eq_approx fail absent
2147+ && switch_defaults_on_undefined sw
2148+ | Lstringswitch (scrutinee , _ , Some fail ) ->
2149+ is_value scrutinee && Lambda. eq_approx fail absent
2150+ (* undefined is unequal to every constant *)
2151+ | Lifthenelse (Lprim {primitive; args}, if_true, if_false)
2152+ when compares_value args -> (
2153+ match is_equality primitive with
2154+ | Some Cneq -> Lambda. eq_approx if_true absent
2155+ | Some Ceq -> Lambda. eq_approx if_false absent
2156+ | Some (Clt | Cgt | Cle | Cge ) | None -> false )
2157+ | _ -> false
2158+
2159+ (* Whether the switch [sw] on an untagged variant takes its default action for
2160+ undefined: no case matches undefined, and each side of the switch that
2161+ undefined can reach has the default action *)
2162+ and switch_defaults_on_undefined (sw : lambda_switch ) =
2163+ match sw.sw_dispatch with
2164+ | Switch_variant {block_types} ->
2165+ block_types <> []
2166+ && (not (List. mem Variant_runtime. UnknownType block_types))
2167+ && (not sw.sw_blocks_full)
2168+ && (not (sw.sw_consts_full && sw.sw_consts <> [] ))
2169+ && List. for_all
2170+ (function
2171+ | Switch_constructor (Constant {literal} ), _ ->
2172+ literal <> Some Variant_runtime. Undefined
2173+ | _ -> true )
2174+ sw.sw_consts
2175+ | Switch_direct -> false
2176+
20572177let lower_constructor_matching_plan ~loc ~arg = function
20582178 | Use_constructor_action action -> action
2179+ | Test_payload_presence {test = Is_present_option ; absent; present}
2180+ when value_test_defaults_on_undefined ~arg ~absent present ->
2181+ present
20592182 | Test_payload_presence {test; absent; present} ->
20602183 let condition =
20612184 match test with
@@ -2465,9 +2588,20 @@ let rec comp_match_handlers comp_fun partial ctx arg first_match next_matchs =
24652588
24662589(* To find reasonable names for variables *)
24672590
2591+ (* A name for the value [pat] matches, also found under an option's [Some] *)
2592+ let id_of_pattern pat =
2593+ match Typecore. id_of_pattern pat with
2594+ | Some id -> Some id
2595+ | None -> (
2596+ match pat.pat_desc with
2597+ | Tpat_construct (_, cstr, [payload])
2598+ when Datarepr. constructor_has_optional_shape cstr ->
2599+ Typecore. id_of_pattern payload
2600+ | _ -> None )
2601+
24682602let rec name_pattern default = function
24692603 | (pat :: _ , _ ) :: rem -> (
2470- match Typecore. id_of_pattern pat with
2604+ match id_of_pattern pat with
24712605 | Some id -> id
24722606 | None -> name_pattern default rem)
24732607 | _ -> Ident. create default
0 commit comments