Skip to content

Commit

Permalink
Improve useless use warnings of constant in sink
Browse files Browse the repository at this point in the history
- Use the actual text of numeric literals the user wrote in code
    - ∞ for ∞ / Inf for Inf / un-normalized nums / non-10-base ints
        Fixes R#1696 #1696
- Refer to big ints as "integer" instead of "value", just
    like we do for small ints
- Refer to rationals as "rational" instead of "value"
  • Loading branch information
zoffixznet committed Apr 7, 2018
1 parent 13382cc commit 54137e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
25 changes: 11 additions & 14 deletions src/Perl6/Optimizer.nqp
Expand Up @@ -2000,25 +2000,17 @@ class Perl6::Optimizer {
my str $warning;
if $want[1] eq 'Ss' && nqp::istype($want[2], QAST::SVal) {
$warning := qq[Useless use of constant string "]
~ nqp::escape($want[2].value)
~ nqp::escape($want[2].node // $want[2].value)
~ qq[" in sink context];
}
elsif $want[1] eq 'Ii' && nqp::istype($want[2], QAST::IVal) {
$warning := qq[Useless use of constant integer ]
~ ~$want[2].value
~ ($want[2].node // $want[2].value)
~ qq[ in sink context];
}
elsif $want[1] eq 'Nn' {
if nqp::istype($want[2], QAST::NVal) {
$warning := qq[Useless use of constant floating-point number ]
~ $want[2].value ~ qq[ in sink context];
}
elsif nqp::istype($want[2], QAST::Op)
&& ($want[2].op eq 'inf' || $want[2].op eq 'nan'
|| $want[2].op eq 'neginf') {
$warning := qq[Useless use of constant floating-point number ]
~ $want[2].node ~ qq[ in sink context];
}
elsif $want[1] eq 'Nn' && nqp::istype($want[2], QAST::NVal) {
$warning := qq[Useless use of constant floating-point number ]
~ ($want[2].node // $want[2].value) ~ qq[ in sink context];
}
if $warning {
$warning := $warning ~ ' (use Nil instead to suppress this warning)' if $want.okifnil;
Expand Down Expand Up @@ -2262,7 +2254,12 @@ class Perl6::Optimizer {
my $suggest := ($visit.okifnil ?? ' (use Nil instead to suppress this warning)' !! '');
unless $value eq 'Nil'
|| $visit.ann('sink-quietly') {
my $warning := qq[Useless use of constant value $value in sink context$suggest];
my $thing := nqp::istype($visit.value,
$!symbols.find_in_setting: 'Int')
?? 'integer' !! nqp::istype($visit.value,
$!symbols.find_in_setting: 'Rational')
?? 'rational' !! 'value';
my $warning := qq[Useless use of constant $thing $value in sink context$suggest];
note($warning) if $!debug;
$!problems.add_worry($visit, $warning)
}
Expand Down
15 changes: 8 additions & 7 deletions src/Perl6/World.nqp
Expand Up @@ -2713,10 +2713,13 @@ class Perl6::World is HLL::World {
# Adds a numeric constant value (int or num) to the constants table.
# Returns PAST to do the lookup of the constant.
method add_numeric_constant($/, $type, $value) {
my $node := $/;
if $type eq 'Int' && (try $value.HOW.name($value)) eq 'Int' {
if nqp::isbig_I($value) {
# cannot unbox to int without loss of information
return self.add_constant('Int', 'bigint', $value);
my $const := self.add_constant('Int', 'bigint', $value);
$const.node: $node;
return $const;
}
# since Int doesn't have any vtables yet (at least while compiling
# the setting), it is inconvenient to work with, so unbox
Expand All @@ -2725,16 +2728,14 @@ class Perl6::World is HLL::World {
my $const := self.add_constant($type, nqp::lc($type), $value);
my $past;
if $type eq 'Int' {
$past := QAST::Want.new($const, 'Ii', QAST::IVal.new( :value($value) ) );
$past := QAST::Want.new: :$node, $const, 'Ii',
QAST::IVal.new: :$node, :$value;
}
else {
$past := QAST::Want.new: :node($/), $const, 'Nn',
QAST::NVal.new: :node($/), :$value;
$past := QAST::Want.new: :$node, $const, 'Nn',
QAST::NVal.new: :$node, :$value;
}
$past.returns($const.returns);
if $/ {
$past.node($/);
}
$past;
}

Expand Down

0 comments on commit 54137e8

Please sign in to comment.