Skip to content

[Clang] Implement CWG2496 #142975

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 4 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ Resolutions to C++ Defect Reports

- Implemented `CWG3005 Function parameters should never be name-independent <https://wg21.link/CWG3005>`_.

- Implemented `CWG2496 ref-qualifiers and virtual overriding <https://wg21.link/CWG2496>`_.

C Language Changes
------------------

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ static bool IsOverloadOrOverrideImpl(Sema &SemaRef, FunctionDecl *New,
// If the function is a class member, its signature includes the
// cv-qualifiers (if any) and ref-qualifier (if any) on the function itself.
auto DiagnoseInconsistentRefQualifiers = [&]() {
if (SemaRef.LangOpts.CPlusPlus23)
if (SemaRef.LangOpts.CPlusPlus23 && !UseOverrideRules)
return false;
if (OldMethod->getRefQualifier() == NewMethod->getRefQualifier())
return false;
Expand Down
31 changes: 31 additions & 0 deletions clang/test/CXX/drs/cwg24xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,34 @@ void (*q)() throw() = S();
// since-cxx17-error@-1 {{no viable conversion from 'S' to 'void (*)() throw()'}}
// since-cxx17-note@#cwg2486-conv {{candidate function}}
} // namespace cwg2486


namespace cwg2496 { // cwg2496: 21
#if __cplusplus >= 201102L
struct S {
virtual void f(); // #cwg2496-f
virtual void g() &; // #cwg2496-g
virtual void h(); // #cwg2496-h
virtual void i();
virtual void j() &;
virtual void k() &&;
virtual void l() &;
};

struct T : S {
virtual void f() &;
// expected-error@-1 {{cannot overload a member function with ref-qualifier '&' with a member function without a ref-qualifier}}
// expected-note@#cwg2496-f {{previous declaration is here}}
virtual void g();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd also like to see a case where it's declared as & in the base class and && in the derived class.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is l() case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i added the l case afterwards :)

// expected-error@-1 {{cannot overload a member function without a ref-qualifier with a member function with ref-qualifier '&'}}
// expected-note@#cwg2496-g {{previous declaration is here}}
virtual void h() &&;
// expected-error@-1 {{cannot overload a member function with ref-qualifier '&&' with a member function without a ref-qualifier}}
// expected-note@#cwg2496-h {{previous declaration is here}}
virtual void i();
virtual void j() &;
virtual void k() &;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be diagnosed? k() && is in the base class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the two functions don't correspond.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I see that wording now, you're right. And since they don't correspond, those are overloads not overrides.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you leave a comment in the test about that?

virtual void l() &&;
};
#endif
}
2 changes: 1 addition & 1 deletion clang/www/cxx_dr_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -14811,7 +14811,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td><a href="https://cplusplus.github.io/CWG/issues/2496.html">2496</a></td>
<td>CD6</td>
<td><I>ref-qualifier</I>s and virtual overriding</td>
<td class="unknown" align="center">Unknown</td>
<td class="unreleased" align="center">Clang 21</td>
</tr>
<tr class="open" id="2497">
<td><a href="https://cplusplus.github.io/CWG/issues/2497.html">2497</a></td>
Expand Down
Loading