Skip to content
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

[clang] crash when meeting template friend function #62265

Closed
HerrCai0907 opened this issue Apr 20, 2023 · 6 comments
Closed

[clang] crash when meeting template friend function #62265

HerrCai0907 opened this issue Apr 20, 2023 · 6 comments
Labels
clang:codegen confirmed Verified by a second party crash-on-valid question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@HerrCai0907
Copy link
Contributor

template <class T> int Foo(T X);
template <int N, int B, int C> struct Thief {
  template <class T> friend int Foo(T X) { 
    return B; 
  }
};
template struct Thief<1,2,3>;
int main() {
  Foo(1.0);
}

https://godbolt.org/z/vEGdvhc3q

@HerrCai0907 HerrCai0907 added crash Prefer [crash-on-valid] or [crash-on-invalid] clang Clang issues not falling into any other category labels Apr 20, 2023
@HerrCai0907
Copy link
Contributor Author

I have done some work for it. The issue is in
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp Line 5065

    MultiLevelTemplateArgumentList TemplateArgs = getTemplateInstantiationArgs(
        Function, /*Final=*/false, nullptr, false, PatternDecl);

TemplateArgs in here is [[T]]. But it should be [[N, B, C], [T]]

@EugeneZelenko EugeneZelenko added clang:codegen and removed clang Clang issues not falling into any other category labels Apr 20, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 20, 2023

@llvm/issue-subscribers-clang-codegen

@shafik
Copy link
Collaborator

shafik commented Apr 20, 2023

Confirmed, feels like something that @erichkeane @alexander-shaposhnikov might know about

@shafik shafik added confirmed Verified by a second party crash-on-valid and removed crash Prefer [crash-on-valid] or [crash-on-invalid] labels Apr 20, 2023
@erichkeane
Copy link
Collaborator

https://reviews.llvm.org/D146178 alters that a bit, but I could definitely believe we have another bug in there. The getTemplateInstantiationArgs function is still a 'work in progress' these days (its being pushed to do things it never had to do before, particularly with concepts), but it looks like this is friend related (which is a little surprising).

Perhaps we're just quitting too early on collecting args on inline-defined-friends like this,

@HerrCai0907
Copy link
Contributor Author

HerrCai0907 commented Apr 21, 2023

but it looks like this is friend related.

Yeah, for inline friend, it does not consider the template in outsider RecordDecl.
But I don't find a solution to get it from FunctionDecl's context. I need a ClassTemplateSpecializationDecl to get the template arg list but I can only get a CXXRecord or ClassTemplateDecl.

@shafik @erichkeane Do you have some suggestion to fix it?

@erichkeane
Copy link
Collaborator

So you SHOULD have been able to get it from the lexicalDeclContext, but it appears that isn't set correctly. So you'll have to fix that FIRST.

I see that the FunctionDecl is created in SemaTemplateInstantiateDecl.cpp ~2138 and ~2160 seems to not be doing the right thing here. For some reason, Owner isn't the right value I think, so some debugging will be needed there.

@EugeneZelenko EugeneZelenko added the question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! label Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen confirmed Verified by a second party crash-on-valid question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

5 participants