4

Follow-up question for Why do conforming implementations behave differently w.r.t. incomplete array types with internal linkage?.

Context: in both gcc and clang (conforming implementations) by default the requirement C11,6.9.2p3 [1] is cancelled, which is positioned as an extension.

Question: can an extension cancel the existing standard requirements while keeping the implementation conforming?

[1] C11, 6.9.2 External object definitions, 3:

If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type.

UPD. Yes. In other words: the standard says: "we do not support this, the diagnostics is required". The extension says: "we do support this (hence, the standard required diagnostics is irrelevant)".

pmor
  • 5,392
  • 4
  • 17
  • 36
  • AFAIK gcc doesn't claim to be conforming unless `-pedantic` is used. – Nate Eldredge Aug 06 '21 at 15:27
  • Isn't the standard's rule just that if the source code violates a constraint, the compiler must issue a diagnostic, and can then go on to do whatever it likes (such as ignoring the violation)? In your other post, `gcc -pedantic` does issue the diagnostic, so it's off the hook. `gcc` by itself doesn't issue the diagnostic, so it's not conforming, but then again it never claimed to be. – Nate Eldredge Aug 06 '21 at 15:33
  • @NateEldredge About `gcc -pedantic`: there is this quote: "A feature to report any failure to conform to ISO C might be useful in some instances, but would require considerable additional work and would be quite different from -Wpedantic. We don't have plans to support such a feature in the near future.". – pmor Aug 06 '21 at 19:20
  • Right. Some non-conforming programs do stuff for which the Standard requires a diagnostic, others don't. `gcc -pedantic` should issue a diagnostic for those of the first kind. This hypothetical feature they mention would address the second kind of program. – Nate Eldredge Aug 06 '21 at 19:25
  • @NateEldredge FYI: observation of gcc: the presence / absence of `-pedantic` does not change the definition of `__STDC__` (which is 1). The same for clang. – pmor Oct 20 '21 at 09:53
  • Sure, but again, there is no rule that says a non-conforming compiler must not define – Nate Eldredge Oct 20 '21 at 14:15

2 Answers2

2

The Standard requires that if a program violates a constraint which is in a constraints section, an implementation must issue at least one diagnostic. There is no requirement as to whether the document be meaningful or have any relation to the constraint violation. An implementation that unconditionally output "Warning: this implementation makes no attempt to enforce constraints its author views as stupid" would suffice. Likewise an implementation that includes a command-line option to output such a message and documents that it may not be conforming unless that option is specified.

Note that even that requirement has a loophole: if a program exceeds an implementation's translation limits, the implementation may behave in any manner whatsoever, without limitation, and without having to issue any sort of diagnostic. Although the Standard requires for each implementation there must exist at least one source program that at least nominally exercises the translation limits given in the Standard without causing the implementation to malfunction, an implementation may impose arbitrary restrictions on how the translation limits interact, e.g. allowing a program to either contain one identifier of up to 63 character, or a larger number of identifiers that are no more than three characters long. There are very few circumstances where anything an otherwise-conforming implementation might do with a particular source text would render it non-conforming.

supercat
  • 77,689
  • 9
  • 166
  • 211
  • Re: if a program exceeds an implementation's translation limits, the implementation may behave in any manner whatsoever: here is a [relevant question](https://stackoverflow.com/questions/68774646/should-implementations-issue-a-diagnostic-if-the-internal-fixed-non-fixed-tran). – pmor Oct 20 '21 at 10:18
  • Re: There are very few circumstances: out of curiosity, what are these very few circumstances? – pmor Oct 20 '21 at 10:23
  • @pmor: If an implementation were contrived so that there was exactly one possible source text that exercised the translation limits which it would process as specified by the Standard, then such an implementation, if it met documentation and diagnostic-message requirements, would be conforming. If, however, it failed to process that program as specified by the Standard, then the "otherwise-conforming" implementation would cease to be conforming. – supercat Oct 20 '21 at 14:37
2

It's not so much that an implementation "cancels" a requirement with an extension, but that extensions add features that the standard doesn't otherwise support. The main requirement is that extensions doesn't alter any strictly conforming programs.

The definition of a conforming implementation is as follows from section 4p6 of the C11 standard:

The two forms of conforming implementation are hosted and freestanding. A conforming hosted implementation shall accept any strictly conforming program. A conforming freestanding implementation shall accept any strictly conforming program in which the use of the features specified in the library clause (clause 7) is confined to the contents of the standard headers [ ... omitted for brevity ... ]. A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program

Where a strictly conforming program is defined in section 4p5:

A strictly conforming program shall use only those features of the language and library specified in this International Standard. It shall not produce output dependent on any unspecified, undefined, or implementation-defined behavior, and shall not exceed any minimum implementation limit

And a conforming program is defined in section 4p7:

A conforming program is one that is acceptable to a conforming implementation.

So given the case of the program from your prior question:

static int arr[ ];

int main( void )
{
        return arr[ 0 ];
}

static int arr[ ] = { 0 };

This is not a strictly conforming program because it violates 6.9.2p3. However some implementations such as gcc allows this as an extension. Supporting such a feature doesn't prevent a similar strictly conforming program such as this

static int arr[1];

int main( void )
{
        return arr[ 0 ];
}

static int arr[ ] = { 0 };

From behaving any differently. Therefore an implementation supporting this feature still qualifies as a conforming implementation. This also means that the first program, while not a strictly conforming program, is a conforming program because it will run in a well defined manner on a conforming implementation.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thanks. In other words: the standard says: "we do not support this, the diagnostics is required". The extension says: "we do support this (hence, the standard required diagnostics is irrelevant)". Additional related question: does it mean that "application of `sizeof` to an incomplete type w/o any diagnostics" can be positioned as an extension as well? – pmor Aug 06 '21 at 18:48
  • @pmor Yes, in fact gcc has exactly this extension for `void`: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html – dbush Aug 06 '21 at 18:52