Skip to content

Commit

Permalink
Warn when pure expressions are used in sink context
Browse files Browse the repository at this point in the history
even if the arguments are not constant, or the expression warns.
By explicit request from TimToady++ at
http://irclog.perlgeek.de/perl6/2013-02-18#i_6469204
  • Loading branch information
moritz committed Feb 19, 2013
1 parent 018b73b commit 80f788b
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/Perl6/Optimizer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,19 @@ class Perl6::Optimizer {
}
if $found {
if nqp::can($obj, 'IS_PURE') && $obj.IS_PURE {
sub widen($m) {
my int $from := $m.from;
my int $to := $m.to;
for $m.list {
$from := $_.from if $_.from < $from;
$to := $_.to if $_.to > $to;
}
nqp::substr($m.orig, $from, $to - $from);
}
if $op.node && $*VOID_CONTEXT && !$*IN_DECLARATION {
my str $text := nqp::escape(widen($op.node));
self.add_worry($op, qq[Useless use of expression "$text" in sink context]);
}
# check if all arguments are known at compile time
my $all_args_known := 1;
my @args := [];
Expand All @@ -323,22 +336,8 @@ class Perl6::Optimizer {
$survived := 0;
}
}
sub widen($m) {
my int $from := $m.from;
my int $to := $m.to;
for $m.list {
$from := $_.from if $_.from < $from;
$to := $_.to if $_.to > $to;
}
nqp::substr($m.orig, $from, $to - $from);
}
if $survived {
if $op.node && $*VOID_CONTEXT && !$*IN_DECLARATION {
my str $text := nqp::escape(widen($op.node));
self.add_worry($op, qq[Useless use of constant expression "$text" in sink context]);
return $NULL;

}
return $NULL if $*VOID_CONTEXT && !$*IN_DECLARATION;
$*W.add_object($ret_value);
my $wval := QAST::WVal.new(:value($ret_value));
if $op.named {
Expand Down

0 comments on commit 80f788b

Please sign in to comment.