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

            Bug ID: 124198
           Summary: nested template-for generating warnings for
                    unused-but-set-variable
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: uchanahome8 at gmail dot com
  Target Milestone: ---

Hi, I am using the gcc-trunk on compiler explorer:
https://godbolt.org/z/E8cWTs13d and was able to reproduce this on local build
of master branch as well.

This shouldn't have triggered the warning for unused-variable `J`.

Code:
```
#include <iostream>
#include <utility>
#include <array>

template <std::size_t start, std::size_t end>
consteval auto make_array() {
    std::array<std::size_t, end - start> arr;
    for (std::size_t i = start; i < end; ++i)
        arr[i - start] = i;
    return arr;
}

template <int... vals>
constexpr bool are_same() {
    template for (constexpr auto I: make_array<0, sizeof...(vals)>()) {
        template for (constexpr auto J: make_array<I + 1, sizeof...(vals)>()) {
            if constexpr (vals...[I] == vals...[J])
                return true;
        }
    }
    return false;
}

int main() {
    std::cout << are_same<1,2,3>();
    return 0;
}
```

Flags: `-std=c++26   -Werror -Wunused-but-set-variable`.

GCC: `trunk`.

Error:
```
<source>: In instantiation of 'constexpr bool are_same() [with int ...vals =
{1, 2, 3}]':
<source>:25:33:   required from here
   25 |     std::cout << are_same<1,2,3>();
      |                  ~~~~~~~~~~~~~~~^~
<source>:16:38: error: variable 'J' set but not used
[-Werror=unused-but-set-variable=]
   16 |         template for (constexpr auto J: make_array<I + 1,
sizeof...(vals)>()) {
      |                                      ^
<source>:16:38: error: variable 'J' set but not used
[-Werror=unused-but-set-variable=]
<source>:16:38: error: variable 'J' set but not used
[-Werror=unused-but-set-variable=]
cc1plus: all warnings being treated as errors
Compiler returned: 1
```

Reply via email to