develooper Front page | perl.perl5.porters | Postings from April 2008

Re: bug or not? constants warn only once

Thread Previous | Thread Next
Nicholas Clark
April 26, 2008 10:44
Re: bug or not? constants warn only once
Message ID:
On Sat, Apr 26, 2008 at 01:51:21AM +0200, Aristotle Pagaltzis wrote:

> The caching of conversions for literals is a bug not because it
> suppresses subsequent warnings, but because literals are supposed
> to be immutable. F.ex., mutable literals mean that bitwise op
> expressions *with literals* can change their behaviour over the
> course of a program ??? which is inarguably broken.
> And if my ignorant surmise is correct, the fix, which would be
> good defensive programming in any case, will be nearly free. In
> contradistinciton, ensuring mutiple warnings seems to me like it
> will require invasive changes: extra state for each variable will
> need to be tracked in several places and checked in many others.
> Perl has gotten a lot slower over time; let???s not add more bloat
> if there isn???t good, or indeed any, reason for it.

(As an aside, do you have good, shareable benchmarks to demonstrate this?)

Do the appended tests codify the behaviour we desire?
Anything I've missed that would be necessary to nail down this behaviour?

Nicholas Clark

#!perl -w

use strict;
use Test::More 'no_plan';

sub warnings {
  my ($got, $expect, $todo) = @_;
  local $Test::Builder::Level = $Test::Builder::Level + 1;
  local $TODO = $todo;

  if ($expect) {
    is(scalar @$got, 1);
    like($got->[0], $expect);
  } else {
    is(scalar @$got, 0);
    diag $_ foreach @$got;

sub xorit {
  my (undef, $expect, $name, $warn, $todo) = @_;
  my @warnings;
  local $SIG{__WARN__} = sub {push @warnings, "@_"};
  local $TODO = $todo;

  # Don't make this a constant as it perturbs the test below, because IT TOO
  # (currently) gets flagged as "number"
  my $spaces = "   ";
  is($_[0] ^ $spaces, $expect, $name);
  warnings(\@warnings, $warn, $todo);
  no warnings;
  $_[0] + 0;

sub addit {
  my (undef, $name, $warn1, $warn2, $todo1, $todo2) = @_;
  my @warnings;
  local $SIG{__WARN__} = sub {push @warnings, "@_"};

  is($_[0] + 2, 2, $name);
  warnings(\@warnings, $warn1, $todo1);
  @warnings = ();
  is($_[0] + 10, 10, "$name, again");
  warnings(\@warnings, $warn2, $todo2);

my $todo = 'constants in the optree should not show side effects of conversion';
for my $i (0, 1) {
  xorit('pie', 'PIE', "xor, constant, iteration $i", 0, $i && $todo);

my $var1 = 'pie';
xorit($var1, 'PIE', 'xor, variable, iteration 0');
xorit($var1, 0, 'xor, variable, iteration 1',
      qr/^Argument "   " isn't numeric in bitwise xor/);

my $warn = qr/Argument "pie" isn't numeric in addition/;
for my $i (0, 1) {
  addit('pie', "add, constant, iteration $i", $warn, $warn, $i && $todo, $todo);

my $var2 = 'pie';
addit($var2, 'add, variable, iteration 0', $warn);
addit($var2, 'add, variable, iteration 1');

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About