https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116277

            Bug ID: 116277
           Summary: "may be used uninitialized
                    [-Werror=maybe-uninitialized]" instead of
                    -Werror=dangling-reference
           Product: gcc
           Version: 14.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: f.heckenb...@fh-soft.de
  Target Milestone: ---

% cat test.cpp 
#include <vector>
#include <algorithm>

auto C ()
{
  auto Reverse = false;
  return [&] (auto a, auto b) { return Reverse ? b < a : a < b; };
}

void f ()
{
  std::vector <int> r { 0, 1 };
  std::sort (begin (r), end (r), C ());
}
% g++ -O3 -std=c++23 -Wshadow -Wall -Werror -Werror=dangling-reference
In lambda function,
    inlined from 'constexpr bool
__gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2)
[with _Iterator1 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >;
_Iterator2 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare =
C()::<lambda(auto:9, auto:10)>]' at
/opt/compiler-explorer/gcc-14.1.0/include/c++/14.1.0/bits/predefined_ops.h:158:30,
    inlined from 'constexpr void std::__insertion_sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator =
__gnu_cxx::__normal_iterator<int*, vector<int> >; _Compare =
__gnu_cxx::__ops::_Iter_comp_iter<C()::<lambda(auto:9, auto:10)> >]' at
/opt/compiler-explorer/gcc-14.1.0/include/c++/14.1.0/bits/stl_algo.h:1777:14,
    inlined from 'constexpr void
std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator,
_Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*,
vector<int> >; _Compare =
__gnu_cxx::__ops::_Iter_comp_iter<C()::<lambda(auto:9, auto:10)> >]' at
/opt/compiler-explorer/gcc-14.1.0/include/c++/14.1.0/bits/stl_algo.h:1822:23,
    inlined from 'constexpr void std::__sort(_RandomAccessIterator,
_RandomAccessIterator, _Compare) [with _RandomAccessIterator =
__gnu_cxx::__normal_iterator<int*, vector<int> >; _Compare =
__gnu_cxx::__ops::_Iter_comp_iter<C()::<lambda(auto:9, auto:10)> >]' at
/opt/compiler-explorer/gcc-14.1.0/include/c++/14.1.0/bits/stl_algo.h:1908:31,
    inlined from 'constexpr void std::sort(_RAIter, _RAIter, _Compare) [with
_RAIter = __gnu_cxx::__normal_iterator<int*, vector<int> >; _Compare =
C()::<lambda(auto:9, auto:10)>]' at
/opt/compiler-explorer/gcc-14.1.0/include/c++/14.1.0/bits/stl_algo.h:4804:18,
    inlined from 'void f()' at <source>:13:13:
<source>:7:48: error: 'Reverse' is used uninitialized [-Werror=uninitialized]
    7 |   return [&] (auto a, auto b) { return Reverse ? b < a : a < b; };
      |                                        ~~~~~~~~^~~~~~~~~~~~~~~
<source>: In function 'void f()':
<source>:6:8: note: 'Reverse' declared here
    6 |   auto Reverse = false;
      |        ^~~~~~~
cc1plus: all warnings being treated as errors
Compiler returned: 1

There is a bug in the code: Reverse is a dangling reference. But the message
doesn't make this clear (and ironically claims it's uninitialized while quoting
its initialization).

Reply via email to