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 type def reopening type from parent namespace #11208

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions spec/compiler/semantic/class_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,20 @@ describe "Semantic: class" do
") { char }
end

it "type def does not reopen type from parent namespace (#11181)" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 "type def" is confusing because typedef is a thing in the language. Maybe "type declaration" or just "class def" or "class declaration"?

Copy link
Member Author

@straight-shoota straight-shoota Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took that term from the lookup_type_def method. But yeah, I also thought it might be considered ambiguous.

A type declaration is var : Type, so I don't think that's a better solution. class works here, and I suppose we can use module in the other spec (I didn't add specs for all kinds of types, but I think that's fine b/c the code is identical).

But it would be better to have a generic term for definitions of all kinds of types (class, struct, module, ...). And I think "type definition" is a natural solution for that with "type" being the abstraction for all kinds of types and it being a "definition".

If we go along with #10031 the term would be free from overloads =) For now, I'd propose to leave it as is. In this context, the meaning is unambiguous.

assert_type <<-CR, inject_primitives: false { types["Baz"].types["Foo"].types["Bar"].metaclass }
class Foo::Bar
end

module Baz
class Foo::Bar
end
end

Baz::Foo::Bar
CR
end

it "finds in global scope if includes module" do
assert_type("
class Baz
Expand Down
14 changes: 14 additions & 0 deletions spec/compiler/semantic/module_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,20 @@ describe "Semantic: module" do
)) { types["Foo"].types["Bar"].types["Baz"].metaclass }
end

it "type def does not reopen type from parent namespace (#11181)" do
assert_type <<-CR, inject_primitives: false { types["Baz"].types["Foo"].types["Bar"].metaclass }
module Foo::Bar
end

module Baz
module Foo::Bar
end
end

Baz::Foo::Bar
CR
end

it "correctly types type var in included module, with a restriction with a free var (bug)" do
assert_type(%(
module Moo(T)
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/top_level_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor

def lookup_type_def_name_creating_modules(path : Path)
base_type = path.global? ? program : current_type
target_type = base_type.lookup_path(path).as?(Type).try &.remove_alias_if_simple
target_type = base_type.lookup_path(path, lookup_in_namespace: false).as?(Type).try &.remove_alias_if_simple

unless target_type
next_type = base_type
Expand Down
Loading