Skip to content

Commit

Permalink
[Sema] cast to CXXRecordDecl correctly when diag a default comparison…
Browse files Browse the repository at this point in the history
… method

Fixed: llvm/llvm-project#62791
Fixed: llvm/llvm-project#62102
in c++20, default comparison is supported. `getLexicalDeclContext` maybe cannot
get the `CXXRecord` if default comparison defined out of `CXXRecord`.
This patch want to get these information from the first function argument.

Reviewed By: #clang-language-wg, erichkeane

Differential Revision: https://reviews.llvm.org/D151365
  • Loading branch information
HerrCai0907 authored and veselypeta committed Aug 25, 2024
2 parents 2bd0fbd + 5df593a commit 53ab564
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ Bug Fixes in This Version
(`#62789 <https://github.com/llvm/llvm-project/issues/62789>`_).
- Fix a crash when instantiating a non-type template argument in a dependent scope.
(`#62533 <https://github.com/llvm/llvm-project/issues/62533>`_).
- Fix crash when diagnosing default comparison method.
(`#62791 <https://github.com/llvm/llvm-project/issues/62791>`_) and
(`#62102 <https://github.com/llvm/llvm-project/issues/62102>`_).

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Sema/SemaTemplateInstantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -961,11 +961,13 @@ void Sema::PrintInstantiationStack() {
<< MD->isExplicitlyDefaulted() << DFK.asSpecialMember()
<< Context.getTagDeclType(MD->getParent());
} else if (DFK.isComparison()) {
QualType RecordType = FD->getParamDecl(0)
->getType()
.getNonReferenceType()
.getUnqualifiedType();
Diags.Report(Active->PointOfInstantiation,
diag::note_comparison_synthesized_at)
<< (int)DFK.asComparison()
<< Context.getTagDeclType(
cast<CXXRecordDecl>(FD->getLexicalDeclContext()));
<< (int)DFK.asComparison() << RecordType;
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CXX/class/class.compare/class.spaceship/p1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,5 @@ namespace Preference {
A a;
std::strong_ordering operator<=>(const B&) const = default; // expected-error {{call to deleted constructor of 'A'}}
};
bool x = B() < B(); // expected-note {{in defaulted three-way comparison operator for 'Preference::B' first required here}}
bool x = B() < B(); // expected-note {{in defaulted three-way comparison operator for 'B' first required here}}
}
17 changes: 17 additions & 0 deletions clang/test/SemaCXX/cxx20-default-compare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %clang_cc1 %s -std=c++23 -verify -Wfloat-equal

struct Foo {
float val;
bool operator==(const Foo &) const;
friend bool operator==(const Foo &, const Foo &);
friend bool operator==(Foo, Foo );
};

// Declare the defaulted comparison function as a member function.
bool Foo::operator==(const Foo &) const = default; // expected-warning {{comparing floating point with == or != is unsafe}} expected-note {{in defaulted equality comparison operator for 'Foo' first required here}}

// Declare the defaulted comparison function as a non-member function.
bool operator==(const Foo &, const Foo &) = default; // expected-warning {{comparing floating point with == or != is unsafe}} expected-note {{in defaulted equality comparison operator for 'Foo' first required here}}

// Declare the defaulted comparison function as a non-member function. Arguments are passed by value.
bool operator==(Foo, Foo) = default; // expected-warning {{comparing floating point with == or != is unsafe}} expected-note {{in defaulted equality comparison operator for 'Foo' first required here}}

0 comments on commit 53ab564

Please sign in to comment.