Skip to content

Commit

Permalink
generalize literal True/False smartmatch test
Browse files Browse the repository at this point in the history
Moved the test from Grammar to Actions, and it now applies to all actions
that call check_smartmatch.  Also applies to first arg of grep and first.
  • Loading branch information
TimToady committed Dec 4, 2015
1 parent cb7c6f4 commit 587f700
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
39 changes: 24 additions & 15 deletions src/Perl6/Actions.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,8 @@ class Perl6::Actions is HLL::Actions does STDActions {
our %commatrap := nqp::hash(
'&categorize', 1,
'&classify', 1,
'&first', 1,
'&first-index', 1,
'&grep', 1,
'&grep-index', 1,
'&last-index', 1,
'&first', 2,
'&grep', 2,
'&map', 1,
'&reduce', 1,
'&sort', 1,
Expand Down Expand Up @@ -1495,7 +1492,7 @@ Compilation unit '$file' contained the following violations:
my $xblock := $<xblock>.ast;
my $sm_exp := $xblock.shift;
my $pblock := $xblock.shift;
check_smartmatch($/,$sm_exp);
check_smartmatch($<xblock>,$sm_exp);

# Handle the smart-match.
my $match_past := QAST::Op.new( :op('callmethod'), :name('ACCEPTS'),
Expand Down Expand Up @@ -1850,7 +1847,7 @@ Compilation unit '$file' contained the following violations:

method statement_mod_cond:sym<when>($/) {
my $pat := $<modifier_expr>.ast;
check_smartmatch($/,$pat);
check_smartmatch($<modifier_expr>,$pat);
make QAST::Op.new( :op<if>,
QAST::Op.new( :name('ACCEPTS'), :op('callmethod'),
$pat,
Expand Down Expand Up @@ -4037,7 +4034,7 @@ Compilation unit '$file' contained the following violations:

# If we have a refinement, make sure it's thunked if needed. If none,
# just always true.
my $refinement := make_where_block($/, $<EXPR> ?? $<EXPR>.ast !!
my $refinement := make_where_block($<EXPR>, $<EXPR> ?? $<EXPR>.ast !!
QAST::Op.new( :op('p6bool'), QAST::IVal.new( :value(1) ) ));

# Create the meta-object.
Expand Down Expand Up @@ -4464,9 +4461,10 @@ Compilation unit '$file' contained the following violations:
:name('signature'),
QAST::Var.new( :name('$_'), :scope('lexical') )
);
my $closure_signature := $<name><sigterm><fakesignature>.ast;
my $fakesig := $<name><sigterm><fakesignature>;
my $closure_signature := $fakesig.ast;

my $where := make_where_block($/, $closure_signature, $get_signature_past);
my $where := make_where_block($fakesig, $closure_signature, $get_signature_past);
%*PARAM_INFO<post_constraints>.push($where);
}

Expand Down Expand Up @@ -4626,15 +4624,15 @@ Compilation unit '$file' contained the following violations:
unless %*PARAM_INFO<post_constraints> {
%*PARAM_INFO<post_constraints> := [];
}
%*PARAM_INFO<post_constraints>.push(make_where_block($/, $<EXPR>.ast));
%*PARAM_INFO<post_constraints>.push(make_where_block($<EXPR>, $<EXPR>.ast));
}
}
else {
if $<signature> {
$/.CURSOR.NYI('Signatures as constraints on variables');
}
else {
make make_where_block($/, $<EXPR>.ast);
make make_where_block($<EXPR>, $<EXPR>.ast);
}
}
}
Expand Down Expand Up @@ -5170,6 +5168,7 @@ Compilation unit '$file' contained the following violations:
$/.CURSOR.missing("comma after block argument to " ~ nqp::substr(@name[0],1));
}
}
check_smartmatch($<args>,$past[0]) if %commatrap{@name[0]} == 2;
}
else {
$past.unshift($*W.symbol_lookup(@name, $/));
Expand Down Expand Up @@ -5927,8 +5926,18 @@ Compilation unit '$file' contained the following violations:
}

sub check_smartmatch($/,$pat) {
if $pat.ann('is_S') {
$/.CURSOR.worry("Smartmatch with S/// can never succeed because the subsequent string match will fail");
if nqp::can($pat,'ann') && $pat.ann('is_S') {
$/.PRECURSOR.worry("Smartmatch with S/// can never succeed because the subsequent string match will fail");
}

if $pat ~~ QAST::WVal && istype($pat.returns, $*W.find_symbol(['Bool'])) && nqp::isconcrete($pat.value) {
my $p := ~$pat.compile_time_value;
if $p eq 'True' {
$/.PRECURSOR.worry("Smartmatch against True always matches; if you mean to test the topic for truthiness, use :so or *.so or ?* instead")
}
elsif $p eq 'False' {
$/.PRECURSOR.worry("Smartmatch against False always fails; if you mean to test the topic for truthiness, use :!so or *.not or !* instead")
}
}
}

Expand All @@ -5941,7 +5950,7 @@ Compilation unit '$file' contained the following violations:
my $old_topic_var := $lhs.unique('old_topic');
my $result_var := $lhs.unique('sm_result');
my $sm_call;
check_smartmatch($/,$rhs);
check_smartmatch($/[1],$rhs);

# Call $rhs.ACCEPTS( $_ ), where $_ is $lhs.
$sm_call := QAST::Op.new(
Expand Down
12 changes: 2 additions & 10 deletions src/Perl6/Grammar.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -4118,8 +4118,8 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
token infix:sym<eqv> { <sym> >> <O('%chaining')> }
token infix:sym<before> { <sym> >> <O('%chaining')> }
token infix:sym<after> { <sym> >> <O('%chaining')> }
token infix:sym<~~> { <sym> <O('%chaining')> <!dumbsmart> }
token infix:sym<!~~> { <sym> <O('%chaining')> <!dumbsmart> }
token infix:sym<~~> { <sym> <O('%chaining')> }
token infix:sym<!~~> { <sym> <O('%chaining')> }
token infix:sym<(elem)> { <sym> <O('%chaining')> }
token infix:sym«» { <sym> <O('%chaining')> }
token infix:sym«» { <sym> <O('%chaining')> }
Expand All @@ -4143,14 +4143,6 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
token infix:sym«(>+)» { <sym> <O('%chaining')> }
token infix:sym«» { <sym> <O('%chaining')> }

token dumbsmart {
# should be
# 'Bool::'? True && <.longname>
# once && in regexes is implemented
| <?before \h* [ 'Bool::'? 'True' && <.longname> ] > <.worry("Smartmatch against True always matches; if you mean to test the topic for truthiness, use :so or *.so or ?* instead")>
| <?before \h* [ 'Bool::'? 'False' && <.longname> ] > <.worry("Smartmatch against False always fails; if you mean to test the topic for truthiness, use :!so or *.not or !* instead")>
}

token infix:sym<&&> { <sym> <O('%tight_and, :iffy<1>, :pasttype<if>')> }

token infix:sym<||> { <sym> <O('%tight_or, :iffy<1>, :assoc<left>, :pasttype<unless>')> }
Expand Down

0 comments on commit 587f700

Please sign in to comment.