Moose 0.34 broke the ability to make a Meta class immutable. This is a
Dave Rolsky [Tue, 22 Jan 2008 00:18:39 +0000 (00:18 +0000)]
fix, though I'm not convinced it's the _right_ fix. An alternative
might be to ensure that attributes for Moose meta classes are always
Moose::Meta::Attribute objects instead of Class::MOP::Attribute
objects. Regardless, the test I added should be useful in exploring
alternative fixes.

MANIFEST
lib/Moose/Meta/Method/Constructor.pm
t/300_immutable/003_immutable_meta_class.t [new file with mode: 0644]

index b62baa0..8d0618a 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -166,6 +166,7 @@ t/200_examples/006_example_Protomoose.t
 t/200_examples/007_Child_Parent_attr_inherit.t
 t/300_immutable/001_immutable_moose.t
 t/300_immutable/002_apply_roles_to_immutable.t
+t/300_immutable/003_immutable_meta_class.t
 t/400_moose_util/001_moose_util.t
 t/400_moose_util/002_moose_util_does_role.t
 t/400_moose_util/003_moose_util_search_class_by_role.t
index 55bd4fe..29c6f2f 100644 (file)
@@ -87,7 +87,13 @@ sub intialize_body {
         # to be picked up in the eval
         my $attrs = $self->attributes;
 
-        my @type_constraints = map { $_->type_constraint } @$attrs;
+        # We need to check if the attribute ->can('type_constraint')
+        # since we may be trying to immutabilize a Moose meta class,
+        # which in turn has attributes which are Class::MOP::Attribute
+        # objects, rather than
+        # Moose::Meta::Attribute. Class::MOP::Attribute attributes
+        # have no type constraints.
+        my @type_constraints = map { $_->type_constraint } grep { $_->can('type_constraint') } @$attrs;
         my @type_constraint_bodies = map {
             $_ && $_->_compiled_type_constraint;
         } @type_constraints;
diff --git a/t/300_immutable/003_immutable_meta_class.t b/t/300_immutable/003_immutable_meta_class.t
new file mode 100644 (file)
index 0000000..445702b
--- /dev/null
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 2;
+use Test::Exception;
+
+BEGIN {
+    use_ok('Moose');
+}
+
+{
+    package My::Meta;
+
+    use Moose;
+
+    extends 'Moose::Meta::Class';
+
+    has 'meta_size' =>
+        ( is  => 'rw',
+          isa => 'Int',
+        );
+}
+
+lives_ok { My::Meta->meta()->make_immutable() } 'can make a meta class immutable';
+