Skip to content

Commit 2e6e9ef

Browse files
committed
report prototype change warnings from correct location
1 parent 5ed8d18 commit 2e6e9ef

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

lib/Class/Method/Modifiers.pm

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ sub install_modifier {
7878
unshift @{ $cache->{$type} }, $code;
7979
}
8080

81+
require Carp;
82+
my $loc = Carp::short_error_loc();
83+
my ($file, $line, $warnmask) = (caller($loc))[1,2,9];
84+
8185
# wrap the method with another layer of around. much simpler than
8286
# the Moose equivalent. :)
8387
if ($type eq 'around') {
@@ -103,8 +107,13 @@ sub install_modifier {
103107

104108
my $sig = _sub_sig($cache->{wrapped});
105109

106-
my $generated = "package $into;\n";
107-
$generated .= "sub $name $sig {";
110+
my $generated
111+
= "BEGIN { \${^WARNING_BITS} = \$warnmask }\n"
112+
. "no warnings 'redefine';\n"
113+
. "no warnings 'closure';\n"
114+
. "package $into;\n"
115+
. "#line $line \"$file\"\n"
116+
. "sub $name $sig {";
108117

109118
# before is easy, it doesn't affect the return value(s)
110119
if (@$before) {
@@ -143,8 +152,6 @@ sub install_modifier {
143152
$generated .= '}';
144153

145154
no strict 'refs';
146-
no warnings 'redefine';
147-
no warnings 'closure';
148155
eval $generated;
149156
};
150157
}

t/141-prototype.t

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use strict;
22
use warnings;
33
use Test::More 0.88;
4-
use Test::Warnings ($ENV{AUTHOR_TESTING} ? () : ':no_end_test'), 'warning';
4+
use Test::Warnings ($ENV{AUTHOR_TESTING} ? () : ':no_end_test'), 'warnings';
55
use Test::Fatal;
66

77
use Class::Method::Modifiers;
@@ -39,7 +39,7 @@ use Class::Method::Modifiers;
3939
sub bog ($) { scalar @_ }
4040

4141
my $around;
42-
my $warn = warning {
42+
my ($warn) = warnings {
4343
around bog => sub ($$) {
4444
my $orig = shift;
4545
$around = @_;
@@ -53,6 +53,22 @@ use Class::Method::Modifiers;
5353
'around modifier applied';
5454
like $warn, qr/Prototype mismatch/,
5555
'changing prototype throws warning';
56+
like $warn, qr/\Q${\__FILE__}\E/,
57+
'warning is reported from correct location';
58+
}
59+
60+
{
61+
sub brog ($) { scalar @_ }
62+
no warnings;
63+
my @warn = warnings {
64+
around brog => sub ($$) {
65+
my $orig = shift;
66+
$orig->(@_);
67+
};
68+
};
69+
70+
is 0+@warn, 0,
71+
'warnings controllable via warning pragma';
5672
}
5773

5874
done_testing;

0 commit comments

Comments
 (0)