Skip to content

Commit

Permalink
Fix crash-on-invalid when trying to recover from a function template
Browse files Browse the repository at this point in the history
being deleted on its second or subsequent declaration.
  • Loading branch information
zygoloid committed Mar 10, 2020
1 parent d07f9e7 commit 4cba668
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions clang/lib/Sema/SemaDeclCXX.cpp
Expand Up @@ -16349,9 +16349,16 @@ void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) {
Diag(Prev->getLocation().isInvalid() ? DelLoc : Prev->getLocation(),
Prev->isImplicit() ? diag::note_previous_implicit_declaration
: diag::note_previous_declaration);
// We can't recover from this; the declaration might have already
// been used.
Fn->setInvalidDecl();
return;
}
// If the declaration wasn't the first, we delete the function anyway for
// recovery.

// To maintain the invariant that functions are only deleted on their first
// declaration, mark the implicitly-instantiated declaration of the
// explicitly-specialized function as deleted instead of marking the
// instantiated redeclaration.
Fn = Fn->getCanonicalDecl();
}

Expand Down
12 changes: 12 additions & 0 deletions clang/test/SemaCXX/cxx0x-cursory-default-delete.cpp
Expand Up @@ -199,3 +199,15 @@ S::S() __attribute((pure)) = default;
using size_t = decltype(sizeof(0));
void *operator new(size_t) = delete; // expected-error {{deleted definition must be first declaration}} expected-note {{implicit}}
void operator delete(void *) noexcept = delete; // expected-error {{deleted definition must be first declaration}} expected-note {{implicit}}

// FIXME: Diagnosing the call as "no matching function" due to substitution
// failure is not ideal.
template<typename T> void delete_template_too_late(); // expected-note {{previous}}
template<typename T> void delete_template_too_late() = delete; // expected-error {{must be first decl}} expected-note {{substitution failure}}
void use_template_deleted_too_late() { delete_template_too_late<int>(); } // expected-error {{no matching function}}

struct DeletedMemberTemplateTooLate {
template<typename T> static void f(); // expected-note {{previous}}
};
template<typename T> void DeletedMemberTemplateTooLate::f() = delete; // expected-error {{must be first decl}} expected-note {{substitution failure}}
void use_member_template_deleted_too_late() { DeletedMemberTemplateTooLate::f<int>(); } // expected-error {{no matching function}}

0 comments on commit 4cba668

Please sign in to comment.