Issue #9941 has been reported by Alexey Muranov.

----------------------------------------
Feature #9941: Issue a warning when `module` or `class` keyword causes re-initialization of a constant that will become the module/class name, instead of creating/reopening the module/class
https://bugs.ruby-lang.org/issues/9941

* Author: Alexey Muranov
* Status: Open
* Priority: Normal
* Assignee: 
* Category: 
* Target version: 
----------------------------------------
I think that it would be appropriate to issue a warning similar to "warning: already initialized constant" when "`module`" or "`class`" keyword causes re-initialization of a constant.  Here is an example of code that looks suspicious to me and IMO should issue such a warning:

```ruby
module A
  module B
    C = 1
  end
end

module D
  include A
end

puts D.constants # => B
puts D::B        # => A::B
puts D::B::C     # => 1

module D::B
end

puts D.constants # => B
puts D::B        # => D::B
puts D::B::C     # => uninitialized constant D::B::C (NameError)
```

P.S. I think this behavior is related to the impossibility to re-open anonymous classes/modules and to the somewhat strange IMO scoping rules for constants :).



-- 
https://bugs.ruby-lang.org/