Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add X::Routine::Banned and ban "length" and "bytes"
  • Loading branch information
lizmat committed Mar 7, 2014
1 parent 44a4661 commit 24c94e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/core/Any.pm
Expand Up @@ -3,6 +3,7 @@ my class Pair { ... }
my class Range { ... }
my class X::Bind::Slice { ... }
my class X::Bind::ZenSlice { ... }
my class X::Routine::Banned { ... }

my class Any { # declared in BOOTSTRAP
# my class Any is Mu {
Expand Down Expand Up @@ -264,6 +265,18 @@ my class Any { # declared in BOOTSTRAP
method KeySet() { DEPRECATED("'SetHash'"); self.SetHash }
method KeyBag() { DEPRECATED("'BagHash'"); self.BagHash }

method length is hidden_from_backtrace {
fail X::Routine::Banned.new(
banned => ".length",
didyoumean => ".chars, .graphs or .codes",
);
}
method bytes is hidden_from_backtrace {
fail X::Routine::Banned.new(
banned => ".bytes",
didyoumean => ".encode(\$encoding).bytes",
);
}
}
Metamodel::ClassHOW.exclude_parent(Any);

Expand Down Expand Up @@ -387,6 +400,21 @@ proto sub item(|) is pure { * }
multi sub item(*@a) { my $ = @a }
multi sub item(Mu $a) { $a }

proto sub length(|) { * }
multi sub length (|) is hidden_from_backtrace {
fail X::Routine::Banned.new(
banned => "length()",
didyoumean => "chars(), graphs() or codes()",
)
}
proto sub bytes(|) { * }
multi sub bytes (|) is hidden_from_backtrace {
fail X::Routine::Banned.new(
banned => "bytes()",
didyoumean => ".encode(\$encoding).bytes",
)
}

my $default= []; # so that we can check missing parameters
sub RWPAIR(\k, \v) { # internal fast pair creation
my \p := nqp::create(Pair);
Expand Down
9 changes: 9 additions & 0 deletions src/core/Exception.pm
Expand Up @@ -1050,6 +1050,15 @@ my class X::Augment::NoSuchType does X::Comp {
method message() { "You tried to augment $.package-kind $.package, but it does not exist" }
}

my class X::Routine::Banned is Exception {
has $.banned = "something";
has $.didyoumean = "something else";
method message() {
qq{"$.banned" is banned in Perl 6.
Did you mean $.didyoumean instead?}
}
}

my class X::Routine::Unwrap is Exception {
method message() { "Cannot unwrap routine: invalid wrap handle" }
}
Expand Down

0 comments on commit 24c94e0

Please sign in to comment.