From: Chao Li <li.evan.chao@gmail.com>
To: Peter Eisentraut <peter@eisentraut.org>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: Re: Fix -Wshadow=local warnings
Date: Wed, 2 Sep 2026 13:19:36 +0800
Message-ID: <12D9618F-F1FF-41E4-8B9E-55E8DDADAFBD@gmail.com> (raw)
In-Reply-To: <84b6f128-91f5-480e-8a9e-7d0e8f538cea@eisentraut.org>
References: <84b6f128-91f5-480e-8a9e-7d0e8f538cea@eisentraut.org>
> On Sep 1, 2026, at 22:58, Peter Eisentraut <peter@eisentraut.org> wrote:
>
> I bumped into some variable shadowings that to my slight surprise the current warning option -Wshadow=compatible-local does not catch. For example
>
> const char *p;
> char *p;
>
> or
>
> bool skipped;
> int64 skipped;
>
> These are not "compatible" in the technical C language sense, but they are mutually assignable, so IMO just as confusing and fragile.
>
> Also, there are things like
>
> EState *estate;
> ExprState *estate;
>
> which are not mutually assignable, but almost as dangerous given the propensity to cast node types around.
>
> These can be caught if we dial up the warning one notch to -Wshadow=local. This then flags all shadowing of a local variable by another local variable. I have fixed all the warnings in the attached patch. I think everything this catches is obviously bad, so this seems well worth fixing. (And if we buy into the idea of -Wshadow=compatible-local, then this is obviously better and more complete.)
>
> So the first patch fixes all the warnings, but doesn't turn up the compiler flag yet. There is a hiccup with the LLVM headers, because they themselves trigger these warnings. So the second patch provides a workaround to silence warnings from those headers. It's a bit different from what we have done before, but I think it works better for this case. Alternative ideas welcome. In the third patch, the warning option is then changed.
> <0001-Fix-Wshadow-local-warnings.patch><0002-Use-isystem-for-LLVM-include-directories.patch><0003-Use-warning-option-Wshadow-local.patch>
I had a patch to fix all warnings from -Wshadow-all, and 0001 seems to be a subset of that patch. My patch was not accepted due to a concern about adding burden to future back-patching work. Anyway, +1 from my side for fixing these warnings.
A few small comments:
1 - 0001 - dependencies.c
```
* expression into *expr.
*/
static bool
-dependency_is_compatible_expression(Node *clause, Index relid, List *statlist, Node **expr)
+dependency_is_compatible_expression(Node *clause, Index relid, List *statlist, Node **stat_expr_p)
```
As “expr” is renamed, the function header comment needs to be updated as well.
2 - 0001 - pg_constraint.c
```
+ CookedConstraint *cooked_constr;
```
In the current RelationGetNotNullConstraints(), other local variables use camelCase naming, for example constrRel, so maybe it would be better to keep the naming style consistent.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: li.evan.chao@gmail.com, peter@eisentraut.org
Subject: Re: Fix -Wshadow=local warnings
In-Reply-To: <12D9618F-F1FF-41E4-8B9E-55E8DDADAFBD@gmail.com>
References: <84b6f128-91f5-480e-8a9e-7d0e8f538cea@eisentraut.org> <12D9618F-F1FF-41E4-8B9E-55E8DDADAFBD@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox