fix: dispatch on whether a comparator is implemented - #2840
Merged
zachdaniel merged 2 commits intoAug 4, 2026
Merged
Conversation
`Comp` cannot dispatch a protocol on two operand types directly, so it encodes the pair into a synthetic module name and dispatches on that. Nothing today pins which implementation a given pair actually reaches, so a change to how that name is resolved could route every pair to the generic fallback without a single test noticing. Pin both outcomes with comparisons the fallback would answer differently: Erlang term order is case-sensitive, orders a struct before a binary, and orders `Decimal` by its fields rather than by its value. Passes against unmodified code.
`Module.safe_concat/1` succeeds when the atom exists, which says nothing about
whether the pair has a comparator behind it. Anything that ever names
`Comparable.Type.<L>.To.<R>` creates that atom — a dynamic `Module.concat/1`, a
typespec, a doc reference — so the same comparison answers from the fallback or
raises `Protocol.UndefinedError` depending on ambient state the caller can
neither see nor control:
Comp.compare(Ash.CiString.new("a"), Decimal.new(1))
#=> :lt
_ = Comparable.Type.Ash.CiString.To.Decimal
Comp.compare(Ash.CiString.new("a"), Decimal.new(1))
#=> ** (Protocol.UndefinedError)
Decide on the implementation instead, which is the condition the rescue was
reaching for. Pairs that do have a comparator still reach it, pinned by the
preceding commit.
This makes the answer consistent, not meaningful: the fallback still orders
`Ash.CiString` against `Decimal` by map size rather than by anything about the
values. `Comparable.compare/1` returns `:lt | :eq | :gt` or raises, so there is
no way to say a pair has no ordering.
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contributor checklist
Leave anything that you believe does not apply unchecked.
Summary
Fixes #2839