plpgsql unreachable code (was BUG #1329: Bug in IF-ELSEIF-ELSE construct)

Neil Conway <neilc@samurai.com>

From: Neil Conway <neilc@samurai.com>
To: PostgreSQL-patches <pgsql-patches@postgresql.org>
Cc: Tom Lane <tgl@sss.pgh.pa.us>, Rico Wind <rw@rico-wind.dk>, pgsql-bugs@postgresql.org
Date: 2004-11-27T13:56:31Z
Lists: pgsql-bugs

Attachments

Neil Conway wrote:
> (BTW, another thing this example exposes is that we don't issue warnings 
> about trivially-dead-code, such as statements in a basic block that 
> follow a RETURN. This would probably be also worth doing.)

Attached is a patch that implements this. Specifically, if there are any 
statements in the same block that follow a RETURN, EXIT (without 
condition) or RAISE EXCEPTION statement, we issue a warning at CREATE 
FUNCTION time:

create function exit_warn() returns int as $$
declare x int;
begin
     x := 5;
     loop
         x := x + 1;
         exit;
         x := x + 2;
     end loop;
     x := x + 3;
     return x;
end;$$ language 'plpgsql';
WARNING:  assignment is unreachable, due to exit near line 6
CONTEXT:  compile of PL/pgSQL function "exit_warn" near line 7

No warning is issued if check_function_bodies is false.

AFAICS there is no current infrastructure for walking a PL/PgSQL 
function's parse tree, so I did this manually (which is easy enough, of 
course). In the future it might be a good idea to refactor this to use 
something akin to the "walker" infrastructure in the backend (for one 
thing the PL/PgSQL function dumping code could use this as well).

(BTW this patch is intended for 8.1, of course.)

-Neil