Ensure that we're not blowing away an inherited constructor
Shawn M Moore [Thu, 5 Feb 2009 01:53:31 +0000 (01:53 +0000)]
lib/Mouse/Meta/Class.pm
t/040-existing-subclass.t [new file with mode: 0644]

index afe9294..cd23c63 100644 (file)
@@ -148,7 +148,14 @@ sub make_immutable {
     my %args = @_;
     my $name = $self->name;
     $self->{is_immutable}++;
-    $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
+
+    if ($self->name->can('new') != Mouse::Object->can('new')) {
+        warn "Not inlining a constructor for ".$self->name." since it is not inheriting the default Mouse::Object constructor\n";
+    }
+    else {
+        $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
+    }
+
     if ($args{inline_destructor}) {
         $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
     }
diff --git a/t/040-existing-subclass.t b/t/040-existing-subclass.t
new file mode 100644 (file)
index 0000000..fe63fad
--- /dev/null
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN {
+    eval "use Test::Output;";
+    plan skip_all => "Test::Output is required for this test" if $@;
+    plan tests => 1;
+}
+
+do {
+    package Parent;
+    sub new { bless {}, shift }
+
+    package Child;
+    BEGIN { our @ISA = 'Parent' }
+    use Mouse;
+};
+
+stderr_is(
+    sub { package Child; __PACKAGE__->meta->make_immutable },
+    "Not inlining a constructor for Child since it is not inheriting the default Mouse::Object constructor\n",
+    'Mouse warns when it would have blown away the inherited constructor',
+);
+