Skip to content

Explicitly use public visibility for define_singleton_method#7055

Merged
headius merged 2 commits intojruby:jruby-9.3from
headius:define_singleton_method_public
Mar 16, 2022
Merged

Explicitly use public visibility for define_singleton_method#7055
headius merged 2 commits intojruby:jruby-9.3from
headius:define_singleton_method_public

Conversation

@headius
Copy link
Member

@headius headius commented Jan 31, 2022

This PR makes public visibility the only visiblity for define_singleton_method methods, and adds a spec testing a simple case of this.

This is a bit of an open question, since in CRuby define_singleton_method ends up producing public methods nearly all of the time due to a quirk in how the frame's visibility is accessed.

There's at least one edge case where define_singleton_method does obey the frame visibility, but it requires calling define_singleton_method from within a scope rooted on the destination singleton class. I would argue this is a peculiar-enough edge case that it should be made to explicitly use public visibility in all cases.

See ruby/ostruct#40 for a related issue, where we warn about aliasing define_singleton_method due to its rarely-used ability to source visibility from the caller's scope.

See https://bugs.ruby-lang.org/issues/18561 for my CRuby issue to codify this behavior and eliminate the edge cases.

This bypasses the frame-checking logic in define_method since we
know the target class (target object's singleton class) will never
match the caller environment.
In CRuby this logic happens to always be public because the target
class (the target object's singleton class) will almost never
match the caller's scope. Pending CRuby confirming that this is
the intended behavior and making it explicit so the caller's frame
is never used for define_singleton_method visibility.
@headius
Copy link
Member Author

headius commented Mar 16, 2022

This change appears to be getting some traction with the CRuby folks (ruby/ruby#5636) so we will optimistically proceed with this change in 9.3.4.

@headius headius merged commit 913747e into jruby:jruby-9.3 Mar 16, 2022
@headius headius deleted the define_singleton_method_public branch March 16, 2022 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant