diff --git a/spec/compiler/semantic/class_spec.cr b/spec/compiler/semantic/class_spec.cr index 88c3dd474a3f..2445a9eedf5f 100644 --- a/spec/compiler/semantic/class_spec.cr +++ b/spec/compiler/semantic/class_spec.cr @@ -383,6 +383,20 @@ describe "Semantic: class" do ") { char } 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 } + 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 diff --git a/spec/compiler/semantic/module_spec.cr b/spec/compiler/semantic/module_spec.cr index a55efaf93a3d..46de0bd0f4c0 100644 --- a/spec/compiler/semantic/module_spec.cr +++ b/spec/compiler/semantic/module_spec.cr @@ -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) diff --git a/src/compiler/crystal/semantic/top_level_visitor.cr b/src/compiler/crystal/semantic/top_level_visitor.cr index 98ceb23df6b3..1fc7119b9ffd 100644 --- a/src/compiler/crystal/semantic/top_level_visitor.cr +++ b/src/compiler/crystal/semantic/top_level_visitor.cr @@ -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