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

Code before:
    if (entry.type_const_id == type_const_id &&
        entry.interface_const_id == interface_const_id) {
      auto facet_type_type_id =
          context.types().GetTypeIdForTypeConstantId(interface_const_id);
      CARBON_DIAGNOSTIC(ImplLookupCycle, Error,
                        "cycle found in search for impl of {0} for type {1}",
                        SemIR::TypeId, SemIR::TypeId);
      auto builder = context.emitter().Build(
          loc_id, ImplLookupCycle, facet_type_type_id,
          context.types().GetTypeIdForTypeConstantId(type_const_id));
      for (auto& active_entry : llvm::drop_begin(stack, i)) {
        if (active_entry.impl_loc.has_value()) {
          CARBON_DIAGNOSTIC(ImplLookupCycleNote, Note,
                            "determining if this impl clause matches", );
          builder.Note(active_entry.impl_loc, ImplLookupCycleNote);
        }
      }
      builder.Emit();
      return SemIR::ErrorInst::SingletonInstId;
    }
  }

  // The `interface_id` is the single interface in the `interface_const_id`
  // facet type. In the future, a facet type may include more than a single
  // interface, but for now that is unhandled with a TODO.
  auto interface_id = [&] {
    auto facet_type_inst_id =
        context.constant_values().GetInstId(interface_const_id);
    auto facet_type_id = context.insts()
                             .GetAs<SemIR::FacetType>(facet_type_inst_id)
                             .facet_type_id;
    const auto& facet_type_info = context.facet_types().Get(facet_type_id);
    if (facet_type_info.impls_constraints.empty()) {
      context.TODO(loc_id,
                   "impl lookup for a FacetType with no interface (using "
                   "`where .Self impls ...` instead?)");
      return SemIR::InterfaceId::None;
    }
    if (facet_type_info.impls_constraints.size() > 1) {
      context.TODO(loc_id,
                   "impl lookup for a FacetType with more than one interface");
      return SemIR::InterfaceId::None;
    }
    return facet_type_info.impls_constraints[0].interface_id;
  }();

  auto witness_id = SemIR::InstId::None;

  stack.push_back({
      .type_const_id = type_const_id,
      .interface_const_id = interface_const_id,

Review comment (refactor):
Is this worth factoring out as a helper function? We do have a style preference for short functions; LookupImplWitness is now just shy of 100 LOC, and this looks like a good cut point. I'll admit, I also have a preference for avoiding lambdas when only used for flow control as here (since it's neither reused, nor a parameter): they make `return` harder to interpret when they get too long.

Code after:
  }
  if (impl.generic_id.has_value()) {
    specific_id = DeduceImplArguments(context, loc_id, impl, type_const_id,
                                      interface_const_id);
    if (!specific_id.has_value()) {
      return SemIR::InstId::None;
    }
  }
  if (!context.constant_values().AreEqualAcrossDeclarations(
          SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
                                            impl.self_id),
          type_const_id)) {
    return SemIR::InstId::None;
  }
  if (!context.constant_values().AreEqualAcrossDeclarations(
          SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
                                            impl.constraint_id),
          interface_const_id)) {
    // TODO: An impl of a constraint type should be treated as implementing
    // the constraint's interfaces.
    return SemIR::InstId::None;
  }
  if (!impl.witness_id.has_value()) {
    // TODO: Diagnose if the impl isn't defined yet?
    return SemIR::InstId::None;
  }
  LoadImportRef(context, impl.witness_id);
  if (specific_id.has_value()) {
    // We need a definition of the specific `impl` so we can access its
    // witness.
    ResolveSpecificDefinition(context, loc_id, specific_id);
  }
  return context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
      context.sem_ir(), specific_id, impl.witness_id));
}

auto LookupImplWitness(Context& context, SemIR::LocId loc_id,
                       SemIR::ConstantId type_const_id,
                       SemIR::ConstantId interface_const_id) -> SemIR::InstId {
  if (type_const_id == SemIR::ErrorInst::SingletonConstantId ||
      interface_const_id == SemIR::ErrorInst::SingletonConstantId) {
    return SemIR::ErrorInst::SingletonInstId;
  }
  auto import_irs =
      FindAssociatedImportIRs(context, type_const_id, interface_const_id);
  for (auto import_ir : import_irs) {
    // TODO: Instead of importing all impls, only import ones that are in some
    // way connected to this query.
    for (auto impl_index : llvm::seq(
             context.import_irs().Get(import_ir).sem_ir->impls().size())) {
      // TODO: Track the relevant impls and only consider those ones and any