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

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

>From ffc9412252cd0e046978f183384375580bd31245 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0...@163.com>
Date: Tue, 10 Oct 2023 07:52:06 +0800
Subject: [PATCH] [clang]Avoid diagnoise invalid consteval call for invalid
 function decl

Fixes:#68542
It is meaningless to diagnoise further error for a invalid function declaration.
---
 clang/lib/Sema/SemaExpr.cpp    |  3 ++-
 clang/test/SemaCXX/PR68542.cpp | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/PR68542.cpp

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@+2{{candidate constructor (the implicit move constructor) not 
viable: no known conversion from 'int' to 'S &&' for 1st argument}}
+// expected-note@+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@+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

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to