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

From: Neil Conway <neilc(at)samurai(dot)com>
To: PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Rico Wind <rw(at)rico-wind(dot)dk>, pgsql-bugs(at)postgresql(dot)org
Subject: plpgsql unreachable code (was BUG #1329: Bug in IF-ELSEIF-ELSE construct)
Date: 2004-11-27 13:56:31
Message-ID: 41A8878F.3020808@samurai.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-patches

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

Attachment Content-Type Size
plpgsql-unreachable-1.patch text/plain 10.8 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Andrew - Supernews 2004-11-27 14:24:40 bug in information_schema?
Previous Message Honza Rameš 2004-11-27 11:44:41 WinXP bug

Browse pgsql-patches by date

  From Date Subject
Next Message Tom Lane 2004-11-27 17:43:53 Re: plpgsql unreachable code (was BUG #1329: Bug in IF-ELSEIF-ELSE construct)
Previous Message Neil Conway 2004-11-27 06:17:40 Re: BUG #1329: Bug in IF-ELSEIF-ELSE construct