@@ -2260,6 +2260,147 @@ let do_check_fragile_param exhaust loc casel pss =
22602260
22612261let do_check_fragile_gadt = do_check_fragile_param exhaust_gadt
22622262
2263+ (* ****************************************)
2264+ (* Payloads shadowed by untagged literals *)
2265+ (* ****************************************)
2266+
2267+ (* An untagged variant tests its literal cases before its payload cases, so in
2268+ [@as("a") A | S(string)] the value "a" is always matched as [A]. A payload
2269+ pattern can therefore never match a value that is also the runtime value of
2270+ a literal case, wherever it appears in the match: [S("a")] is dead. *)
2271+
2272+ type shadowed_payload = {
2273+ payload_constructor : string ;
2274+ value : Variant_runtime .literal_tag ;
2275+ literal_constructor : string ;
2276+ }
2277+
2278+ type payload_liveness =
2279+ | Never_matches of shadowed_payload list
2280+ | Matches of (pattern * shadowed_payload list ) list
2281+ (* * The pattern can match, except for these dead or-pattern alternatives *)
2282+
2283+ let is_type_path path ty =
2284+ match (Ctype. repr ty).desc with
2285+ | Tconstr (p , _ , _ ) -> Path. same p path
2286+ | _ -> false
2287+
2288+ (* The runtime value of the only value [p] matches, when it is a literal *)
2289+ let rec pattern_literal p : Variant_runtime.literal_tag option =
2290+ match p.pat_desc with
2291+ | Tpat_constant (Const_string s ) -> Some (String s)
2292+ | Tpat_constant (Const_int i | Const_char i ) -> Some (Int i)
2293+ | Tpat_constant (Const_float f ) -> Some (Float f)
2294+ | Tpat_constant (Const_bigint _ ) ->
2295+ (* Bigints are never compared, see [Variant_runtime.same_literal_value] *)
2296+ None
2297+ | Tpat_alias (p , _ , _ ) -> pattern_literal p
2298+ | Tpat_construct (_, {cstr_name; cstr_res}, [] )
2299+ when is_type_path Predef. path_bool cstr_res ->
2300+ Some (Bool (cstr_name = " true" ))
2301+ | Tpat_construct (_, {cstr_res}, _)
2302+ when List. exists
2303+ (fun path -> is_type_path path cstr_res)
2304+ [Predef. path_unit; Predef. path_list; Predef. path_option] ->
2305+ None
2306+ | Tpat_construct (_ , {cstr_kind = Ordinary_constructor r } , args ) -> (
2307+ match (Variant_runtime. representation r, args) with
2308+ | Constant tag , [] -> Some tag.literal
2309+ | Block (Untagged _ ), [arg] ->
2310+ (* An untagged payload is represented by the payload itself *)
2311+ pattern_literal arg
2312+ | (Constant _ | Block _ ), _ -> None )
2313+ | Tpat_construct (_, {cstr_kind = Extension_constructor _}, _)
2314+ | Tpat_any | Tpat_var _ | Tpat_interval _ | Tpat_tuple _ | Tpat_variant _
2315+ | Tpat_record _ | Tpat_dict _ | Tpat_array _ | Tpat_or _ ->
2316+ None
2317+
2318+ (* The literal cases that take precedence over an untagged payload constructor,
2319+ as runtime values and constructor names *)
2320+ let shadowing_literal_cases cd =
2321+ match cd.cstr_kind with
2322+ | Ordinary_constructor r -> (
2323+ match Variant_runtime. representation r with
2324+ | Block (Untagged _ ) ->
2325+ let layout = Variant_runtime. get_layout r.variant in
2326+ List. init (Variant_runtime. length layout) Fun. id
2327+ |> List. filter_map (fun position ->
2328+ match Variant_runtime. constructor_at layout position with
2329+ | Constant tag -> Some (tag.literal, tag.name)
2330+ | Block _ -> None )
2331+ | Constant _ | Block (Tagged _ ) -> [] )
2332+ | Extension_constructor _ -> []
2333+
2334+ let or_liveness liveness p1 p2 =
2335+ match (liveness p1, liveness p2) with
2336+ | Never_matches s1 , Never_matches s2 -> Never_matches (s1 @ s2)
2337+ | Matches d1 , Never_matches s2 -> Matches (d1 @ [(p2, s2)])
2338+ | Never_matches s1 , Matches d2 -> Matches ((p1, s1) :: d2)
2339+ | Matches d1 , Matches d2 -> Matches (d1 @ d2)
2340+
2341+ let rec payload_liveness p =
2342+ match p.pat_desc with
2343+ | Tpat_or (p1 , p2 , _ ) -> or_liveness payload_liveness p1 p2
2344+ | Tpat_construct (_ , cd , [arg ]) -> (
2345+ match shadowing_literal_cases cd with
2346+ | [] -> subpatterns_liveness p
2347+ | literal_cases ->
2348+ shadowed_argument_liveness ~payload_constructor: cd.cstr_name
2349+ ~literal_cases arg)
2350+ | _ -> subpatterns_liveness p
2351+
2352+ (* A pattern that is not an or-pattern matches nothing as soon as one of its
2353+ subpatterns matches nothing *)
2354+ and subpatterns_liveness p =
2355+ let result = ref (Matches [] ) in
2356+ Typedtree. iter_pattern_desc
2357+ (fun sub ->
2358+ match (! result, payload_liveness sub) with
2359+ | Never_matches _ , _ -> ()
2360+ | Matches _ , (Never_matches _ as dead ) -> result := dead
2361+ | Matches d1 , Matches d2 -> result := Matches (d1 @ d2))
2362+ p.pat_desc;
2363+ ! result
2364+
2365+ and shadowed_argument_liveness ~payload_constructor ~literal_cases arg =
2366+ match arg.pat_desc with
2367+ | Tpat_or (p1 , p2 , _ ) ->
2368+ or_liveness
2369+ (shadowed_argument_liveness ~payload_constructor ~literal_cases )
2370+ p1 p2
2371+ | _ -> (
2372+ let shadowing_case =
2373+ match pattern_literal arg with
2374+ | Some value ->
2375+ List. find_opt
2376+ (fun (literal , _ ) -> Variant_runtime. same_literal_value value literal)
2377+ literal_cases
2378+ | None -> None
2379+ in
2380+ match shadowing_case with
2381+ | Some (value , literal_constructor ) ->
2382+ Never_matches [{payload_constructor; value; literal_constructor}]
2383+ | None -> payload_liveness arg)
2384+
2385+ let literal_to_string : Variant_runtime.literal_tag -> string = function
2386+ | String s -> " \" " ^ s ^ " \" "
2387+ | Int i -> string_of_int i
2388+ | Float f -> f
2389+ | BigInt b -> b ^ " n"
2390+ | Bool b -> string_of_bool b
2391+ | Null -> " null"
2392+ | Undefined -> " undefined"
2393+
2394+ let shadowed_payload_reason shadowed =
2395+ shadowed |> List. sort_uniq compare
2396+ |> List. map (fun {payload_constructor; value; literal_constructor} ->
2397+ Printf. sprintf
2398+ " %s is the runtime value of constructor %s, so it is always matched as \
2399+ %s, never as %s."
2400+ (literal_to_string value) literal_constructor literal_constructor
2401+ payload_constructor)
2402+ |> String. concat " \n "
2403+
22632404(* *******************************)
22642405(* Exported unused clause check *)
22652406(* *******************************)
@@ -2268,11 +2409,40 @@ let lower_cases casel =
22682409 let lower = Dict_pattern. lowering (List. map (fun c -> c.c_lhs) casel) in
22692410 List. map (fun c -> {c with c_lhs = lower c.c_lhs}) casel
22702411
2412+ (* Report cases and or-pattern alternatives that can never match because of
2413+ [payload_liveness]. Returns the cases that match nothing, and whether a
2414+ sub-pattern has already been reported. *)
2415+ let check_shadowed_payloads casel =
2416+ let reported = ref [] in
2417+ let dead_cases =
2418+ List. filter
2419+ (fun {c_lhs = q } ->
2420+ match payload_liveness q with
2421+ | Never_matches shadowed ->
2422+ Location. prerr_warning q.pat_loc
2423+ (Warnings. Unused_match (shadowed_payload_reason shadowed));
2424+ true
2425+ | Matches dead_alternatives ->
2426+ List. iter
2427+ (fun (p , shadowed ) ->
2428+ reported := p.pat_loc :: ! reported;
2429+ Location. prerr_warning p.pat_loc
2430+ (Warnings. Unused_pat (shadowed_payload_reason shadowed)))
2431+ dead_alternatives;
2432+ false )
2433+ casel
2434+ in
2435+ (dead_cases, fun p -> List. mem p.pat_loc ! reported)
2436+
22712437let check_unused pred casel =
2272- if Warnings. is_active Warnings. Unused_match then
2438+ if Warnings. is_active ( Warnings. Unused_match " " ) then
22732439 let casel = lower_cases casel in
2440+ let dead_cases, already_reported = check_shadowed_payloads casel in
22742441 let rec do_rec pref = function
22752442 | [] -> ()
2443+ | case :: rem when List. memq case dead_cases ->
2444+ (* A case that matches nothing cannot make later cases unused *)
2445+ do_rec pref rem
22762446 | {c_lhs = q ; c_guard} :: rem ->
22772447 let qs = [q] in
22782448 (try
@@ -2323,13 +2493,16 @@ let check_unused pred casel =
23232493 | _ -> r
23242494 in
23252495 match r with
2326- | Unused -> Location. prerr_warning q.pat_loc Warnings. Unused_match
2496+ | Unused ->
2497+ Location. prerr_warning q.pat_loc (Warnings. Unused_match " " )
23272498 | Upartial ps ->
23282499 ps
23292500 |> List. filter (fun p ->
2330- not (Variant_type_spread. is_pat_from_variant_spread_attr p))
2501+ not
2502+ (Variant_type_spread. is_pat_from_variant_spread_attr p
2503+ || already_reported p))
23312504 |> List. iter (fun p ->
2332- Location. prerr_warning p.pat_loc Warnings. Unused_pat )
2505+ Location. prerr_warning p.pat_loc ( Warnings. Unused_pat " " ) )
23332506 | Used -> ()
23342507 with Empty | Not_found | NoGuard -> assert false );
23352508
@@ -2361,8 +2534,62 @@ let check_partial_param do_check_partial do_check_fragile loc casel =
23612534 do_check_fragile loc casel pss;
23622535 total
23632536
2537+ (* For the exhaustiveness check only: a literal case of an untagged variant
2538+ also covers the payload values equal to its runtime value. This matters for
2539+ a bool payload, whose values can all be covered, as in
2540+ [@as(true) Yes | Ans(bool)] where [Yes | Ans(false)] is exhaustive. Other
2541+ payload types have infinitely many values, so the overlap can't complete a
2542+ match and is left out. *)
2543+ let rec with_bool_payload_overlaps p =
2544+ match p.pat_desc with
2545+ | Tpat_construct (_ , {cstr_kind = Ordinary_constructor r } , [] ) -> (
2546+ match Variant_runtime. representation r with
2547+ | Constant tag -> (
2548+ match tag.literal with
2549+ | Bool b -> (
2550+ let is_bool_payload cd =
2551+ match cd.cstr_kind with
2552+ | Ordinary_constructor r -> (
2553+ match Variant_runtime. representation r with
2554+ | Block (Untagged {block_type = BooleanType } ) -> true
2555+ | Constant _ | Block _ -> false )
2556+ | Extension_constructor _ -> false
2557+ in
2558+ match
2559+ List. find_opt is_bool_payload
2560+ (get_variant_constructors p.pat_env p.pat_type)
2561+ with
2562+ | Some payload_cd ->
2563+ let bool_cd =
2564+ List. find
2565+ (fun cd -> cd.cstr_name = string_of_bool b)
2566+ (get_variant_constructors p.pat_env (Predef. type_bool () ))
2567+ in
2568+ let construct cd args ty =
2569+ make_pat
2570+ (Tpat_construct (mknoloc (Longident. Lident cd.cstr_name), cd, args))
2571+ ty p.pat_env
2572+ in
2573+ orify p
2574+ (construct payload_cd
2575+ [construct bool_cd [] (Predef. type_bool () )]
2576+ p.pat_type)
2577+ | None -> p)
2578+ | String _ | Int _ | Float _ | BigInt _ | Null | Undefined -> p)
2579+ | Block _ -> p)
2580+ | _ ->
2581+ {
2582+ p with
2583+ pat_desc =
2584+ Typedtree. map_pattern_desc with_bool_payload_overlaps p.pat_desc;
2585+ }
2586+
23642587let check_partial_gadt ?partial_match_warning_hint pred loc casel =
2365- let casel = lower_cases casel in
2588+ let casel =
2589+ List. map
2590+ (fun c -> {c with c_lhs = with_bool_payload_overlaps c.c_lhs})
2591+ (lower_cases casel)
2592+ in
23662593 check_partial_param
23672594 (do_check_partial_gadt ?partial_match_warning_hint pred)
23682595 do_check_fragile_gadt loc casel
0 commit comments