Change copyObject() to use typeof_unqual

Peter Eisentraut <peter@eisentraut.org>

Commit: 59292f7aac72305f8e47f19155e6c7e0a070acb9
Author: Peter Eisentraut <peter@eisentraut.org>
Date: 2026-03-13T06:06:57Z
Change copyObject() to use typeof_unqual

Currently, when the argument of copyObject() is const-qualified, the
return type is also, because the use of typeof carries over all the
qualifiers.  This is incorrect, since the point of copyObject() is to
make a copy to mutate.  But apparently no code ran into it.

The new implementation uses typeof_unqual, which drops the qualifiers,
making this work correctly.

typeof_unqual is standardized in C23, but all recent versions of all
the usual compilers support it even in non-C23 mode, at least as
__typeof_unqual__.  We add a configure/meson test for typeof_unqual
and __typeof_unqual__ and use it if it's available, else we use the
existing fallback of just returning void *.

This is the second attempt, after the first attempt in commit
4cfce4e62c8 was reverted.  The following two points address problems
with the earlier version:

We test the underscore variant first so that there is a higher chance
that clang used for bitcode also supports it, since we don't test that
separately.

Unlike the typeof test, the typeof_unqual test also tests with a void
pointer similar to how copyObject() would use it, because that is not
handled by MSVC, so we want the test to fail there.

Reviewed-by: David Geier <geidav.pg@gmail.com>
Reviewed-by: Jelte Fennema-Nio <postgres@jeltef.nl>
Discussion: https://www.postgresql.org/message-id/flat/92f9750f-c7f6-42d8-9a4a-85a3cbe808f3%40eisentraut.org

Files

PathChange+/−
config/c-compiler.m4 modified +65 −0
configure modified +108 −0
configure.ac modified +2 −0
meson.build modified +62 −0
src/include/c.h modified +12 −0
src/include/nodes/nodes.h modified +2 −2
src/include/pg_config.h.in modified +14 −0
src/test/modules/test_cplusplusext/test_cplusplusext.cpp modified +2 −1

Discussion