Repository: carbon-language/carbon-lang
File: toolchain/check/import.cpp (C++)

Code before:
                                const SemIR::File& import_sem_ir,
                                SemIR::InstId import_inst_id,
                                SemIR::NameScopeId parent_scope_id,
                                SemIR::NameId name_id) -> void {
  // Leave a placeholder that the inst comes from the other IR.
  auto& parent_scope = context.name_scopes().Get(parent_scope_id);
  auto [inserted, entry_id] = parent_scope.LookupOrAdd(
      name_id,
      // This InstId is temporary and would be overridden if used.
      SemIR::InstId::None, SemIR::AccessKind::Public);
  auto& entry = parent_scope.GetEntry(entry_id);
  if (inserted) {
    auto entity_name_id = context.entity_names().Add(
        {.name_id = name_id, .parent_scope_id = parent_scope_id});
    auto import_ref = AddImportRef(
        context, {.ir_id = ir_id, .inst_id = import_inst_id}, entity_name_id);
    entry.result = SemIR::ScopeLookupResult::MakeFound(
        import_ref, SemIR::AccessKind::Public);

    // Import references for non-owning declarations that match current library.
    if (auto function_decl =
            import_sem_ir.insts().TryGetAs<SemIR::FunctionDecl>(
                import_inst_id)) {
      LoadImportForOwningFunction(
          context, import_sem_ir,
          import_sem_ir.functions().Get(function_decl.value().function_id),
          import_ref);
    }
    return;
  }

  auto inst_id = entry.result.target_inst_id();
  auto prev_ir_inst = GetCanonicalImportIRInst(context, inst_id);
  VerifySameCanonicalImportIRInst(context, inst_id, prev_ir_inst, ir_id,
                                  &import_sem_ir, import_inst_id);
}

namespace {
// A scope in the API file that still needs to be copied to the implementation
// file. Only used for API file imports.
struct TodoScope {
  // The scope's instruction in the API file.
  SemIR::InstId api_inst_id;
  // The scope in the API file.
  SemIR::NameScopeId api_scope_id;
  // The already-translated scope name in the implementation file.
  SemIR::NameId impl_name_id;
  // The already-copied parent scope in the implementation file.
  SemIR::NameScopeId impl_parent_scope_id;
};
}  // namespace

Review comment (suggestion):
```suggestion
          import_sem_ir.functions().Get(function_decl->function_id),
```



Code after:
      if (str_owner_library == str_decl_library) {
        LoadImportRef(context, import_ref);
      }
    }
  }
}

// Adds an ImportRef for an entity, handling merging if needed.
static auto AddImportRefOrMerge(Context& context, SemIR::ImportIRId ir_id,
                                const SemIR::File& import_sem_ir,
                                SemIR::InstId import_inst_id,
                                SemIR::NameScopeId parent_scope_id,
                                SemIR::NameId name_id) -> void {
  // Leave a placeholder that the inst comes from the other IR.
  auto& parent_scope = context.name_scopes().Get(parent_scope_id);
  auto [inserted, entry_id] = parent_scope.LookupOrAdd(
      name_id,
      // This InstId is temporary and would be overridden if used.
      SemIR::InstId::None, SemIR::AccessKind::Public);
  auto& entry = parent_scope.GetEntry(entry_id);
  if (inserted) {
    auto entity_name_id = context.entity_names().Add(
        {.name_id = name_id, .parent_scope_id = parent_scope_id});
    auto import_ref = AddImportRef(
        context, {.ir_id = ir_id, .inst_id = import_inst_id}, entity_name_id);
    entry.result = SemIR::ScopeLookupResult::MakeFound(
        import_ref, SemIR::AccessKind::Public);

    // Import references for non-owning declarations that match current library.
    if (auto function_decl =
            import_sem_ir.insts().TryGetAs<SemIR::FunctionDecl>(
                import_inst_id)) {
      LoadImportForOwningFunction(
          context, import_sem_ir,
          import_sem_ir.functions().Get(function_decl->function_id),
          import_ref);
    }
    return;
  }

  auto inst_id = entry.result.target_inst_id();
  auto prev_ir_inst = GetCanonicalImportIRInst(context, inst_id);
  VerifySameCanonicalImportIRInst(context, name_id, inst_id, prev_ir_inst,
                                  ir_id, &import_sem_ir, import_inst_id);
}

namespace {
// A scope in the API file that still needs to be copied to the implementation
// file. Only used for API file imports.
struct TodoScope {
  // The scope's instruction in the API file.