Also see Moose::Manual::Delta for more details of, and workarounds
for, noteworthy changes.
+ [API CHANGES]
+
+ * You cannot pass "coerce => 1" for an attribute unless its type constraint
+ has a coercion defined. If it doesn't, an error will be thrown when the
+ attribute is defined. (Dave Rolsky)
+
[NEW FEATURES]
* We no longer unimport strict and warnings when Moose, Moose::Role, or
=over 4
+=item You cannot pass C<< coerce => 1 >> unless the attribute's type constraint has a coercion
+
+Previously, this was accepted, and it sort of worked, except that if you
+attempted to set the attribute after the object was created, you would get a
+runtime error.
+
+Now you will get an error when you attempt to define the attribute.
+
=item C<no Moose>, C<no Moose::Role>, and C<no Moose::Exporter> no longer unimport strict and warnings
This change was made in 1.05, and has now been reverted. We don't know if the
|| $class->throw_error("You cannot have coercion without specifying a type constraint on attribute ($name)", data => $options);
$class->throw_error("You cannot have a weak reference to a coerced value on attribute ($name)", data => $options)
if $options->{weak_ref};
+
+ $options->{type_constraint}->has_coercion
+ || $class->throw_error("You cannot coerce an attribute ($name) unless its type has a coercion", data => $options);
}
if (exists $options->{trigger}) {
return $val unless $self->has_type_constraint;
- my $type_constraint = $self->type_constraint;
- if ($self->should_coerce && $type_constraint->has_coercion) {
- $val = $type_constraint->coerce($val);
- }
+ $val = $self->type_constraint->coerce($val)
+ if $self->should_coerce;
$self->verify_against_type_constraint($val, instance => $instance);
push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
if ($is_moose && $attr->has_type_constraint) {
- if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
+ if ($attr->should_coerce) {
push @source => $self->_generate_type_coercion(
$attr,
'$type_constraints[' . $index . ']',
return unless $attr->has_type_constraint;
my @source;
- if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
+ if ($attr->should_coerce) {
push @source => $self->_generate_type_coercion(
$attr,
'$type_constraints[' . $index . ']',
--- /dev/null
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Exception;
+
+{
+ package Foo;
+
+ use Moose;
+
+ ::throws_ok{ has foo => (
+ is => 'ro',
+ isa => 'Str',
+ coerce => 1,
+ );
+ } qr/\QYou cannot coerce an attribute (foo) unless its type has a coercion/,
+ 'Cannot coerce unless the type has a coercion';
+}
+
+done_testing;