ruby-changes:46385
From: nobu <ko1@a...>
Date: Fri, 28 Apr 2017 17:06:38 +0900 (JST)
Subject: [ruby-changes:46385] nobu:r58499 (trunk): parse.y: fix line in rescue
nobu 2017-04-28 17:06:33 +0900 (Fri, 28 Apr 2017) New Revision: 58499 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58499 Log: parse.y: fix line in rescue * parse.y (set_line_body, primary): fix line number of bodystmt as the beginning of the block. [ruby-core:79388] [Bug #13181] Modified files: trunk/parse.y trunk/test/ruby/test_parse.rb Index: test/ruby/test_parse.rb =================================================================== --- test/ruby/test_parse.rb (revision 58498) +++ test/ruby/test_parse.rb (revision 58499) @@ -965,6 +965,19 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L965 end; end + def test_method_location_in_rescue + bug = '[ruby-core:79388] [Bug #13181]' + obj, line = Object.new, __LINE__+1 + def obj.location + # + raise + rescue + caller_locations(1, 1)[0] + end + + assert_equal(line, obj.location.lineno, bug) + end + =begin def test_past_scope_variable assert_warning(/past scope/) {catch {|tag| eval("BEGIN{throw tag}; tap {a = 1}; a")}} Index: parse.y =================================================================== --- parse.y (revision 58498) +++ parse.y (revision 58499) @@ -327,6 +327,17 @@ parser_set_line(NODE *n, int l) https://github.com/ruby/ruby/blob/trunk/parse.y#L327 nd_set_line(n, l); } +static inline void +set_line_body(NODE *body, int line) +{ + if (!body) return; + switch (nd_type(body)) { + case NODE_RESCUE: + case NODE_ENSURE: + nd_set_line(body, line); + } +} + #ifndef RIPPER #define yyparse ruby_yyparse @@ -2463,9 +2474,7 @@ primary : literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2474 $$ = NEW_NIL(); } else { - if (nd_type($3) == NODE_RESCUE || - nd_type($3) == NODE_ENSURE) - nd_set_line($3, $<num>2); + set_line_body($3, $<num>2); $$ = NEW_BEGIN($3); } nd_set_line($$, $<num>2); @@ -2742,6 +2751,7 @@ primary : literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2751 { /*%%%*/ $$ = NEW_CLASS($2, $5, $3); + set_line_body($5, $<num>4); nd_set_line($$, $<num>4); /*% $$ = dispatch3(class, $2, $3, $5); @@ -2761,6 +2771,7 @@ primary : literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2771 { /*%%%*/ $$ = NEW_SCLASS($3, $6); + set_line_body($6, nd_line($3)); fixpos($$, $3); /*% $$ = dispatch2(sclass, $3, $6); @@ -2784,6 +2795,7 @@ primary : literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2795 { /*%%%*/ $$ = NEW_MODULE($2, $4); + set_line_body($4, $<num>3); nd_set_line($$, $<num>3); /*% $$ = dispatch2(module, $2, $4); @@ -2808,6 +2820,7 @@ primary : literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2820 NODE *body = remove_begin($6); reduce_nodes(&body); $$ = NEW_DEFN($2, $5, body, METHOD_VISI_PRIVATE); + set_line_body(body, $<num>1); nd_set_line($$, $<num>1); /*% $$ = dispatch3(def, $2, $5, $6); @@ -2833,6 +2846,7 @@ primary : literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2846 NODE *body = remove_begin($8); reduce_nodes(&body); $$ = NEW_DEFS($2, $5, $7, body); + set_line_body(body, $<num>1); nd_set_line($$, $<num>1); /*% $$ = dispatch5(defs, $2, $<val>3, $5, $7, $8); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/