Skip to content

Commit

Permalink
Allow private method invocation with self receiver (crystal-lang#6075)
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust authored and chris-huxtable committed Jun 6, 2018
1 parent 00e5fdb commit f1395a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions spec/compiler/semantic/visibility_modifiers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/semantic/call_error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f1395a9

Please sign in to comment.