Allows calling a private method only with bare self

This commit is contained in:
Nobuyoshi Nakada 2019-09-20 22:03:37 +09:00
parent 9e171b1fa0
commit e81a3e6df5
No known key found for this signature in database
GPG Key ID: 4BC7D6DF58D8DF60
5 changed files with 18 additions and 5 deletions

View File

@ -4599,7 +4599,16 @@ compile_cpath(LINK_ANCHOR *const ret, rb_iseq_t *iseq, const NODE *cpath)
}
}
#define private_recv_p(node) (nd_type((node)->nd_recv) == NODE_SELF)
static inline int
private_recv_p(const NODE *node)
{
if (nd_type(node->nd_recv) == NODE_SELF) {
NODE *self = node->nd_recv;
return self->nd_state != 0;
}
return 0;
}
static void
defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
const NODE *const node, LABEL **lfinish, VALUE needstr);

7
eval.c
View File

@ -189,13 +189,15 @@ rb_ec_cleanup(rb_execution_context_t *ec, volatile int ex)
volatile VALUE errs[2] = { Qundef, Qundef };
int nerr;
rb_thread_t *th = rb_ec_thread_ptr(ec);
rb_thread_t *volatile const th0 = th;
volatile int sysex = EXIT_SUCCESS;
volatile int step = 0;
rb_threadptr_interrupt(th);
rb_threadptr_check_signal(th);
EC_PUSH_TAG(ec);
if ((state = EC_EXEC_TAG()) == TAG_NONE) {
th = th0;
if ((state = EC_EXEC_TAG(), th = th0, state) == TAG_NONE) {
SAVE_ROOT_JMPBUF(th, { RUBY_VM_CHECK_INTS(ec); });
step_0: step++;
@ -548,10 +550,9 @@ static void
setup_exception(rb_execution_context_t *ec, int tag, volatile VALUE mesg, VALUE cause)
{
VALUE e;
const char *file = 0;
int line;
const char *const file = rb_source_location_cstr(&line);
file = rb_source_location_cstr(&line);
if ((file && !NIL_P(mesg)) || (cause != Qundef)) {
volatile int state = 0;

2
node.h
View File

@ -369,7 +369,7 @@ typedef struct RNode {
#define NEW_COLON3(i,loc) NEW_NODE(NODE_COLON3,0,i,0,loc)
#define NEW_DOT2(b,e,loc) NEW_NODE(NODE_DOT2,b,e,0,loc)
#define NEW_DOT3(b,e,loc) NEW_NODE(NODE_DOT3,b,e,0,loc)
#define NEW_SELF(loc) NEW_NODE(NODE_SELF,0,0,0,loc)
#define NEW_SELF(loc) NEW_NODE(NODE_SELF,0,0,1,loc)
#define NEW_NIL(loc) NEW_NODE(NODE_NIL,0,0,0,loc)
#define NEW_TRUE(loc) NEW_NODE(NODE_TRUE,0,0,0,loc)
#define NEW_FALSE(loc) NEW_NODE(NODE_FALSE,0,0,0,loc)

View File

@ -2595,6 +2595,7 @@ primary : literal
| tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
{
/*%%%*/
if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
$$ = $2;
/*% %*/
/*% ripper: paren!($2) %*/
@ -2602,6 +2603,7 @@ primary : literal
| tLPAREN compstmt ')'
{
/*%%%*/
if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
$$ = $2;
/*% %*/
/*% ripper: paren!($2) %*/

View File

@ -674,6 +674,7 @@ class TestMethod < Test::Unit::TestCase
assert_nothing_raised { self.mv1 }
assert_nothing_raised { self.mv2 }
assert_raise(NoMethodError) { (self).mv2 }
assert_nothing_raised { self.mv3 }
v = Visibility.new