diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp
index 2423785..6aa5782 100644
--- a/lib/Serialization/ASTReader.cpp
+++ b/lib/Serialization/ASTReader.cpp
@@ -8255,6 +8255,9 @@ void ASTReader::diagnoseOdrViolations() {
 
     if (!Found) {
       D->setInvalidDecl();
+      // Insert CanonDef now in case printing the diagnostic also deserializes
+      // something that lands us back here.
+      DiagnosedOdrMergeFailures.insert(CanonDef);
 
       std::string CanonDefModule =
           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
@@ -8271,8 +8274,6 @@ void ASTReader::diagnoseOdrViolations() {
                diag::note_module_odr_violation_possible_decl)
             << Candidates[I];
       }
-
-      DiagnosedOdrMergeFailures.insert(CanonDef);
     }
   }
 
diff --git a/test/Modules/odr.cpp b/test/Modules/odr.cpp
index 5ab10d2..ef14fd5 100644
--- a/test/Modules/odr.cpp
+++ b/test/Modules/odr.cpp
@@ -1,12 +1,18 @@
 // RUN: rm -rf %t
-// RUN: %clang_cc1 -x objective-c++ -fmodules -fmodules-cache-path=%t -I %S/Inputs/odr %s -verify -std=c++11
+// RUN: %clang_cc1 -x objective-c++ -fmodules -fmodules-cache-path=%t -I %S/Inputs/odr %s -verify -std=c++11 -DIMPORT
+// RUN: %clang_cc1 -x c++ -fmodules -fmodules-cache-path=%t -I %S/Inputs/odr %s -verify -std=c++11 -Wauto-import
 
 // expected-error@a.h:8 {{'X::n' from module 'a' is not present in definition of 'X' provided earlier}}
 struct X { // expected-note {{definition has no member 'n'}}
 };
 
+#ifdef IMPORT
 @import a;
 @import b;
+#else
+#include "a.h"
+#include "b.h"
+#endif
 
 // Trigger the declarations from a and b to be imported.
 int x = f() + g();
