[clang] [clang]Avoid diagnose invalid consteval call for invalid function decl (PR #68646)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 9 19:07:59 PDT 2023
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/68646
>From ffc9412252cd0e046978f183384375580bd31245 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 10 Oct 2023 07:52:06 +0800
Subject: [PATCH 1/3] [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 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
>From 3f48ddf36577c1750c59d80a0b803197ef7b85b0 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 10 Oct 2023 09:54:16 +0800
Subject: [PATCH 2/3] release note
---
clang/docs/ReleaseNotes.rst | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ea737fdb5fdad15..31f4844f22ed37b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -358,6 +358,8 @@ Bug Fixes in This Version
Fixes (`#67690 <https://github.com/llvm/llvm-project/issues/67690>`_)
- Fixes a ``clang-17`` regression where ``LLVM_UNREACHABLE_OPTIMIZE=OFF``
cannot be used with ``Release`` mode builds. (`#68237 <https://github.com/llvm/llvm-project/issues/68237>`_).
+- Fix crash in evaluating ``constexpr`` value for invalid template function.
+ Fixes (`#68542 <https://github.com/llvm/llvm-project/issues/68542>`_)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>From f3c8ba15dae0511004975ea54ea0965017d95588 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 10 Oct 2023 10:07:44 +0800
Subject: [PATCH 3/3] update test
---
clang/lib/Sema/SemaExpr.cpp | 1 +
clang/test/SemaCXX/PR68542.cpp | 8 ++++----
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 2e01b82b13d819e..7ede275bb5a05e1 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -18406,6 +18406,7 @@ 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())
diff --git a/clang/test/SemaCXX/PR68542.cpp b/clang/test/SemaCXX/PR68542.cpp
index bc94fffe5de289d..fc767a78c8b0013 100644
--- a/clang/test/SemaCXX/PR68542.cpp
+++ b/clang/test/SemaCXX/PR68542.cpp
@@ -1,7 +1,5 @@
// 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;
};
@@ -16,5 +14,7 @@ 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
+constexpr S x = f(0); // expected-error{{constexpr variable 'x' must be initialized by a constant expression}}
+// expected-note at -1{{in instantiation of function template specialization 'f<int>' requested here}}
+// expected-note at 3{{candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'S &&' for 1st argument}}
+// expected-note at 3{{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const S &' for 1st argument}}
More information about the cfe-commits
mailing list