Skip to content

Fix out of line Concept-comparisons of NestedNameSpecifiers #65993

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

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ Bug Fixes to C++ Support
(`#65067 <https://github.com/llvm/llvm-project/issues/65067>`_` and
`#63675 <https://github.com/llvm/llvm-project/issues/63675>`_`)

- Clang now properly handles out of line template specializations when there is
a non-template inner-class between the function and the class template.
(`#65810 <https://github.com/llvm/llvm-project/issues/65810>`_)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed an import failure of recursive friend class template.
Expand Down
24 changes: 17 additions & 7 deletions clang/lib/Sema/SemaTemplateInstantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,25 @@ Response HandleFunction(const FunctionDecl *Function,
Response HandleFunctionTemplateDecl(const FunctionTemplateDecl *FTD,
MultiLevelTemplateArgumentList &Result) {
if (!isa<ClassTemplateSpecializationDecl>(FTD->getDeclContext())) {
Result.addOuterTemplateArguments(
const_cast<FunctionTemplateDecl *>(FTD),
const_cast<FunctionTemplateDecl *>(FTD)->getInjectedTemplateArgs(),
/*Final=*/false);

NestedNameSpecifier *NNS = FTD->getTemplatedDecl()->getQualifier();
const Type *Ty;
const TemplateSpecializationType *TSTy;
if (NNS && (Ty = NNS->getAsType()) &&
(TSTy = Ty->getAs<TemplateSpecializationType>()))
Result.addOuterTemplateArguments(const_cast<FunctionTemplateDecl *>(FTD),
TSTy->template_arguments(),
/*Final=*/false);

while (const Type *Ty = NNS ? NNS->getAsType() : nullptr) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This make sense, I was just looking at the AST: https://godbolt.org/z/P3brjcsMG

and I see that InnerClass has:

| |-NestedNameSpecifier TypeSpec 'Base<T>'

and I wondering what the prefix is there? Maybe the AST is not telling the whole story?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So that one you're seeing there is for the InnerClass on line 13, the NestedNameSpecifier is for 'everything else' outside of that declaration. So in THAT case, the Prefix of that one is nothing, since the Base<T> is the entirety of the thing.

However, that AST doesn't show the NNS of func on line 22, for some reason we're not dumping that. In THAT case, the NNS is Base<T>::InnerClass. At that point, the InnerClass doesn't have template arguments (since it isn't a template!), but its Prefix is just the Base<T>, which has them (which is why/how this loop works).

Copy link
Collaborator

Choose a reason for hiding this comment

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

It does not look like there is a simple way of getting like w/ CXXRecordDecl so some work would be involved.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't understand what you mean? TagDecl (IIRC?) has a getQualifier() to get the appropriate NestedNameSpecifier. I believe our NNS tree is correct the way I've implemented it above, with the exception of the issue I posted about a moment ago.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I just realized I was looking at the code a moment ago FTD->getTemplatedDecl()->getQualifier()

if (NNS->isInstantiationDependent()) {
if (const auto *TSTy = Ty->getAs<TemplateSpecializationType>())
Result.addOuterTemplateArguments(
const_cast<FunctionTemplateDecl *>(FTD), TSTy->template_arguments(),
/*Final=*/false);
}

NNS = NNS->getPrefix();
}
}

return Response::ChangeDecl(FTD->getLexicalDeclContext());
}

Expand Down
48 changes: 48 additions & 0 deletions clang/test/SemaTemplate/concepts-out-of-line-def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,51 @@ template<typename T> concept A = true;
template<typename T> struct X { A<T> auto f(); };
template<typename T> A<T> auto X<T>::f() {}
}

namespace GH65810 {
template<typename Param>
concept TrivialConcept =
requires(Param param) {
(void)param;
};

template <typename T>
struct Base {
class InnerClass;
};

template <typename T>
class Base<T>::InnerClass {
template <typename Param>
requires TrivialConcept<Param>
int func(Param param) const;
};

template <typename T>
template <typename Param>
requires TrivialConcept<Param>
int Base<T>::InnerClass::func(Param param) const {
return 0;
}

template<typename T>
struct Outermost {
struct Middle {
template<typename U>
struct Innermost {
template <typename Param>
requires TrivialConcept<Param>
int func(Param param) const;
};
};
};

template <typename T>
template <typename U>
template <typename Param>
requires TrivialConcept<Param>
int Outermost<T>::Middle::Innermost<U>::func(Param param) const {
return 0;
}

} // namespace GH65810