This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 113d. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-03-20


2406. [[fallthrough]] attribute and iteration statements

Section: 9.12.6  [dcl.attr.fallthrough]     Status: CD5     Submitter: Mike Miller     Date: 2019-03-08

[Accepted as a DR at the July, 2019 meeting.]

According to 9.12.6 [dcl.attr.fallthrough] paragraph 1,

A fallthrough statement may only appear within an enclosing switch statement (8.5.3 [stmt.switch]). The next statement that would be executed after a fallthrough statement shall be a labeled statement whose label is a case label or default label for the same switch statement.

The meaning of “next statement that would be executed” is unclear with respect to the controlled substatement of an iteration statement. There is implementation divergence on an example like:

  void f(int n) {
    switch (n) {
      case 0:
        while (true)
          [[fallthrough]]; // Well-formed?
      case 1:
        break;
    }
  }

Proposed resolution, April, 2019:

  1. Change 9.12.6 [dcl.attr.fallthrough] paragraph 1 as follows:

  2. The attribute-token fallthrough may be applied to a null statement (8.3 [stmt.expr]); such a statement is a fallthrough statement. The attribute-token fallthrough shall appear at most once in each attribute-list and no attribute-argument-clause shall be present. A fallthrough statement may only appear within an enclosing switch statement (8.5.3 [stmt.switch]). The next statement that would be executed after a fallthrough statement shall be a labeled statement whose label is a case label or default label for the same switch statement and, if the fallthrough statement is contained in an iteration statement, the next statement shall be part of the same execution of the substatement of the innermost enclosing iteration statement. The program is ill-formed if there is no such statement.
  3. Change the example in 9.12.6 [dcl.attr.fallthrough] paragraph 3 as follows:

  4. [Example:

      void f(int n) {
        void g(), h(), i();
        switch (n) {
        case 1:
        case 2:
          g();
          [[fallthrough]];
        case 3:              // warning on fallthrough discouraged
          do {
            [[fallthrough]]; // error: next statement is not part of the same substatement execution
          } while (false);
        case 6:
          do {
            [[fallthrough]]; // error: next statement is not part of the same substatement execution
          } while (n--);
        case 7:
          while (false) {
            [[fallthrough]]; // error: next statement is not part of the same substatement execution
          }
        case 5:
          h();
        case 4:              // implementation may warn on fallthrough
          i();
          [[fallthrough]];   // ill-formed
        }
      }
    

    end example]