[clang] [Sema] Fix assertion error in Sema::FindInstantiatedDecl (PR #96509)
Alejandro Álvarez Ayllón via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 10 02:11:03 PDT 2024
https://github.com/alejandro-alvarez-sonarsource updated https://github.com/llvm/llvm-project/pull/96509
>From 96b0c2c18c197a1ce03d31b01c14d1b18348e9ee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?=
<alejandro.alvarez at sonarsource.com>
Date: Thu, 1 Jun 2023 16:30:54 +0200
Subject: [PATCH 1/5] [Sema] Fix crash in Sema::FindInstantiatedDecl
when looking for a template instantiation with a non-type
parameter of unknown type and with a default value.
This can happen when a template non-type parameter has a broken
expression that gets replaced by a `RecoveryExpr`.
---
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 2 +-
.../instantiate-template-broken-nontype-default.cpp | 12 ++++++++++++
2 files changed, 13 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/instantiate-template-broken-nontype-default.cpp
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 0681520764d9a..999fec0ebb259 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6300,7 +6300,7 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc));
}
QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args);
- if (T.isNull())
+ if (T.isNull() || T->containsErrors())
return nullptr;
CXXRecordDecl *SubstRecord = T->getAsCXXRecordDecl();
diff --git a/clang/test/SemaCXX/instantiate-template-broken-nontype-default.cpp b/clang/test/SemaCXX/instantiate-template-broken-nontype-default.cpp
new file mode 100644
index 0000000000000..a644cc3d057cb
--- /dev/null
+++ b/clang/test/SemaCXX/instantiate-template-broken-nontype-default.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+
+constexpr Missing a = 0; // expected-error {{unknown type name 'Missing'}}
+
+template < typename T, Missing b = a> // expected-error {{unknown type name 'Missing'}}
+class Klass { // expected-note {{candidate template ignored: could not match 'Klass<T, b>' against 'int'}}
+ Klass(T); // expected-note {{candidate template ignored: substitution failure [with T = int, b = <recovery-expr>()]}}
+};
+
+Klass foo{5}; // no-crash \
+ expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'Klass'}}
+
>From 9111797355ba860d0c6b62fde3a7b1e347a71dc6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?=
<alejandro.alvarez at sonarsource.com>
Date: Fri, 5 Jul 2024 15:12:47 +0200
Subject: [PATCH 2/5] Move the check to `CheckTemplateIdType()`
---
clang/lib/Sema/SemaTemplate.cpp | 3 ++-
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index a032e3ec6f635..2ea18ec82b958 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4700,7 +4700,8 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
// template<typename T, typename U = T> struct A;
CanonType = Context.getCanonicalTemplateSpecializationType(
Name, CanonicalConverted);
-
+ if (CanonType->containsErrors())
+ return QualType();
// This might work out to be a current instantiation, in which
// case the canonical type needs to be the InjectedClassNameType.
//
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 999fec0ebb259..0681520764d9a 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6300,7 +6300,7 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc));
}
QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args);
- if (T.isNull() || T->containsErrors())
+ if (T.isNull())
return nullptr;
CXXRecordDecl *SubstRecord = T->getAsCXXRecordDecl();
>From cf315c17ac5221e683af1260ef429c4b13fb5715 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?=
<alejandro.alvarez at sonarsource.com>
Date: Tue, 9 Jul 2024 11:21:44 +0200
Subject: [PATCH 3/5] Revert "Move the check to `CheckTemplateIdType()`"
This reverts commit 9111797355ba860d0c6b62fde3a7b1e347a71dc6.
---
clang/lib/Sema/SemaTemplate.cpp | 3 +--
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 2 +-
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 2ea18ec82b958..a032e3ec6f635 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4700,8 +4700,7 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
// template<typename T, typename U = T> struct A;
CanonType = Context.getCanonicalTemplateSpecializationType(
Name, CanonicalConverted);
- if (CanonType->containsErrors())
- return QualType();
+
// This might work out to be a current instantiation, in which
// case the canonical type needs to be the InjectedClassNameType.
//
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 0681520764d9a..999fec0ebb259 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6300,7 +6300,7 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc));
}
QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args);
- if (T.isNull())
+ if (T.isNull() || T->containsErrors())
return nullptr;
CXXRecordDecl *SubstRecord = T->getAsCXXRecordDecl();
>From b8baf3e496833360e908c6bf01f60ca890412369 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?=
<alejandro.alvarez at sonarsource.com>
Date: Wed, 10 Jul 2024 11:01:09 +0200
Subject: [PATCH 4/5] Add clarification
---
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 999fec0ebb259..73874228abbbf 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6300,6 +6300,11 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc));
}
QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args);
+ // We may get a non-null type with errors, in which case
+ // `getAsCXXRecordDecl` will return `nullptr`. For instance, this
+ // happens when one of the template arguments is an invalid
+ // expression. We return early to avoid triggering the assertion
+ // about the `CodeSynthesisContext`.
if (T.isNull() || T->containsErrors())
return nullptr;
CXXRecordDecl *SubstRecord = T->getAsCXXRecordDecl();
>From dbe06c0ddef2bbbc108e88d4920543860f5249d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?=
<alejandro.alvarez at sonarsource.com>
Date: Wed, 10 Jul 2024 11:05:41 +0200
Subject: [PATCH 5/5] Update ReleaseNotes.rst
---
clang/docs/ReleaseNotes.rst | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cd3a4c2b1be1a..d3586352b7168 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -794,6 +794,9 @@ Bug Fixes in This Version
- Fixed `static_cast` to array of unknown bound. Fixes (#GH62863).
+- Fixed an assertion failure when a template non-type parameter contains
+ an invalid expression.
+
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
More information about the cfe-commits
mailing list