diff --git a/spec/compiler/semantic/visibility_modifiers_spec.cr b/spec/compiler/semantic/visibility_modifiers_spec.cr index 63b0f3c13cd4..37c08e66b60d 100644 --- a/spec/compiler/semantic/visibility_modifiers_spec.cr +++ b/spec/compiler/semantic/visibility_modifiers_spec.cr @@ -74,6 +74,22 @@ describe "Visibility modifiers" do "private method 'bar' called for Foo" end + it "allows invoking private method from the same class" do + assert_type(%( + class Foo + private def foo + 1 + end + + def bar + self.foo + end + end + + Foo.new.bar + )) { int32 } + end + it "allows invoking protected method from the same class" do assert_type(%( class Foo diff --git a/src/compiler/crystal/semantic/call_error.cr b/src/compiler/crystal/semantic/call_error.cr index 57a2ca412101..86cae7d6275b 100644 --- a/src/compiler/crystal/semantic/call_error.cr +++ b/src/compiler/crystal/semantic/call_error.cr @@ -568,8 +568,8 @@ class Crystal::Call case match.def.visibility when .private? if obj = @obj - if obj.is_a?(Var) && obj.name == "self" && match.def.name.ends_with?('=') - # Special case: private setter can be called with self + if obj.is_a?(Var) && obj.name == "self" + # Special case: private method can be called with self return end