diff --git a/lib/stdlib/src/binary.erl b/lib/stdlib/src/binary.erl index 9ee1318145af..ef990097bad3 100644 --- a/lib/stdlib/src/binary.erl +++ b/lib/stdlib/src/binary.erl @@ -950,8 +950,9 @@ _Example:_ -doc(#{since => <<"OTP 27.0">>}). -spec join([binary()], binary()) -> binary(). join([], _Separator) -> <<>>; -join([H], _Separator) when is_binary(H) -> H; -join([H | T], Separator) -> +join([H], Separator) when is_binary(H) andalso is_binary(Separator) -> H; +join([H | T], Separator) when is_binary(Separator) -> + %% Starting with an empty binary convinces the compiler to use the new "private append" optimisation Acc = <<>>, join(T, Separator, <>); join(Arg, Separator) -> diff --git a/lib/stdlib/test/binary_module_SUITE.erl b/lib/stdlib/test/binary_module_SUITE.erl index 51e18ae76161..b2d4013f0756 100644 --- a/lib/stdlib/test/binary_module_SUITE.erl +++ b/lib/stdlib/test/binary_module_SUITE.erl @@ -261,6 +261,7 @@ badargs(Config) when is_list(Config) -> badarg = ?MASK_ERROR(binary:encode_hex(#{})), badarg = ?MASK_ERROR(binary:encode_hex(foo)), + badarg = ?MASK_ERROR(binary:join(<<"">>, ",")), badarg = ?MASK_ERROR(binary:join([""], <<",">>)), badarg = ?MASK_ERROR(binary:join([123], <<",">>)), badarg = ?MASK_ERROR(binary:join(123, <<",">>)),