Skip to content

[Clang][Sema] fix noexecpt mismatch of friend declaration #102267

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ Bug Fixes to C++ Support
- Clang now preserves the unexpanded flag in a lambda transform used for pack expansion. (#GH56852), (#GH85667),
(#GH99877).
- Fixed a bug when diagnosing ambiguous explicit specializations of constrained member functions.
- Fix mismatch of noexecpt in friend function declaration by copying template instantiated parameters to
current local scope.(#GH101330).

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
15 changes: 15 additions & 0 deletions clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4698,7 +4698,22 @@ void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
// Enter the scope of this instantiation. We don't use
// PushDeclContext because we don't have a scope.
Sema::ContextRAII savedContext(*this, Decl);

FunctionDecl *Source = Proto->getExtProtoInfo().ExceptionSpec.SourceTemplate;
FunctionTemplateDecl *SourceTemplate = Source->getDescribedFunctionTemplate();
llvm::SmallDenseMap<clang::Decl *, clang::Decl *> InstTemplateParams;
if (CurrentInstantiationScope && SourceTemplate)
if (TemplateParameterList *TPL = SourceTemplate->getTemplateParameters())
for (NamedDecl *TemplateParam : *TPL)
if (auto *Found =
CurrentInstantiationScope->findInstantiationOf(TemplateParam))
if (auto *InstTemplateParam = Found->dyn_cast<clang::Decl *>())
InstTemplateParams[TemplateParam] = InstTemplateParam;

Comment on lines +4707 to +4712
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is handling the pack case..
Do we have a generic way to copy instantiated template params? @zyn0217 @mizvekov @Sirraide
We do similar things in a bunch of places (including when checking constraints)

Copy link
Contributor

Choose a reason for hiding this comment

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

Not that I’m aware of. I think we can wait for @sdkrystian‘s refactoring patch of MLTAL which might make such things easier.

Copy link
Contributor

Choose a reason for hiding this comment

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

... including when checking constraints ...

This might not be the case for template parameters, as we (almost always, in my recollection) would have transformed the associated template parameters before calling the constraint matching function, so...

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we should wait until I reland #106585 and then we can fix this. What we can then do is normalize the exception specification the same way we normalize constraints prior to comparing them.

Copy link
Contributor

Choose a reason for hiding this comment

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

... the same way we normalize constraints prior to comparing them.

Which means we don't need an individual instantiation scope for the noexcept specification thereafter, so the question here could be resolved naturally I presume?

LocalInstantiationScope Scope(*this);
for (auto [TemplateParam, InstTemplateParam] : InstTemplateParams) {
Scope.InstantiatedLocal(TemplateParam, InstTemplateParam);
}

MultiLevelTemplateArgumentList TemplateArgs =
getTemplateInstantiationArgs(Decl, Decl->getLexicalDeclContext(),
Expand Down
16 changes: 16 additions & 0 deletions clang/test/SemaCXX/pr101330.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
// expected-no-diagnostics

template <typename T>
struct C {
template <int N, typename U>
friend void func(const C<U> &m) noexcept(N == 0);
};

template <int N, typename U>
void func(const C<U> &m) noexcept(N == 0) {}

int main() {
C<int> t;
return 0;
}
Loading