elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.54k stars 3.38k forks source link

Do not allow `defmodule true | false` #9030

Closed rodrigues closed 5 years ago

rodrigues commented 5 years ago

Environment

Current behavior

Please close this issue if it doesn't make sense, it just seems to me this could be improved at the language level. 👍🏼

As this tweet points out, you can call defmodule passing true or false as a module name:

defmodule false, do: def foo, do: "bar"
> false.foo
"bar"

The point is not about $handle_undefined_function – an erlang feature mentioned in the tweet but beyond the scope of this ticket – but about allowing these module names to be defined.

As much as true and false are just atoms in the end of the day, I think defmodule would be a bit more robust if it doesn't allow these to be defined as module names, just as it does with nil.

Expected behavior

The same when you call defmodule nil, fail:

iex(5)> defmodule nil, do: def a, do: 1
** (ArgumentError) cannot invoke def/2 outside module
    (elixir) lib/kernel.ex:5362: Kernel.assert_module_scope/3
    (elixir) lib/kernel.ex:4101: Kernel.define/4
    (elixir) expanding macro: Kernel.def/2
    iex:5: (file)
whatyouhide commented 5 years ago

The way defmodule nil fails is also terrible. If we want to forbid special atoms to be module names I think we should do it by returning a nicer error.

josevalim commented 5 years ago

Agree, we should forbid those three and raise nice error messages.

whatyouhide commented 5 years ago

@rodrigues do you want to work on a PR that adds this? :)

rodrigues commented 5 years ago

@whatyouhide yes sirrr, just don't know if I can do it, hehehe, gonna take a look at it tonight 👍🏼

whatyouhide commented 5 years ago

@rodrigues of course, no rush. Please feel free to ping us here or on IRC or the Elixir Slack if you need some help or guidance. :)

josevalim commented 5 years ago

Closing in favor of the PR.