diff --git a/lib/compiler/src/beam_types.erl b/lib/compiler/src/beam_types.erl index 0c1b30538ed0..595c083ae595 100644 --- a/lib/compiler/src/beam_types.erl +++ b/lib/compiler/src/beam_types.erl @@ -260,10 +260,15 @@ join(#t_tuple{}=A, #t_tuple{}=B) -> lub(A, B); {_Key, none} -> lub(A, B); - {KeyA, KeyB} when KeyA < KeyB -> - #t_union{tuple_set=[{KeyA, A}, {KeyB, B}]}; - {KeyA, KeyB} when KeyA > KeyB -> - #t_union{tuple_set=[{KeyB, B}, {KeyA, A}]} + {KeyA, KeyB} -> + %% We must use total ordering rather than plain '<' as -0.0 differs + %% from +0.0 + case total_compare(KeyA, KeyB, fun erlang:'<'/2) of + true -> + #t_union{tuple_set=[{KeyA, A}, {KeyB, B}]}; + false -> + #t_union{tuple_set=[{KeyB, B}, {KeyA, A}]} + end end; join(#t_tuple{}=A, B) -> %% All other combinations have been tried already, so B must be 'other' diff --git a/lib/compiler/test/beam_type_SUITE.erl b/lib/compiler/test/beam_type_SUITE.erl index 69bfcbe7b448..d209dabe06d5 100644 --- a/lib/compiler/test/beam_type_SUITE.erl +++ b/lib/compiler/test/beam_type_SUITE.erl @@ -1514,6 +1514,7 @@ float_confusion(_Config) -> {'EXIT', _} = catch float_confusion_3(id(0.0)), ok = float_confusion_4(id(1)), {'EXIT', _} = catch float_confusion_5(), + {'EXIT', _} = catch float_confusion_6(), ok. float_confusion_1(_, _) -> @@ -1547,6 +1548,22 @@ float_confusion_5() -> end * 0, ok. +%% GH-8097: Record keys weren't compared in total order, confusing +0.0 and +%% -0.0 and crashing the compiler. +float_confusion_6() -> + << + (ok) + || _ := {_V1} <- ok, + (maybe + {} ?= _V1 + else + -0.0 -> + []; + 0.0 -> + [] + end) + >>. + %%% %%% Common utilities. %%%