newXS_len_flags() shouldn't change the line number on PL_curcop when warning.
authorNicholas Clark <nick@ccl4.org>
Mon, 13 Aug 2012 20:00:07 +0000 (22:00 +0200)
committerNicholas Clark <nick@ccl4.org>
Mon, 3 Sep 2012 10:55:23 +0000 (12:55 +0200)
This can actually generate incorrect line numbers in runtime warnings, when
XSUBs are redefined from calls made from BEGIN blocks, and the line number
from the opening brace of the begin block is mashed with the filename of the
current line. For compiletime warnings, PL_curcop == &PL_compiling, so the
line numbers will be correct whether taken from PL_compiling or PL_parser.

This code dates back to perl-5.000, when it was added to newXS(). It appears
to be a copy of code present in newSUB() since alpha 2.

op.c
t/lib/warnings/op

diff --git a/op.c b/op.c
index 48ebb2b..7305ab5 100644 (file)
--- a/op.c
+++ b/op.c
@@ -7432,14 +7432,10 @@ Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
                 /* Redundant check that allows us to avoid creating an SV
                    most of the time: */
                 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
-                    const line_t oldline = CopLINE(PL_curcop);
-                    if (PL_parser && PL_parser->copline != NOLINE)
-                        CopLINE_set(PL_curcop, PL_parser->copline);
                     report_redefined_cv(newSVpvn_flags(
                                          name,len,(flags&SVf_UTF8)|SVs_TEMP
                                         ),
                                         cv, const_svp);
-                    CopLINE_set(PL_curcop, oldline);
                 }
                 SvREFCNT_dec(cv);
                 cv = NULL;
index eabf8c9..69c3cd3 100644 (file)
@@ -1504,3 +1504,24 @@ sub ᚠርƊ () { 1 }
 EXPECT
 Constant subroutine main::ᚠርƊ redefined at - line 5.
 ########
+# OPTION regex
+sub DynaLoader::dl_error {};
+use warnings;
+# We're testing that the warnings report the same line number:
+eval <<'EOC' or die $@;
+{
+    DynaLoader::boot_DynaLoader("DynaLoader");
+}
+EOC
+eval <<'EOC' or die $@;
+BEGIN {
+    DynaLoader::boot_DynaLoader("DynaLoader");
+}
+1
+EOC
+EXPECT
+OPTION regex
+\ASubroutine DynaLoader::dl_error redefined at \(eval 1\) line 2\.
+(?s).*
+Subroutine DynaLoader::dl_error redefined at \(eval 2\) line 2\.
+########