Skip to content

Commit

Permalink
More MAIN USAGE fixes
Browse files Browse the repository at this point in the history
- Fix string literal breakage caused by original fix[^1] to #1346
- Make quote post-processing only apply when we're dealing with
    a string literal, not with variable names (another reason why
    I think we shouldn't do it:
    https://irclog.perlgeek.de/perl6-dev/2017-12-31#i_15636320 )

[1] e543c890ad58d4c707f73403
  • Loading branch information
zoffixznet committed Dec 31, 2017
1 parent 1d772dc commit 27fbd7a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
46 changes: 32 additions & 14 deletions src/core/Main.pm
Expand Up @@ -106,11 +106,22 @@ my sub MAIN_HELPER($retval = 0) {
for $sub.signature.params -> $param {
my $argument;

my int $literals-as-constraint = 0;
my int $total-constraints = 0;
my $constraints = ~unique $param.constraint_list.map: {
my \g = .gist;
g.contains('#`(Block|')
?? 'where { ... }'
!! g.substr: 1, *-1 # remove ( ) parens around name
++$total-constraints;
nqp::if(
nqp::istype($_, Callable),
'where { ... }',
nqp::stmts(
(my \g = .gist),
nqp::if(
nqp::isconcrete($_),
nqp::stmts(
++$literals-as-constraint,
g), # we constrained by some literal; gist as is
nqp::substr(g, 1, nqp::chars(g)-2))))
# ^ remove ( ) parens around name in the gist
}
$_ eq 'where { ... }' and $_ = "$param.type.^name() $_"
with $constraints;
Expand Down Expand Up @@ -145,16 +156,23 @@ my sub MAIN_HELPER($retval = 0) {
}
}
else {
$argument = "<{
$param.name
?? $param.usage-name
!! $constraints || $param.type.^name
}>";

$argument = "[$argument ...]" if $param.slurpy;
$argument = "[$argument]" if $param.optional;
$argument .= trans(["'"] => [q|'"'"'|]) if $argument.contains("'");
$argument = "'$argument'" if $argument.contains(' ' | '"');
$argument = $param.name
?? "<$param.usage-name()>"
!! $constraints
?? ($literals-as-constraint == $total-constraints)
?? $constraints
!! "<{$constraints}>"
!! "<$param.type.^name()>";

$argument = "[$argument ...]" if $param.slurpy;
$argument = "[$argument]" if $param.optional;
if $total-constraints
&& $literals-as-constraint == $total-constraints {
$argument .= trans(["'"] => [q|'"'"'|])
if $argument.contains("'");
$argument = "'$argument'"
if $argument.contains(' ' | '"');
}
@positional.push($argument);
}
@arg-help.push($argument => $param.WHY.contents) if $param.WHY and (@arg-help.grep:{ .key eq $argument}) == Empty; # Use first defined
Expand Down
7 changes: 6 additions & 1 deletion t/05-messages/02-errors.t
Expand Up @@ -147,7 +147,9 @@ throws-like { sprintf "%d" }, X::Str::Sprintf::Directives::Count,
}

# https://github.com/rakudo/rakudo/issues/1346
subtest 'USAGE with subsets/where' => {
subtest 'USAGE with subsets/where and variables with quotes' => {
plan 3;

sub uhas (\sig, Mu \c, \desc) {
is-run sub MAIN ( ~ sig ~ ) {},
:err{.contains: c}, :out(*), :exitcode(*), desc
Expand All @@ -167,6 +169,9 @@ subtest 'USAGE with subsets/where' => {
uhas UInt $ where 42, '<UInt where { ... }>',
'subset + where clauses shown sanely';
}

uhas $don't, <don't>,
'variable name does not get special quote treatment';
}

# vim: ft=perl6 expandtab sw=4

0 comments on commit 27fbd7a

Please sign in to comment.