https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123651
Bug ID: 123651
Summary: `-g2` triggers `-Wuninitialized` on internal SRA temp
(`SR.*`), not reproducible with `-g1`
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: teodor_spaeren at riseup dot net
Target Milestone: ---
So I don't know if this is multiple bugs or just one, so please feel free to
split this up into two tickets.
The problem is that when compiling with "-g2", I get a warning about an
internal SR: 'SR.5' is used uninitialized [-Wuninitialized].
There is two things I think are a bug:
1. The user should not be exposed to the internal SRA temp variable
2. There shouldn't be a difference if you are using -g1 or -g2 for if it's
produced or not.
I first encountered this as a warning inside of `std::shared_ptr` in a project
at work, and I'm still trying to create a stand alone example,
that shows it using the `std::shared_ptr` type, but for now, I have been able
to create this reproducer:
> void use(int *);
>
> struct Count {
> int *p;
> ~Count() { use(p); }
> Count(Count &) {}
> };
>
> struct Ptr {
> int x;
> Count c;
> };
>
> void sink(int, Ptr) {}
>
> struct Holder {
> template <typename T> void put(T v) { sink(v, ref_); }
> Ptr ref_;
> };
>
> void test(Holder h) { h.put(0); }
I've tried to do some digging on what could be the reason, with the help of a
LLM, and the takeaways where this:
## Key toggles
- `-g2 -fno-var-tracking` or `-g2 -fno-var-tracking-assignments` suppresses the
warning.
- `-g2 -fno-tree-sra` suppresses the warning.
## Likely interaction in GCC 15.2.0
- `MAY_HAVE_DEBUG_BIND_STMTS` is `flag_var_tracking_assignments`
(`gcc/tree.h:1311-1316`).
- `-g2` enables var‑tracking/var‑tracking‑assignments
(`gcc/opts.cc:1430-1467`).
- SRA creates `SR.*` replacements and explicitly mentions uninit warnings
(`gcc/tree-sra.cc:2545-2577`).
- With `MAY_HAVE_DEBUG_BIND_STMTS`, SRA performs debug replacements
(`gcc/tree-sra.cc:2963-2977`).
- `warn_uninit` uses `SSA_NAME_VAR` for diagnostics
(`gcc/tree-ssa-uninit.cc:138-205`).
## Hypothesis
`-g2` enables var‑tracking‑assignments (debug bind stmts) which causes SRA to
create additional replacements; the late uninit pass then diagnoses an
uninitialized SRA temp (`SR.*`) instead of a user variable. This makes debug
level affect diagnostics and exposes internal names.
Feel free to totally disregard this as AI slop!
Here is the output of my gcc version and the error message it gives:
> Reading specs from
> /data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../lib/gcc/>
> x86_64-conda-linux-gnu/15.2.0/specs
> Reading specs from
> /data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../lib/gcc/>
> x86_64-conda-linux-gnu/15.2.0/conda.specs
> COLLECT_GCC=g++
> Target: x86_64-conda-linux-gnu
> Configured with: ../configure
> --prefix=/home/conda/feedstock_root/build_artifacts/>
> gcc_compilers_1765252935691/>
> _h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pla>
>
> cehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho
> >
> --with-slibdir=/home/conda/feedstock_root/build_artifacts/gcc_compilers_1765252935691/>
>
> _h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pla>
>
> cehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib
> --libdir=/>
> home/conda/feedstock_root/build_artifacts/gcc_compilers_1765252935691/>
> _h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pla>
>
> cehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib
> --mandir=/>
> home/conda/feedstock_root/build_artifacts/gcc_compilers_1765252935691/>
> _h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pla>
>
> cehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/man
> > --build=x86_64-conda-linux-gnu --host=x86_64-conda-linux-gnu
> --target=x86_64-conda-linux-gnu >
> --enable-languages=c,c++,fortran,objc,obj-c++ --enable-libgomp
> --disable-libssp > --enable-libquadmath --enable-libquadmath-support
> --disable-nls --disable-bootstrap > --disable-multilib --enable-long-long
> --without-zstd --with-native-system-header-dir=/usr/include >
> --with-gxx-include-dir=/home/conda/feedstock_root/build_artifacts/gcc_compilers_1765252935691/>
>
> _h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pla>
>
> cehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/gcc/>
> x86_64-conda-linux-gnu/15.2.0/include/c++
> --with-gxx-libcxx-include-dir=/home/conda/feedstock_root/>
> build_artifacts/gcc_compilers_1765252935691/>
> _h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pla>
>
> cehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/gcc/>
> x86_64-conda-linux-gnu/15.2.0/../../../../include/c++/v1
> --with-pkgversion='conda-forge gcc 15.2.> 0-16'
> --with-bugurl=https://github.com/conda-forge/ctng-compilers-feedstock/issues/new/choose
> > --enable-libsanitizer --enable-default-pie --enable-threads=posix
> --enable-__cxa_atexit >
> --with-sysroot=/home/conda/feedstock_root/build_artifacts/gcc_compilers_1765252935691/>
>
> _h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pla>
>
> cehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/>
> x86_64-conda-linux-gnu/sysroot
> --with-build-sysroot=/home/conda/feedstock_root/build_artifacts/>
> gcc_compilers_1765252935691/_build_env/x86_64-conda-linux-gnu/sysroot
> --enable-plugin --enable-lto
> Thread model: posix
> Supported LTO compression algorithms: zlib
> gcc version 15.2.0 (conda-forge gcc 15.2.0-16)
> COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-std=gnu++23' '-Wall' '-Wextra'
> '-c' '-g2' > '-shared-libgcc' '-mtune=generic' '-march=x86-64'
>
> /data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../libexec/gcc/>
> x86_64-conda-linux-gnu/15.2.0/cc1plus -fpreprocessed ../minimal.reduced.i
> -quiet -dumpbase minimal.> reduced.i -dumpbase-ext .i -mtune=generic
> -march=x86-64 -g2 -O3 -Wall -Wextra -std=gnu++23 > -version -idirafter
> /data/home/tspaere/misc/before-submitting/.pixi/envs/default/include -o >
> minimal.reduced.s
> GNU C++23 (conda-forge gcc 15.2.0-16) version 15.2.0 (x86_64-conda-linux-gnu)
> compiled by GNU C version 15.2.0, GMP version 6.2.1, MPFR version
> 4.1.0, MPC version 1.2.1, > isl version isl-0.24-GMP
>
> GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
> Compiler executable checksum: 8bfa1ec1c3cf8dd82758fc8ec897dad3
> In destructor 'Count::~Count()',
> inlined from 'Ptr::~Ptr()' at ../minimal.reduced.i:9:8,
> inlined from 'void Holder::put(T) [with T = int]' at
> ../minimal.reduced.i:17:45,
> inlined from 'void test(Holder)' at ../minimal.reduced.i:21:28:
> ../minimal.reduced.i:5:17: warning: 'SR.5' is used uninitialized
> [-Wuninitialized]
> 5 | ~Count() { use(p); }
> | ~~~^~~
> ../minimal.reduced.i: In function 'void test(Holder)':
> ../minimal.reduced.i:17:45: note: 'SR.5' was declared here
> 17 | template <typename T> void put(T v) { sink(v, ref_); }
> | ~~~~^~~~~~~~~
> COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-std=gnu++23' '-Wall' '-Wextra'
> '-c' '-g2' > '-shared-libgcc' '-mtune=generic' '-march=x86-64'
>
> /data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../lib/gcc/x86_64-conda-linux-gnu/>
> 15.2.0/../../../../x86_64-conda-linux-gnu/bin/as -v --gdwarf-5 --64 -o
> minimal.reduced.o minimal.> reduced.s
> GNU assembler version 2.45 (x86_64-conda-linux-gnu) using BFD version (GNU
> Binutils) 2.45
> COMPILER_PATH=/data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../libexec/gcc/>
>
> x86_64-conda-linux-gnu/15.2.0/:/data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../>
>
> libexec/gcc/:/data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../lib/gcc/>
> x86_64-conda-linux-gnu/15.2.0/../../../../x86_64-conda-linux-gnu/bin/
> LIBRARY_PATH=/data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../lib/gcc/>
>
> x86_64-conda-linux-gnu/15.2.0/:/data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../>
>
> lib/gcc/:/data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../lib/gcc/>
>
> x86_64-conda-linux-gnu/15.2.0/../../../../x86_64-conda-linux-gnu/lib/../lib/:/data/home/tspaere/>
>
> misc/before-submitting/.pixi/envs/default/bin/../lib/gcc/x86_64-conda-linux-gnu/15.2.0/../../../../>
>
> lib/:/data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../x86_64-conda-linux-gnu/>
>
> sysroot/lib/../lib/:/data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../>
>
> x86_64-conda-linux-gnu/sysroot/usr/lib/../lib/:/data/home/tspaere/misc/before-submitting/.pixi/envs/>
>
> default/bin/../lib/gcc/x86_64-conda-linux-gnu/15.2.0/../../../../x86_64-conda-linux-gnu/lib/:/data/>
>
> home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../lib/gcc/x86_64-conda-linux-gnu/15.2.>
>
> 0/../../../:/data/home/tspaere/misc/before-submitting/.pixi/envs/default/bin/../>
>
> x86_64-conda-linux-gnu/sysroot/lib/:/data/home/tspaere/misc/before-submitting/.pixi/envs/default/>
> bin/../x86_64-conda-linux-gnu/sysroot/usr/lib/
> COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-std=gnu++23' '-Wall' '-Wextra'
> '-c' '-g2' > '-shared-libgcc' '-mtune=generic' '-march=x86-64'
Best regards,
Teodor Spæren