Skip to content

Commit 2866e64

Browse files
committed
Fatalization of calling import/unimport method with argument
This commit is mostly a manual reversion of commit f1cf82e. Return to using a perl_croak in universal.c; adjust regen/warnings.pl as needed; run 'make regen'; get t/op/universal.t passing. No documentation changes yet. Add test for undefined unimport method, then refactor repeated code into a subroutine. Perform a pattern match rather than a string equality test because testing for exact line numbers inside a test program is too fragile for maintenance purposes. 'mispelled' was misspelled in one location; correct. Suppress 'used only once' warning in one location. POD formatting improvements as suggested by Elvin Aslanov, with one other word change. Correct case of one character in error message so that it's the same in both universal.c and pod/perldiag.pod. This enables us to preserve status of universal.c in t/porting/diag.t. Per: Tony Cook review. For: GH #23623
1 parent 34bbffc commit 2866e64

File tree

7 files changed

+211
-227
lines changed

7 files changed

+211
-227
lines changed

lib/warnings.pm

Lines changed: 170 additions & 176 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pod/perldiag.pod

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,20 +1339,14 @@ a string overload and is also not a blessed CODE reference. In short the
13391339
C<require> function does not know what to do with the object.
13401340
See also L<perlfunc/require>.
13411341

1342-
=item Attempt to call undefined %s method with arguments ("%s"%s)
1343-
via package "%s" (Perhaps you forgot to load the package?)
1344-
1345-
(D deprecated::missing_import_called_with_args) You called the
1346-
C<import()> or C<unimport()> method of a class that has no import method
1347-
defined in its inheritance graph, and passed an argument to the method.
1348-
This is very often the sign of a misspelled package name in a use or
1349-
require statement that has silently succeded due to a case insensitive
1350-
file system.
1342+
=item Attempt to call undefined %s method with arguments via package
1343+
"%s" (Perhaps you forgot to load the package?)
13511344

1352-
Another common reason this may happen is when mistakenly attempting to
1353-
import or unimport a symbol from a class definition or package which
1354-
does not use C<Exporter> or otherwise define its own C<import> or
1355-
C<unimport> method.
1345+
(F) You called the C<import()> or C<unimport()> method of a class that has no
1346+
such method defined in its inheritance graph, and passed an argument to the
1347+
method. This is very often the sign of a misspelled package name in a C<use>
1348+
or C<require> statement that has silently succeded due to a case insensitive
1349+
file system.
13561350

13571351
=item Can't locate package %s for @%s::ISA
13581352

regen/warnings.pl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#
1717
# This script is normally invoked from regen.pl.
1818

19-
$VERSION = '1.75';
19+
$VERSION = '1.76';
2020

2121
BEGIN {
2222
require './regen/regen_lib.pl';
@@ -189,6 +189,7 @@ BEGIN
189189
# the experiments were successful (or abandoned),
190190
# so no warning bit is needed anymore
191191
my %NO_BIT_FOR = map { ( uc $_ => 1, $_ => 1 ) } qw(
192+
deprecated::missing_import_called_with_args
192193
deprecated::smartmatch
193194
experimental::lexical_subs
194195
experimental::postderef

t/op/universal.t

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,25 +196,28 @@ my $x = {}; bless $x, 'X';
196196
ok $x->isa('UNIVERSAL');
197197
ok $x->isa('UNIVERSAL');
198198

199-
200-
{
201-
my $err;
202-
$SIG{__WARN__}= sub { die $_[0] };
203-
eval { Some::Package->import("bar") };
204-
my $err = $@;
205-
$err=~s!t/op!op!;
206-
is $err, "Attempt to call undefined import method with arguments (\"bar\")"
207-
. " via package \"Some::Package\" (Perhaps you forgot to load"
208-
. " the package?) at op/universal.t line 203.\n";
209-
eval { Some::Package->unimport(1.234) };
210-
$err = $@;
199+
sub test_undefined_method {
200+
my $method = shift;
201+
my @message_components = (
202+
q|Attempt to call undefined|,
203+
q|method with arguments via package "Some::Package"|,
204+
q|(Perhaps you forgot to load the package?)|,
205+
);
206+
eval { Some::Package->$method("bar") };
207+
my $err= $@;
211208
$err=~s!t/op!op!;
212-
is $err, "Attempt to call undefined unimport method with arguments (\"1.234\")"
213-
. " via package \"Some::Package\" (Perhaps you forgot to load"
214-
. " the package?) at op/universal.t line 209.\n";
215-
209+
my $message = join ' ' => (
210+
$message_components[0],
211+
$method,
212+
@message_components[1,2],
213+
);
214+
my $pattern = qr/\Q$message\E/;
215+
like $err, $pattern, "Got expected pattern for undefined $method";
216216
}
217217

218+
test_undefined_method('import');
219+
test_undefined_method('unimport');
220+
218221
# This segfaulted in a blead.
219222
fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
220223

t/porting/diag.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ while (<$diagfh>) {
137137
&& !$entries{$cur_entry}{cattodo}) {
138138
my $data_line= $entries{$cur_entry}{todo_line};
139139
TODO: {
140+
no warnings 'once';
140141
local $::TODO = "Remove the TODO entry \"$cur_entry\" from DATA "
141142
. "at $0 line $data_line as it is already in $pod near line $.";
142143
ok($cur_entry);

universal.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,10 @@ XS(XS_UNIVERSAL_import_unimport)
451451
* depends on it has its own "no import" logic that produces better
452452
* warnings than this does. */
453453
if (strNE(class_pv,"_charnames"))
454-
ck_warner_d(packWARN(WARN_DEPRECATED__MISSING_IMPORT_CALLED_WITH_ARGS),
455-
"Attempt to call undefined %s method with arguments "
456-
"(%" SVf_QUOTEDPREFIX "%s) via package "
457-
"%" SVf_QUOTEDPREFIX " (Perhaps you forgot to load the package?)",
458-
ix ? "unimport" : "import",
459-
SVfARG(ST(1)),
460-
(items > 2 ? " ..." : ""),
461-
SVfARG(ST(0)));
454+
Perl_croak(aTHX_
455+
"Attempt to call undefined %s method with arguments via package "
456+
"%" SVf_QUOTEDPREFIX " (Perhaps you forgot to load the package?)",
457+
ix ? "unimport" : "import", SVfARG(ST(0)));
462458
}
463459
XSRETURN_EMPTY;
464460
}

warnings.h

Lines changed: 8 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)