Skip to content

Commit b8732bd

Browse files
committed
Improve error message for invalid module attribute usage, closes #12656
1 parent 2252a8f commit b8732bd

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,10 +3555,12 @@ defmodule Kernel do
35553555
not bootstrapped?(Macro) ->
35563556
nil
35573557

3558-
not function? and __CALLER__.context == :match ->
3558+
not function? and (__CALLER__.context == :match or __CALLER__.context == :guard) ->
35593559
raise ArgumentError,
35603560
"""
3561-
invalid write attribute syntax. If you want to define an attribute, don't do this:
3561+
invalid usage of module attributes. Module attributes cannot be used inside \
3562+
pattern matching (and guards) outside of a function. If you are trying to \
3563+
define an attribute, do not do this:
35623564
35633565
@foo = :value
35643566

lib/elixir/test/elixir/kernel_test.exs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,25 @@ defmodule KernelTest do
831831
end
832832

833833
test "matching attribute" do
834-
assert_raise ArgumentError, ~r"invalid write attribute syntax", fn ->
834+
assert_raise ArgumentError, ~r"invalid usage of module attributes", fn ->
835835
defmodule MatchAttributeInModule do
836836
@foo = 42
837837
end
838838
end
839+
840+
assert_raise ArgumentError, ~r"invalid usage of module attributes", fn ->
841+
defmodule MatchAttributeInModule do
842+
@foo 16
843+
<<_::@foo>> = "ab"
844+
end
845+
end
846+
847+
assert_raise ArgumentError, ~r"invalid usage of module attributes", fn ->
848+
defmodule MatchAttributeInModule do
849+
@foo 16
850+
<<_::size(@foo)>> = "ab"
851+
end
852+
end
839853
end
840854
end
841855

0 commit comments

Comments
 (0)