فهرست منبع

Add the package and library to an import diagnostic. (#3876)

While most of the examples in our tests are made clear by the code
snippet, sometimes that's not the case. This makes it more clear when
the implicit `Main` package is used for example. I added this because I
got an error message without any source code to show in the snippet
(importing the prelude for example) and this makes the error much more
understandable.
Chandler Carruth 2 سال پیش
والد
کامیت
f54f53adf5

+ 17 - 5
toolchain/check/check.cpp

@@ -805,6 +805,16 @@ static auto GetImportKey(UnitInfo& unit_info, IdentifierId file_package_id,
 
 static constexpr llvm::StringLiteral ExplicitMainName = "Main";
 
+static auto RenderImportKey(ImportKey import_key) -> std::string {
+  if (import_key.first.empty()) {
+    import_key.first = ExplicitMainName;
+  }
+  if (import_key.second.empty()) {
+    return import_key.first.str();
+  }
+  return llvm::formatv("{0}//{1}", import_key.first, import_key.second).str();
+}
+
 // Marks an import as required on both the source and target file.
 //
 // The ID comparisons between the import and unit are okay because they both
@@ -930,11 +940,13 @@ static auto TrackImport(
     // The imported api is missing.
     package_imports_it->second.has_load_error = true;
     CARBON_DIAGNOSTIC(LibraryApiNotFound, Error,
-                      "Corresponding API not found.");
-    CARBON_DIAGNOSTIC(ImportNotFound, Error, "Imported API not found.");
-    unit_info.emitter.Emit(import.node_id, explicit_import_map
-                                               ? ImportNotFound
-                                               : LibraryApiNotFound);
+                      "Corresponding API for '{0}' not found.", std::string);
+    CARBON_DIAGNOSTIC(ImportNotFound, Error, "Imported API '{0}' not found.",
+                      std::string);
+    unit_info.emitter.Emit(
+        import.node_id,
+        explicit_import_map ? ImportNotFound : LibraryApiNotFound,
+        RenderImportKey(import_key));
   }
 }
 

+ 3 - 3
toolchain/check/testdata/packages/fail_api_not_found.carbon

@@ -6,7 +6,7 @@
 
 // --- fail_no_api.impl.carbon
 
-// CHECK:STDERR: fail_no_api.impl.carbon:[[@LINE+4]]:1: ERROR: Corresponding API not found.
+// CHECK:STDERR: fail_no_api.impl.carbon:[[@LINE+4]]:1: ERROR: Corresponding API for 'Foo' not found.
 // CHECK:STDERR: package Foo impl;
 // CHECK:STDERR: ^~~~~~~
 // CHECK:STDERR:
@@ -14,7 +14,7 @@ package Foo impl;
 
 // --- fail_no_api_lib.impl.carbon
 
-// CHECK:STDERR: fail_no_api_lib.impl.carbon:[[@LINE+4]]:1: ERROR: Corresponding API not found.
+// CHECK:STDERR: fail_no_api_lib.impl.carbon:[[@LINE+4]]:1: ERROR: Corresponding API for 'Foo//Bar' not found.
 // CHECK:STDERR: package Foo library "Bar" impl;
 // CHECK:STDERR: ^~~~~~~
 // CHECK:STDERR:
@@ -22,7 +22,7 @@ package Foo library "Bar" impl;
 
 // --- fail_no_api_main_lib.impl.carbon
 
-// CHECK:STDERR: fail_no_api_main_lib.impl.carbon:[[@LINE+3]]:1: ERROR: Corresponding API not found.
+// CHECK:STDERR: fail_no_api_main_lib.impl.carbon:[[@LINE+3]]:1: ERROR: Corresponding API for 'Main//Bar' not found.
 // CHECK:STDERR: library "Bar" impl;
 // CHECK:STDERR: ^~~~~~~
 library "Bar" impl;

+ 1 - 1
toolchain/check/testdata/packages/fail_import_invalid.carbon

@@ -85,7 +85,7 @@ import Implicit library "lib";
 // --- fail_not_found.carbon
 package NotFound api;
 
-// CHECK:STDERR: fail_not_found.carbon:[[@LINE+3]]:1: ERROR: Imported API not found.
+// CHECK:STDERR: fail_not_found.carbon:[[@LINE+3]]:1: ERROR: Imported API 'ImportNotFound' not found.
 // CHECK:STDERR: import ImportNotFound;
 // CHECK:STDERR: ^~~~~~
 import ImportNotFound;

+ 1 - 1
toolchain/check/testdata/packages/fail_name_with_import_failure.carbon

@@ -6,7 +6,7 @@
 
 // --- fail_implicit.impl.carbon
 
-// CHECK:STDERR: fail_implicit.impl.carbon:[[@LINE+3]]:1: ERROR: Corresponding API not found.
+// CHECK:STDERR: fail_implicit.impl.carbon:[[@LINE+3]]:1: ERROR: Corresponding API for 'Implicit' not found.
 // CHECK:STDERR: package Implicit impl;
 // CHECK:STDERR: ^~~~~~~
 package Implicit impl;