[flang] Improve "Error reading module file" error message

Instead of using a message attachment with further details,
emit the details as part of a single message.

Differential Revision: https://reviews.llvm.org/D96465

GitOrigin-RevId: 0bfa4ac6c6fceffcffb3ae44027859cf6f5e06c0
diff --git a/lib/Semantics/mod-file.cpp b/lib/Semantics/mod-file.cpp
index af3267a..c3b95a7 100644
--- a/lib/Semantics/mod-file.cpp
+++ b/lib/Semantics/mod-file.cpp
@@ -861,13 +861,13 @@
 parser::Message &ModFileReader::Say(const SourceName &name,
     const std::string &ancestor, parser::MessageFixedText &&msg,
     const std::string &arg) {
-  return context_
-      .Say(name,
-          ancestor.empty()
-              ? "Error reading module file for module '%s'"_err_en_US
-              : "Error reading module file for submodule '%s' of module '%s'"_err_en_US,
-          name, ancestor)
-      .Attach(name, std::move(msg), arg);
+  return context_.Say(name, "Cannot read module file for %s: %s"_err_en_US,
+      parser::MessageFormattedText{ancestor.empty()
+              ? "module '%s'"_en_US
+              : "submodule '%s' of module '%s'"_en_US,
+          name, ancestor}
+          .MoveString(),
+      parser::MessageFormattedText{std::move(msg), arg}.MoveString());
 }
 
 // program was read from a .mod file for a submodule; return the name of the
diff --git a/test/Flang-Driver/include-module.f90 b/test/Flang-Driver/include-module.f90
index 3abf6c8..26e26a1 100644
--- a/test/Flang-Driver/include-module.f90
+++ b/test/Flang-Driver/include-module.f90
@@ -31,8 +31,8 @@
 !-----------------------------------------
 ! EXPECTED OUTPUT FOR MISSING MODULE FILE
 !-----------------------------------------
-! SINGLEINCLUDE:Error reading module file for module 'basictestmoduletwo'
-! SINGLEINCLUDE-NOT:Error reading module file for module 'basictestmoduletwo'
+! SINGLEINCLUDE:error: Cannot read module file for module 'basictestmoduletwo'
+! SINGLEINCLUDE-NOT:error: Cannot read module file for module 'basictestmoduletwo'
 ! SINGLEINCLUDE-NOT:error: Derived type 't1' not found
 ! SINGLEINCLUDE:error: Derived type 't2' not found
 
@@ -44,7 +44,7 @@
 !---------------------------------------
 ! EXPECTED OUTPUT FOR ALL MODULES FOUND
 !---------------------------------------
-! INCLUDED-NOT:Error reading module file
+! INCLUDED-NOT:error: Cannot read module file
 ! INCLUDED-NOT:error: Derived type 't1' not found
 ! INCLUDED:error: Derived type 't2' not found
 
diff --git a/test/Semantics/resolve12.f90 b/test/Semantics/resolve12.f90
index 967a0dc..241047a 100644
--- a/test/Semantics/resolve12.f90
+++ b/test/Semantics/resolve12.f90
@@ -6,7 +6,7 @@
 end
 
 use m1
-!ERROR: Error reading module file for module 'm2'
+!ERROR: Cannot read module file for module 'm2': Source file 'm2.mod' was not found
 use m2
 !ERROR: 'sub' is not a module
 use sub
diff --git a/test/Semantics/resolve26.f90 b/test/Semantics/resolve26.f90
index daf5fbe..6daf8b6 100644
--- a/test/Semantics/resolve26.f90
+++ b/test/Semantics/resolve26.f90
@@ -16,10 +16,10 @@
 submodule(m1) s1
 end
 
-!ERROR: Error reading module file for submodule 's1' of module 'm2'
+!ERROR: Cannot read module file for submodule 's1' of module 'm2': Source file 'm2-s1.mod' was not found
 submodule(m2:s1) s2
 end
 
-!ERROR: Error reading module file for module 'm3'
+!ERROR: Cannot read module file for module 'm3': Source file 'm3.mod' was not found
 submodule(m3:s1) s3
 end