Skip to content

assert macro allows for guards in match context #13814

@lukaszsamson

Description

@lukaszsamson

Elixir and Erlang/OTP versions

Erlang/OTP 26 [erts-14.2.5.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]

Elixir 1.17.2 (compiled with Erlang/OTP 26)

Operating system

any

Current behavior

The following test unexpectedly passes.

defmodule FooTest do
  use ExUnit.Case, async: true

  test "foo" do
    assert (a when is_integer(a)) = 5
  end
end

Support for guards is not mentioned in https://hexdocs.pm/ex_unit/1.17.2/ExUnit.Assertions.html#assert/1

Expected behavior

Basing on the documentation I expect assert to not change language semantics. This code should emit a compilation error as guards are not allowed in match context. The same code without assert errors with

    error: cannot invoke remote function :erlang.is_integer/1 inside a match
    │
  5 │      (a when is_integer(a)) = 5
    │              ^
    │
    └─ test/foo_test.exs:5:14: FooTest."test foo"/1

Excluding tags: [pending: true]
    error: cannot find or invoke local when/2 inside a match. Only macros can be invoked inside a match and they must be defined before their invocation. Called as: a when is_integer(a)
    │
  5 │      (a when is_integer(a)) = 5
    │         ^
    │
    └─ test/foo_test.exs:5:9: FooTest."test foo"/1
    
== Compilation error in file test/foo_test.exs ==
** (CompileError) test/foo_test.exs: cannot compile module FooTest (errors have been logged)

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

    Issue actions