check.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 "common/check.h"
  6. #include "toolchain/base/pretty_stack_trace_function.h"
  7. #include "toolchain/base/value_store.h"
  8. #include "toolchain/check/context.h"
  9. #include "toolchain/diagnostics/diagnostic_emitter.h"
  10. #include "toolchain/lex/token_kind.h"
  11. #include "toolchain/parse/tree.h"
  12. #include "toolchain/parse/tree_node_location_translator.h"
  13. #include "toolchain/sem_ir/file.h"
  14. #include "toolchain/sem_ir/typed_insts.h"
  15. namespace Carbon::Check {
  16. struct UnitInfo {
  17. // A given import within the file, with its destination.
  18. struct Import {
  19. Parse::Tree::PackagingNames names;
  20. UnitInfo* unit_info;
  21. };
  22. // A file's imports corresponding to a single package, for the map.
  23. struct PackageImports {
  24. // Use the constructor so that the SmallVector is only constructed
  25. // as-needed.
  26. explicit PackageImports(Parse::NodeId node) : node(node) {}
  27. // The first `import` directive in the file, which declared the package's
  28. // identifier (even if the import failed). Used for associating diagnostics
  29. // not specific to a single import.
  30. Parse::NodeId node;
  31. // Whether there's an import that failed to load.
  32. bool has_load_error = false;
  33. // The list of valid imports.
  34. llvm::SmallVector<Import> imports;
  35. };
  36. explicit UnitInfo(Unit& unit)
  37. : unit(&unit),
  38. translator(unit.tokens, unit.tokens->source().filename(),
  39. unit.parse_tree),
  40. err_tracker(*unit.consumer),
  41. emitter(translator, err_tracker) {}
  42. Unit* unit;
  43. // Emitter information.
  44. Parse::NodeLocationTranslator translator;
  45. ErrorTrackingDiagnosticConsumer err_tracker;
  46. DiagnosticEmitter<Parse::NodeLocation> emitter;
  47. // A map of package names to outgoing imports. If the
  48. // import's target isn't available, the unit will be nullptr to assist with
  49. // name lookup. Invalid imports (for example, `import Main;`) aren't added
  50. // because they won't add identifiers to name lookup.
  51. llvm::DenseMap<IdentifierId, PackageImports> package_imports_map;
  52. // The remaining number of imports which must be checked before this unit can
  53. // be processed.
  54. int32_t imports_remaining = 0;
  55. // A list of incoming imports. This will be empty for `impl` files, because
  56. // imports only touch `api` files.
  57. llvm::SmallVector<UnitInfo*> incoming_imports;
  58. };
  59. // Add imports to the root block.
  60. static auto InitPackageScopeAndImports(Context& context, UnitInfo& unit_info)
  61. -> void {
  62. // Define the package scope, with an instruction for `package` expressions to
  63. // reference.
  64. auto package_scope_id =
  65. context.name_scopes().Add(SemIR::InstId::PackageNamespace);
  66. CARBON_CHECK(package_scope_id == SemIR::NameScopeId::Package);
  67. auto package_inst = context.AddInst(SemIR::Namespace{
  68. Parse::NodeId::Invalid,
  69. context.GetBuiltinType(SemIR::BuiltinKind::NamespaceType),
  70. SemIR::NameScopeId::Package});
  71. CARBON_CHECK(package_inst == SemIR::InstId::PackageNamespace);
  72. // Add imports from the current package.
  73. auto self_import = unit_info.package_imports_map.find(IdentifierId::Invalid);
  74. if (self_import != unit_info.package_imports_map.end()) {
  75. auto& package_scope =
  76. context.name_scopes().Get(SemIR::NameScopeId::Package);
  77. package_scope.has_error = self_import->second.has_load_error;
  78. for (const auto& import : self_import->second.imports) {
  79. const auto& import_sem_ir = **import.unit_info->unit->sem_ir;
  80. const auto& import_scope =
  81. import_sem_ir.name_scopes().Get(SemIR::NameScopeId::Package);
  82. // If an import of the current package caused an error for the imported
  83. // file, it transitively affects the current file too.
  84. package_scope.has_error |= import_scope.has_error;
  85. auto ir_id = context.sem_ir().cross_ref_irs().Add(&import_sem_ir);
  86. for (const auto& [import_name_id, import_inst_id] : import_scope.names) {
  87. // Translate the name to the current IR.
  88. auto name_id = SemIR::NameId::Invalid;
  89. if (auto import_identifier_id = import_name_id.AsIdentifierId();
  90. import_identifier_id.is_valid()) {
  91. auto name = import_sem_ir.identifiers().Get(import_identifier_id);
  92. name_id =
  93. SemIR::NameId::ForIdentifier(context.identifiers().Add(name));
  94. } else {
  95. // A builtin name ID which is equivalent cross-IR.
  96. name_id = import_name_id;
  97. }
  98. // Leave a placeholder that the inst comes from the other IR.
  99. auto target_id = context.AddInst(
  100. SemIR::LazyImportRef{.ir_id = ir_id, .inst_id = import_inst_id});
  101. // TODO: The scope's names should be changed to allow for ambiguous
  102. // names.
  103. package_scope.names.insert({name_id, target_id});
  104. }
  105. }
  106. // Push the scope.
  107. context.PushScope(package_inst, SemIR::NameScopeId::Package,
  108. package_scope.has_error);
  109. } else {
  110. // Push the scope; there are no names to add.
  111. context.PushScope(package_inst, SemIR::NameScopeId::Package);
  112. }
  113. for (auto& [package_id, package_imports] : unit_info.package_imports_map) {
  114. if (!package_id.is_valid()) {
  115. // Current package is handled above.
  116. continue;
  117. }
  118. llvm::SmallVector<const SemIR::File*> sem_irs;
  119. for (auto import : package_imports.imports) {
  120. sem_irs.push_back(&**import.unit_info->unit->sem_ir);
  121. }
  122. context.AddPackageImports(package_imports.node, package_id, sem_irs,
  123. package_imports.has_load_error);
  124. }
  125. }
  126. // Loops over all nodes in the tree. On some errors, this may return early,
  127. // for example if an unrecoverable state is encountered.
  128. static auto ProcessParseNodes(Context& context,
  129. ErrorTrackingDiagnosticConsumer& err_tracker)
  130. -> bool {
  131. for (auto parse_node : context.parse_tree().postorder()) {
  132. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  133. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  134. switch (auto parse_kind = context.parse_tree().node_kind(parse_node)) {
  135. // TODO: Switch to `Parse::Name##Id(parse_node)` here.
  136. #define CARBON_PARSE_NODE_KIND(Name) \
  137. case Parse::NodeKind::Name: { \
  138. if (!Check::Handle##Name(context, parse_node)) { \
  139. CARBON_CHECK(err_tracker.seen_error()) \
  140. << "Handle" #Name " returned false without printing a diagnostic"; \
  141. return false; \
  142. } \
  143. break; \
  144. }
  145. #include "toolchain/parse/node_kind.def"
  146. }
  147. }
  148. return true;
  149. }
  150. // Produces and checks the IR for the provided Parse::Tree.
  151. static auto CheckParseTree(const SemIR::File& builtin_ir, UnitInfo& unit_info,
  152. llvm::raw_ostream* vlog_stream) -> void {
  153. unit_info.unit->sem_ir->emplace(
  154. *unit_info.unit->value_stores,
  155. unit_info.unit->tokens->source().filename().str(), &builtin_ir);
  156. // For ease-of-access.
  157. SemIR::File& sem_ir = **unit_info.unit->sem_ir;
  158. Context context(*unit_info.unit->tokens, unit_info.emitter,
  159. *unit_info.unit->parse_tree, sem_ir, vlog_stream);
  160. PrettyStackTraceFunction context_dumper(
  161. [&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
  162. // Add a block for the file.
  163. context.inst_block_stack().Push();
  164. InitPackageScopeAndImports(context, unit_info);
  165. if (!ProcessParseNodes(context, unit_info.err_tracker)) {
  166. context.sem_ir().set_has_errors(true);
  167. return;
  168. }
  169. // Pop information for the file-level scope.
  170. sem_ir.set_top_inst_block_id(context.inst_block_stack().Pop());
  171. context.PopScope();
  172. context.VerifyOnFinish();
  173. sem_ir.set_has_errors(unit_info.err_tracker.seen_error());
  174. #ifndef NDEBUG
  175. if (auto verify = sem_ir.Verify(); !verify.ok()) {
  176. CARBON_FATAL() << sem_ir << "Built invalid semantics IR: " << verify.error()
  177. << "\n";
  178. }
  179. #endif
  180. }
  181. // The package and library names, used as map keys.
  182. using ImportKey = std::pair<llvm::StringRef, llvm::StringRef>;
  183. // Returns a key form of the package object. file_package_id is only used for
  184. // imports, not the main package directive; as a consequence, it will be invalid
  185. // for the main package directive.
  186. static auto GetImportKey(UnitInfo& unit_info, IdentifierId file_package_id,
  187. Parse::Tree::PackagingNames names) -> ImportKey {
  188. auto* stores = unit_info.unit->value_stores;
  189. llvm::StringRef package_name =
  190. names.package_id.is_valid() ? stores->identifiers().Get(names.package_id)
  191. : file_package_id.is_valid() ? stores->identifiers().Get(file_package_id)
  192. : "";
  193. llvm::StringRef library_name =
  194. names.library_id.is_valid()
  195. ? stores->string_literal_values().Get(names.library_id)
  196. : "";
  197. return {package_name, library_name};
  198. }
  199. static constexpr llvm::StringLiteral ExplicitMainName = "Main";
  200. // Marks an import as required on both the source and target file.
  201. //
  202. // The ID comparisons between the import and unit are okay because they both
  203. // come from the same file.
  204. static auto TrackImport(
  205. llvm::DenseMap<ImportKey, UnitInfo*>& api_map,
  206. llvm::DenseMap<ImportKey, Parse::NodeId>* explicit_import_map,
  207. UnitInfo& unit_info, Parse::Tree::PackagingNames import) -> void {
  208. const auto& packaging = unit_info.unit->parse_tree->packaging_directive();
  209. IdentifierId file_package_id =
  210. packaging ? packaging->names.package_id : IdentifierId::Invalid;
  211. auto import_key = GetImportKey(unit_info, file_package_id, import);
  212. // True if the import has `Main` as the package name, even if it comes from
  213. // the file's packaging (diagnostics may differentiate).
  214. bool is_explicit_main = import_key.first == ExplicitMainName;
  215. // Explicit imports need more validation than implicit ones. We try to do
  216. // these in an order of imports that should be removed, followed by imports
  217. // that might be valid with syntax fixes.
  218. if (explicit_import_map) {
  219. // Diagnose redundant imports.
  220. if (auto [insert_it, success] =
  221. explicit_import_map->insert({import_key, import.node});
  222. !success) {
  223. CARBON_DIAGNOSTIC(RepeatedImport, Error,
  224. "Library imported more than once.");
  225. CARBON_DIAGNOSTIC(FirstImported, Note, "First import here.");
  226. unit_info.emitter.Build(import.node, RepeatedImport)
  227. .Note(insert_it->second, FirstImported)
  228. .Emit();
  229. return;
  230. }
  231. // True if the file's package is implicitly `Main` (by omitting an explicit
  232. // package name).
  233. bool is_file_implicit_main =
  234. !packaging || !packaging->names.package_id.is_valid();
  235. // True if the import is using implicit "current package" syntax (by
  236. // omitting an explicit package name).
  237. bool is_import_implicit_current_package = !import.package_id.is_valid();
  238. // True if the import is using `default` library syntax.
  239. bool is_import_default_library = !import.library_id.is_valid();
  240. // True if the import and file point at the same package, even by
  241. // incorrectly specifying the current package name to `import`.
  242. bool is_same_package = is_import_implicit_current_package ||
  243. import.package_id == file_package_id;
  244. // True if the import points at the same library as the file's library.
  245. bool is_same_library =
  246. is_same_package &&
  247. (packaging ? import.library_id == packaging->names.library_id
  248. : is_import_default_library);
  249. // Diagnose explicit imports of the same library, whether from `api` or
  250. // `impl`.
  251. if (is_same_library) {
  252. CARBON_DIAGNOSTIC(ExplicitImportApi, Error,
  253. "Explicit import of `api` from `impl` file is "
  254. "redundant with implicit import.");
  255. CARBON_DIAGNOSTIC(ImportSelf, Error, "File cannot import itself.");
  256. bool is_impl =
  257. !packaging || packaging->api_or_impl == Parse::Tree::ApiOrImpl::Impl;
  258. unit_info.emitter.Emit(import.node,
  259. is_impl ? ExplicitImportApi : ImportSelf);
  260. return;
  261. }
  262. // Diagnose explicit imports of `Main//default`. There is no `api` for it.
  263. // This lets other diagnostics handle explicit `Main` package naming.
  264. if (is_file_implicit_main && is_import_implicit_current_package &&
  265. is_import_default_library) {
  266. CARBON_DIAGNOSTIC(ImportMainDefaultLibrary, Error,
  267. "Cannot import `Main//default`.");
  268. unit_info.emitter.Emit(import.node, ImportMainDefaultLibrary);
  269. return;
  270. }
  271. if (!is_import_implicit_current_package) {
  272. // Diagnose explicit imports of the same package that use the package
  273. // name.
  274. if (is_same_package || (is_file_implicit_main && is_explicit_main)) {
  275. CARBON_DIAGNOSTIC(
  276. ImportCurrentPackageByName, Error,
  277. "Imports from the current package must omit the package name.");
  278. unit_info.emitter.Emit(import.node, ImportCurrentPackageByName);
  279. return;
  280. }
  281. // Diagnose explicit imports from `Main`.
  282. if (is_explicit_main) {
  283. CARBON_DIAGNOSTIC(ImportMainPackage, Error,
  284. "Cannot import `Main` from other packages.");
  285. unit_info.emitter.Emit(import.node, ImportMainPackage);
  286. return;
  287. }
  288. }
  289. } else if (is_explicit_main) {
  290. // An implicit import with an explicit `Main` occurs when a `package` rule
  291. // has bad syntax, which will have been diagnosed when building the API map.
  292. // As a consequence, we return silently.
  293. return;
  294. }
  295. // Get the package imports.
  296. auto package_imports_it =
  297. unit_info.package_imports_map.try_emplace(import.package_id, import.node)
  298. .first;
  299. if (auto api = api_map.find(import_key); api != api_map.end()) {
  300. // Add references between the file and imported api.
  301. package_imports_it->second.imports.push_back({import, api->second});
  302. ++unit_info.imports_remaining;
  303. api->second->incoming_imports.push_back(&unit_info);
  304. } else {
  305. // The imported api is missing.
  306. package_imports_it->second.has_load_error = true;
  307. CARBON_DIAGNOSTIC(LibraryApiNotFound, Error,
  308. "Corresponding API not found.");
  309. CARBON_DIAGNOSTIC(ImportNotFound, Error, "Imported API not found.");
  310. unit_info.emitter.Emit(
  311. import.node, explicit_import_map ? ImportNotFound : LibraryApiNotFound);
  312. }
  313. }
  314. // Builds a map of `api` files which might be imported. Also diagnoses issues
  315. // related to the packaging because the strings are loaded as part of getting
  316. // the ImportKey (which we then do for `impl` files too).
  317. static auto BuildApiMapAndDiagnosePackaging(
  318. llvm::SmallVector<UnitInfo, 0>& unit_infos)
  319. -> llvm::DenseMap<ImportKey, UnitInfo*> {
  320. llvm::DenseMap<ImportKey, UnitInfo*> api_map;
  321. for (auto& unit_info : unit_infos) {
  322. const auto& packaging = unit_info.unit->parse_tree->packaging_directive();
  323. // An import key formed from the `package` or `library` directive. Or, for
  324. // Main//default, a placeholder key.
  325. auto import_key = packaging ? GetImportKey(unit_info, IdentifierId::Invalid,
  326. packaging->names)
  327. // Construct a boring key for Main//default.
  328. : ImportKey{"", ""};
  329. // Diagnose explicit `Main` uses before they become marked as possible
  330. // APIs.
  331. if (import_key.first == ExplicitMainName) {
  332. CARBON_DIAGNOSTIC(ExplicitMainPackage, Error,
  333. "`Main//default` must omit `package` directive.");
  334. CARBON_DIAGNOSTIC(ExplicitMainLibrary, Error,
  335. "Use `library` directive in `Main` package libraries.");
  336. unit_info.emitter.Emit(packaging->names.node, import_key.second.empty()
  337. ? ExplicitMainPackage
  338. : ExplicitMainLibrary);
  339. continue;
  340. }
  341. bool is_impl =
  342. packaging && packaging->api_or_impl == Parse::Tree::ApiOrImpl::Impl;
  343. // Add to the `api` map and diagnose duplicates. This occurs before the
  344. // file extension check because we might emit both diagnostics in situations
  345. // where the user forgets (or has syntax errors with) a package line
  346. // multiple times.
  347. if (!is_impl) {
  348. auto [entry, success] = api_map.insert({import_key, &unit_info});
  349. if (!success) {
  350. llvm::StringRef prev_filename =
  351. entry->second->unit->tokens->source().filename();
  352. if (packaging) {
  353. CARBON_DIAGNOSTIC(DuplicateLibraryApi, Error,
  354. "Library's API previously provided by `{0}`.",
  355. std::string);
  356. unit_info.emitter.Emit(packaging->names.node, DuplicateLibraryApi,
  357. prev_filename.str());
  358. } else {
  359. CARBON_DIAGNOSTIC(DuplicateMainApi, Error,
  360. "Main//default previously provided by `{0}`.",
  361. std::string);
  362. // Use the invalid node because there's no node to associate with.
  363. unit_info.emitter.Emit(Parse::NodeId::Invalid, DuplicateMainApi,
  364. prev_filename.str());
  365. }
  366. }
  367. }
  368. // Validate file extensions. Note imports rely the packaging directive, not
  369. // the extension. If the input is not a regular file, for example because it
  370. // is stdin, no filename checking is performed.
  371. if (unit_info.unit->tokens->source().is_regular_file()) {
  372. auto filename = unit_info.unit->tokens->source().filename();
  373. static constexpr llvm::StringLiteral ApiExt = ".carbon";
  374. static constexpr llvm::StringLiteral ImplExt = ".impl.carbon";
  375. bool is_api_with_impl_ext = !is_impl && filename.ends_with(ImplExt);
  376. auto want_ext = is_impl ? ImplExt : ApiExt;
  377. if (is_api_with_impl_ext || !filename.ends_with(want_ext)) {
  378. CARBON_DIAGNOSTIC(IncorrectExtension, Error,
  379. "File extension of `{0}` required for `{1}`.",
  380. llvm::StringLiteral, Lex::TokenKind);
  381. auto diag = unit_info.emitter.Build(
  382. packaging ? packaging->names.node : Parse::NodeId::Invalid,
  383. IncorrectExtension, want_ext,
  384. is_impl ? Lex::TokenKind::Impl : Lex::TokenKind::Api);
  385. if (is_api_with_impl_ext) {
  386. CARBON_DIAGNOSTIC(IncorrectExtensionImplNote, Note,
  387. "File extension of `{0}` only allowed for `{1}`.",
  388. llvm::StringLiteral, Lex::TokenKind);
  389. diag.Note(Parse::NodeId::Invalid, IncorrectExtensionImplNote, ImplExt,
  390. Lex::TokenKind::Impl);
  391. }
  392. diag.Emit();
  393. }
  394. }
  395. }
  396. return api_map;
  397. }
  398. auto CheckParseTrees(const SemIR::File& builtin_ir,
  399. llvm::MutableArrayRef<Unit> units,
  400. llvm::raw_ostream* vlog_stream) -> void {
  401. // Prepare diagnostic emitters in case we run into issues during package
  402. // checking.
  403. //
  404. // UnitInfo is big due to its SmallVectors, so we default to 0 on the stack.
  405. llvm::SmallVector<UnitInfo, 0> unit_infos;
  406. unit_infos.reserve(units.size());
  407. for (auto& unit : units) {
  408. unit_infos.emplace_back(unit);
  409. }
  410. llvm::DenseMap<ImportKey, UnitInfo*> api_map =
  411. BuildApiMapAndDiagnosePackaging(unit_infos);
  412. // Mark down imports for all files.
  413. llvm::SmallVector<UnitInfo*> ready_to_check;
  414. ready_to_check.reserve(units.size());
  415. for (auto& unit_info : unit_infos) {
  416. if (const auto& packaging =
  417. unit_info.unit->parse_tree->packaging_directive()) {
  418. if (packaging->api_or_impl == Parse::Tree::ApiOrImpl::Impl) {
  419. // An `impl` has an implicit import of its `api`.
  420. auto implicit_names = packaging->names;
  421. implicit_names.package_id = IdentifierId::Invalid;
  422. TrackImport(api_map, nullptr, unit_info, implicit_names);
  423. }
  424. }
  425. llvm::DenseMap<ImportKey, Parse::NodeId> explicit_import_map;
  426. for (const auto& import : unit_info.unit->parse_tree->imports()) {
  427. TrackImport(api_map, &explicit_import_map, unit_info, import);
  428. }
  429. // If there were no imports, mark the file as ready to check for below.
  430. if (unit_info.imports_remaining == 0) {
  431. ready_to_check.push_back(&unit_info);
  432. }
  433. }
  434. // Check everything with no dependencies. Earlier entries with dependencies
  435. // will be checked as soon as all their dependencies have been checked.
  436. for (int check_index = 0;
  437. check_index < static_cast<int>(ready_to_check.size()); ++check_index) {
  438. auto* unit_info = ready_to_check[check_index];
  439. CheckParseTree(builtin_ir, *unit_info, vlog_stream);
  440. for (auto* incoming_import : unit_info->incoming_imports) {
  441. --incoming_import->imports_remaining;
  442. if (incoming_import->imports_remaining == 0) {
  443. ready_to_check.push_back(incoming_import);
  444. }
  445. }
  446. }
  447. // If there are still units with remaining imports, it means there's a
  448. // dependency loop.
  449. if (ready_to_check.size() < unit_infos.size()) {
  450. // Go through units and mask out unevaluated imports. This breaks everything
  451. // associated with a loop equivalently, whether it's part of it or depending
  452. // on a part of it.
  453. // TODO: Better identify cycles, maybe try to untangle them.
  454. for (auto& unit_info : unit_infos) {
  455. if (unit_info.imports_remaining > 0) {
  456. for (auto& [package_id, package_imports] :
  457. unit_info.package_imports_map) {
  458. for (auto* import_it = package_imports.imports.begin();
  459. import_it != package_imports.imports.end();) {
  460. if (*import_it->unit_info->unit->sem_ir) {
  461. // The import is checked, so continue.
  462. ++import_it;
  463. } else {
  464. // The import hasn't been checked, indicating a cycle.
  465. CARBON_DIAGNOSTIC(ImportCycleDetected, Error,
  466. "Import cannot be used due to a cycle. Cycle "
  467. "must be fixed to import.");
  468. unit_info.emitter.Emit(import_it->names.node,
  469. ImportCycleDetected);
  470. // Make this look the same as an import which wasn't found.
  471. package_imports.has_load_error = true;
  472. import_it = package_imports.imports.erase(import_it);
  473. }
  474. }
  475. }
  476. }
  477. }
  478. // Check the remaining file contents, which are probably broken due to
  479. // incomplete imports.
  480. for (auto& unit_info : unit_infos) {
  481. if (unit_info.imports_remaining > 0) {
  482. CheckParseTree(builtin_ir, unit_info, vlog_stream);
  483. }
  484. }
  485. }
  486. }
  487. } // namespace Carbon::Check