-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn about name conflicts in nested modules #12451
Conversation
Move whatever that module is checking into the same test that checks for the warning :) |
@josevalim done and done ✅ |
@@ -129,14 +137,25 @@ defmodule Macro.AliasTest.Aliaser do | |||
end | |||
end | |||
|
|||
defmodule Macro.AliasTest.User do | |||
defmodule Macro.AliasTest do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is most likely no longer needs to be a separate module, we can fold into the previous one?
lib/elixir/lib/kernel.ex
Outdated
"the nested call to \"defmodule #{defmod_name}\" will not implicitly alias " <> | ||
"#{inspect(alias)} here because there is already an alias #{inspect(other_parent)} " <> | ||
"that resolves to the same alias. To fix this, either remove the " <> | ||
"#{inspect(other_parent)} alias, or define the nested module outside of the parent", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am trying to think about the error message. What do you think about:
you are defining a module named Child.Comment, inside module Post,
but the module will be defined as RootChild.Comment instead of Post.Child.Comment
due to the "alias RootChild, as: Child". You must address this ambiguity
either by removing the alias Child or by defining the module outside of the parent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's not what happens though, because there is technically no ambiguity, is just that we don't set up an implicit alias. I updated the error message, I like it a lot better now. If you're good with it, I'll merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nits and ship it!
Either I fail to understand what's the real issue here or those error messages are not correct.
gives
and code with conflicts
gives
I'm seeing that the implicit Regarding the messages, it's not clear where this implicit alias is not being set. Is it in the parent module body or in the child module body or both |
The implicit overriding is what we are warning about. |
Ah, I see your point, the original report says the alias is not defined, but the alias is always defined and overrides. Which I would say is the correct behaviour! So I think we can go ahead and close this? Thanks for sanity checking @lukaszsamson! |
The original problem from the discussion list was aboutan ecto issue. Maybe ecto macros make some assumptions about aliases that are not fully correct |
If we want, we can add a warning that a previously defined alias is being overridden by the module.
But it feels like it is expected new aliases override old aliases, so I would close it. |
Yes, Ecto is showing the behaviour we thought we were seeing in this pull request. But not Elixir. |
I will close this. I think the overridde warning is ok. Later aliases always override earlier ones. Sorry @whatyouhide! |
That's easy to understand in case of ordinary aliases. defmodule implicit ones are not so obvious (case in point - I've found another bug in elixir_sense alias handling) |
@josevalim yeah I think the behaviour is correct per se, but it's hard to understand that the |
Yeah, let's give it a try with an updated message. And we can run on Elixir main for a couple weeks before release and see if we catch anything in any of our existing projects. |
I wanted to see how this alias overwriting work in case of external submodules and I've found something odd
So it removes the alias. This does not happen for nested external submodule e.g. |
I think that makes sense. Otherwise you wouldn't be able to refer to it as Child in the same module. |
OK but why there's no similar behavior in this case?
A nested external submodule does not unalias |
Ok, we should either do it for both or non. This is a bug. |
submodule now properly sets alias in its own scope (introduced in 9de7dd8) submodule overwrites alias external submodule unaliases See discussions in elixir-lang/elixir#12451
submodule now properly sets alias in its own scope (introduced in 9de7dd8) submodule overwrites alias external submodule unaliases See discussions in elixir-lang/elixir#12451
@josevalim so, do we still want the warning in this PR, or should we close this? 🙃 |
I think we can close this and we have added a warning to Ecto. Sorry for the false alarm here. |
Closes #11130.
I’m not sure what to do for this particular warning in this particular test, since we're testing exactly the behaviour that warns here 😕