Closed p5pRT closed 10 years ago
C\<return doit() or die "did it wrong";>
Would be nice if that warns "unreachable statement".
"rvtol@xs4all.nl (via RT)" schreef:
return doit() or die "did it wrong";
Would be nice if that warns "unreachable statement".
Another variant of "unreachable":
perl -wle 'my $x = \<>; $x++ or die; print $x' perl -wle 'my $x = \<>; $x-- or die; print $x'
(am not sure about the relation yet)
-- Affijn\, Ruud
"Gewoon is een tijger."
The RT System itself - Status changed from 'new' to 'open'
"Dr.Ruud" \rvtol\+news@​isolution\.nl writes:
"rvtol@xs4all.nl (via RT)" schreef:
return doit() or die "did it wrong";
Would be nice if that warns "unreachable statement".
Another variant of "unreachable":
perl -wle 'my $x = \<>; $x++ or die; print $x' perl -wle 'my $x = \<>; $x-- or die; print $x'
How so unreachable?
echo -n | perl -wle 'my $x = \<>; $x++ or die; print $x' echo -n 0 | perl -wle 'my $x = \<>; $x++ or die; print $x' echo 0 | perl -wle 'my $x = \<>; $x++ or die; print $x' echo -n | perl -wle 'my $x = \<>; $x-- or die; print $x' echo -n 0 | perl -wle 'my $x = \<>; $x-- or die; print $x' echo 0 | perl -wle 'my $x = \<>; $x-- or die; print $x'
Died at -e line 1. Died at -e line 1\, \<> line 1. 1 Died at -e line 1. Died at -e line 1\, \<> line 1. -1
(am not sure about the relation yet)
(?)
Eirik -- You just paddle around there awhile\, and I'll explain about these poles ... -- Sally Brown Is that in Europe? -- Joyce Brown
Eirik Berg Hanssen schreef:
Dr.Ruud:
Another variant of "unreachable":
perl -wle 'my $x = \<>; $x++ or die; print $x' perl -wle 'my $x = \<>; $x-- or die; print $x'
How so unreachable?
Oops\, please ignore. (forgot to chomp my test values)
-- Affijn\, Ruud
"Gewoon is een tijger."
"rvtol(AT)xs4all.nl (via RT)" schreef:
C\<return doit() or die "did it wrong";>
Would be nice if that warns "unreachable statement".
Some examples can be seen here: http://www.google.com/codesearch?q=+lang:perl+\+return\+.*\+or\+die http://www.google.com/codesearch?q=lang:perl+return\+\S%2B\s%2Bor
There is one in:
http://search.cpan.org/src/GRICHTER/ExtUtils-XSBuilder-0.28/XSBuilder/ParseSource.pm
A multi-line example: http://search.cpan.org/~markf/Attempt-1.01/lib/Sub/Attempts.pm
-- Affijn\, Ruud
"Gewoon is een tijger."
this one looks like a typical offender:
\<\<COPIED BatchSystem-SBS-0.32/lib/BatchSystem/SBS/Common.pm - 3 identical
88: if($simpleLocker){ return $simpleLocker->trylock($f) or CORE::die "cannot lock [$f]: $!"; }else{
97: if($simpleLocker){ return $simpleLocker->unlock($f) or CORE::die "cannot lock [$f]: $!"; }else{
www.cpan.org/authors/id/A/AL/ALEXMASS/BatchSystem-SBS-0.32.tar.gz COPIED
Refusing to allow return in non-void contexts would also disallow legitmate uses such as
(wearedone and return) or domore;
but how legit is that?
wearedone and return; domore;
would be a non-violating rewrite.
Alternately\, dropping the precedence of return would make C\<return something or die> do what the coder wants.
a low-precedence return would surely expose some bugs in currently unreachable code\, would it break anything that is written well?
On Tue\, Oct 21\, 2008 at 6:19 PM\, Dr.Ruud \rvtol\+news@​isolution\.nl wrote:
Would be nice if that warns "unreachable statement".
Some examples can be seen here: http://www.google.com/codesearch?q=+lang:perl+\+return\+.*\+or\+die
Hello David\,
I do not understand the problem.\,\,
That's true that I was supposing the precedence of the return.
I slightly modified the code as $simpleLocker->trylock($f) and return 1; die "cannot lock [$f]: $!";
would that make this piece of code more secure? to what extent? cheers Alex
David Nicol wrote:
this one looks like a typical offender:
\<\<COPIED BatchSystem-SBS-0.32/lib/BatchSystem/SBS/Common.pm - 3 identical
88​: if\($simpleLocker\)\{ return $simpleLocker\->trylock\($f\) or CORE​::die "cannot
lock [$f]: $!"; }else{
wearedone and return; domore;
would be a non-violating rewrite.
Alternately\, dropping the precedence of return would make C\<return something or die> do what the coder wants.
a low-precedence return would surely expose some bugs in currently unreachable code\, would it break anything that is written well?
On Tue\, Oct 21\, 2008 at 6:19 PM\, Dr.Ruud \rvtol\+news@​isolution\.nl wrote:
Would be nice if that warns "unreachable statement".
Some examples can be seen here: http://www.google.com/codesearch?q=+lang:perl+\+return\+.*\+or\+die
-- Alexandre Masselot\, phD Senior bioinformatician www.genebio.com voice: +41 22 702 99 00
Return *is* low precedence already.
Compare:
return ($a + $b) * $c;
with
print ($a + $b) * $c;
--tom
I reviewed this 5-year-old ticket tonight. I don't see any claim that there is a bug in perl\, nor do I see a request for a change in documentation or source code. All I see is some back and forth that would better be handled on IRC or on perlmonks.
I am taking this ticket for the purpose of closing it in seven days -- unless someone can see something I haven't and takes the ticket over.
Thank you very much. Jim Keenan
On 19 February 2013 02:51\, James E Keenan via RT \perlbug\-followup@​perl\.org wrote:
I reviewed this 5-year-old ticket tonight. I don't see any claim that there is a bug in perl\, nor do I see a request for a change in documentation or source code. All I see is some back and forth that would better be handled on IRC or on perlmonks.
I am taking this ticket for the purpose of closing it in seven days -- unless someone can see something I haven't and takes the ticket over.
Well\, Im not going to take it over\, but IMO:
return $x or die "...";
should warn or something.
So maybe the ticket should be marked stalled and not closed?
Yves
-- perl -Mre=debug -e "/just|another|perl|hacker/"
On 2013-02-19 03:03\, demerphq wrote:
On 19 February 2013 02:51\, James E Keenan via RT \perlbug\-followup@​perl\.org wrote:
I reviewed this 5-year-old ticket tonight. I don't see any claim that there is a bug in perl\, nor do I see a request for a change in documentation or source code. All I see is some back and forth that would better be handled on IRC or on perlmonks.
I am taking this ticket for the purpose of closing it in seven days -- unless someone can see something I haven't and takes the ticket over.
Well\, Im not going to take it over\, but IMO:
return $x or die "...";
should warn or something.
So maybe the ticket should be marked stalled and not closed?
Indeed\, the requested change is to warn on popular kinds of unreachable code\, for example:
return ... or ...;
Or leave it to perlcritic/perltidy?
-- Ruud
On Tue\, Feb 19\, 2013 at 11:44:21AM +0100\, Dr.Ruud wrote:
On 2013-02-19 03:03\, demerphq wrote:
Well\, Im not going to take it over\, but IMO:
return $x or die "...";
should warn or something.
So maybe the ticket should be marked stalled and not closed?
Indeed\, the requested change is to warn on popular kinds of unreachable code\, for example:
return ... or ...;
(subexpression that always causes control flow) [operator] ...
where ... is unreachable.
Or leave it to perlcritic/perltidy?
The code that we're suggesting should warn\, is buggy as written. I don't think that that's really a "tidy" issue\, or even a style issue.
To my mind\, it's a legitimate todo.
Nicholas Clark
On Tue\, Feb 19\, 2013 at 3:03 AM\, demerphq \demerphq@​gmail\.com wrote:
Well\, Im not going to take it over\, but IMO:
return $x or die "...";
should warn or something.
So maybe the ticket should be marked stalled and not closed?
Yeah\, this is always buggy\, so we might as well warn.
Leon
* Leon Timmermans \fawaka@​gmail\.com [2013-02-19 13:15]:
Yeah\, this is always buggy\, so we might as well warn.
Except in Perl poetry. I am sure that Paul Fenwick\, for one\, would appreciate `return 1 or die`. :-) \
-- *AUTOLOAD=*_;sub _{s/::([^:]*)$/print$1\,("\,$\/"\," ")[defined wantarray]/e;chop;$_} &Just->another->Perl->hack; #Aristotle Pagaltzis // \<http://plasmasturm.org/>
* Leon Timmermans \fawaka@​gmail\.com [2013-02-19T07:12:42]
On Tue\, Feb 19\, 2013 at 3:03 AM\, demerphq \demerphq@​gmail\.com wrote:
return $x or die "...";
should warn or something. Yeah\, this is always buggy\, so we might as well warn.
Agreed. I've made this mistake and then wondered why I didn't get an unreachable code warning\, in the past.
-- rjbs
"Dr.Ruud" \rvtol\+usenet@​isolution\.nl wrote on Tue\, 19 Feb 2013 11:44:21 +0100:
On 2013-02-19 03:03\, demerphq wrote:
On 19 February 2013 02:51\, James E Keenan via RT \perlbug\-followup@​perl\.org wrote:
I reviewed this 5-year-old ticket tonight. I don't see any claim that there is a bug in perl\, nor do I see a request for a change in documentation or source code. All I see is some back and forth that would better be handled on IRC or on perlmonks.
I am taking this ticket for the purpose of closing it in seven days -- unless someone can see something I haven't and takes the ticket over.
Well\, Im not going to take it over\, but IMO:
return $x or die "...";
should warn or something.
So maybe the ticket should be marked stalled and not closed?
Indeed\, the requested change is to warn on popular kinds of unreachable code\, for example:
return ... or ...;
Or leave it to perlcritic/perltidy?
What is it that gardenpaths people into making this sort of error\, anyway?
I'm serious.
--tom
Leon Timmermans \fawaka@​gmail\.com wrote on Tue\, 19 Feb 2013 13:12:42 +0100:
On Tue\, Feb 19\, 2013 at 3:03 AM\, demerphq \demerphq@​gmail\.com wrote:
Well\, Im not going to take it over\, but IMO:
return $x or die "...";
should warn or something.
So maybe the ticket should be marked stalled and not closed?
Yeah\, this is always buggy\, so we might as well warn.
Precedence is different on return than anywhere else in Perl. Try explaining why this elicits warning 1:
sub t1 { return(0) || warn "warning 1" } sub t2 { return(0) or warn "warning 2" } $x = t1(); $y = t2();
--tom
Ricardo Signes \perl\.p5p@​rjbs\.manxome\.org wrote on Tue\, 19 Feb 2013 09:59:58 EST:
* Leon Timmermans \fawaka@​gmail\.com [2013-02-19T07:12:42]
On Tue\, Feb 19\, 2013 at 3:03 AM\, demerphq \demerphq@​gmail\.com wrote:
return $x or die "...";
should warn or something.
Yeah\, this is always buggy\, so we might as well warn.
Agreed. I've made this mistake and then wondered why I didn't get an unreachable code warning\, in the past.
Again I ask: what is it that leads anyone ever to make that particular error? I do not understand.
--tom
On 19 February 2013 17:02\, Tom Christiansen \tchrist@​perl\.com wrote:
Ricardo Signes \perl\.p5p@​rjbs\.manxome\.org wrote on Tue\, 19 Feb 2013 09:59:58 EST:
* Leon Timmermans \fawaka@​gmail\.com [2013-02-19T07:12:42]
On Tue\, Feb 19\, 2013 at 3:03 AM\, demerphq \demerphq@​gmail\.com wrote:
return $x or die "...";
should warn or something.
Yeah\, this is always buggy\, so we might as well warn.
Agreed. I've made this mistake and then wondered why I didn't get an unreachable code warning\, in the past.
Again I ask: what is it that leads anyone ever to make that particular error? I do not understand.
Who cares? People do it and it causes trouble and Perl should probably be able to figure out how to warn about it.
cheers\, Yves
-- perl -Mre=debug -e "/just|another|perl|hacker/"
On Tue\, Feb 19\, 2013 at 1:02 PM\, Tom Christiansen \tchrist@​perl\.com wrote:
Ricardo Signes \perl\.p5p@​rjbs\.manxome\.org wrote on Tue\, 19 Feb 2013 09:59:58 EST:
* Leon Timmermans \fawaka@​gmail\.com [2013-02-19T07:12:42]
On Tue\, Feb 19\, 2013 at 3:03 AM\, demerphq \demerphq@​gmail\.com wrote:
return $x or die "...";
should warn or something.
Yeah\, this is always buggy\, so we might as well warn.
Agreed. I've made this mistake and then wondered why I didn't get an unreachable code warning\, in the past.
Again I ask: what is it that leads anyone ever to make that particular error? I do not understand.
Playing the devil's advocate\, how about: Blind code standardization\, the sort that goes "every function should have an explicit return!"
demerphq \demerphq@​gmail\.com wrote on Tue\, 19 Feb 2013 17:10:14 +0100:
On 19 February 2013 17:02\, Tom Christiansen \tchrist@​perl\.com wrote:
Ricardo Signes \perl\.p5p@​rjbs\.manxome\.org wrote on Tue\, 19 Feb 2013 09:59:58 EST:
* Leon Timmermans \fawaka@​gmail\.com [2013-02-19T07:12:42]
On Tue\, Feb 19\, 2013 at 3:03 AM\, demerphq \demerphq@​gmail\.com wrote:
return $x or die "...";
should warn or something.
Yeah\, this is always buggy\, so we might as well warn.
Agreed. I've made this mistake and then wondered why I didn't get an unreachable code warning\, in the past.
Again I ask: what is it that leads anyone ever to make that particular error? I do not understand.
Who cares? People do it and it causes trouble and Perl should probably be able to figure out how to warn about it.
The reason it matters is because I have no idea what that is supposed to mean.
There are uncountably many similar constructs that Perl does not warn on either\, like
$ perl -cwe 'die "ABC"; die "XYZ"' -e syntax OK
$ perl -cwe 'die("ABC") || die("XYZ")' -e syntax OK
$ perl -cwe 'die("ABC") && die("XYZ")' -e syntax OK
None of which are caught and all of which should be. Remember
/* NOTREACHED */
I want to understand what it is that the people who make this error were thinking\, and how they came to think that.
--tom
On Tue\, Feb 19\, 2013 at 7:25 PM\, Tom Christiansen \tchrist@​perl\.com wrote:
The reason it matters is because I have no idea what that is supposed to mean.
There are uncountably many similar constructs that Perl does not warn on either\, like
$ perl \-cwe 'die "ABC"; die "XYZ"' \-e syntax OK $ perl \-cwe 'die\("ABC"\) || die\("XYZ"\)' \-e syntax OK $ perl \-cwe 'die\("ABC"\) && die\("XYZ"\)' \-e syntax OK
None of which are caught and all of which should be. Remember
/\* NOTREACHED \*/
I want to understand what it is that the people who make this error were thinking\, and how they came to think that.
It's a simple precedence mistake.
Leon
On Tue\, Feb 19\, 2013 at 3:08 PM\, Aristoteles Pagaltzis \pagaltzis@​gmx\.de wrote:
Except in Perl poetry. I am sure that Paul Fenwick\, for one\, would appreciate `return 1 or die`. :-) \
Fortunately\, one usually doesn't run poetry with warnings enabled ;-)
Leon
* Tom Christiansen \tchrist@​perl\.com [2013-02-19 17:00]:
Precedence is different on return than anywhere else in Perl. Try explaining why this elicits warning 1:
sub t1 { return(0) || warn "warning 1" } sub t2 { return(0) or warn "warning 2" } $x = t1(); $y = t2();
This smells to me like a super-special parse reminiscent of how Perl parses
sort foo($x)
which has cost me way too many hairs over the years. I run into it just rarely enough to never suspect the real problem\, not only because the rarity means I forget\, but also because it almost invariably happens in a chain of data structure-munging list ops\, so the symptom is a weird number of elements returned at the end\, and at some very surprising deref level. So I end up on a long goose chase of conjectures every time before I finally start converging on the real problem and eventually remember with a start that `sort` is hatefully super-special. Ugh.
Err.
Iām afraid Iām not sure any of that is even relevant to your pointā¦ all I have is the suspicion that this is a similar case. But I feel a need to rant about `sort` whenever I am reminded of this aspect of it; see above for why.
-- *AUTOLOAD=*_;sub _{s/::([^:]*)$/print$1\,("\,$\/"\," ")[defined wantarray]/e;chop;$_} &Just->another->Perl->hack; #Aristotle Pagaltzis // \<http://plasmasturm.org/>
Aristotle Pagaltzis \pagaltzis@​gmx\.de wrote on Wed\, 20 Feb 2013 00:06:02 +0100:
* Tom Christiansen \tchrist@​perl\.com [2013-02-19 17:00]:
Precedence is different on return than anywhere else in Perl. Try explaining why this elicits warning 1:
sub t1 { return(0) || warn "warning 1" } sub t2 { return(0) or warn "warning 2" } $x = t1(); $y = t2();
This smells to me like a super-special parse reminiscent of how Perl parses
sort foo\($x\)
which has cost me way too many hairs over the years. I run into it just rarely enough to never suspect the real problem\, not only because the rarity means I forget\, but also because it almost invariably happens in a chain of data structure-munging list ops\, so the symptom is a weird number of elements returned at the end\, and at some very surprising deref level. So I end up on a long goose chase of conjectures every time before I finally start converging on the real problem and eventually remember with a start that `sort` is hatefully super-special. Ugh.
Err.
Iām afraid Iām not sure any of that is even relevant to your pointā¦ all I have is the suspicion that this is a similar case. But I feel a need to rant about `sort` whenever I am reminded of this aspect of it; see above for why.
The thing about return is that it is design to make it hard to make precedence errors with. You can even
return ($a + 5) * 9;
and have it do the right thing. I just had never heard of anybody making a precedence error with it before\, because it tries so hard to make that impossible.
Leave it that skunky "or" thing to screw that up. Thank goodness I never used it\, and so never have precedence problems. :)
--tom
* Tom Christiansen \tchrist@​perl\.com [2013-02-19T11:02:02]
Again I ask: what is it that leads anyone ever to make that particular error? I do not understand.
I don't remember how it came about. I assume it was the result of absent-minded refactoring\, where:
my $x = foo or die; return $x;
turned into
return foo or die;
...but I can't remember. I only remember that I've gotten that sort of nonsense in my code. If I find evidence of commits fixing it or\, Heaven forbid\, I make that mistake again\, I'll try to let you know how it came about.
I do think it's useful to know what kind of confusion of ideas that could provoke such a mistake.
-- rjbs
On Tue\, Feb 19\, 2013 at 4:54 PM\, Ricardo Signes \perl\.p5p@​rjbs\.manxome\.org wrote:
I don't remember how it came about. I assume it was the result of absent-minded refactoring\, where:
my $x = foo or die; return $x;
turned into
return foo or die;
Just for the record\, I've also seen
return foo() and bar(); # supposed to return TRUE only when both foo() and bar() are true
in the wild (can't remember where). Which I guess proves Tom's point that adding and/or didn't make things safer for beginners.
Cheers\, -Jan
On Wed\, Feb 20\, 2013 at 2:07 AM\, Jan Dubois \jand@​activestate\.com wrote:
in the wild (can't remember where). Which I guess proves Tom's point that adding and/or didn't make things safer for beginners.
People write bugs on any precedence level\, I don't think that that argument is convincing.
Leon
Leon Timmermans \fawaka@​gmail\.com wrote on Wed\, 20 Feb 2013 02:51:49 +0100:
On Wed\, Feb 20\, 2013 at 2:07 AM\, Jan Dubois \jand@​activestate\.com wrote:
in the wild (can't remember where). Which I guess proves Tom's point that adding and/or didn't make things safer for beginners.
People write bugs on any precedence level\, I don't think that that argument is convincing.
The argument is that the only people who do not make precedence mistakes are the people who use parentheses. You are not convinced?
Thinking that you -- and everybody else! -- always know\, remember\, and correctly apply all 24 (twenty-four) different precedence levels in all situations without using parens is not realistic\, and it leads to error.
1: left terms and list operators (leftward) 2: left -> 3: nonassoc ++ -- 4: right ** 5: right ! ~ \ and unary + and - 6: left =~ !~ 7: left * / % x 8: left + - . 9: left \<\< >> 10: nonassoc named unary operators 11: nonassoc \< > \<= >= lt gt le ge 12: nonassoc == != \<=> eq ne cmp ~~ 13: left & 14: left | ^ 15: left && 16: left || // 17: nonassoc .. ... 18: right ?: 19: right = += -= *= etc. 20: left \, => 21: nonassoc list operators (rightward) 22: right not 23: left and 24: left or xor
That is the reasoning behind "just use parens". Once you start doing that\, all of these precedence "problems" disappear. And so do the funny bits at the bottom of the table. :)
--tom
On Wed\, Feb 20\, 2013 at 4:11 AM\, Tom Christiansen \tchrist@​perl\.com wrote:
People write bugs on any precedence level\, I don't think that that argument is convincing.
The argument is that the only people who do not make precedence mistakes are the people who use parentheses. You are not convinced?
Indeed I am (not convinced) that ((parentheses) always (make code
(more readable))). Specially not when you're using multiple sets of
parentheses. The precedence levels are such that most of the time you
don't need any parentheses. IMHO they're extra noise in your code.
They add to the mental effort required for parsing. In case of C\
Thinking that you -- and everybody else! -- always know\, remember\, and correctly apply all 24 (twenty-four) different precedence levels in all situations without using parens is not realistic\, and it leads to error.
To me that sounds like optimizing writability over readability. Then again\, style discussions are worst then bike-shedding\, there is no ultimate truth\, etceteraā¦
Leon
On Tue\, Feb 19\, 2013 at 9:11 PM\, Tom Christiansen \tchrist@​perl\.com wrote:
21​: nonassoc list operators \(rightward\) 22​: right not 23​: left and 24​: left or xor
That is the reasoning behind "just use parens". Once you start doing that\, all of these precedence "problems" disappear. And so do the funny bits at the bottom of the table. :)
--tom
question:
would adding a 25th level for C\
answer:
yes\, that would break any code that was lazily written by inserting a
return or a die within a chain of C\
conclusion: it's a feature.
example: given a routine like so:
sub check_for_various_conditions{ critical_condition or severe_condition or medium_condition or mild_condition or 0 }
and the coder who wrote it doesn't want to care about mild conditions any more\, at least during the current phase of the development cycle.
Instead of commenting out the mild_condition line\, someone who proudly understands Perl precedence decides to show off and insert "return 0 or" above it.
Does p5p support that coder?
On 19.02.2013 19:25\, Tom Christiansen wrote:
There are uncountably many similar constructs that Perl does not warn on either\, like
$ perl \-cwe 'die "ABC"; die "XYZ"' \-e syntax OK $ perl \-cwe 'die\("ABC"\) || die\("XYZ"\)' \-e syntax OK $ perl \-cwe 'die\("ABC"\) && die\("XYZ"\)' \-e syntax OK
Add 'die die die "ABC"' and 'return return "XYZ"' to that list.
Hey\, die and return are idempotent. :-)
-- Lukas Mai \l\.mai@​web\.de
On Sun\, Mar 3\, 2013 at 12:01 AM\, Lukas Mai \l\.mai@​web\.de wrote:
On 19.02.2013 19:25\, Tom Christiansen wrote:
There are uncountably many similar constructs that Perl does not warn on either\, like
$ perl \-cwe 'die "ABC"; die "XYZ"' \-e syntax OK $ perl \-cwe 'die\("ABC"\) || die\("XYZ"\)' \-e syntax OK $ perl \-cwe 'die\("ABC"\) && die\("XYZ"\)' \-e syntax OK
Add 'die die die "ABC"' and 'return return "XYZ"' to that list.
Hey\, die and return are idempotent. :-)
BEGIN { *CORE::GLOBAL::die = sub { return unless $really_meant_it++; goto &CORE::die } } die die !!!111;
On 19 Feb 2013 at 17:07:05\, Jan Dubois wrote:
Just for the record\, I've also seen
return foo() and bar(); # supposed to return TRUE only when both foo() and bar() are true
I ran into this problem last night. The offending line was
return ($self->true_count($idx) > 0) xor ($self->false_count($idx) > 0);
and in this case it's easy to explain what I had in mind: I wanted my function to return the logical xor of those two expressions! Once I'd tracked the problem down it was easy enough to fix by reparenthesising\, but a warning would have been very useful.
Miles
On Tue Feb 19 02:54:23 2013\, nicholas wrote:
On Tue\, Feb 19\, 2013 at 11:44:21AM +0100\, Dr.Ruud wrote:
On 2013-02-19 03:03\, demerphq wrote:
Well\, Im not going to take it over\, but IMO:
return $x or die "...";
should warn or something.
So maybe the ticket should be marked stalled and not closed?
Indeed\, the requested change is to warn on popular kinds of unreachable code\, for example:
return ... or ...;
(subexpression that always causes control flow) [operator] ...
where ... is unreachable.
Or leave it to perlcritic/perltidy?
The code that we're suggesting should warn\, is buggy as written. I don't think that that's really a "tidy" issue\, or even a style issue.
To my mind\, it's a legitimate todo.
Nicholas Clark
Is there anyone actually willing to take on the job of creating a warning that will run when code is unreachable?
Thank you very much. Jim Keenan
Is there anyone actually willing to take on the job of creating a warning that will run when code is unreachable?
Thank you very much. Jim Keenan
Hi\,
Yes\, I have a prototype patch (see attached).
The warning is triggered in a couple of (cpan) modules included in perlcore. This list includes at least Test::Builder\, Parse::CPAN::Meta and JSON::PP. Each of these three modules have received a bug report[1].
I do also see a couple of test failures from applying the patch. From what I can tell\, they are all caused by the new warning showing up in the abovementioned modules. That said\, the new warning does not cause all tests triggering it to fail\, so I may I have missed a buggy module or two.
Thanks for considering\, ~Niels
PS: Please CC
[1] Test::Builder: https://github.com/schwern/test-more/pull/385
Parse::Cpan::Meta: https://rt.cpan.org/Ticket/Display.html?id=86947
JSON::PP: https://rt.cpan.org/Ticket/Display.html?id=86948
On Mon Jul 15 15:53:55 2013\, niels@thykier.net wrote:
Is there anyone actually willing to take on the job of creating a warning that will run when code is unreachable?
Thank you very much. Jim Keenan
Hi\,
Yes\, I have a prototype patch (see attached).
The warning is triggered in a couple of (cpan) modules included in perlcore. This list includes at least Test::Builder\, Parse::CPAN::Meta and JSON::PP. Each of these three modules have received a bug report[1].
I do also see a couple of test failures from applying the patch. From what I can tell\, they are all caused by the new warning showing up in the abovementioned modules. That said\, the new warning does not cause all tests triggering it to fail\, so I may I have missed a buggy module or two.
Thanks for considering\, ~Niels
Thank you. The patch looks good (though I havenāt tested it yet). I have one quibble\, though: perldiagās messages are in alphabetical order\, but the patch does not respect that.
--
Father Chrysostomos
On Mon Jul 15 18:20:35 2013\, sprout wrote:
On Mon Jul 15 15:53:55 2013\, niels@thykier.net wrote:
Is there anyone actually willing to take on the job of creating a warning that will run when code is unreachable?
Thank you very much. Jim Keenan
Hi\,
Yes\, I have a prototype patch (see attached).
The warning is triggered in a couple of (cpan) modules included in perlcore. This list includes at least Test::Builder\, Parse::CPAN::Meta and JSON::PP. Each of these three modules have received a bug report[1].
Thank you.
I do also see a couple of test failures from applying the patch. From what I can tell\, they are all caused by the new warning showing up in the abovementioned modules.
That is sufficient reason to wait until newer versions are released and then imported into perl.git before applying the patch.
That said\, the new warning does not cause all tests triggering it to fail\, so I may I have missed a buggy module or two.
Thanks for considering\, ~Niels
Thank you. The patch looks good (though I havenāt tested it yet). I have one quibble\, though: perldiagās messages are in alphabetical order\, but the patch does not respect that.
One more thing: This could be extended to other ops that donāt return\, such as last\, goto\, die\, etc.
--
Father Chrysostomos
On 2013-07-16 05:31\, Father Chrysostomos via RT wrote:
[...]
Thank you. The patch looks good (though I havenāt tested it yet). I have one quibble\, though: perldiagās messages are in alphabetical order\, but the patch does not respect that.
Here is a revised patch.
The warning has been inserted in its proper place in perldiag.pod.
One more thing: This could be extended to other ops that donāt return\, such as last\, goto\, die\, etc.
The warning is now emitted for any of the following ops: OP_{NEXT\,LAST\,REDO\,RETURN\,DIE\,EXIT\,GOTO}
I noticed and have documented that some possible intended uses of this precedence. An example being:
use constant FEATURE => 0; sub { not FEATURE and return or do_feature(); }
perl correctly parses this as:
sub { (not FEATURE and return) or do_feature(); }
but it still causes a warning. I am not sure how to detect this case\, so I am not able to devise a fix for this false-positive (other than recommending that people add the parentheses themselves to express the intended grouping).
The problem in detecting this is that by the time the warning is emitted\, constant folding has eliminated the context. The steps are (something like):
Start with "not FEATURE # and return or do_feature();". (The "#" denotes the part not parsed/examined yet).
Fold "not FEATURE" to 1 leaving "1 # and return or do_feature();"
Parse/Consume "and return" giving "1 and return # or do_feature();"
Reduce "1 and return #or do_feature();" to "return # or do_feature();"
Parse/Consume "or do_feature();" giving "return or do_feature();"
Warn as "return" is on the left-hand side of a (logical) binary operator (and is not enclosed in parentheses).
~Niels
On 2013-07-16 05:31\, Father Chrysostomos via RT wrote:
[...]
I do also see a couple of test failures from applying the patch. From what I can tell\, they are all caused by the new warning showing up in the abovementioned modules.
That is sufficient reason to wait until newer versions are released and then imported into perl.git before applying the patch.
Certainly.
That said\, the new warning does not cause all tests triggering it to fail\, so I may I have missed a buggy module or two.
Thanks for considering\, ~Niels
Thank you. The patch looks good (though I havenāt tested it yet). I have one quibble\, though: perldiagās messages are in alphabetical order\, but the patch does not respect that.
One more thing: This could be extended to other ops that donāt return\, such as last\, goto\, die\, etc.
Seems reasonable\, will look into it.
[From previous mail]
Thank you. The patch looks good (though I havenāt tested it yet). I have one quibble\, though: perldiagās messages are in alphabetical order\, but the patch does not respect that.
Ah\, haven't noticed that. Will fix that in the next revision.
~Niels
On Tue Jul 16 13:35:28 2013\, niels@thykier.net wrote:
On 2013-07-16 05:31\, Father Chrysostomos via RT wrote:
[...]
Thank you. The patch looks good (though I havenāt tested it yet). I have one quibble\, though: perldiagās messages are in alphabetical order\, but the patch does not respect that.
Here is a revised patch.
The warning has been inserted in its proper place in perldiag.pod.
One more thing: This could be extended to other ops that donāt return\, such as last\, goto\, die\, etc.
The warning is now emitted for any of the following ops: OP_{NEXT\,LAST\,REDO\,RETURN\,DIE\,EXIT\,GOTO}
I noticed and have documented that some possible intended uses of this precedence. An example being:
use constant FEATURE => 0; sub \{ not FEATURE and return or do\_feature\(\); \}
perl correctly parses this as:
sub \{ \(not FEATURE and return\) or do\_feature\(\); \}
but it still causes a warning. I am not sure how to detect this case\, so I am not able to devise a fix for this false-positive (other than recommending that people add the parentheses themselves to express the intended grouping).
I have an idea: We have three spare bits in op->op_spare. We could use one of them to indicate folding.
We already set OPf_SPECIAL on regexp and tr operators in S_new_logop and newCONDOP to indicate this. We also set OPpCONST_FOLDED on constants in those two places.
If we also set\, say\, op->op_folded to 1 in those same code paths\, then we can detect the fold.
(And we could simplify the code by having match-like ops and constants us the same bit.)
--
Father Chrysostomos
On 2013-07-17 00:54\, Father Chrysostomos via RT wrote:
I have an idea: We have three spare bits in op->op_spare. We could use one of them to indicate folding.
That would be possible\, indeed.
We already set OPf_SPECIAL on regexp and tr operators in S_new_logop and newCONDOP to indicate this. We also set OPpCONST_FOLDED on constants in those two places.
If we also set\, say\, op->op_folded to 1 in those same code paths\, then we can detect the fold.
(And we could simplify the code by having match-like ops and constants us the same bit.)
You wrote "also set op->op_folded" and later imply the change could simplify the code by using the same bit. Just to confirm here\, did you want the "op_folded" member replace OPpCONST_FOLDED and (for regexp/tr operators) OPf_SPECIAL? And if so\, should OPf_SPECIAL still be set in these cases since it is a public flag or should it be cleared so we can repurpose it?
~Niels
On Wed Jul 17 01:30:56 2013\, niels@thykier.net wrote:
On 2013-07-17 00:54\, Father Chrysostomos via RT wrote:
I have an idea: We have three spare bits in op->op_spare. We could use one of them to indicate folding.
That would be possible\, indeed.
We already set OPf_SPECIAL on regexp and tr operators in S_new_logop and newCONDOP to indicate this. We also set OPpCONST_FOLDED on constants in those two places.
If we also set\, say\, op->op_folded to 1 in those same code paths\, then we can detect the fold.
(And we could simplify the code by having match-like ops and constants us the same bit.)
You wrote "also set op->op_folded" and later imply the change could simplify the code by using the same bit. Just to confirm here\, did you want the "op_folded" member replace OPpCONST_FOLDED and (for regexp/tr operators) OPf_SPECIAL?
That would be nice. But it is not a prerequisite for getting your patch in. :-)
And if so\, should OPf_SPECIAL still be set in these cases since it is a public flag or should it be cleared so we can repurpose it?
I think (off the top of my head) that B::Deparse currently uses it. So B::Deparse may have to change. I donāt remember now.
OPf_SPECIAL is a private flag\, at least in terms of its use\, but stored with the public flags.
--
Father Chrysostomos
On 2013-07-17 18:56\, Father Chrysostomos via RT wrote:
On Wed Jul 17 01:30:56 2013\, niels@thykier.net wrote:
[...]
You wrote "also set op->op_folded" and later imply the change could simplify the code by using the same bit. Just to confirm here\, did you want the "op_folded" member replace OPpCONST_FOLDED and (for regexp/tr operators) OPf_SPECIAL?
That would be nice. But it is not a prerequisite for getting your patch in. :-)
And if so\, should OPf_SPECIAL still be set in these cases since it is a public flag or should it be cleared so we can repurpose it?
I think (off the top of my head) that B::Deparse currently uses it. So B::Deparse may have to change. I donāt remember now.
OPf_SPECIAL is a private flag\, at least in terms of its use\, but stored with the public flags.
Included is a patch to add the op_folded member and my previous patch has been rebased on top of it.
RE: op_folded-patch - I made OPpCONST_FOLDED redundant internally in the code (i.e. perl does the right thing even without it). However\, I left it in (and kept it maintained) for now until B::Concise can be updated. I presume that will involve exposing op_folded via B.pm\, which I have left as "future work" for now. I made no attempt to have it replace OPf_SPECIAL on the regexp/tr operators either.
~Niels
On Wed Jul 17 12:37:07 2013\, niels@thykier.net wrote:
On 2013-07-17 18:56\, Father Chrysostomos via RT wrote:
On Wed Jul 17 01:30:56 2013\, niels@thykier.net wrote:
[...]
You wrote "also set op->op_folded" and later imply the change could simplify the code by using the same bit. Just to confirm here\, did you want the "op_folded" member replace OPpCONST_FOLDED and (for regexp/tr operators) OPf_SPECIAL?
That would be nice. But it is not a prerequisite for getting your patch in. :-)
And if so\, should OPf_SPECIAL still be set in these cases since it is a public flag or should it be cleared so we can repurpose it?
I think (off the top of my head) that B::Deparse currently uses it. So B::Deparse may have to change. I donāt remember now.
OPf_SPECIAL is a private flag\, at least in terms of its use\, but stored with the public flags.
Included is a patch to add the op_folded member and my previous patch has been rebased on top of it.
RE: op_folded-patch - I made OPpCONST_FOLDED redundant internally in the code (i.e. perl does the right thing even without it). However\, I left it in (and kept it maintained) for now until B::Concise can be updated. I presume that will involve exposing op_folded via B.pm\, which I have left as "future work" for now. I made no attempt to have it replace OPf_SPECIAL on the regexp/tr operators either.
Those patches look good. Thank you. Now we just have to wait for Parse::CPAN::Meta.
--
Father Chrysostomos
Migrated from rt.perl.org#59802 (status was 'resolved')
Searchable as RT59802$