Skip to content

[clang] When checking for covariant return types, make sure the pointers or references are to *classes* #111856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 16, 2024

Conversation

bricknerb
Copy link
Contributor

https://eel.is/c++draft/class.virtual#8.1

This prevents overriding methods with non class return types that have less cv-qualification.

Fixes: #111742

…ers 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.
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 10, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 10, 2024

@llvm/pr-subscribers-clang

Author: Boaz Brickner (bricknerb)

Changes

https://eel.is/c++draft/class.virtual#8.1

This prevents overriding methods with non class return types that have less cv-qualification.

Fixes: #111742


Full diff: https://github.com/llvm/llvm-project/pull/111856.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+2-2)
  • (modified) clang/test/SemaCXX/virtual-override.cpp (+10)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 9cb2ed02a3f764..6195b62b8afa16 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -18273,7 +18273,7 @@ bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New,
   }
 
   // The return types aren't either both pointers or references to a class type.
-  if (NewClassTy.isNull()) {
+  if (NewClassTy.isNull() || !NewClassTy->isStructureOrClassType()) {
     Diag(New->getLocation(),
          diag::err_different_return_type_for_overriding_virtual_function)
         << New->getDeclName() << NewTy << OldTy
@@ -18296,7 +18296,7 @@ bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New,
                               diag::err_covariant_return_incomplete,
                               New->getDeclName()))
         return true;
-    }
+      }
 
     // Check if the new class derives from the old class.
     if (!IsDerivedFrom(New->getLocation(), NewClassTy, OldClassTy)) {
diff --git a/clang/test/SemaCXX/virtual-override.cpp b/clang/test/SemaCXX/virtual-override.cpp
index 72abfc3cf51e1f..6008b8ed063f20 100644
--- a/clang/test/SemaCXX/virtual-override.cpp
+++ b/clang/test/SemaCXX/virtual-override.cpp
@@ -289,3 +289,13 @@ namespace PR8168 {
     static void foo() {} // expected-error{{'static' member function 'foo' overrides a virtual function}}
   };
 }
+
+namespace T13 {
+  struct A {
+    virtual const int *f() const; // expected-note{{overridden virtual function is here}}
+  };
+
+  struct B : A {
+    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 *')}}
+  };
+}

Copy link

github-actions bot commented Oct 10, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation seems fine, but this is still missing a release note (in clang/docs/ReleaseNotes.rst, probably somewhere in the ‘Potentially Breaking Changes’ section).

CC @AaronBallman @cor3ntin Do we want to make this a warning that defaults to an error instead? Imo it’s probably fine as-is because I don’t imagine too many people would write code that abuses us not diagnosing this...

…en return type doesn't point to a class to release notes.
…e pointers to non classes when calculating covariance.
@cor3ntin
Copy link
Contributor

CC @AaronBallman @cor3ntin Do we want to make this a warning that defaults to an error instead? Imo it’s probably fine as-is because I don’t imagine too many people would write code that abuses us not diagnosing this...

It looks fine to me as is.

Code that would do something like static_cast<BaseReturnType>(derived->f()) (which I think is perfectly reasonable) would explode, despite the standard guarantees it should work.

If people scream we have plenty of time to reconsider making it a warning

@bricknerb bricknerb changed the title [clang] When checking for covariant return types, make sure the poiners or references are to *classes* [clang] When checking for covariant return types, make sure the pointers or references are to *classes* Oct 11, 2024
…ts into T2 and T6.

In T2, we check that return types that are pointers to different non-class types fail.
In T6, we check that unlike class return types, non-class return types do not allow to change qualifiers at all (add or remove).
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@ilya-biryukov ilya-biryukov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ilya-biryukov ilya-biryukov merged commit b333edd into llvm:main Oct 16, 2024
9 checks passed
Copy link

@bricknerb Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@bricknerb bricknerb deleted the covariant branch October 17, 2024 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clang wrongly allows overriding method to vary its return type
5 participants