Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.91k stars 542 forks source link

Make my $pi := 4; a syntax error #10854

Closed p5pRT closed 13 years ago

p5pRT commented 13 years ago

Migrated from rt.perl.org#79820 (status was 'resolved')

Searchable as RT79820$

p5pRT commented 13 years ago

From @nwc10

Currently the := in my $pi := 4; is interpreted as an empty attribute list. As of 5.12.0\, using := here issue a deprecation warning.

No Perl 5 code on CPAN is using :=

If code generators need the flexibility to generate empty attribute lists\, it's trivial to do so by adding a space before the =

I believe that there is consensus that

a​: it would be useful to have a binding operator in Perl 5 b​: that := is the obvious candidate

hence at some point we need to re-use := for this.

Use of := for binding can't be distinguished from use of it to mean an empty attribute list. Hence I propose that we make := a syntax error for 5.14\, so that there is at least 1 major version of Perl 5 where any accidental use will be caught with a clear\, comprehensible syntax error message.

Nicholas Clark

p5pRT commented 13 years ago

From @nwc10

0001-Make-my-pi-4-a-syntax-error.patch ```diff From 1ffc188c9c525d2aefe152ce2f858a1d63a57aba Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Fri, 26 Nov 2010 15:53:56 +0000 Subject: [PATCH] Make my $pi := 4; a syntax error. Previously it interpreted := as an empty attribute list, and issued a deprecation warning. This change permits := to be used as a binding operator. --- dist/B-Deparse/t/deparse.t | 3 +- pod/perldelta.pod | 16 +++++++++--- pod/perldiag.pod | 14 ++++++---- t/lib/warnings/toke | 59 -------------------------------------------- t/op/attrs.t | 57 +++++++++++++++++++++++++++++++++++++++++- toke.c | 3 +- 6 files changed, 79 insertions(+), 73 deletions(-) diff --git a/dist/B-Deparse/t/deparse.t b/dist/B-Deparse/t/deparse.t index 3ae14e9..38ce876 100644 --- a/dist/B-Deparse/t/deparse.t +++ b/dist/B-Deparse/t/deparse.t @@ -619,7 +619,8 @@ foreach (0..3) { # no attribute list my $pi = 4; #### -# := empty attribute list +# SKIP ?$] > 5.013006 && ":= is now a syntax error" +# := treated as an empty attribute list no warnings; my $pi := 4; >>>> diff --git a/pod/perldelta.pod b/pod/perldelta.pod index b445eeb..ad19733 100644 --- a/pod/perldelta.pod +++ b/pod/perldelta.pod @@ -62,12 +62,20 @@ L section. =head1 Incompatible Changes -XXX For a release on a stable branch, this section aspires to be: +=head2 Attempting to use C<:=> as an empty attribute list is now a syntax error - There are no changes intentionally incompatible with 5.XXX.XXX. If any - exist, they are bugs and reports are welcome. +Previously C was exactly equivalent to C, +with the C<:> being treated as the start of an attribute list, ending before +the C<=>. The use of C<:=> to mean C<: => was deprecated in 5.12.0, and is now +a syntax error. This will allow the future use of C<:=> as a new token. -[ List each incompatible change as a =head2 entry ] +We find no Perl 5 code on CPAN using this construction, outside the core's +tests for it, so we believe that this change will have very little impact on +real-world codebases. + +If it is absolutely necessary to have empty attribute lists (for example, +because of a code generator) then avoid the warning by adding a space +before the C<=>. =head1 Deprecations diff --git a/pod/perldiag.pod b/pod/perldiag.pod index c186919..7250057 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -4991,13 +4991,15 @@ modifier is not presently meaningful in substitutions. use the /g modifier. Currently, /c is meaningful only when /g is used. (This may change in the future.) -=item Use of := for an empty attribute list is deprecated +=item Use of := for an empty attribute list is not allowed -(D deprecated) The construction C currently -parses correctly in perl, being equivalent to C -(applying an empty attribute list to C<$x>). This useless -construct is now deprecated, so C<:=> can be reclaimed as a new -operator in the future. +(F) The construction C used to parse as equivalent to +C (applying an empty attribute list to C<$x>). +This construct was deprecated in 5.12.0, and has now been made a syntax +error, so C<:=> can be reclaimed as a new operator in the future. + +If you need an empty attribute list, for example in a code generator, add +a space before the C<=>. =item Use of ?PATTERN? without explicit operator is deprecated diff --git a/t/lib/warnings/toke b/t/lib/warnings/toke index 7096620..5468ef0 100644 --- a/t/lib/warnings/toke +++ b/t/lib/warnings/toke @@ -909,65 +909,6 @@ Possible unintended interpolation of $\ in regex at - line 3. Possible unintended interpolation of $\ in regex at - line 5. ######## # toke.c -use feature 'state'; -# This one is fine as an empty attribute list -my $holy_Einstein : = ''; -# This one is deprecated -my $krunch := 4; -our $FWISK_FWISK_FWIZZACH_FWACH_ZACHITTY_ZICH_SHAZZATZ_FWISK := ''; -state $thump := 'Trumpets'; -# Lather rinse repeat in my usual obsessive style -my @holy_perfect_pitch : = (); -my @zok := (); -our @GUKGUK := (); -# state @widget_mark := (); -my %holy_seditives : = (); -my %bang := (); -our %GIGAZING := (); -# state %hex := (); -no warnings 'deprecated'; -my $holy_giveaways : = ''; -my $eee_yow := []; -our $TWOYYOYYOING_THUK_UGH := 1 == 1; -state $octothorn := 'Tinky Winky'; -my @holy_Taj_Mahal : = (); -my @touche := (); -our @PLAK_DAK_THUK_FRIT := (); -# state @hash_mark := (); -my %holy_priceless_collection_of_Etruscan_snoods : = (); -my %wham_eth := (); -our %THWUK := (); -# state %octalthorpe := (); -use warnings; -my $holy_sewer_pipe : = ''; -my $thunk := undef; -our $BLIT := time; -state $crunch := 'Laa Laa'; -my @glurpp := (); -my @holy_harem : = (); -our @FABADAP := (); -# state @square := (); -my %holy_pin_cushions : = (); -my %swoosh := (); -our %RRRRR := (); -# state %scratchmark := (); -EXPECT -Use of := for an empty attribute list is deprecated at - line 6. -Use of := for an empty attribute list is deprecated at - line 7. -Use of := for an empty attribute list is deprecated at - line 8. -Use of := for an empty attribute list is deprecated at - line 11. -Use of := for an empty attribute list is deprecated at - line 12. -Use of := for an empty attribute list is deprecated at - line 15. -Use of := for an empty attribute list is deprecated at - line 16. -Use of := for an empty attribute list is deprecated at - line 33. -Use of := for an empty attribute list is deprecated at - line 34. -Use of := for an empty attribute list is deprecated at - line 35. -Use of := for an empty attribute list is deprecated at - line 36. -Use of := for an empty attribute list is deprecated at - line 38. -Use of := for an empty attribute list is deprecated at - line 41. -Use of := for an empty attribute list is deprecated at - line 42. -######## -# toke.c use warnings 'syntax' ; my $a = "\o"; my $a = "\o{"; diff --git a/t/op/attrs.t b/t/op/attrs.t index 3013893..4e1a4c3 100644 --- a/t/op/attrs.t +++ b/t/op/attrs.t @@ -14,8 +14,6 @@ BEGIN { use warnings; -plan 92; - $SIG{__WARN__} = sub { die @_ }; sub eval_ok ($;$) { @@ -243,3 +241,58 @@ ok !defined(attributes::get(\PVBM)), $c=undef; eval 'sub t5 :Foo { }'; main::ok $c == \&{"t5"} && $c == $t5b && $c == $t5a; } + +my @tests = grep {/^[^#]/} split /\n/, <<'EOT'; +# This one is fine as an empty attribute list +my $holy_Einstein : = ''; +# This one is deprecated +my $krunch := 4; +our $FWISK_FWISK_FWIZZACH_FWACH_ZACHITTY_ZICH_SHAZZATZ_FWISK := ''; +state $thump := 'Trumpets'; +# Lather rinse repeat in my usual obsessive style +my @holy_perfect_pitch : = (); +my @zok := (); +our @GUKGUK := (); +# state @widget_mark := (); +my %holy_seditives : = (); +my %bang := (); +our %GIGAZING := (); +# state %hex := (); +my $holy_giveaways : = ''; +my $eee_yow := []; +our $TWOYYOYYOING_THUK_UGH := 1 == 1; +state $octothorn := 'Tinky Winky'; +my @holy_Taj_Mahal : = (); +my @touche := (); +our @PLAK_DAK_THUK_FRIT := (); +# state @hash_mark := (); +my %holy_priceless_collection_of_Etruscan_snoods : = (); +my %wham_eth := (); +our %THWUK := (); +# state %octalthorpe := (); +my $holy_sewer_pipe : = ''; +my $thunk := undef; +our $BLIT := time; +state $crunch := 'Laa Laa'; +my @glurpp := (); +my @holy_harem : = (); +our @FABADAP := (); +# state @square := (); +my %holy_pin_cushions : = (); +my %swoosh := (); +our %RRRRR := (); +# state %scratchmark := (); +EOT + +foreach my $test (@tests) { + use feature 'state'; + eval $test; + if ($test =~ /:=/) { + like $@, qr/Use of := for an empty attribute list is not allowed/, + "Parse error for q{$test}"; + } else { + is $@, '', "No error for q{$test}"; + } +} + +done_testing(); diff --git a/toke.c b/toke.c index 584aea0..d2868c2 100644 --- a/toke.c +++ b/toke.c @@ -5228,7 +5228,8 @@ Perl_yylex(pTHX) break; PL_bufptr = s; /* update in case we back off */ if (*s == '=') { - deprecate(":= for an empty attribute list"); + Perl_croak(aTHX_ + "Use of := for an empty attribute list is not allowed"); } goto grabattrs; case XATTRBLOCK: -- 1.6.0 ```
p5pRT commented 13 years ago

From @rurban

2010/11/26 Nicholas Clark \perlbug\-followup@&#8203;perl\.org​:

# New Ticket Created by  Nicholas Clark # Please include the string​:  [perl #79820] # in the subject line of all future correspondence about this issue. # \<URL​: http​://rt.perl.org/rt3/Ticket/Display.html?id=79820 >

Currently the := in my $pi := 4; is interpreted as an empty attribute list. As of 5.12.0\, using := here issue a deprecation warning.

No Perl 5 code on CPAN is using :=

If code generators need the flexibility to generate empty attribute lists\, it's trivial to do so by adding a space before the =

I believe that there is consensus that

a​: it would be useful to have a binding operator in Perl 5 b​: that := is the obvious candidate

hence at some point we need to re-use := for this.

Use of := for binding can't be distinguished from use of it to mean an empty attribute list. Hence I propose that we make := a syntax error for 5.14\, so that there is at least 1 major version of Perl 5 where any accidental use will be caught with a clear\, comprehensible syntax error message.

Yes

When can we expect Chip to come up with BIND? How can we help? -- Reini Urban

p5pRT commented 13 years ago

The RT System itself - Status changed from 'new' to 'open'

p5pRT commented 13 years ago

From @iabyn

On Fri\, Nov 26\, 2010 at 08​:04​:17AM -0800\, Nicholas Clark wrote​:

Hence I propose that we make := a syntax error for 5.14\,

+1

-- Never do today what you can put off till tomorrow.

p5pRT commented 13 years ago

From @obra

On Sun\, Nov 28\, 2010 at 12​:38​:36PM +0000\, Dave Mitchell wrote​:

On Fri\, Nov 26\, 2010 at 08​:04​:17AM -0800\, Nicholas Clark wrote​:

Hence I propose that we make := a syntax error for 5.14\,

+1 +1

-- Never do today what you can put off till tomorrow.

--

p5pRT commented 13 years ago

From @cpansprout

On Fri Nov 26 08​:04​:16 2010\, nicholas wrote​:

I believe that there is consensus that

a​: it would be useful to have a binding operator in Perl 5 b​: that := is the obvious candidate

hence at some point we need to re-use := for this.

No consensus was ever reached. Many still prefer the \$foo = \$bar syntax as it makes the edge cases obvious​:

\$foo = \$bar; # simple alias \@​foo = \@​bar; # simple alias \(@​foo) = \(@​bar); # alias each element \(@​foo) = @​list; # alias each element (RHS must contain scalar refs) \@​foo[0..$#list] = @​list; # similar \(@​foo) = \$bar; # alias $foo[0] to $bar \(@​foo) = \@​bar; # error; RHS is not a scalar ref \@​foo[0..5] = \@​bar; # error; RHS is not a scalar ref \@​foo = $bar; # only works if bar is an array ref

But\, nonetheless\, I see nothing wrong with making this a syntax error. Three others have shown their approval. So I have just applied it (fancy that! that *I* should be applying *your* patches!) as 2dc78664.

p5pRT commented 13 years ago

@cpansprout - Status changed from 'open' to 'resolved'