Skip to content

Commit 786d31e

Browse files
committed
[clang] When checking for covariant return types, make sure the pointers or references are to *classes*.
https://eel.is/c++draft/class.virtual#8.1 This prevents overriding methods with non class return types that have less cv-qualification.
1 parent 55d51dd commit 786d31e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18273,7 +18273,7 @@ bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New,
1827318273
}
1827418274

1827518275
// The return types aren't either both pointers or references to a class type.
18276-
if (NewClassTy.isNull()) {
18276+
if (NewClassTy.isNull() || !NewClassTy->isStructureOrClassType()) {
1827718277
Diag(New->getLocation(),
1827818278
diag::err_different_return_type_for_overriding_virtual_function)
1827918279
<< New->getDeclName() << NewTy << OldTy
@@ -18296,7 +18296,7 @@ bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New,
1829618296
diag::err_covariant_return_incomplete,
1829718297
New->getDeclName()))
1829818298
return true;
18299-
}
18299+
}
1830018300

1830118301
// Check if the new class derives from the old class.
1830218302
if (!IsDerivedFrom(New->getLocation(), NewClassTy, OldClassTy)) {

clang/test/SemaCXX/virtual-override.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,13 @@ namespace PR8168 {
289289
static void foo() {} // expected-error{{'static' member function 'foo' overrides a virtual function}}
290290
};
291291
}
292+
293+
namespace T13 {
294+
struct A {
295+
virtual const int *f() const; // expected-note{{overridden virtual function is here}}
296+
};
297+
298+
struct B : A {
299+
int *f() const override; // expected-error{{virtual function 'f' has a different return type ('int *') than the function it overrides (which has return type 'const int *')}}
300+
};
301+
}

0 commit comments

Comments
 (0)