-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Clang][Sema] Fix crash when using name of UnresolvedUsingValueDecl with template arguments #83842
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
@llvm/pr-subscribers-clang Author: Krystian Stasiowski (sdkrystian) ChangesThe following snippet causes a crash (godbolt link): template<typename T>
struct A : T {
using T::f;
void f();
void g() {
f<int>(); // crash here
}
}; This happens because we cast the result of Full diff: https://github.com/llvm/llvm-project/pull/83842.diff 3 Files Affected:
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 5a8fae76a43a4d..28dd69b8e45758 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -9200,7 +9200,8 @@ TemplateName
ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
UnresolvedSetIterator End) const {
unsigned size = End - Begin;
- assert(size > 1 && "set is not overloaded!");
+ assert((size == 1 && isa<UnresolvedUsingValueDecl>(*Begin)) ||
+ size > 1 && "set is not overloaded!");
void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
size * sizeof(FunctionTemplateDecl*));
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 9fdd8eb236d1ee..c62e4ce7d0f9c4 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1115,7 +1115,8 @@ Sema::NameClassification Sema::ClassifyName(Scope *S, CXXScopeSpec &SS,
bool IsFunctionTemplate;
bool IsVarTemplate;
TemplateName Template;
- if (Result.end() - Result.begin() > 1) {
+
+ if ((Result.end() - Result.begin() > 1) || Result.isUnresolvableResult()) {
IsFunctionTemplate = true;
Template = Context.getOverloadedTemplateName(Result.begin(),
Result.end());
diff --git a/clang/test/SemaTemplate/unqual-unresolved-using-value.cpp b/clang/test/SemaTemplate/unqual-unresolved-using-value.cpp
new file mode 100644
index 00000000000000..7c45342adce783
--- /dev/null
+++ b/clang/test/SemaTemplate/unqual-unresolved-using-value.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+
+template<typename T>
+struct A : T {
+ using T::f;
+ using T::g;
+
+ void f();
+ void g();
+
+ void h() {
+ f<int>();
+ g<int>(); // expected-error{{no member named 'g' in 'A<B>'}}
+ }
+};
+
+struct B {
+ template<typename T>
+ void f();
+
+ void g();
+};
+
+template struct A<B>; // expected-note{{in instantiation of member function 'A<B>::h' requested here}}
|
Actually, I don't think the proposed fix here is quite right. If we only find an |
9a464d3
to
de29206
Compare
Updated with new fix (still need a release note) |
✅ With the latest revision this PR passed the C/C++ code formatter. |
de29206
to
763c7d0
Compare
Please update the patch message in github (which should allow editing) to reflect the new approach. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems right to me, modulo the release note/updated commit message.
fcd5980
to
6b93bef
Compare
Hi:
Is this a known issue or any thing obvious to fix? If not, I will try to get a reduced reproducer. |
@wlei-llvm Not a known issue... could you provide a repro? I'll look into this in the meantime. |
@sdkrystian we, at google, bisected lots (~1k) of clang crashes to this revision. Looks to me Facebook code is also impacted. We need some time to provide a repro. Until then can you please revert? |
…ueDecl with template arguments (llvm#83842)" This reverts commit a642eb8.
@bgra8 Reverted. Any sort of repro would be appreciated :) |
Thanks a lot @sdkrystian ! We have a reducer session running! We'll post here when we have it! |
Here is the repro from our side. It's reduced by creduce, there are some unrelated error, the assertion is the real issue.
|
|
@wlei-llvm Thank you! I've reduced the repro to this: struct A { };
template<typename T>
void f(A);
struct B {
void f();
void g() {
f<A>(A());
}
}; |
So, it seems that this crash occurs because we filter out all non-template functions, which will trigger ADL if the only class member we found was a non-template function. The current behavior (without this patch applied) isn't correct either, since we clear all lookup results (which triggers ADL). |
The following snippet causes a crash (godbolt link):
This happens because we cast the result of
getAsTemplateNameDecl
as aTemplateDecl
inSema::ClassifyName
, which we cannot do for anUnresolvedUsingValueDecl
.I believe the correct thing to do here is create anThis patch fixes the crash by considering a name to be that of a template if any function declaration is found per [temp.names] p3.3.OverloadedTemplateName
, since it may instantiate to a using declaration naming any number of declarations.