Skip to content

[Sema] Fix assertion error in Sema::FindInstantiatedDecl #96509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

alejandro-alvarez-sonarsource
Copy link
Contributor

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.

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`.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 24, 2024

@llvm/pr-subscribers-clang

Author: Alejandro Álvarez Ayllón (alejandro-alvarez-sonarsource)

Changes

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.


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaTemplateInstantiateDecl.cpp (+1-1)
  • (added) clang/test/SemaCXX/instantiate-template-broken-nontype-default.cpp (+12)
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'}}
+

@alejandro-alvarez-sonarsource
Copy link
Contributor Author

Ping (@cor3ntin, @Endilll ?)

Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, but wait for other reviewers.
You published this PR on the first day of WG21 meeting, so it didn't get the usual amount of attention.

@Endilll Endilll requested review from AaronBallman and Sirraide July 3, 2024 10:04
@cor3ntin
Copy link
Contributor

cor3ntin commented Jul 6, 2024

Thanks for the fix; I think this makes sense. Can you provide a release note?

@alejandro-alvarez-sonarsource alejandro-alvarez-sonarsource changed the title [Sema] Fix crash in Sema::FindInstantiatedDecl [Sema] Fix assertion error in Sema::FindInstantiatedDecl Jul 10, 2024
@alejandro-alvarez-sonarsource
Copy link
Contributor Author

Thanks for the fix; I think this makes sense. Can you provide a release note?

Added (and fixed the conflict)

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Do you need me to merge that for you?

@alejandro-alvarez-sonarsource
Copy link
Contributor Author

alejandro-alvarez-sonarsource commented Jul 17, 2024

LGTM. Do you need me to merge that for you?

Yes, thanks! (P.S And another conflict resolution done)

@Sirraide Sirraide merged commit e5df657 into llvm:main Jul 19, 2024
8 checks passed
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
...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`.
@alejandro-alvarez-sonarsource alejandro-alvarez-sonarsource deleted the aa/fix_crash_missing_non_type_param branch September 10, 2024 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants