Bug 107571 - Missing fallthrough attribute diagnostics
Summary: Missing fallthrough attribute diagnostics
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 13.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2022-11-08 14:49 UTC by Jakub Jelinek
Modified: 2022-11-28 19:22 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-11-08 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Jelinek 2022-11-08 14:49:02 UTC
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.
Comment 1 Jakub Jelinek 2022-11-08 22:27:04 UTC
Apparently this is
https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2406
which we probably never implemented.
Comment 2 Jakub Jelinek 2022-11-08 22:40:48 UTC
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;
    }
Comment 3 joseph@codesourcery.com 2022-11-09 00:36:49 UTC
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.