check.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/check.h"
  5. #include <string>
  6. #include <utility>
  7. #include "common/check.h"
  8. #include "common/map.h"
  9. #include "toolchain/check/check_unit.h"
  10. #include "toolchain/check/context.h"
  11. #include "toolchain/check/diagnostic_emitter.h"
  12. #include "toolchain/check/diagnostic_helpers.h"
  13. #include "toolchain/diagnostics/diagnostic.h"
  14. #include "toolchain/diagnostics/format_providers.h"
  15. #include "toolchain/lex/token_kind.h"
  16. #include "toolchain/parse/node_ids.h"
  17. #include "toolchain/parse/tree.h"
  18. #include "toolchain/sem_ir/file.h"
  19. #include "toolchain/sem_ir/typed_insts.h"
  20. namespace Carbon::Check {
  21. // The package and library names, used as map keys.
  22. using ImportKey = std::pair<llvm::StringRef, llvm::StringRef>;
  23. // Returns a key form of the package object. file_package_id is only used for
  24. // imports, not the main package declaration; as a consequence, it will be
  25. // `None` for the main package declaration.
  26. static auto GetImportKey(UnitAndImports& unit_info,
  27. PackageNameId file_package_id,
  28. Parse::Tree::PackagingNames names) -> ImportKey {
  29. auto* stores = unit_info.unit->value_stores;
  30. PackageNameId package_id =
  31. names.package_id.has_value() ? names.package_id : file_package_id;
  32. llvm::StringRef package_name;
  33. if (package_id.has_value()) {
  34. auto package_ident_id = package_id.AsIdentifierId();
  35. package_name = package_ident_id.has_value()
  36. ? stores->identifiers().Get(package_ident_id)
  37. : package_id.AsSpecialName();
  38. }
  39. llvm::StringRef library_name =
  40. names.library_id.has_value()
  41. ? stores->string_literal_values().Get(names.library_id)
  42. : "";
  43. return {package_name, library_name};
  44. }
  45. static constexpr llvm::StringLiteral CppPackageName = "Cpp";
  46. static constexpr llvm::StringLiteral MainPackageName = "Main";
  47. static auto RenderImportKey(ImportKey import_key) -> std::string {
  48. if (import_key.first.empty()) {
  49. import_key.first = MainPackageName;
  50. }
  51. if (import_key.second.empty()) {
  52. return import_key.first.str();
  53. }
  54. return llvm::formatv("{0}//{1}", import_key.first, import_key.second).str();
  55. }
  56. // Marks an import as required on both the source and target file.
  57. //
  58. // The ID comparisons between the import and unit are okay because they both
  59. // come from the same file.
  60. static auto TrackImport(Map<ImportKey, UnitAndImports*>& api_map,
  61. Map<ImportKey, Parse::NodeId>* explicit_import_map,
  62. UnitAndImports& unit_info,
  63. Parse::Tree::PackagingNames import, bool fuzzing)
  64. -> void {
  65. const auto& packaging = unit_info.parse_tree().packaging_decl();
  66. PackageNameId file_package_id =
  67. packaging ? packaging->names.package_id : PackageNameId::None;
  68. const auto import_key = GetImportKey(unit_info, file_package_id, import);
  69. const auto& [import_package_name, import_library_name] = import_key;
  70. if (import_package_name == CppPackageName) {
  71. if (import_library_name.empty()) {
  72. CARBON_DIAGNOSTIC(CppInteropMissingLibrary, Error,
  73. "`Cpp` import missing library");
  74. unit_info.emitter.Emit(import.node_id, CppInteropMissingLibrary);
  75. return;
  76. }
  77. if (fuzzing) {
  78. // Clang is not crash-resilient.
  79. CARBON_DIAGNOSTIC(CppInteropFuzzing, Error,
  80. "`Cpp` import found during fuzzing");
  81. unit_info.emitter.Emit(import.node_id, CppInteropFuzzing);
  82. return;
  83. }
  84. unit_info.cpp_import_names.push_back(import);
  85. return;
  86. }
  87. // True if the import has `Main` as the package name, even if it comes from
  88. // the file's packaging (diagnostics may differentiate).
  89. bool is_explicit_main = import_package_name == MainPackageName;
  90. // Explicit imports need more validation than implicit ones. We try to do
  91. // these in an order of imports that should be removed, followed by imports
  92. // that might be valid with syntax fixes.
  93. if (explicit_import_map) {
  94. // Diagnose redundant imports.
  95. if (auto insert_result =
  96. explicit_import_map->Insert(import_key, import.node_id);
  97. !insert_result.is_inserted()) {
  98. CARBON_DIAGNOSTIC(RepeatedImport, Error,
  99. "library imported more than once");
  100. CARBON_DIAGNOSTIC(FirstImported, Note, "first import here");
  101. unit_info.emitter.Build(import.node_id, RepeatedImport)
  102. .Note(insert_result.value(), FirstImported)
  103. .Emit();
  104. return;
  105. }
  106. // True if the file's package is implicitly `Main` (by omitting an explicit
  107. // package name).
  108. bool is_file_implicit_main =
  109. !packaging || !packaging->names.package_id.has_value();
  110. // True if the import is using implicit "current package" syntax (by
  111. // omitting an explicit package name).
  112. bool is_import_implicit_current_package = !import.package_id.has_value();
  113. // True if the import is using `default` library syntax.
  114. bool is_import_default_library = !import.library_id.has_value();
  115. // True if the import and file point at the same package, even by
  116. // incorrectly specifying the current package name to `import`.
  117. bool is_same_package = is_import_implicit_current_package ||
  118. import.package_id == file_package_id;
  119. // True if the import points at the same library as the file's library.
  120. bool is_same_library =
  121. is_same_package &&
  122. (packaging ? import.library_id == packaging->names.library_id
  123. : is_import_default_library);
  124. // Diagnose explicit imports of the same library, whether from `api` or
  125. // `impl`.
  126. if (is_same_library) {
  127. CARBON_DIAGNOSTIC(ExplicitImportApi, Error,
  128. "explicit import of `api` from `impl` file is "
  129. "redundant with implicit import");
  130. CARBON_DIAGNOSTIC(ImportSelf, Error, "file cannot import itself");
  131. bool is_impl = !packaging || packaging->is_impl;
  132. unit_info.emitter.Emit(import.node_id,
  133. is_impl ? ExplicitImportApi : ImportSelf);
  134. return;
  135. }
  136. // Diagnose explicit imports of `Main//default`. There is no `api` for it.
  137. // This lets other diagnostics handle explicit `Main` package naming.
  138. if (is_file_implicit_main && is_import_implicit_current_package &&
  139. is_import_default_library) {
  140. CARBON_DIAGNOSTIC(ImportMainDefaultLibrary, Error,
  141. "cannot import `Main//default`");
  142. unit_info.emitter.Emit(import.node_id, ImportMainDefaultLibrary);
  143. return;
  144. }
  145. if (!is_import_implicit_current_package) {
  146. // Diagnose explicit imports of the same package that use the package
  147. // name.
  148. if (is_same_package || (is_file_implicit_main && is_explicit_main)) {
  149. CARBON_DIAGNOSTIC(
  150. ImportCurrentPackageByName, Error,
  151. "imports from the current package must omit the package name");
  152. unit_info.emitter.Emit(import.node_id, ImportCurrentPackageByName);
  153. return;
  154. }
  155. // Diagnose explicit imports from `Main`.
  156. if (is_explicit_main) {
  157. CARBON_DIAGNOSTIC(ImportMainPackage, Error,
  158. "cannot import `Main` from other packages");
  159. unit_info.emitter.Emit(import.node_id, ImportMainPackage);
  160. return;
  161. }
  162. }
  163. } else if (is_explicit_main) {
  164. // An implicit import with an explicit `Main` occurs when a `package` rule
  165. // has bad syntax, which will have been diagnosed when building the API map.
  166. // As a consequence, we return silently.
  167. return;
  168. }
  169. // Get the package imports, or create them if this is the first.
  170. auto create_imports = [&]() -> int32_t {
  171. int32_t index = unit_info.package_imports.size();
  172. unit_info.package_imports.push_back(
  173. PackageImports(import.package_id, import.node_id));
  174. return index;
  175. };
  176. auto insert_result =
  177. unit_info.package_imports_map.Insert(import.package_id, create_imports);
  178. PackageImports& package_imports =
  179. unit_info.package_imports[insert_result.value()];
  180. if (auto api_lookup = api_map.Lookup(import_key)) {
  181. // Add references between the file and imported api.
  182. UnitAndImports* api = api_lookup.value();
  183. package_imports.imports.push_back({import, api});
  184. ++unit_info.imports_remaining;
  185. api->incoming_imports.push_back(&unit_info);
  186. // If this is the implicit import, note we have it.
  187. if (!explicit_import_map) {
  188. CARBON_CHECK(!unit_info.api_for_impl);
  189. unit_info.api_for_impl = api;
  190. }
  191. } else {
  192. // The imported api is missing.
  193. package_imports.has_load_error = true;
  194. if (!explicit_import_map && import_package_name == CppPackageName) {
  195. // Don't diagnose the implicit import in `impl package Cpp`, because we'll
  196. // have diagnosed the use of `Cpp` in the declaration.
  197. return;
  198. }
  199. CARBON_DIAGNOSTIC(LibraryApiNotFound, Error,
  200. "corresponding API for '{0}' not found", std::string);
  201. CARBON_DIAGNOSTIC(ImportNotFound, Error, "imported API '{0}' not found",
  202. std::string);
  203. unit_info.emitter.Emit(
  204. import.node_id,
  205. explicit_import_map ? ImportNotFound : LibraryApiNotFound,
  206. RenderImportKey(import_key));
  207. }
  208. }
  209. // Builds a map of `api` files which might be imported. Also diagnoses issues
  210. // related to the packaging because the strings are loaded as part of getting
  211. // the ImportKey (which we then do for `impl` files too).
  212. static auto BuildApiMapAndDiagnosePackaging(
  213. llvm::MutableArrayRef<UnitAndImports> unit_infos)
  214. -> Map<ImportKey, UnitAndImports*> {
  215. Map<ImportKey, UnitAndImports*> api_map;
  216. for (auto& unit_info : unit_infos) {
  217. const auto& packaging = unit_info.parse_tree().packaging_decl();
  218. // An import key formed from the `package` or `library` declaration. Or, for
  219. // Main//default, a placeholder key.
  220. auto import_key = packaging ? GetImportKey(unit_info, PackageNameId::None,
  221. packaging->names)
  222. // Construct a boring key for Main//default.
  223. : ImportKey{"", ""};
  224. // Diagnose restricted package names before they become marked as possible
  225. // APIs.
  226. if (import_key.first == MainPackageName) {
  227. CARBON_DIAGNOSTIC(ExplicitMainPackage, Error,
  228. "`Main//default` must omit `package` declaration");
  229. CARBON_DIAGNOSTIC(
  230. ExplicitMainLibrary, Error,
  231. "use `library` declaration in `Main` package libraries");
  232. unit_info.emitter.Emit(packaging->names.node_id,
  233. import_key.second.empty() ? ExplicitMainPackage
  234. : ExplicitMainLibrary);
  235. continue;
  236. } else if (import_key.first == CppPackageName) {
  237. CARBON_DIAGNOSTIC(CppPackageDeclaration, Error,
  238. "`Cpp` cannot be used by a `package` declaration");
  239. unit_info.emitter.Emit(packaging->names.node_id, CppPackageDeclaration);
  240. continue;
  241. }
  242. bool is_impl = packaging && packaging->is_impl;
  243. // Add to the `api` map and diagnose duplicates. This occurs before the
  244. // file extension check because we might emit both diagnostics in situations
  245. // where the user forgets (or has syntax errors with) a package line
  246. // multiple times.
  247. if (!is_impl) {
  248. auto insert_result = api_map.Insert(import_key, &unit_info);
  249. if (!insert_result.is_inserted()) {
  250. llvm::StringRef prev_filename =
  251. insert_result.value()->source().filename();
  252. if (packaging) {
  253. CARBON_DIAGNOSTIC(DuplicateLibraryApi, Error,
  254. "library's API previously provided by `{0}`",
  255. std::string);
  256. unit_info.emitter.Emit(packaging->names.node_id, DuplicateLibraryApi,
  257. prev_filename.str());
  258. } else {
  259. CARBON_DIAGNOSTIC(DuplicateMainApi, Error,
  260. "`Main//default` previously provided by `{0}`",
  261. std::string);
  262. // Use `NodeId::None` because there's no node to associate with.
  263. unit_info.emitter.Emit(Parse::NodeId::None, DuplicateMainApi,
  264. prev_filename.str());
  265. }
  266. }
  267. }
  268. // Validate file extensions. Note imports rely the packaging declaration,
  269. // not the extension. If the input is not a regular file, for example
  270. // because it is stdin, no filename checking is performed.
  271. if (unit_info.source().is_regular_file()) {
  272. auto filename = unit_info.source().filename();
  273. static constexpr llvm::StringLiteral ApiExt = ".carbon";
  274. static constexpr llvm::StringLiteral ImplExt = ".impl.carbon";
  275. bool is_api_with_impl_ext = !is_impl && filename.ends_with(ImplExt);
  276. auto want_ext = is_impl ? ImplExt : ApiExt;
  277. if (is_api_with_impl_ext || !filename.ends_with(want_ext)) {
  278. CARBON_DIAGNOSTIC(
  279. IncorrectExtension, Error,
  280. "file extension of `{0:.impl|}.carbon` required for {0:`impl`|api}",
  281. Diagnostics::BoolAsSelect);
  282. auto diag = unit_info.emitter.Build(
  283. packaging ? packaging->names.node_id : Parse::NodeId::None,
  284. IncorrectExtension, is_impl);
  285. if (is_api_with_impl_ext) {
  286. CARBON_DIAGNOSTIC(
  287. IncorrectExtensionImplNote, Note,
  288. "file extension of `.impl.carbon` only allowed for `impl`");
  289. diag.Note(Parse::NodeId::None, IncorrectExtensionImplNote);
  290. }
  291. diag.Emit();
  292. }
  293. }
  294. }
  295. return api_map;
  296. }
  297. auto CheckParseTrees(
  298. llvm::MutableArrayRef<Unit> units,
  299. llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
  300. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs, llvm::StringRef target,
  301. const CheckParseTreesOptions& options) -> void {
  302. // UnitAndImports is big due to its SmallVectors, so we default to 0 on the
  303. // stack.
  304. llvm::SmallVector<UnitAndImports, 0> unit_infos(
  305. llvm::map_range(units, [&](Unit& unit) {
  306. return UnitAndImports(
  307. &unit, tree_and_subtrees_getters[unit.sem_ir->check_ir_id().index]);
  308. }));
  309. Map<ImportKey, UnitAndImports*> api_map =
  310. BuildApiMapAndDiagnosePackaging(unit_infos);
  311. // Mark down imports for all files.
  312. llvm::SmallVector<UnitAndImports*> ready_to_check;
  313. ready_to_check.reserve(units.size());
  314. for (auto& unit_info : unit_infos) {
  315. const auto& packaging = unit_info.parse_tree().packaging_decl();
  316. if (packaging && packaging->is_impl) {
  317. // An `impl` has an implicit import of its `api`.
  318. auto implicit_names = packaging->names;
  319. implicit_names.package_id = PackageNameId::None;
  320. TrackImport(api_map, nullptr, unit_info, implicit_names, options.fuzzing);
  321. }
  322. Map<ImportKey, Parse::NodeId> explicit_import_map;
  323. // Add the prelude import. It's added to explicit_import_map so that it can
  324. // conflict with an explicit import of the prelude.
  325. if (options.prelude_import &&
  326. !(packaging && packaging->names.package_id == PackageNameId::Core)) {
  327. auto prelude_id =
  328. unit_info.unit->value_stores->string_literal_values().Add("prelude");
  329. TrackImport(api_map, &explicit_import_map, unit_info,
  330. {.node_id = Parse::NoneNodeId(),
  331. .package_id = PackageNameId::Core,
  332. .library_id = prelude_id},
  333. options.fuzzing);
  334. }
  335. for (const auto& import : unit_info.parse_tree().imports()) {
  336. TrackImport(api_map, &explicit_import_map, unit_info, import,
  337. options.fuzzing);
  338. }
  339. // If there were no imports, mark the file as ready to check for below.
  340. if (unit_info.imports_remaining == 0) {
  341. ready_to_check.push_back(&unit_info);
  342. }
  343. }
  344. // Check everything with no dependencies. Earlier entries with dependencies
  345. // will be checked as soon as all their dependencies have been checked.
  346. for (int check_index = 0;
  347. check_index < static_cast<int>(ready_to_check.size()); ++check_index) {
  348. auto* unit_info = ready_to_check[check_index];
  349. CheckUnit(unit_info, tree_and_subtrees_getters, fs, target,
  350. options.vlog_stream)
  351. .Run();
  352. for (auto* incoming_import : unit_info->incoming_imports) {
  353. --incoming_import->imports_remaining;
  354. if (incoming_import->imports_remaining == 0) {
  355. ready_to_check.push_back(incoming_import);
  356. }
  357. }
  358. }
  359. // If there are still units with remaining imports, it means there's a
  360. // dependency loop.
  361. if (ready_to_check.size() < unit_infos.size()) {
  362. // Go through units and mask out unevaluated imports. This breaks everything
  363. // associated with a loop equivalently, whether it's part of it or depending
  364. // on a part of it.
  365. // TODO: Better identify cycles, maybe try to untangle them.
  366. for (auto& unit_info : unit_infos) {
  367. if (unit_info.imports_remaining > 0) {
  368. for (auto& package_imports : unit_info.package_imports) {
  369. for (auto* import_it = package_imports.imports.begin();
  370. import_it != package_imports.imports.end();) {
  371. if (import_it->unit_info->is_checked) {
  372. // The import is checked, so continue.
  373. ++import_it;
  374. } else {
  375. // The import hasn't been checked, indicating a cycle.
  376. CARBON_DIAGNOSTIC(ImportCycleDetected, Error,
  377. "import cannot be used due to a cycle; cycle "
  378. "must be fixed to import");
  379. unit_info.emitter.Emit(import_it->names.node_id,
  380. ImportCycleDetected);
  381. // Make this look the same as an import which wasn't found.
  382. package_imports.has_load_error = true;
  383. if (unit_info.api_for_impl == import_it->unit_info) {
  384. unit_info.api_for_impl = nullptr;
  385. }
  386. import_it = package_imports.imports.erase(import_it);
  387. }
  388. }
  389. }
  390. }
  391. }
  392. // Check the remaining file contents, which are probably broken due to
  393. // incomplete imports.
  394. for (auto& unit_info : unit_infos) {
  395. if (unit_info.imports_remaining > 0) {
  396. CheckUnit(&unit_info, tree_and_subtrees_getters, fs, target,
  397. options.vlog_stream)
  398. .Run();
  399. }
  400. }
  401. }
  402. }
  403. } // namespace Carbon::Check