Skip to content

Commit 4276805

Browse files
committed
report prototype change warnings from correct location
1 parent 146cf39 commit 4276805

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

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: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,21 @@ use Class::Method::Modifiers;
3434
}
3535

3636
{
37-
sub bog ($) { scalar @_ }
38-
3937
my $around;
4038

39+
sub bog ($) { scalar @_ }
40+
my $wrap_bog = sub ($$) {
41+
my $orig = shift;
42+
$around = @_;
43+
$orig->(@_);
44+
};
45+
46+
my $warn_line;
4147
my @warn;
4248
{
4349
local $SIG{__WARN__} = sub { push @warn, @_ };
44-
around bog => sub ($$) {
45-
my $orig = shift;
46-
$around = @_;
47-
$orig->(@_);
48-
};
50+
$warn_line = __LINE__ + 1;
51+
around bog => $wrap_bog;
4952
}
5053

5154
is eval q{ bog( @{[5, 6]}, @{[10, 11]} ) }, 2,
@@ -55,6 +58,26 @@ use Class::Method::Modifiers;
5558
my $warn = join '', @warn;
5659
like $warn, qr/Prototype mismatch/,
5760
'changing prototype throws warning';
61+
like $warn, qr/\Q${\__FILE__}\E line $warn_line\b/,
62+
'warning is reported from correct location';
63+
}
64+
65+
{
66+
sub brog ($) { scalar @_ }
67+
my $wrap_brog = sub ($$) {
68+
my $orig = shift;
69+
$orig->(@_);
70+
};
71+
72+
my @warn;
73+
{
74+
local $SIG{__WARN__} = sub { push @warn, @_ };
75+
no warnings;
76+
around brog => $wrap_brog;
77+
}
78+
79+
is 0+@warn, 0,
80+
'warnings controllable via warning pragma';
5881
}
5982

6083
done_testing;

0 commit comments

Comments
 (0)