Re: Use an enum for RELKIND_*?

From: Andres Freund <andres(at)anarazel(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Kyotaro HORIGUCHI <horiguchi(dot)kyotaro(at)lab(dot)ntt(dot)co(dot)jp>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Use an enum for RELKIND_*?
Date: 2018-12-19 04:35:16
Message-ID: 20181219043516.z7x57tdi3uvomxvj@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On 2018-12-18 23:17:54 -0500, Tom Lane wrote:
> Andres Freund <andres(at)anarazel(dot)de> writes:
> > It'd be nice if there were an easy way to write a switch() where the
> > compiler enforces that all enum values are checked, but still had the
> > possibility to have a 'default:' block for error checking... I can't
> > quite come up with a good approach to emulate that though.
>
> Yeah, that would sure make things better. I was considering
> something like
>
> switch (enumvalue)
> {
> case A: ...
> case B: ...
> ...
>
> #ifndef USE_ASSERT_CHECKING
> default:
> elog(ERROR, ...);
> #endif
> }
>
> so that you get the runtime protection in production builds but not
> debug builds ... except that yes, you really want that protection in
> debug builds too. Maybe the #if could be on some other symbol so that
> the default: is normally enabled in all builds, but we have some
> lonely buildfarm animal that disables it and builds with -Werror to
> get our attention for omitted cases?

Yea, that's the best I can come up with too. I think we'd probably want
to have the warning available during development mainly, so I'm not sure
the "lonely buildfarm animal" approach buys us enough.

There's -Wswitch-enum which causes a warning to emitted for missing enum
cases even if there's default:. Obviously that's not generally usable,
because there's a lot of cases where intentionally do not want to be
exhaustive. We could just cast to int in those, or locally disable the
warnings, but neither sounds particularly enticing...

Greetings,

Andres Freund

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2018-12-19 04:53:28 Re: pg_dumpall --exclude-database option
Previous Message Tom Lane 2018-12-19 04:17:54 Re: Use an enum for RELKIND_*?