[perl #114658] Fix line numbers at the end of string eval
authorFather Chrysostomos <sprout@cpan.org>
Wed, 10 Oct 2012 20:14:31 +0000 (13:14 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Wed, 10 Oct 2012 20:15:14 +0000 (13:15 -0700)
$ perl -e 'eval "{;"; print $@'
Missing right curly or square bracket at (eval 1) line 1, at end of line
syntax error at (eval 1) line 1, at EOF
$ perl -e 'eval "{"; print $@'
Missing right curly or square bracket at (eval 1) line 2, at end of line
syntax error at (eval 1) line 2, at EOF

Notice how the line number goes up when there is no semicolon.

What happens is that eval tacks "\n;" on to the end of the string if
it does not already end with a semicolon.

I actually changed this in blead in commit 11076590 to tack "\n;"
on to the end all the time, to make eval "q;;" and
eval "return #comment;" work.

This caused the line number to increase for eval "{;".

This commit fixes both examples above by modifying S_incline to
account for the "\n;" at the end of a string eval.

Existing tests had to be modified, as they were testing for the wrong
line number.

t/lib/warnings/pad
t/op/eval.t
toke.c

index b226239..03c4ddb 100644 (file)
@@ -173,7 +173,7 @@ sub {
 }->();
 f();
 EXPECT
-Variable "$x" is not available at (eval 1) line 2.
+Variable "$x" is not available at (eval 1) line 1.
 ########
 # pad.c
 use warnings 'closure' ;
@@ -195,7 +195,7 @@ sub f {
 }
 f()->();
 EXPECT
-Variable "$x" is not available at (eval 1) line 2.
+Variable "$x" is not available at (eval 1) line 1.
 ########
 use warnings 'closure' ;
 {
@@ -205,7 +205,7 @@ use warnings 'closure' ;
 }
 f2();
 EXPECT
-Variable "$x" is not available at (eval 1) line 2.
+Variable "$x" is not available at (eval 1) line 1.
 ########
 use warnings 'closure' ;
 for my $x (1,2,3) {
@@ -214,7 +214,7 @@ for my $x (1,2,3) {
 }
 f();
 EXPECT
-Variable "$x" is not available at (eval 4) line 2.
+Variable "$x" is not available at (eval 4) line 1.
 ########
 # pad.c
 no warnings 'closure' ;
@@ -433,7 +433,7 @@ sub {
 }->();
 f();
 EXPECT
-Variable "$に" is not available at (eval 1) line 2.
+Variable "$に" is not available at (eval 1) line 1.
 ########
 # pad.c
 # see bugid 1754
@@ -446,7 +446,7 @@ sub f {
 }
 f()->();
 EXPECT
-Variable "$に" is not available at (eval 1) line 2.
+Variable "$に" is not available at (eval 1) line 1.
 ########
 use warnings 'closure' ;
 BEGIN { binmode STDERR, 'utf8'; }
@@ -458,7 +458,7 @@ BEGIN { binmode STDERR, 'utf8'; }
 }
 f2();
 EXPECT
-Variable "$に" is not available at (eval 1) line 2.
+Variable "$に" is not available at (eval 1) line 1.
 ########
 use warnings 'closure' ;
 BEGIN { binmode STDERR, 'utf8'; }
@@ -469,7 +469,7 @@ for my $に (1,2,3) {
 }
 f();
 EXPECT
-Variable "$に" is not available at (eval 4) line 2.
+Variable "$に" is not available at (eval 4) line 1.
 ########
 # pad.c
 use warnings 'closure' ;
@@ -534,7 +534,7 @@ sub {
 }->();
 f();
 EXPECT
-Variable "$è" is not available at (eval 1) line 2.
+Variable "$è" is not available at (eval 1) line 1.
 ########
 # pad.c
 # see bugid 1754
@@ -547,7 +547,7 @@ sub f {
 }
 f()->();
 EXPECT
-Variable "$è" is not available at (eval 1) line 2.
+Variable "$è" is not available at (eval 1) line 1.
 ########
 use warnings 'closure' ;
 BEGIN { binmode STDERR, 'utf8'; }
@@ -559,7 +559,7 @@ BEGIN { binmode STDERR, 'utf8'; }
 }
 f2();
 EXPECT
-Variable "$è" is not available at (eval 1) line 2.
+Variable "$è" is not available at (eval 1) line 1.
 ########
 use warnings 'closure' ;
 BEGIN { binmode STDERR, 'utf8'; }
@@ -570,5 +570,5 @@ for my $è (1,2,3) {
 }
 f();
 EXPECT
-Variable "$è" is not available at (eval 4) line 2.
+Variable "$è" is not available at (eval 4) line 1.
 ########
index 9866ca7..49f7494 100644 (file)
@@ -6,7 +6,7 @@ BEGIN {
     require './test.pl';
 }
 
-plan(tests => 126);
+plan(tests => 128);
 
 eval 'pass();';
 
@@ -609,3 +609,12 @@ pass("phew! dodged the assertion after a parsing (not lexing) error");
      qr/Unbalanced string table/,
     'Errors in finalize_optree do not leak string eval op tree';
 }
+
+# [perl #114658] Line numbers at end of string eval
+for("{;", "{") {
+    eval $_; is $@ =~ s/eval \d+/eval 1/rag, <<'EOE',
+Missing right curly or square bracket at (eval 1) line 1, at end of line
+syntax error at (eval 1) line 1, at EOF
+EOE
+       qq'Right line number for eval "$_"';
+}
diff --git a/toke.c b/toke.c
index e9a06eb..1079e94 100644 (file)
--- a/toke.c
+++ b/toke.c
@@ -1559,6 +1559,12 @@ S_incline(pTHX_ const char *s)
     PERL_ARGS_ASSERT_INCLINE;
 
     COPLINE_INC_WITH_HERELINES;
+    if (!PL_rsfp && !PL_parser->filtered && PL_lex_state == LEX_NORMAL
+     && s+1 == PL_bufend && *s == ';') {
+       /* fake newline in string eval */
+       CopLINE_dec(PL_curcop);
+       return;
+    }
     if (*s++ != '#')
        return;
     while (SPACE_OR_TAB(*s))