Skip to content

Commit

Permalink
Make error message on 'my int $a is default(42)' etc. more friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Aug 27, 2013
1 parent 17b17a4 commit 3bdc0c3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
11 changes: 11 additions & 0 deletions src/core/Exception.pm
Expand Up @@ -462,6 +462,17 @@ my class X::Trait::Unknown is Exception {
}
my class X::Comp::Trait::Unknown is X::Trait::Unknown does X::Comp { };

my class X::Trait::NotOnNative is Exception {
has $.type; # is, will, of etc.
has $.subtype; # wrong subtype being tried
has $.native; # type of native (optional)
method message () {
"Can't use trait '$.type $.subtype' on a native"
~ ( $.native ?? " $.native." !! "." );
}
}
my class X::Comp::Trait::NotOnNative is X::Trait::NotOnNative does X::Comp { };

my class X::OutOfRange is Exception {
has $.what = 'Argument';
has $.got = '<unknown>';
Expand Down
48 changes: 32 additions & 16 deletions src/core/Variable.pm
@@ -1,6 +1,7 @@
# for our tantrums
my class X::Comp::NYI { ... };
my class X::Comp::Trait::Unknown { ... };
my class X::Comp::Trait::NotOnNative { ... };

# Variable traits come here, not in traits.pm, since we declare Variable
# in the setting rather than BOOTSTRAP.
Expand All @@ -13,7 +14,7 @@ my class Variable {
has $.slash;

# make throwing easier
submethod throw ( |c ) {
submethod throw ( |c ) is hidden_from_backtrace {
$*W.throw( self.slash, |c );
}
}
Expand All @@ -35,23 +36,33 @@ multi trait_mod:<is>(Variable:D $v, Mu:U $is ) {
multi trait_mod:<is>(Variable:D $v, :$default!) {
my $var := $v.var;
my $what := $var.VAR.WHAT;
nqp::getattr(
$var,
$what.HOW.mixin_base($what),
'$!descriptor',
).set_default(nqp::decont($default));
try { nqp::getattr(
$var,
$what.HOW.mixin_base($what),
'$!descriptor',
).set_default(nqp::decont($default));
CATCH {
$v.throw( 'X::Comp::Trait::NotOnNative',
:type<is>, :subtype<default> ); # can't find out native type yet
}
}

# make sure we start with the default if a scalar
$var = $default if nqp::istype($what, Scalar);
}
multi trait_mod:<is>(Variable:D $v, :$dynamic!) {
my $var := $v.var;
my $what := $var.VAR.WHAT;
nqp::getattr(
$var,
$what.HOW.mixin_base($what),
'$!descriptor',
).set_dynamic($dynamic);
try { nqp::getattr(
$var,
$what.HOW.mixin_base($what),
'$!descriptor',
).set_dynamic($dynamic);
CATCH {
$v.throw( 'X::Comp::Trait::NotOnNative',
:type<is>, :subtype<dynamic> ); # can't find out native type yet
}
}
}

# "of" traits
Expand All @@ -67,11 +78,16 @@ multi trait_mod:<of>(Variable:D $v, Mu:U $of ) {
my $var := $v.var;
my $what := $var.VAR.WHAT;
my $how := $what.HOW;
nqp::getattr(
$var,
$how.mixin_base($what),
'$!descriptor'
).set_of(nqp::decont($of));
try { nqp::getattr(
$var,
$how.mixin_base($what),
'$!descriptor'
).set_of(nqp::decont($of));
CATCH {
$v.throw( 'X::Comp::Trait::NotOnNative',
:type<of>, :subtype($of.HOW.name($of)) ); # can't find out native type yet
}
}
# probably can go if we have a COMPOSE phaser for PARAMETERIZE
$how.set_name($what,"{$how.name($what)}[{$of.HOW.name($of)}]");
}
Expand Down

0 comments on commit 3bdc0c3

Please sign in to comment.