Skip to content

Commit

Permalink
Improve USAGE message for subset/where params
Browse files Browse the repository at this point in the history
Fixes #1346

- Display `where` constraints as `where { ... }` instead of
    `-> ;; $_? is raw { #`(Block|83868104) ... }`. Looks like we
    did this awhile back too, but the detection of custom wheres got
    broken when .gist of blocks got changed
- Display subset names instead of their nominal types in named params
  • Loading branch information
zoffixznet committed Dec 31, 2017
1 parent c91bcc2 commit e543c89
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/core/Main.pm
Expand Up @@ -105,6 +105,16 @@ my sub MAIN_HELPER($retval = 0) {

for $sub.signature.params -> $param {
my $argument;

my $constraints = ~unique $param.constraint_list.map: {
my \g = .gist;
g.contains('#`(Block|')
?? 'where { ... }'
!! g.substr: 1, *-1 # remove ( ) parens around name
}
$_ eq 'where { ... }' and $_ = "$param.type.^name() $_"
with $constraints;

if $param.named {
if $param.slurpy {
if $param.name { # ignore anon *%
Expand All @@ -116,7 +126,9 @@ my sub MAIN_HELPER($retval = 0) {
my @names = $param.named_names.reverse;
$argument = @names.map({($^n.chars == 1 ?? '-' !! '--') ~ $^n}).join('|');
if $param.type !=== Bool {
$argument ~= "=<{$param.type.^name}>";
$argument ~= "=<{
$constraints || $param.type.^name
}>";
if Metamodel::EnumHOW.ACCEPTS($param.type.HOW) {
my $options = $param.type.^enum_values.keys.sort.Str;
$argument ~= $options.chars > 50
Expand All @@ -133,11 +145,11 @@ my sub MAIN_HELPER($retval = 0) {
}
}
else {
my $constraints = $param.constraint_list.map(*.gist).join(' ');
my $simple-const = $constraints && $constraints !~~ /^_block/;
$argument = $param.name ?? "<$param.usage-name()>" !!
$simple-const ?? $constraints !!
'<' ~ $param.type.^name ~ '>' ;
$argument = "<{
$param.name
?? $param.usage-name
!! $constraints || $param.type.^name
}>";

$argument = "[$argument ...]" if $param.slurpy;
$argument = "[$argument]" if $param.optional;
Expand Down
25 changes: 24 additions & 1 deletion t/05-messages/02-errors.t
Expand Up @@ -2,7 +2,7 @@ use lib <t/packages/>;
use Test;
use Test::Helpers;

plan 21;
plan 22;

# RT #132295

Expand Down Expand Up @@ -146,4 +146,27 @@ throws-like { sprintf "%d" }, X::Str::Sprintf::Directives::Count,
'error message when :deleting from natively typed array';
}

# https://github.com/rakudo/rakudo/issues/1346
subtest 'USAGE with subsets/where' => {
sub uhas (\sig, Mu \c, \desc) {
is-run sub MAIN ( ~ sig ~ ) {},
:err{.contains: c}, :out(*), :exitcode(*), desc
}

subtest 'named params' => {
uhas UInt :$x!, '<UInt>', 'mentions subset name';
uhas Int :$x! where 42, '<Int where { ... }>',
'Type + where clauses shown sanely';
uhas UInt :$x! where 42, '<UInt where { ... }>',
'subset + where clauses shown sanely';
}
subtest 'anon positional params' => {
uhas UInt $, '<UInt>', 'mentions subset name';
uhas Int $ where 42, '<Int where { ... }>',
'where clauses shown sanely';
uhas UInt $ where 42, '<UInt where { ... }>',
'subset + where clauses shown sanely';
}
}

# vim: ft=perl6 expandtab sw=4

0 comments on commit e543c89

Please sign in to comment.