From dc3468ff993df86fbf957559838d312cb102b4af Mon Sep 17 00:00:00 2001 From: Jonathan Gnagy Date: Fri, 16 Oct 2026 23:04:24 -0700 Subject: [PATCH 1/2] Fix protocol consolidation crash on beams from older Elixir versions Commit 558215b ("Dispatch consolidated protocols through literal external funs") changed the definitions generated by defprotocol to __select_impl__/1, __dispatch_impl__/4, and __select_struct__/1, and Protocol.consolidate/5 assumed they are always present. Consolidating a protocol beam compiled before that change raised a bare MatchError (no match of right hand side value: nil) whenever an existing _build was reused, since Mix manifests do not record the Elixir version and the version string stayed 1.21.0-dev. * Bump Mix.Compilers.Elixir's @manifest_vsn from 37 to 38 so builds compiled with previous versions are fully recompiled * Raise a proper ArgumentError with an actionable message in Protocol.consolidate/5 when the expected definitions are missing, instead of a bare MatchError Fixes #15994 Assisted-by: OpenCode:glm-5.3:cloud Signed-off-by: Jonathan Gnagy --- CHANGELOG.md | 2 ++ lib/elixir/lib/protocol.ex | 27 +++++++++++++++-- .../elixir/protocol/consolidation_test.exs | 29 +++++++++++++++++++ lib/mix/lib/mix/compilers/elixir.ex | 2 +- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5495540658..5e1a06da004 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ * [Keyword] Delete duplicate keys when `Keyword.get_and_update/3` and `Keyword.get_and_update!/3` return `:pop` * [Macro] Properly escape C1 control characters and Unicode noncharacters * [NaiveDateTime] Fix `NaiveDateTime.diff/3` over-counting incomplete units + * [Protocol] Fix protocol consolidation crashing with a MatchError when the protocol beam was compiled with an incompatible version of Elixir, raising a proper error message instead * [Range] Fix `Range.disjoint?/2` for ranges beyond floating-point precision * [Range] Fix `Range.disjoint?/2` for single-element ranges with a negative step * [String] Fix `String.reverse/1` grapheme ordering around invalid UTF-8 bytes @@ -83,6 +84,7 @@ #### Mix + * [Mix] Bump the Elixir compiler manifest version, so builds compiled before the new consolidated protocol dispatch are recompiled * [Mix] Prevent synchronization lock files from being overwritten with empty contents * [Mix.Release] Accept chardata paths in `Mix.Release.make_boot_script/4` * [Mix.SCM.Git] Raise if Git refspecs start with `-` diff --git a/lib/elixir/lib/protocol.ex b/lib/elixir/lib/protocol.ex index 7f07a1294e8..9033d65cfda 100644 --- a/lib/elixir/lib/protocol.ex +++ b/lib/elixir/lib/protocol.ex @@ -636,9 +636,14 @@ defmodule Protocol do built_in_plus_any = [Any] ++ for {mod, _guard} <- built_in(), do: mod structs = types -- built_in_plus_any - {select_impl, definitions} = List.keytake(definitions, {:__select_impl__, 1}, 0) - {dispatch_impl, definitions} = List.keytake(definitions, {:__dispatch_impl__, 4}, 0) - {select_struct, definitions} = List.keytake(definitions, {:__select_struct__, 1}, 0) + {select_impl, definitions} = + keytake_definition(definitions, {:__select_impl__, 1}, protocol) + + {dispatch_impl, definitions} = + keytake_definition(definitions, {:__dispatch_impl__, 4}, protocol) + + {select_struct, definitions} = + keytake_definition(definitions, {:__select_struct__, 1}, protocol) protocol_funs = get_protocol_functions(protocol_def) @@ -673,6 +678,22 @@ defmodule Protocol do end end + # The definitions below are generated by defprotocol. If any of them is + # missing, the beam was compiled with an incompatible version of Elixir. + defp keytake_definition(definitions, {name, arity}, protocol) do + case List.keytake(definitions, {name, arity}, 0) do + nil -> + raise ArgumentError, + "cannot consolidate protocol #{Macro.inspect_atom(:literal, protocol)}: " <> + "it does not define #{name}/#{arity}, which means it was compiled " <> + "with an incompatible version of Elixir, please recompile the protocol " <> + "with the current version" + + {entry, definitions} -> + {entry, definitions} + end + end + defp new_signatures(definitions, protocol_funs, protocol, types, structs) do alias Module.Types.Descr types_minus_any = List.delete(types, Any) diff --git a/lib/elixir/test/elixir/protocol/consolidation_test.exs b/lib/elixir/test/elixir/protocol/consolidation_test.exs index 7540f1739c3..40513872fe1 100644 --- a/lib/elixir/test/elixir/protocol/consolidation_test.exs +++ b/lib/elixir/test/elixir/protocol/consolidation_test.exs @@ -260,6 +260,35 @@ defmodule Protocol.ConsolidationTest do assert Protocol.consolidate(NoBeam, []) == {:error, :no_beam_info} end + test "consolidation errors on incompatible protocol beams" do + import PathHelpers + + # A protocol compiled before Elixir v1.21 does not define the + # __select_impl__/1, __dispatch_impl__/4, and __select_struct__/1 + # definitions used during consolidation. + write_beam( + defmodule IncompatibleProtocol do + Module.register_attribute(__MODULE__, :__protocol__, persist: true) + @__protocol__ [fallback_to_any: false] + + def __protocol__(:module), do: __MODULE__ + def __protocol__(:functions), do: [ok: 1] + def __protocol__(:consolidated?), do: false + def __protocol__(:impls), do: :not_consolidated + end + ) + + message = + "cannot consolidate protocol Protocol.ConsolidationTest.IncompatibleProtocol: " <> + "it does not define __select_impl__/1, which means it was compiled " <> + "with an incompatible version of Elixir, please recompile the protocol " <> + "with the current version" + + assert_raise ArgumentError, message, fn -> + Protocol.consolidate(IncompatibleProtocol, []) + end + end + test "protocol not implemented" do message = "protocol Protocol.ConsolidationTest.Sample not implemented for Atom. " <> diff --git a/lib/mix/lib/mix/compilers/elixir.ex b/lib/mix/lib/mix/compilers/elixir.ex index 3ee3e388a44..25964880fad 100644 --- a/lib/mix/lib/mix/compilers/elixir.ex +++ b/lib/mix/lib/mix/compilers/elixir.ex @@ -5,7 +5,7 @@ defmodule Mix.Compilers.Elixir do @moduledoc false - @manifest_vsn 37 + @manifest_vsn 38 @checkpoint_vsn 5 import Record From cb4f70e14f213795afd74e178fb32c6983d70e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 17 Oct 2026 09:35:39 +0200 Subject: [PATCH 2/2] Apply batched suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Valim --- CHANGELOG.md | 2 -- .../elixir/protocol/consolidation_test.exs | 29 ------------------- 2 files changed, 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e1a06da004..e5495540658 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,7 +63,6 @@ * [Keyword] Delete duplicate keys when `Keyword.get_and_update/3` and `Keyword.get_and_update!/3` return `:pop` * [Macro] Properly escape C1 control characters and Unicode noncharacters * [NaiveDateTime] Fix `NaiveDateTime.diff/3` over-counting incomplete units - * [Protocol] Fix protocol consolidation crashing with a MatchError when the protocol beam was compiled with an incompatible version of Elixir, raising a proper error message instead * [Range] Fix `Range.disjoint?/2` for ranges beyond floating-point precision * [Range] Fix `Range.disjoint?/2` for single-element ranges with a negative step * [String] Fix `String.reverse/1` grapheme ordering around invalid UTF-8 bytes @@ -84,7 +83,6 @@ #### Mix - * [Mix] Bump the Elixir compiler manifest version, so builds compiled before the new consolidated protocol dispatch are recompiled * [Mix] Prevent synchronization lock files from being overwritten with empty contents * [Mix.Release] Accept chardata paths in `Mix.Release.make_boot_script/4` * [Mix.SCM.Git] Raise if Git refspecs start with `-` diff --git a/lib/elixir/test/elixir/protocol/consolidation_test.exs b/lib/elixir/test/elixir/protocol/consolidation_test.exs index 40513872fe1..7540f1739c3 100644 --- a/lib/elixir/test/elixir/protocol/consolidation_test.exs +++ b/lib/elixir/test/elixir/protocol/consolidation_test.exs @@ -260,35 +260,6 @@ defmodule Protocol.ConsolidationTest do assert Protocol.consolidate(NoBeam, []) == {:error, :no_beam_info} end - test "consolidation errors on incompatible protocol beams" do - import PathHelpers - - # A protocol compiled before Elixir v1.21 does not define the - # __select_impl__/1, __dispatch_impl__/4, and __select_struct__/1 - # definitions used during consolidation. - write_beam( - defmodule IncompatibleProtocol do - Module.register_attribute(__MODULE__, :__protocol__, persist: true) - @__protocol__ [fallback_to_any: false] - - def __protocol__(:module), do: __MODULE__ - def __protocol__(:functions), do: [ok: 1] - def __protocol__(:consolidated?), do: false - def __protocol__(:impls), do: :not_consolidated - end - ) - - message = - "cannot consolidate protocol Protocol.ConsolidationTest.IncompatibleProtocol: " <> - "it does not define __select_impl__/1, which means it was compiled " <> - "with an incompatible version of Elixir, please recompile the protocol " <> - "with the current version" - - assert_raise ArgumentError, message, fn -> - Protocol.consolidate(IncompatibleProtocol, []) - end - end - test "protocol not implemented" do message = "protocol Protocol.ConsolidationTest.Sample not implemented for Atom. " <>