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

2

12

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?
Ehrenburg answered 23/8, 2018 at 10:21 Comment(6)
Relevant part of the Standard: eel.is/c++draft/dcl.attr.fallthroughFungous
@0xbaadf00d I highly doubt that, both standard and cppreference use nearly exactly the same example (i.e. [[fallthrough]] at the end) as an ill-formed construction. I'd say this is not a fallthrough question, but rather handling of an ill-formed construct. One can copy paste ill-formed example from standard into gcc and it compiles cleanly: godbolt.org/z/fftSM-.Cardwell
Yea, I agree. That's why I removed my comment before you could post yours.Redfin
BTW, this question prompted this bug report gcc.gnu.org/bugzilla/show_bug.cgi?id=87068Finzer
as a side note, I'd suggest to use gcc >=7.2, 7.1 contains too many bugs and isn't stable enoughVicious
@Cardwell not both clang and MSVC produce a diagnosticEpaulet
E
9
  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.

Epaulet answered 23/8, 2018 at 13:41 Comment(0)
E
3

Sometime after I posted this question, a bug was reported based on the question.
I waited to see if it would be accepted. It was accepted and assigned, and based on this comment in the bug report, a patch is going to be generated. So this effectively answers both my questions.

However, I will accept the answer given by @ShafikYaghmour because it contains important points addressing my questions.

Ehrenburg answered 24/8, 2018 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.