From 85d946090007b668a07ed0ca95c3d290db6a007d Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Sun, 29 Jan 2017 22:18:55 -0600 Subject: [PATCH] Fix warning when attribute for delegation does not have definition context --- Changes | 5 +++++ dist.ini | 1 + lib/Moose/Meta/Method/Delegation.pm | 12 ++++++++--- t/bugs/moo_delegation.t | 31 +++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 t/bugs/moo_delegation.t diff --git a/Changes b/Changes index 3393e9d6f..d21aadcdc 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,11 @@ for, noteworthy changes. {{$NEXT}} + [BUG FIXES] + + - Creating a Moose subclass of a Moo class with an attribute with a + delegation would cause a warning. + 2.2001 2017-01-29 [TESTS] diff --git a/dist.ini b/dist.ini index 9e6d27b00..383ee9f30 100644 --- a/dist.ini +++ b/dist.ini @@ -358,6 +358,7 @@ IO::String = 0 Locale::US = 0 Module::CPANTS::Analyse = 0.92 Module::Refresh = 0 +Moo = 0 MooseX::MarkAsMethods = 0 MooseX::NonMoose = 0 PadWalker = 0 diff --git a/lib/Moose/Meta/Method/Delegation.pm b/lib/Moose/Meta/Method/Delegation.pm index 11cd81069..b7f6b73de 100644 --- a/lib/Moose/Meta/Method/Delegation.pm +++ b/lib/Moose/Meta/Method/Delegation.pm @@ -130,13 +130,19 @@ sub { } EOF - my $definition = $attr->definition_context; my $description = 'inline delegation in ' . $self->package_name . ' for ' . $attr->name . '->' - . $delegate - . " (attribute declared in $definition->{file} at line $definition->{line})"; + . $delegate; + + my $definition = $attr->definition_context; + # While all attributes created in the usual way (via Moose's has()) will + # define this, there's no guarantee that this must be defined. For + # example, when Moo inflates a class to Moose it does not define these (as + # of Moo 2.003). + $description .= " (attribute declared in $definition->{file} at line $definition->{line})" + if defined $definition->{file} && defined $definition->{line}; return try { $self->_compile_code( diff --git a/t/bugs/moo_delegation.t b/t/bugs/moo_delegation.t new file mode 100644 index 000000000..4630f1cac --- /dev/null +++ b/t/bugs/moo_delegation.t @@ -0,0 +1,31 @@ +use strict; +use warnings; +use Test::More; + +use Test::Requires 'Moo', 'Test::Warnings'; +use Test::Warnings qw( warnings :no_end_test ); + +{ + package Foo; + + use Moo; + + has foo => ( + is => 'ro', + handles => ['bar'], + ); +} + +{ + package Bar; + + use Moose; + + ::is_deeply( + [ ::warnings { extends 'Foo' } ], + [], + 'no warnings when extending Moo class that has a delegation' + ); +} + +done_testing();