Skip to content

Mildly confusing with and else clause error #14569

@lawik

Description

@lawik
Contributor

Elixir and Erlang/OTP versions

Elixir 1.18 and main (around 1.19)
OTP 27

Operating system

Linux

Current behavior

This code:

with [] <- System.ls!() do
  :ok
else
    :error
end

Causes this error:

    error: expected -> clauses for :else in "with"
    │
  1 │ with [] <- System.ls!() do
    │ ^
    │
    └─ bla.exs:1:1

** (CompileError) bla.exs: cannot compile file (errors have been logged)

I was quite tripped up for a moment looking at where the arrow is pointing at the start of with which is not the problem.

I took a brief look at it but fell down a dark hole and couldn't crawl out before my discretionary time ran out. So I figured I'd report it :)

Expected behavior

Ideally it would point at the else clause or at the bad value inside of it.

Activity

josevalim

josevalim commented on Jun 10, 2025

@josevalim
Member

Unfortunately we cannot provide precise location here because of the AST structure but we can provide a better error message. Something like:

you have specified "else" block in "with" but instead you must specify "pattern -> expr" clauses

lawik

lawik commented on Jun 10, 2025

@lawik
ContributorAuthor

Ah, that figures then. The information is good but it didn't make me snap to the solution. I think your suggested change makes sense.

Is the expected -> clauses for %Key in %Kind used for more structures than with? It seemed fairly generic in how it was written but I don't know if the error is actually only for with. I could PR the error if I get a chance.

josevalim

josevalim commented on Jun 10, 2025

@josevalim
Member

I will push something soon, no worries.

added a commit that references this issue on Jun 10, 2025
5946e8b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @josevalim@lawik

        Issue actions

          Mildly confusing `with` and `else` clause error · Issue #14569 · elixir-lang/elixir