diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -5302,8 +5302,7 @@ // trivial in almost all cases, except if a union member has an in-class // initializer: // union { int n = 0; }; - if (!Invalid) - ActOnUninitializedDecl(Anon); + ActOnUninitializedDecl(Anon); } Anon->setImplicit(); @@ -9109,8 +9108,10 @@ // C++ [class.union]p2 // A union can have member functions, but not virtual functions. - if (isVirtual && Parent->isUnion()) + if (isVirtual && Parent->isUnion()) { Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_virtual_in_union); + NewFD->setInvalidDecl(); + } } SetNestedNameSpecifier(*this, NewFD, D); diff --git a/clang/test/SemaCXX/PR49534.cpp b/clang/test/SemaCXX/PR49534.cpp --- a/clang/test/SemaCXX/PR49534.cpp +++ b/clang/test/SemaCXX/PR49534.cpp @@ -1,6 +1,5 @@ // RUN: %clang_cc1 -x c++ -fsyntax-only %s -verify static union { // expected-warning {{declaration does not declare anything}} - virtual int a(); // expected-error {{unions cannot have virtual functions}} \ - // expected-error {{functions cannot be declared in an anonymous union}} + virtual int a(); // expected-error {{unions cannot have virtual functions}} }; diff --git a/clang/test/SemaCXX/virtual-function-in-union.cpp b/clang/test/SemaCXX/virtual-function-in-union.cpp --- a/clang/test/SemaCXX/virtual-function-in-union.cpp +++ b/clang/test/SemaCXX/virtual-function-in-union.cpp @@ -1,5 +1,8 @@ // RUN: %clang_cc1 -fsyntax-only -verify %s -union x { - virtual void f(); // expected-error {{unions cannot have virtual functions}} +union U { + int d; + virtual int f() { return d; }; // expected-error {{unions cannot have virtual functions}} }; + +int foo() { U u; return u.d; }