-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[Clang] Fix CXXRewrittenBinaryOperator::getDecomposedForm to handle case when spaceship operator returns comparison category by reference #66270
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
[Clang] Fix CXXRewrittenBinaryOperator::getDecomposedForm to handle case when spaceship operator returns comparison category by reference #66270
Conversation
@llvm/pr-subscribers-clang ChangesCurrently CXXRewrittenBinaryOperator::getDecomposedForm(...) may crash if the spaceship operator returns a comparison category by reference. This because IgnoreImplicitAsWritten() does not look through CXXConstructExpr. The fix is to use IgnoreUnlessSpelledInSource() which will look though CXXConstructExpr.This fixes: #64162 -- 2 Files Affected:
diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp index efb64842fb6d96d..06163255f9b5e54 100644 --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -111,7 +111,7 @@ CXXRewrittenBinaryOperator::getDecomposedForm() const { return Result; // Otherwise, we expect a <=> to now be on the LHS. - E = Result.LHS->IgnoreImplicitAsWritten(); + E = Result.LHS->IgnoreUnlessSpelledInSource(); if (auto *BO = dyn_cast<BinaryOperator>(E)) { assert(BO->getOpcode() == BO_Cmp); Result.LHS = BO->getLHS(); diff --git a/clang/test/SemaCXX/compare-cxx2a.cpp b/clang/test/SemaCXX/compare-cxx2a.cpp index 619e16aa7458179..15a0baccfca17a2 100644 --- a/clang/test/SemaCXX/compare-cxx2a.cpp +++ b/clang/test/SemaCXX/compare-cxx2a.cpp @@ -479,3 +479,26 @@ void DoSomething() { // expected-note {{undefined function 'operator++' cannot be used in a constant expression}} } } + +namespace GH64162 { +struct S { + const std::strong_ordering& operator<=>(const S&) const = default; +}; +bool test(S s) { + return s < s; // We expect this not to crash anymore +} + +// Following example never crashed but worth adding in because it is related +struct A {}; +bool operator<(A, int); + +struct B { + operator A(); +}; + +struct C { + B operator<=>(C); +}; + +bool f(C c) { return c < c; } +} |
…ase when spaceship operator returns comparison category by reference Currently CXXRewrittenBinaryOperator::getDecomposedForm(...) may crash if the spaceship operator returns a comparison category by reference. This because IgnoreImplicitAsWritten() does not look through CXXConstructExpr. The fix is to use IgnoreUnlessSpelledInSource() which will look though CXXConstructExpr. This fixes: llvm#64162
ed43ed6
to
b934841
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it needs a release note?
…ase when spaceship operator returns comparison category by reference Currently CXXRewrittenBinaryOperator::getDecomposedForm(...) may crash if the spaceship operator returns a comparison category by reference. This because IgnoreImplicitAsWritten() does not look through CXXConstructExpr. The fix is to use IgnoreUnlessSpelledInSource() which will look though CXXConstructExpr. This fixes: llvm#64162
…ub.com/shafik/llvm-project into fix_unexpected_rewritten_operator_form
You would think I would remember this by now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - I was trying to find some hope in the standard that this was ill-formed. Alas.
Ping @shafik, just needs a rebase :) |
…ase when spaceship operator returns comparison category by reference Currently CXXRewrittenBinaryOperator::getDecomposedForm(...) may crash if the spaceship operator returns a comparison category by reference. This because IgnoreImplicitAsWritten() does not look through CXXConstructExpr. The fix is to use IgnoreUnlessSpelledInSource() which will look though CXXConstructExpr. This fixes: llvm#64162
…ub.com/shafik/llvm-project into fix_unexpected_rewritten_operator_form
Currently CXXRewrittenBinaryOperator::getDecomposedForm(...) may crash if the
spaceship operator returns a comparison category by reference. This because
IgnoreImplicitAsWritten() does not look through CXXConstructExpr. The fix is to
use IgnoreUnlessSpelledInSource() which will look though CXXConstructExpr.
This fixes: #64162