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

Code before:
              StructInitMissingFieldInLiteral, Error,
              "missing value for field `{0}` in struct initialization",
              SemIR::NameId);
          context.emitter().Emit(value_loc_id, StructInitMissingFieldInLiteral,
                                 dest_field.name_id);
        } else {
          CARBON_DIAGNOSTIC(StructInitMissingFieldInConversion, Error,
                            "cannot convert from struct type {0} to {1}: "
                            "missing field `{2}` in source type",
                            TypeOfInstId, SemIR::TypeId, SemIR::NameId);
          context.emitter().Emit(value_loc_id,
                                 StructInitMissingFieldInConversion, value_id,
                                 target.type_id, dest_field.name_id);
        }
        return SemIR::InstId::BuiltinErrorInst;
      }
    }
    auto src_field = src_elem_fields[src_field_index];

    // TODO: This call recurses back into conversion. Switch to an iterative
    // approach.
    auto init_id =
        ConvertAggregateElement<SemIR::StructAccess, TargetAccessInstT>(
            context, value_loc_id, value_id, src_field.type_id, literal_elems,
            inner_kind, target.init_id, dest_field.type_id, target.init_block,
            src_field_index, src_field_index + dest_has_vptr);
    if (init_id == SemIR::InstId::BuiltinErrorInst) {
      return SemIR::InstId::BuiltinErrorInst;
    }
    new_block.Set(i, init_id);
  }

  if (ToClass) {
    target.init_block->InsertHere();
    CARBON_CHECK(is_init,
                 "Converting directly to a class value is not supported");
    return context.AddInst<SemIR::ClassInit>(value_loc_id,
                                             {.type_id = target.type_id,
                                              .elements_id = new_block.id(),
                                              .dest_id = target.init_id});
  } else if (is_init) {
    target.init_block->InsertHere();
    return context.AddInst<SemIR::StructInit>(value_loc_id,
                                              {.type_id = target.type_id,
                                               .elements_id = new_block.id(),
                                               .dest_id = target.init_id});
  } else {
    return context.AddInst<SemIR::StructValue>(
        value_loc_id,
        {.type_id = target.type_id, .elements_id = new_block.id()});
  }

Review comment (suggestion):
```suggestion
            src_field_index, src_field_index + dest_has_vptr ? 1 : 0);
```

Whenever I see code that treats a boolean as a number, I have to stop and think through what the rules for that are.

Code after:
          CARBON_DIAGNOSTIC(
              StructInitMissingFieldInLiteral, Error,
              "missing value for field `{0}` in struct initialization",
              SemIR::NameId);
          context.emitter().Emit(value_loc_id, StructInitMissingFieldInLiteral,
                                 dest_field.name_id);
        } else {
          CARBON_DIAGNOSTIC(StructInitMissingFieldInConversion, Error,
                            "cannot convert from struct type {0} to {1}: "
                            "missing field `{2}` in source type",
                            TypeOfInstId, SemIR::TypeId, SemIR::NameId);
          context.emitter().Emit(value_loc_id,
                                 StructInitMissingFieldInConversion, value_id,
                                 target.type_id, dest_field.name_id);
        }
        return SemIR::InstId::BuiltinErrorInst;
      }
    }
    auto src_field = src_elem_fields[src_field_index];

    // TODO: This call recurses back into conversion. Switch to an iterative
    // approach.
    auto init_id =
        ConvertAggregateElement<SemIR::StructAccess, TargetAccessInstT>(
            context, value_loc_id, value_id, src_field.type_id, literal_elems,
            inner_kind, target.init_id, dest_field.type_id, target.init_block,
            src_field_index, src_field_index + dest_vptr_offset);
    if (init_id == SemIR::InstId::BuiltinErrorInst) {
      return SemIR::InstId::BuiltinErrorInst;
    }
    new_block.Set(i, init_id);
  }

  if (ToClass) {
    target.init_block->InsertHere();
    CARBON_CHECK(is_init,
                 "Converting directly to a class value is not supported");
    return context.AddInst<SemIR::ClassInit>(value_loc_id,
                                             {.type_id = target.type_id,
                                              .elements_id = new_block.id(),
                                              .dest_id = target.init_id});
  } else if (is_init) {
    target.init_block->InsertHere();
    return context.AddInst<SemIR::StructInit>(value_loc_id,
                                              {.type_id = target.type_id,
                                               .elements_id = new_block.id(),
                                               .dest_id = target.init_id});
  } else {
    return context.AddInst<SemIR::StructValue>(
        value_loc_id,
        {.type_id = target.type_id, .elements_id = new_block.id()});