Abbreviated method assignment with private attr_writer/attr_reader does not work.
This looks like a hole in the specification:
private def foo=(*) end public def foo; 0 end self.foo = 42 self.foo += 42 # private method `foo=' called for main:Object (NoMethodError) private :foo self.foo += 42 # private method `foo' called for main:Object (NoMethodError)
There is an exception for
private writers in the rule for private message sends, but apparently that exception needs to broadened so that it also works in the case of abbreviated assignments. I'm not entirely sure what this rule would be, but I don't think it would break backwards compatibility, since all situations that would work differently with the changed rule would currently raise a
The rule should be something like:
privatemethods can only be called without an explicit receiver.
- An exception is made for method assignments, where the literal receiver
selfis also allowed in the assignee method expression.
- This also applies to compound assignments:
self.foo ω= barshall always succeed if either or both of
Updated by nobu (Nobuyoshi Nakada) about 8 years ago
- Status changed from Open to Closed
- % Done changed from 0 to 100
Updated by headius (Charles Nutter) almost 8 years ago
I'm confused about how private dispatch against self should behave. I expected that at least all
self.x calls would be considered private dispatch, but that's not the case.
For this code:
private def foo; @a; end def foo=(a); @a = a; end puts begin; foo; rescue; 'foo failed'; else; 'foo worked'; end puts begin; self.foo; rescue; 'self.foo failed'; else; 'self.foo worked'; end puts begin; self.foo = 1; rescue; 'self.foo = 1 failed'; else; 'self.foo = 1 worked'; end puts begin; self.foo += 1; rescue; 'self.foo += 1 failed'; else; 'self.foo += 1 worked'; end puts begin; o = self; o.foo; rescue; 'o = self; o.foo failed'; else; 'o = self; o.foo worked'; end puts begin; o = self; o.foo = 1; rescue; 'o = self; o.foo = 1 failed'; else; 'o = self; o.foo = 1 worked'; end
I have the following behavior with 2.2.0-preview1:
foo worked self.foo failed self.foo = 1 worked self.foo += 1 worked o = self; o.foo failed o = self; o.foo = 1 failed
On 2.1, the
self.foo += 1 case failed, which is the purpose of this bug.
However, I don't see why that case should pass and self.foo should fail.
self.foo += must do an implicit self.foo call, meaning the "self" exception for private dispatch propagates through. It makes no sense that directly calling
self.foo would produce an error and implicitly calling
self.foo would not.
I would also argue that the "o" cases should pass as well, but I'm more concerned about the inconsistent spec change in this bug.
Updated by headius (Charles Nutter) over 7 years ago
The following two pieces of code should both work, since they both do the same thing. VM-level exceptions hurt understandability, and it should come as a surprise to any Rubyist that self.foo+= can skip visibility checks but self.foo can't.
self.foo += 1 # ok
self.foo = self.foo + 1 # visibility error
The first line essentially expands to the second line. The first line works, the second line doesn't. That makes no sense to me. Also, what if I had "self.foo += 1" (working fine after this bug) and later wanted to expand the logic to add some additional operations. It would break, and I wouldn't understand why.
self.foo = self.foo * 5 + 1 # visibility error
I think consistency wins here and self.foo should be able to skip visibility checks too.
Updated by Eregon (Benoit Daloze) over 7 years ago
Until now, the ability to call private methods has always been a property verifiable at parse time
(no receiver or "receiver is self and methods ends with =").
I think it is good to remain that way so it is really easy to know whether you may call private method, as a Ruby user.
So I think the "o" cases above should not be able to call private methods.
On the other hand, the current semantics seem less clear and more complex than they could be.
I agree with Charles, the "no explicit receiver or receiver is literally
self" rule is simpler, more consistent and avoid problems with special cases such as assignments.
Having a hidden implicit visibility such as in the committed change seems harder for everyone to understand and may only solve a part of the special cases.