Fix -Wshadow=local warnings

Started by Peter Eisentraut19 days ago4 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

appliessuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t253638
psql -h localhost -U postgres

Built from patchset v4 (message #4), September 20, 2026 at 09:49 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t253638_4 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t253638_4 && git checkout t253638_4

Patchset v4 (message #4) is on t253638_4

Jump to latest
#1Peter Eisentraut
peter_e@gmx.net

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.

Attachments:

t253638_1
0001-Fix-Wshadow-local-warnings.patchtext/plain; charset=UTF-8; name=0001-Fix-Wshadow-local-warnings.patchDownload+392-382
0002-Use-isystem-for-LLVM-include-directories.patchtext/plain; charset=UTF-8; name=0002-Use-isystem-for-LLVM-include-directories.patchDownload+14-6
0003-Use-warning-option-Wshadow-local.patchtext/plain; charset=UTF-8; name=0003-Use-warning-option-Wshadow-local.patchDownload+23-24
In reply to: Peter Eisentraut (#1)
Re: Fix -Wshadow=local warnings

On Tue, Sep 1, 2026 at 10:59 AM Peter Eisentraut <peter@eisentraut.org> wrote:

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.)

+1 to fixing all of these, and to using -Wshadow=compatible-local.

--
Peter Geoghegan

#3Chao Li
li.evan.chao@gmail.com
In reply to: Peter Eisentraut (#1)
Re: Fix -Wshadow=local warnings

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/

#4Peter Eisentraut
peter_e@gmx.net
In reply to: Chao Li (#3)
Re: Fix -Wshadow=local warnings

On 02.09.26 07:19, Chao Li wrote:

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.

Thanks, the patch with the code changes has been committed, with your
suggestions incorporated.

The patch to activate the option couldn't be committed yet because the
workaround for the LLVM headers didn't work and had to be reverted
(commit 7a0aa99e51c). I have worked on a new solution that works more
along the lines of how other per-file or per-module "-Wno-..." options
are handled. I think this will be simpler and less fragile. See
attached patch.

Attachments:

t253638_4
v2-0001-Use-warning-option-Wshadow-local.patchtext/plain; charset=UTF-8; name=v2-0001-Use-warning-option-Wshadow-local.patchDownload+127-93