Support ... in map and struct patterns - #7
Merged
Conversation
dannote
requested changes
Jul 20, 2026
dannote
left a comment
Collaborator
There was a problem hiding this comment.
The crash fix looks good, but this also codifies an existing inconsistency in map matching.
| describe "map and struct subset matching via search" do | ||
| test "a plain map pattern matches only the exact key set" do | ||
| src = "one = %{a: 1}\ntwo = %{a: 1, b: 2}\n" | ||
| assert [%{}] = ExAST.Patcher.find_all(src, "%{a: 1}") |
Collaborator
There was a problem hiding this comment.
This test locks in behavior that doesn't match the rest of the matcher. Maps are documented as partial, and nested maps already behave that way:
Patcher.find_all("x = %{a: 1, b: 2}", "_ = %{a: 1}")
# one matchThe top-level pattern only looks exact because candidate filtering checks the number of entries. We should fix that inconsistency rather than document it as the intended behavior. The ellipsis crash fix itself looks good.
`%{...}`, `%{..., k: v}` and `%Struct{...}` raised FunctionClauseError:
match_subset/3 had no clause for the `...` entry. Skip `...` during subset
matching so it imposes no key constraint.
The candidate filter already treated a `...` map/struct as any-arity, so
this completes the feature end to end: `...` matches any map/struct
(including empty), and `%{..., k: v}` matches any map with at least that
entry. Plain `%{k: v}` is unchanged — still an exact key set.
Map patterns matched a subset of keys at the matcher level, but the
top-level search silently required an exact key set: a map pattern's
candidate signature was {:call, :%{}, n}, treating the entry count as a
call arity, so candidate prefiltering rejected any map with a different
number of entries before the matcher ran. Nested maps (e.g. as the RHS
of `_ = %{a: 1}`) were never prefiltered this way and already matched
partially, and the docs describe maps as partial — so top-level search
was the inconsistent case.
Give map patterns a :any-arity signature in both signature/1 and
nested_call_signature/1, so prefiltering still restricts candidates to
map nodes but no longer gates on entry count. Subset matching is then
handled by the matcher as intended.
Update the search test to assert subset behavior: `%{a: 1}` now matches
both `%{a: 1}` and `%{a: 1, b: 2}`.
# Conflicts: # lib/ex_ast/pattern.ex # test/ex_ast/pattern_test.exs
dannote
approved these changes
Jul 22, 2026
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.
A literal
...in a map or struct pattern crashed —match_subset/3had no clause for the...node — so%{...},%{..., k: v}and%Struct{...}all raisedFunctionClauseError.The candidate filter already recognized these as any-arity maps/structs, so the feature was half-wired: patterns passed the filter and then blew up in subset matching.
What you can search for now with
mix ex_ast.search:%{role: :admin}%{..., role: :admin}role: :admin%{..., status: s}statuskey, capturing its value%{...}%Struct{...}Plain map patterns (
%{k: v}) are unchanged — still an exact key set. Structs already matched by subset of fields;%Struct{...}just drops the field constraint entirely.Before
After