Fix crash-on-invalid when trying to recover from a function template
authorRichard Smith <richard@metafoo.co.uk>
Tue, 10 Mar 2020 23:33:42 +0000 (16:33 -0700)
committerRichard Smith <richard@metafoo.co.uk>
Tue, 10 Mar 2020 23:34:27 +0000 (16:34 -0700)
being deleted on its second or subsequent declaration.

clang/lib/Sema/SemaDeclCXX.cpp
clang/test/SemaCXX/cxx0x-cursory-default-delete.cpp

index 7331c33..2dfefa3 100644 (file)
@@ -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();
   }
 
index 7a581fd..1ec484a 100644 (file)
@@ -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}}