Bug 5 – false warning: no return at end of function

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
PowerPC
OS
Mac OS X
Creation time
2006-02-18T14:48:00Z
Last change time
2014-02-15T02:09:59Z
Assigned to
bugzilla
Creator
braddr

Attachments

IDFilenameSummaryContent-TypeSize
5bug5.diffChange CompoundStatement and TryFinallyStatement.text/plain1132

Comments

Comment #0 by braddr — 2006-02-18T14:48:45Z
==== bug1.d ==== class F { } int foo() { auto F f = new F(); // comment out and warning goes away return 0; } ==== $ gdc -g -Wall -c bug1.d warning - bug1.d:2: function bug1.foo no return at end of function On a hunch.. ==== bug2.d ==== int foo() { try { return 0; } finally { } } ==== $ gdc -g -Wall -c bug2.d warning - bug2.d:1: function bug2.foo no return at end of function Testing with dmd 0.146 shows that it exhibits the same bug.
Comment #1 by braddr — 2006-02-18T14:50:10Z
I think I've found a fix for the problem: 2494 int TryFinallyStatement::fallOffEnd() 2495 { int result; 2496 2497 result = body->fallOffEnd(); 2498 if (finalbody) >>>2499 result &= finalbody->fallOffEnd(); 2500 return result; 2501 } line 2499 originally lacked the &. Looking at other ::fallOffEnd methods, a few of them seem questionable, but I haven't thought through them enough to say that there are bugs. A quick scan without having constructed test cases: Statement::fallOffEnd -- should that also be falloff &= s->fallOffEnd(); so that once it's determined that it _doesn't_ fall off, that it keeps track of that? WhileStatement::fallOffEnd DoStatement::fallOffEnd ForStatement::fallOffEnd ForeachStatement::fallOffEnd SwitchStatement::fallOffEnd -- they don't all fall off the end, shouldn't it be: >>return<< body->fallOffEnd() -- in While and Do forms, can body be null? For[each] has a check but the others don't.
Comment #2 by bugs-d — 2006-03-18T19:20:09Z
The code for loops is always returning true for cases like this: int foo() { int[] a; foreach (int v; a) { return 1; } } Which, in this case, is an error (a is empty.) As I recall, this is similar to examples Walter gave of why he dislikes warnings. It's still calling it's insides' fallOffEnd() for "statement is not reachable" warnings. It appears the intention of the code in CompoundStatement's fallOffEnd() seems to be not to show a zillion "statement is not reachable" messages. But, it is the cause of why this says "no return at end of function": int foo() { bar(); assert(false); bar(); } It seems to me that TryFinallyStatement should indeed be changed as you suggest. I'm going to attach a patch because I think they are much more expressive for communication between programmers than words are. Thanks, -[Unknown]
Comment #3 by bugs-d — 2006-03-18T19:27:49Z
Created attachment 5 Change CompoundStatement and TryFinallyStatement. This patch: 1. Changes the behavior of CompoundStatement so that, if there is ever a child statement which does fall off, it does not issue a "no return" warning. 2. Keeps the existing functionality of showing a "statement not reachable" warning only once per early return. 3. Changes TryFinallyStatement so that the finally may fall off so long as the try does not. -[Unknown]
Comment #4 by bugzilla — 2006-03-19T02:05:52Z
Fixed 0.150.