Skip to content

Commit 799fd3b

Browse files
author
Father Chrysostomos
committed
Restore autouse’s exemption from redef warnings
This also restores the subroutine redefinition warning for newly-cre- ated XSUBs outside the autouse package. See below. This commit added the exemption to fix a known bug, that loading a module and importing from it would cause a redefinition warning if there were an autouse stub: perl-5.004_03-1092-g2f34f9d commit 2f34f9d Author: Ilya Zakharevich <[email protected]> Date: Mon Mar 2 16:36:02 1998 -0500 Make autouse -w-safe p4raw-id: //depot/perl@781 The subroutine redefinition warning occurs in three places. This commit removed the autouse exemption from two of them. I can’t see how it wasn’t a mistake, as <5104D4DBC598D211B5FE0000F8FE7EB202D49EE9@mbtlipnt02.btlabs.bt.co.uk> (the apparent source of the patch, makes no mention of it: perl-5.005_02-2920-ge476b1b commit e476b1b Author: Gurusamy Sarathy <[email protected]> Date: Sun Feb 20 22:58:09 2000 +0000 lexical warnings update, ability to inspect bitmask in calling scope, among other things (from Paul Marquess) p4raw-id: //depot/perl@5170 This commit refactored things to remove some compiler warnings, but in doing so reversed the logic of the condition, causing redefini- tion warnings for newly-created XSUBs to apply only to subs from the autouse package: perl-5.8.0-5131-g66a1b24 commit 66a1b24 Author: Andy Lester <[email protected]> Date: Mon Jun 6 05:11:07 2005 -0500 Random cleanups #47 Message-ID: <[email protected]> p4raw-id: //depot/perl@24735 I’ve basically reinstated the changes in 2f34f9d, but with tests this time. It may not make sense for autouse to be exempt for newATTRSUB and newXS, but keeping the logic surrounding the warning as close as possible to being the same could allow future refactorings to merge them.
1 parent 53e2b49 commit 799fd3b

File tree

6 files changed

+75
-13
lines changed

6 files changed

+75
-13
lines changed

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,6 +2983,7 @@ dist/Attribute-Handlers/t/linerep.t See if Attribute::Handlers works
29832983
dist/Attribute-Handlers/t/multi.t See if Attribute::Handlers works
29842984
dist/autouse/lib/autouse.pm Load and call a function only when it's used
29852985
dist/autouse/t/autouse.t See if autouse works
2986+
dist/autouse/t/lib/MyTestModule2.pm Test module for autouse
29862987
dist/autouse/t/lib/MyTestModule.pm Test module for autouse
29872988
dist/base/Changes base.pm changelog
29882989
dist/base/lib/base.pm Establish IS-A relationship at compile time

dist/autouse/t/autouse.t

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ BEGIN {
88
}
99
}
1010

11-
use Test::More tests => 12;
11+
use Test::More tests => 15;
1212

1313
BEGIN {
1414
require autouse;
@@ -69,3 +69,34 @@ autouse->import("MyTestModule" => 'test_function');
6969
my $ret = test_function();
7070
is( $ret, 'works' );
7171

72+
# Test that autouse is exempt from all methods of triggering the subroutine
73+
# redefinition warning.
74+
SKIP: {
75+
skip "Fails in 5.15.5 and below (perl bug)", 2 if $] < 5.0150051;
76+
use warnings; local $^W = 1;
77+
my $w;
78+
local $SIG{__WARN__} = sub { $w .= shift };
79+
use autouse MyTestModule2 => 'test_function2';
80+
*MyTestModule2::test_function2 = \&test_function2;
81+
require MyTestModule2;
82+
is $w, undef,
83+
'no redefinition warning when clobbering autouse stub with new sub';
84+
undef $w;
85+
import MyTestModule2 'test_function2';
86+
is $w, undef,
87+
'no redefinition warning when clobbering autouse stub via *a=\&b';
88+
}
89+
SKIP: {
90+
skip "Fails from 5.10 to 5.15.5 (perl bug)", 1
91+
if $] < 5.0150051 and $] > 5.0099;
92+
use Config;
93+
skip "no B", 1 unless $Config{extensions} =~ /\bB\b/;
94+
use warnings; local $^W = 1;
95+
my $w;
96+
local $SIG{__WARN__} = sub { $w .= shift };
97+
use autouse B => "sv_undef";
98+
*B::sv_undef = \&sv_undef;
99+
require B;
100+
is $w, undef,
101+
'no redefinition warning when clobbering autouse stub with new XSUB';
102+
}

dist/autouse/t/lib/MyTestModule2.pm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package MyTestModule2;
2+
use warnings;
3+
4+
@ISA = Exporter;
5+
require Exporter;
6+
@EXPORT_OK = 'test_function2';
7+
8+
sub test_function2 {
9+
return 'works';
10+
}
11+
12+
1;

op.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6583,7 +6583,14 @@ Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
65836583
&& block->op_type != OP_NULL
65846584
#endif
65856585
) {
6586-
if (ckWARN(WARN_REDEFINE)
6586+
const char *hvname;
6587+
if ( (ckWARN(WARN_REDEFINE)
6588+
&& !(
6589+
CvGV(cv) && GvSTASH(CvGV(cv))
6590+
&& HvNAMELEN(GvSTASH(CvGV(cv))) == 7
6591+
&& (hvname = HvNAME(GvSTASH(CvGV(cv))),
6592+
strEQ(hvname, "autouse"))
6593+
))
65876594
|| (CvCONST(cv)
65886595
&& (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
65896596
{
@@ -7005,25 +7012,28 @@ Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
70057012
else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
70067013
/* already defined (or promised) */
70077014
if (ckWARN(WARN_REDEFINE)) {
7015+
const line_t oldline = CopLINE(PL_curcop);
70087016
GV * const gvcv = CvGV(cv);
70097017
if (gvcv) {
70107018
HV * const stash = GvSTASH(gvcv);
70117019
if (stash) {
70127020
const char *redefined_name = HvNAME_get(stash);
70137021
if ( redefined_name &&
70147022
strEQ(redefined_name,"autouse") ) {
7015-
const line_t oldline = CopLINE(PL_curcop);
7016-
if (PL_parser && PL_parser->copline != NOLINE)
7017-
CopLINE_set(PL_curcop, PL_parser->copline);
7018-
Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
7019-
CvCONST(cv) ? "Constant subroutine %s redefined"
7020-
: "Subroutine %s redefined"
7021-
,name);
7022-
CopLINE_set(PL_curcop, oldline);
7023+
goto nope;
70237024
}
70247025
}
70257026
}
7027+
if (PL_parser && PL_parser->copline != NOLINE)
7028+
CopLINE_set(PL_curcop, PL_parser->copline);
7029+
Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
7030+
CvCONST(cv)
7031+
? "Constant subroutine %s redefined"
7032+
: "Subroutine %s redefined"
7033+
,name);
7034+
CopLINE_set(PL_curcop, oldline);
70267035
}
7036+
nope:
70277037
SvREFCNT_dec(cv);
70287038
cv = NULL;
70297039
}

sv.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3810,6 +3810,7 @@ S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr)
38103810
if (!GvCVGEN((const GV *)dstr) &&
38113811
(CvROOT(cv) || CvXSUB(cv)))
38123812
{
3813+
const char *hvname;
38133814
/* Redefining a sub - warning is mandatory if
38143815
it was a const and its value changed. */
38153816
if (CvCONST(cv) && CvCONST((const CV *)sref)
@@ -3823,7 +3824,14 @@ S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr)
38233824
when a constant is exported twice. Don't warn.
38243825
*/
38253826
}
3826-
else if (ckWARN(WARN_REDEFINE)
3827+
else if ((ckWARN(WARN_REDEFINE)
3828+
&& !(
3829+
CvGV(cv) && GvSTASH(CvGV(cv)) &&
3830+
HvNAMELEN(GvSTASH(CvGV(cv))) == 7 &&
3831+
(hvname = HvNAME(GvSTASH(CvGV(cv))),
3832+
strEQ(hvname, "autouse"))
3833+
)
3834+
)
38273835
|| (CvCONST(cv)
38283836
&& (!CvCONST((const CV *)sref)
38293837
|| sv_cmp(cv_const_sv(cv),

t/op/stash.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ fresh_perl_is(
2828
# Used to segfault, too
2929
SKIP: {
3030
skip_if_miniperl('requires XS');
31-
fresh_perl_is(
31+
fresh_perl_like(
3232
'sub foo::bar{}; $mro::{get_mro}=*foo::bar; undef %foo::; require mro',
33-
'',
33+
qr/^Subroutine mro::get_mro redefined at /,
3434
{ switches => [ '-w' ] },
3535
q(Defining an XSUB over an existing sub with no stash under warnings),
3636
);

0 commit comments

Comments
 (0)