Search code examples
c++gccc++17gcc-warningfall-through

Should there be a diagnostic from GCC compiler for this ill-formed C++ code involving [[fallthrough]] attribute?


I was testing C++17 features on GCC compiler version 7.1.0. This is related to the fallthrough attribute and the following example (live example) is adapted from online CPP reference here

#include "iostream"
using namespace std;

int f(int n) {

  switch (n) {
    case 1:
    case 2:
      n = n + 20;
     [[fallthrough]];
    case 3: // no warning on fallthrough      
      n = n + 30;
    case 4: // compiler may warn on fallthrough      
      [[fallthrough]]; // ill­formed, not before a case label
      //n = n + 40;  //commented out to test if compiler will warn.
  }
  return n;
}    

int main()
{
    cout << f(1) << endl;
    cout << f(2) << endl;
    cout << f(3) << endl;
    cout << f(4) << endl;
    return 0;
}

The last [[fallthrough]] (for case 4:) is ill-formed.

The question on "What is the C++ compiler required to do with ill-formed programs according to the Standard?" here has the top answer stating that:

So to sum it up: if an ill-formed program contains a diagnosable violation for which the Standard does not explicitly specify "no diagnostic required", conforming implementations should emit a diagnostic.

So, I looked up the standard (N4713) to see if it stated that there was no diagnostic was required for this issue. I was not able to find any such statement.

Interestingly, after all this, when I added the following statement after the last [[fallthrough]]

n = n + 40;

the compiler warns (live example):

warning: attribute 'fallthrough' not preceding a case label or default label

So, two questions here:

  1. Has the compiler missed out on emitting a diagnostic, or am I missing something here?
  2. If it is a compiler issue, is it serious enough to be reported?

Solution

    1. If it is a compiler issue, is it serious enough to be reported?

    Yes, conformance bugs are important bugs, developers rely on compilers conforming to the standard (compiler may have modes that don't require strict conformance though i.e. gcc requires -pedantic to obtain all diagnostics required by the standard) What priority a bug gets is a different story but merely documenting the bug and having the compiler team acknowledge it as a bug can be a huge help to future developers who run into the bug.

    1. Has the compiler missed out on emitting a diagnostic, or am I missing something here?

    Yes, this is ill-formed as per [dcl.attr.fallthrough#]p1:

    ... 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 program is ill-formed if there is no such statement.

    and the compiler is required to issue at least a diagnostic as per [intro.compliance]p2.2:

    If a program contains a violation of any diagnosable rule or an occurrence of a construct described in this document as “conditionally-supported” when the implementation does not support that construct, a conforming implementation shall issue at least one diagnostic message.