Skip to content

Commit

Permalink
Fix bug when a child class imported t() and the parent already had …
Browse files Browse the repository at this point in the history
…a `t()`

We were checking `$child->can('t')` to determine whether to install a `t()` in
the child class, but we need to check if the `$child` package itself has a
`t()` sub.

Patch from Kerin Millar.
  • Loading branch information
autarch committed Jun 11, 2022
1 parent 7fdd0bc commit c5272c4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{{$NEXT}}

- Importing types into a class which inherited from another class that had
imported types wouldn't work, leaving the child class with no `t()`
sub. Patch by Kerin Millar.


0.47 2021-01-29

- Change Specio constraint object's stringification overloading to return the
Expand Down
9 changes: 8 additions & 1 deletion lib/Specio/Helpers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ sub install_t_sub {
my $types = shift;

# XXX - check to see if their t() is something else entirely?
return if $caller->can('t');
{
## no critic (TestingAndDebugging::ProhibitNoStrict)
no strict 'refs';

# We used to check ->can('t') but that was wrong, since it would
# return if a parent class had a t() sub.
return if *{ $caller . '::t' }{CODE};
}

my $t = sub {
my $name = shift;
Expand Down
35 changes: 35 additions & 0 deletions t/inheritance.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## no critic (Modules::ProhibitMultiplePackages)
use strict;
use warnings;

use Test::More 0.96;

use Specio::Library::Builtins;

# This test is about a bug where a parent class with a t() sub causes the t()
# sub to not be added in a child class that uses a type-exporter.
{
package Parent;

use Specio::Library::Builtins;

sub type {
t('Int');
}
}

{
package Child;

use parent -norequire => 'Parent';

use Specio::Library::Builtins;

sub type {
t('Str');
}
}

is( Child::type(), t('Str'), 'Child class has a t() sub' );

done_testing();

0 comments on commit c5272c4

Please sign in to comment.