On: void foo (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]]; // error } } mentioned in https://isocpp.org/files/papers/D2552R1.pdf we don't diagnose misplaced [[fallthrough]] in 2 spots. The original dump shows: switch (n) { case 1:; case 2:; <<cleanup_point <<< Unknown tree: expr_stmt g () >>>>>; <<cleanup_point <<< Unknown tree: expr_stmt .FALLTHROUGH () >>>>>; case 3:; <<cleanup_point <<< Unknown tree: expr_stmt .FALLTHROUGH () >>>>>; case 6:; <D.2778>:; <<cleanup_point <<< Unknown tree: expr_stmt .FALLTHROUGH () >>>>>; if (<<cleanup_point n-- != 0>>) goto <D.2778>; else goto <D.2776>; <D.2776>:; case 7:; goto <D.2779>; <<cleanup_point <<< Unknown tree: expr_stmt .FALLTHROUGH () >>>>>; <D.2779>:; case 5:; <<cleanup_point <<< Unknown tree: expr_stmt h () >>>>>; case 4:; <<cleanup_point <<< Unknown tree: expr_stmt i () >>>>>; <<cleanup_point <<< Unknown tree: expr_stmt .FALLTHROUGH () >>>>>; } so the reason we don't warn in the do { ... } while (false); case is that it disappears probably during genericize_c_loop and the while (false) case because the genericization in that case makes the loop body followed by artificial label.
Apparently this is https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2406 which we probably never implemented.
And looking at the C wording in n2596.pdf, seems it is different again: "The next block item(6.8.2) that would be encountered after a fallthrough declaration shall be a case label or default label associated with the smallest enclosing switch statement." So, if my understanding is well, int j = 0; switch (n) { case 1: for (int i = 0; i < 1; ++i) { [[fallthrough]]; // Invalid in both C and C++ } case 2: while (++j < 2) { [[fallthrough]]; // Invalid in both C and C++ } case 3: do { [[fallthrough]]; // Invalid in both C and C++ } while (0); case 4: if (1) { [[fallthrough]]; // Invalid in C, valid in C++? } case 5: for (int i = 0; i < 1; ++i) [[fallthrough]]; // Invalid in C++, dunno about C case 6: while (++j < 2) [[fallthrough]]; // Invalid in C++, dunno about C case 7: do [[fallthrough]]; // Invalid in C++, dunno about C while (0); case 8: if (1) [[fallthrough]]; // Dunno about either C or C++ case 9: { [[fallthrough]]; // Invalid in C, valid in C++? } default: break; }
On Tue, 8 Nov 2022, jakub at gcc dot gnu.org via Gcc-bugs wrote: > And looking at the C wording in n2596.pdf, seems it is different again: That's a very old version. N3054 is the most recent public draft (SC22 N5777 is more recent than that and is the actual CD ballot text). > "The next block item(6.8.2) that would be encountered after a fallthrough > declaration shall be a case label or default label associated with the smallest > enclosing switch statement." It's not exactly clear what "next block item" is for any of the examples you give - next lexically (OK once the current one is exited) or in execution (no good for a Constraint)? And thus not clear that any of these are invalid. I've noted that the inconsistency with C++ should be raised in an NB comment.