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]]; // illformed, 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:
- Has the compiler missed out on emitting a diagnostic, or am I missing something here?
- 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.
Yes, this is ill-formed as per [dcl.attr.fallthrough#]p1:
and the compiler is required to issue at least a diagnostic as per [intro.compliance]p2.2: