Constexpr evaluator should treat [[gnu::weak]] member pointer comparisons as evaluati...
authorTakuya Shimizu <shimizu2486@gmail.com>
Mon, 17 Apr 2023 13:48:58 +0000 (09:48 -0400)
committerAaron Ballman <aaron@aaronballman.com>
Mon, 17 Apr 2023 13:50:46 +0000 (09:50 -0400)
This patch fixes the wrong signal from the constexpr evaluator that
[[gnu::weak]] member pointer comparison is valid, while it is emitting
notes on them.

I found a crashing case fixed by this change and added it as a test
case: https://godbolt.org/z/8391fGjGn

I noticed this while I was working on D146358.

Differential Revision: https://reviews.llvm.org/D148419

clang/docs/ReleaseNotes.rst
clang/lib/AST/ExprConstant.cpp
clang/test/SemaCXX/crash-lambda-weak-attr.cpp [new file with mode: 0644]

index 9c8a0e1..05189bf 100644 (file)
@@ -312,6 +312,8 @@ Bug Fixes in This Version
   (`#61417 <https://github.com/llvm/llvm-project/issues/61417>`_)
 - Fix crash after suggesting typo correction to constexpr if condition.
   (`#61885 <https://github.com/llvm/llvm-project/issues/61885>`_)
+- Clang constexpr evaluator now treats comparison of [[gnu::weak]]-attributed
+  member pointer as an invalid expression.
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
index fbe92d0..6bfb3a3 100644 (file)
@@ -13142,12 +13142,12 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
     if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) {
       Info.FFDiag(E, diag::note_constexpr_mem_pointer_weak_comparison)
           << LHSValue.getDecl();
-      return true;
+      return false;
     }
     if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) {
       Info.FFDiag(E, diag::note_constexpr_mem_pointer_weak_comparison)
           << RHSValue.getDecl();
-      return true;
+      return false;
     }
 
     // C++11 [expr.eq]p2:
diff --git a/clang/test/SemaCXX/crash-lambda-weak-attr.cpp b/clang/test/SemaCXX/crash-lambda-weak-attr.cpp
new file mode 100644 (file)
index 0000000..28a5169
--- /dev/null
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+struct Weak {
+    [[gnu::weak]]void weak_method();
+};
+static_assert([](){ return &Weak::weak_method != nullptr; }()); // expected-error {{static assertion expression is not an integral constant expression}} \
+                                                                // expected-note {{comparison against pointer to weak member 'Weak::weak_method' can only be performed at runtime}} \
+                                                                // expected-note {{in call to}}
+