From f54f53adf5053c16014e1acd74da70a54023be33 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Wed, 10 Apr 2024 01:40:20 -0700 Subject: [PATCH] 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. --- toolchain/check/check.cpp | 22 ++++++++++++++----- .../packages/fail_api_not_found.carbon | 6 ++--- .../packages/fail_import_invalid.carbon | 2 +- .../fail_name_with_import_failure.carbon | 2 +- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/toolchain/check/check.cpp b/toolchain/check/check.cpp index 555967d7fd10..faf53a28eb04 100644 --- a/toolchain/check/check.cpp +++ b/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)); } } diff --git a/toolchain/check/testdata/packages/fail_api_not_found.carbon b/toolchain/check/testdata/packages/fail_api_not_found.carbon index 4693eac0aca1..4431fa663083 100644 --- a/toolchain/check/testdata/packages/fail_api_not_found.carbon +++ b/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; diff --git a/toolchain/check/testdata/packages/fail_import_invalid.carbon b/toolchain/check/testdata/packages/fail_import_invalid.carbon index b146d3d940a6..7d0abf257a26 100644 --- a/toolchain/check/testdata/packages/fail_import_invalid.carbon +++ b/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; diff --git a/toolchain/check/testdata/packages/fail_name_with_import_failure.carbon b/toolchain/check/testdata/packages/fail_name_with_import_failure.carbon index 91fd8f64e01d..1b5b57c05b8e 100644 --- a/toolchain/check/testdata/packages/fail_name_with_import_failure.carbon +++ b/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;