ruby-changes:57843
From: Nobuyoshi <ko1@a...>
Date: Fri, 20 Sep 2019 22:10:49 +0900 (JST)
Subject: [ruby-changes:57843] e81a3e6df5 (master): Allows calling a private method only with bare `self`
https://git.ruby-lang.org/ruby.git/commit/?id=e81a3e6df5 From e81a3e6df54842b5a836dad7055a4295cf4155bc Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Fri, 20 Sep 2019 22:03:37 +0900 Subject: Allows calling a private method only with bare `self` diff --git a/compile.c b/compile.c index 368a698..51b7b9f 100644 --- a/compile.c +++ b/compile.c @@ -4599,7 +4599,16 @@ compile_cpath(LINK_ANCHOR *const ret, rb_iseq_t *iseq, const NODE *cpath) https://github.com/ruby/ruby/blob/trunk/compile.c#L4599 } } -#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); diff --git a/eval.c b/eval.c index 5d402e5..c548c34 100644 --- a/eval.c +++ b/eval.c @@ -189,13 +189,15 @@ rb_ec_cleanup(rb_execution_context_t *ec, volatile int ex) https://github.com/ruby/ruby/blob/trunk/eval.c#L189 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 https://github.com/ruby/ruby/blob/trunk/eval.c#L550 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; diff --git a/node.h b/node.h index a493dfa..276b4d4 100644 --- a/node.h +++ b/node.h @@ -369,7 +369,7 @@ typedef struct RNode { https://github.com/ruby/ruby/blob/trunk/node.h#L369 #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) diff --git a/parse.y b/parse.y index 03db19f..a51328c 100644 --- a/parse.y +++ b/parse.y @@ -2595,6 +2595,7 @@ primary : literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2595 | 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 https://github.com/ruby/ruby/blob/trunk/parse.y#L2603 | tLPAREN compstmt ')' { /*%%%*/ + if (nd_type($2) == NODE_SELF) $2->nd_state = 0; $$ = $2; /*% %*/ /*% ripper: paren!($2) %*/ diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index a8064c4..a141202 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -674,6 +674,7 @@ class TestMethod < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_method.rb#L674 assert_nothing_raised { self.mv1 } assert_nothing_raised { self.mv2 } + assert_raise(NoMethodError) { (self).mv2 } assert_nothing_raised { self.mv3 } v = Visibility.new -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/