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

Code before:
    auto rhs_id = GetLocalConstantInstId(resolver, rewrite.rhs_id);
    if (local_facet_type_info) {
      local_facet_type_info->rewrite_constraints.push_back(
          {.lhs_id = lhs_id, .rhs_id = rhs_id});
    }
  }
}

static auto TryResolveTypedInst(ImportRefResolver& resolver,
                                SemIR::FacetType inst) -> ResolveResult {
  CARBON_CHECK(inst.type_id == SemIR::TypeType::TypeId);

  const SemIR::FacetTypeInfo& import_facet_type_info =
      resolver.import_facet_types().Get(inst.facet_type_id);
  // Ensure values are present, but discard them to avoid allocations.
  BuildFacetTypeInfo(resolver, import_facet_type_info,
                     /*local_facet_type_info=*/nullptr);
  if (resolver.HasNewWork()) {
    return ResolveResult::Retry();
  }

  SemIR::FacetTypeInfo local_facet_type_info = {
      .builtin_constraint_mask = import_facet_type_info.builtin_constraint_mask,
      // TODO: Also process the other requirements.
      .other_requirements = import_facet_type_info.other_requirements};
  // Re-fetch and values to the local `FacetTypeInfo`.
  BuildFacetTypeInfo(resolver, import_facet_type_info, &local_facet_type_info);

  SemIR::FacetTypeId facet_type_id =
      resolver.local_facet_types().Add(std::move(local_facet_type_info));
  return ResolveResult::Deduplicated<SemIR::FacetType>(
      resolver,
      {.type_id = SemIR::TypeType::TypeId, .facet_type_id = facet_type_id});
}

static auto TryResolveTypedInst(ImportRefResolver& resolver,
                                SemIR::FacetValue inst) -> ResolveResult {
  auto type_id = GetLocalConstantId(resolver, inst.type_id);
  auto type_inst_id = GetLocalTypeInstId(resolver, inst.type_inst_id);
  auto witnesses = GetLocalInstBlockContents(resolver, inst.witnesses_block_id);
  if (resolver.HasNewWork()) {
    return ResolveResult::Retry();
  }

  return ResolveResult::Deduplicated<SemIR::FacetValue>(
      resolver,
      {.type_id =
           resolver.local_context().types().GetTypeIdForTypeConstantId(type_id),
       .type_inst_id = type_inst_id,
       .witnesses_block_id = GetLocalCanonicalInstBlockId(
           resolver, inst.witnesses_block_id, witnesses)});

Review comment (suggestion):
```suggestion
  // Re-resolve and add values to the local `FacetTypeInfo`.
```

Code after:
    auto rhs_id = GetLocalConstantInstId(resolver, rewrite.rhs_id);
    if (local_facet_type_info) {
      local_facet_type_info->rewrite_constraints.push_back(
          {.lhs_id = lhs_id, .rhs_id = rhs_id});
    }
  }
}

static auto TryResolveTypedInst(ImportRefResolver& resolver,
                                SemIR::FacetType inst) -> ResolveResult {
  CARBON_CHECK(inst.type_id == SemIR::TypeType::TypeId);

  const SemIR::FacetTypeInfo& import_facet_type_info =
      resolver.import_facet_types().Get(inst.facet_type_id);
  // Ensure values are imported, but discard them to avoid allocations.
  ResolveFacetTypeInfo(resolver, import_facet_type_info,
                       /*local_facet_type_info=*/nullptr);
  if (resolver.HasNewWork()) {
    return ResolveResult::Retry();
  }

  SemIR::FacetTypeInfo local_facet_type_info = {
      .builtin_constraint_mask = import_facet_type_info.builtin_constraint_mask,
      // TODO: Also process the other requirements.
      .other_requirements = import_facet_type_info.other_requirements};
  // Re-resolve and add values to the local `FacetTypeInfo`.
  ResolveFacetTypeInfo(resolver, import_facet_type_info,
                       &local_facet_type_info);

  SemIR::FacetTypeId facet_type_id =
      resolver.local_facet_types().Add(std::move(local_facet_type_info));
  return ResolveResult::Deduplicated<SemIR::FacetType>(
      resolver,
      {.type_id = SemIR::TypeType::TypeId, .facet_type_id = facet_type_id});
}

static auto TryResolveTypedInst(ImportRefResolver& resolver,
                                SemIR::FacetValue inst) -> ResolveResult {
  auto type_id = GetLocalConstantId(resolver, inst.type_id);
  auto type_inst_id = GetLocalTypeInstId(resolver, inst.type_inst_id);
  auto witnesses = GetLocalInstBlockContents(resolver, inst.witnesses_block_id);
  if (resolver.HasNewWork()) {
    return ResolveResult::Retry();
  }

  return ResolveResult::Deduplicated<SemIR::FacetValue>(
      resolver,
      {.type_id =
           resolver.local_context().types().GetTypeIdForTypeConstantId(type_id),
       .type_inst_id = type_inst_id,
       .witnesses_block_id = GetLocalCanonicalInstBlockId(