[Bug 12200] "noreturn" follows virtual method implementation (opposite of g++)

This is a GCC attribute, so my first inclination is to say that GCC gets to
define its semantics. We've presented this attribute with different
semantics for some time, and it's possible that people are somehow relying
on that, but for the most part, switching to GCC's semantics would be
backwards-compatible (we may start producing -Wmissing-noreturn warnings on
derived class functions that don't return and override a noreturn function,
for instance, and we'll lose some optimization opportunities).

Do we actually have a strong case for diverging from GCC here? We have
evidence that we're breaking existing code and damaging our portability
story by doing so. (FWIW, C++11's [[noreturn]] behaves like GNU's
[[noreturn]], and our implementation of _Noreturn does the same. I don't
know how __declspec(noreturn) behaves.)

Is there a specification for it that I’ve missed? My understanding is that this is accidental fallout from a particularly poor representation choice that prevents them from supporting noreturn on member functions at all, and that GCC would consider this a (perhaps low priority or even unfixable) bug in their implementation. Certainly our model is more consistent with the original philosophy behind making attribute((noreturn)) part of the function type. Our model is also more expressive: among other things, it makes it meaningful to put attribute((noreturn)) on a virtual function declaration (including a pure one), and it enables things like a member function that polymorphically throws its object as an exception. In contrast, overriding a noreturn virtual function with a returning implementation seems basically useless — what code could ever reasonably use it? — unless the overridden function is not meant to ever actually be called, and frankly there are much more appropriate tools for that pattern (like builtin_unreachable, or just making the base class abstract).

Analogies to [[noreturn]] and _Noreturn are flawed because they are defined by the standards to not be part of the type. If you want to argue that all of these attributes should be interchangeable, you’re really arguing for a fundamental change to the GCC extension.

John.

*Comment # 12 <http://llvm.org/bugs/show_bug.cgi?id=12200#c12&gt; on bug
12200 <http://llvm.org/bugs/show_bug.cgi?id=12200&gt; from John McCall
<rjmccall@apple.com> *

(In reply to comment #10 <http://llvm.org/bugs/show_bug.cgi?id=12200#c10&gt;\)> There's also an IRgen problem: it should ignore 'noreturn' when making a
> (non-devirtualized) virtual call -- that's a property of the function, not
> of the vtable slot.

That's the GCC model. Our design has always been that attribute((noreturn))
--- when declared in the class definition --- is a property of the signature of
the virtual function.

[Moving this discussion to cfe-dev for visibility.]

This is a GCC attribute, so my first inclination is to say that GCC gets
to define its semantics.

Is there a specification for it that I've missed?

Sure, it's right there in GCC subversion :wink:

My understanding is that this is accidental fallout from a particularly

poor representation choice that prevents them from supporting noreturn on
member functions at all, and that GCC would consider this a (perhaps low
priority or even unfixable) bug in their implementation. Certainly our
model is more consistent with the original philosophy behind making
__attribute__((noreturn)) part of the function type.

I agree, but our model is incompatible with GCC's model, and this breaks
real code.

[For posterity, GCC's (bizarre) model is as follows... When applied to a
function, it means that function does not return. When applied to a
function pointer type or a function reference type, it affects the type.
The type of a function declared __attribute__((noreturn)) is *not* a
noreturn function type, and taking the address of a function declared
__attribute__((noreturn)) does *not* give a noreturn function pointer.]

Our model is also more expressive: among other things, it makes it

meaningful to put __attribute__((noreturn)) on a virtual function
declaration (including a pure one),

That's also meaningful in the GCC model: it means a devirtualized or
non-virtual call is known to not return.

and it enables things like a member function that polymorphically throws
its object as an exception. In contrast, overriding a noreturn virtual
function with a returning implementation seems basically useless -- what
code could ever reasonably use it?

Code where the base class implements a function that always throws (maybe a
"not implemented" exception) and the derived class does not? This doesn't
seem unreasonable, and bug 12200 and its duplicate are examples reduced
from real code that did exactly this, as far as I can see.

Do we actually have a strong case for diverging from GCC here? We have
evidence that we're breaking existing code and damaging our portability
story by doing so. (FWIW, C++11's [[noreturn]] behaves like GNU's
[[noreturn]], and our implementation of _Noreturn does the same. I don't
know how __declspec(noreturn) behaves.)

Analogies to [[noreturn]] and _Noreturn are flawed because they are
defined by the standards to not be part of the type. If you want to argue
that all of these attributes should be interchangeable, you're really
arguing for a fundamental change to the GCC extension.

I don't want to argue these should be interchangeable -- just observing
that we are the only ones providing this
noreturn-virtual-function-implies-overrides-are-noreturn behavior (though
admittedly it's a small sample set, since _Noreturn doesn't count).

What I'm suggesting is that perhaps our GCC attribute should match GCC, and
if not, we should have a solid reason for the difference, documented
somewhere visible, explaining why we thought our semantics for a GCC
attribute were more important than compatibility with GCC, that we can
point people to when we get the next duplicate of this bug filed.