Skip to content
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

Fix path lookup when ancestor finds type with same name as current scope #10901

Merged

Conversation

HertzDevil
Copy link
Contributor

Fixes #4086.

class Foo
  class Bar
    class Baz < Foo
      def self.foo
        # `Baz` is now tried in the following order:
        #
        # * `Baz` relative to `::Foo::Bar::Baz` (direct namespace child)
        # * `::Foo::Bar::Baz` (current scope)
        # * `Baz` relative to `::Foo` (ancestors)
        # * `Baz` relative to `::Foo::Bar` (enclosing namespace)
        #
        # this only applies to the first path item; subsequent items must
        # descend along namespaces so `Baz::Baz` is still undefined
        Baz
      end
    end
  end

  class Baz
  end
end

Foo::Bar::Baz.foo # => Foo::Bar::Baz

This behaviour is consistent with Ruby and C++.

The path lookup rules are unchanged if the path's first item and the current scope's last path item do not match.

@HertzDevil HertzDevil marked this pull request as draft July 8, 2021 02:20
@HertzDevil HertzDevil marked this pull request as ready for review July 8, 2021 03:09
@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:lang:type-system topic:compiler:semantic labels Jul 8, 2021
@beta-ziliani beta-ziliani added this to the 1.2.0 milestone Sep 6, 2021
@straight-shoota straight-shoota merged commit 6dd03f3 into crystal-lang:master Sep 7, 2021
@HertzDevil HertzDevil deleted the bug/path-lookup-self-first branch September 8, 2021 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic topic:lang:type-system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

crystal choose wrong initialize method
3 participants