Skip to content

Fix bare imports breaking pattern matching with arguments#4

Merged
dannote merged 1 commit into
elixir-vibe:masterfrom
rodrigues:import_bug
Jun 29, 2026
Merged

Fix bare imports breaking pattern matching with arguments#4
dannote merged 1 commit into
elixir-vibe:masterfrom
rodrigues:import_bug

Conversation

@rodrigues

@rodrigues rodrigues commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Hi @dannote, thanks for the lib!

I've hit this regression after upgrading, let me know if this makes sense, or if you need any changes.

Thanks!


A plain import Foo was treated as importing every name, so the matcher rewrote every local call to Foo.<name> and any pattern with arguments stopped matching. Now we only rewrite calls that an import actually lists via :only.

# given a module with `import Enum`
mix ex_ast.search 'Logger.info(...)' demo.ex   # was 0, now 1
mix ex_ast.search 'def add(_, _, _)' demo.ex    # was 0, now 1

Also fixes multi-arity imports: import Foo, only: [bar: 1, bar: 2] used Keyword.get/2 to check membership, which only sees the first arity, so later arities of the same name never resolved. Now it matches the whole {name, arity} pair.

One consequence: a bare import Enum no longer expands anything, so searching Enum.map(_, _) won't find a bare map(...). Doing that right would mean knowing what Enum actually exports, and we can't get that from the AST alone — the import line doesn't say which local calls came from it. This is potentially addressed by this draft PR #5

A plain `import Foo` was treated as importing every name, so the matcher
rewrote every local call to `Foo.<name>` and any pattern with arguments
stopped matching. Now we only rewrite calls that an import actually lists
via `:only`.

    # given a module with `import Enum`
    mix ex_ast.search 'Logger.info(...)' demo.ex   # was 0, now 1
    mix ex_ast.search 'def add(_, _, _)' demo.ex    # was 0, now 1

Also fixes multi-arity imports: `import Foo, only: [bar: 1, bar: 2]` used
`Keyword.get/2` to check membership, which only sees the first arity, so
later arities of the same name never resolved. Now it matches the whole
`{name, arity}` pair.

One consequence: a bare `import Enum` no longer expands anything, so
searching `Enum.map(_, _)` won't find a bare `map(...)`. Doing that right
would mean knowing what Enum actually exports, and we can't get that from
the AST alone — the import line doesn't say which local calls came from
it.
@dannote dannote merged commit 45a5d9c into elixir-vibe:master Jun 29, 2026
@dannote

dannote commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Thanks, great catch, merged!

@rodrigues rodrigues deleted the import_bug branch June 29, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants