[clang] [clang]Avoid diagnose invalid consteval call for invalid function decl (PR #68646)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 9 17:01:36 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Congcong Cai (HerrCai0907)

<details>
<summary>Changes</summary>

Fixes:#<!-- -->68542
It is meaningless to diagnose further error for a invalid function declaration.

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


2 Files Affected:

- (modified) clang/lib/Sema/SemaExpr.cpp (+2-1) 
- (added) clang/test/SemaCXX/PR68542.cpp (+20) 


``````````diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 9c5f96eebd04165..2e01b82b13d819e 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -18406,9 +18406,10 @@ static void EvaluateAndDiagnoseImmediateInvocation(
       FD = Call->getConstructor();
     else if (auto *Cast = dyn_cast<CastExpr>(InnerExpr))
       FD = dyn_cast_or_null<FunctionDecl>(Cast->getConversionFunction());
-
     assert(FD && FD->isImmediateFunction() &&
            "could not find an immediate function in this expression");
+    if (FD->isInvalidDecl())
+      return;
     SemaRef.Diag(CE->getBeginLoc(), diag::err_invalid_consteval_call)
         << FD << FD->isConsteval();
     if (auto Context =
diff --git a/clang/test/SemaCXX/PR68542.cpp b/clang/test/SemaCXX/PR68542.cpp
new file mode 100644
index 000000000000000..bc94fffe5de289d
--- /dev/null
+++ b/clang/test/SemaCXX/PR68542.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+
+// expected-note at +2{{candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'S &&' for 1st argument}}
+// expected-note at +1{{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const S &' for 1st argument}}
+struct S {
+    int e;
+};
+
+template<class T>
+consteval int get_format() {
+	return nullptr; // expected-error{{cannot initialize return object of type 'int' with an rvalue of type 'std::nullptr_t'}}
+}
+
+template<class T>
+constexpr S f(T) noexcept {
+	return get_format<T>(); // expected-error{{no viable conversion from returned value of type 'int' to function return type 'S'}}
+}
+
+// expected-note at +1{{in instantiation of function template specialization 'f<int>' requested here}}
+constexpr S x = f(0); // expected-error{{constexpr variable 'x' must be initialized by a constant expression}}
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/68646


More information about the cfe-commits mailing list