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

            Bug ID: 129267
           Summary: Broken handling of continue stmt in template for
                    bodies during potential_constant_expression_1
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jakub at gcc dot gnu.org
  Target Milestone: ---

// { dg-do compile { target c++23 } }
// { dg-options "" }

void foo ();

constexpr int
bar ()
{
  int r = 0;
 lab1:
  template for (constexpr int i : { 1, 2 })     // { dg-warning "'template for'
only available with" "" { target c++23_down } }
    {
      while (true)
        {
          r += i;
          continue lab1;        // { dg-warning "'continue' statement with an
identifier only available with" "" { target c++26_down } }
        }
      foo ();
    }
 lab2:
  template for (constexpr int i : { 3, 4 })     // { dg-warning "'template for'
only available with" "" { target c++23_down } }
    {
      do
        {
          r += i;
          continue lab2;        // { dg-warning "'continue' statement with an
identifier only available with" "" { target c++26_down } }
        }
      while (0);
      foo ();
    }
 lab3:
  template for (constexpr int i : { 5, 6 })     // { dg-warning "'template for'
only available with" "" { target c++23_down } }
    {
      for (int j = 0; ; ++j)
        {
          r += i;
          continue lab3;        // { dg-warning "'continue' statement with an
identifier only available with" "" { target c++26_down } }
        }
      foo ();
    }
 lab4:
  template for (constexpr int i : { 7, 8 })     // { dg-warning "'template for'
only available with" "" { target c++23_down } }
    {
      template for (constexpr int j : { 1, 2 }) // { dg-warning "'template for'
only available with" "" { target c++23_down } }
        {
          r += i;
          continue lab4;        // { dg-warning "'continue' statement with an
identifier only available with" "" { target c++26_down } }
        }
      foo ();
    }
  return r;
}

constexpr int
baz ()
{
  int r = 0;
 lab5:
  template for (constexpr int i : { 1, 2 })     // { dg-warning "'template for'
only available with" "" { target c++23_down } }
    {
      for (int j = 0; j < 2; ++j)
        {
          r += i;
          continue lab5;        // { dg-warning "'continue' statement with an
identifier only available with" "" { target c++26_down } }
        }
      foo ();
    }
  return r;
}

static_assert (bar () == 36);
static_assert (baz () == 3);

should IMHO pass, all the foo () calls to non-constexpr functions are in dead
code.
Unfortunately during potential_constant_expression_1 we don't handle that
right, that
continue is lowered to GOTO_EXPR to a LABEL_DECL_CONTINUE artificial label
(different in each iteration aka body instantiation), and
1) we don't handle that case at all in check_for_return_continue
2) any loop where continues is true (and since r17-7060 unless it is a
CONTINUE_STMT with no name or matching name) just clears *jump_target.  For all
loops but DO_STMT we should never do this for the LABEL_DECL_CONTINUE case and
for DO_STMT need to actually add a DO_STMT even for continue and check matching
name.

Reply via email to