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

Code before:

  // If we're forming an initializer, then we want an initializer for each
  // element. Otherwise, we want a value representation for each element.
  // Perform a final destination store if we're performing an in-place
  // initialization.
  bool is_init = target.is_initializer();
  ConversionTarget::Kind inner_kind =
      !is_init ? ConversionTarget::Value
      : SemIR::InitRepr::ForType(sem_ir, target.type_id).kind ==
              SemIR::InitRepr::InPlace
          ? ConversionTarget::FullInitializer
          : ConversionTarget::Initializer;

  // Initialize each element of the destination from the corresponding element
  // of the source.
  // TODO: Annotate diagnostics coming from here with the element index.
  auto new_block =
      literal_elems_id.has_value() && !dest_has_vptr
          ? SemIR::CopyOnWriteInstBlock(sem_ir, literal_elems_id)
          : SemIR::CopyOnWriteInstBlock(
                sem_ir, SemIR::CopyOnWriteInstBlock::UninitializedBlock{
                            dest_elem_fields.size()});
  for (auto [i, dest_field] : llvm::enumerate(dest_elem_fields)) {
    if (dest_field.name_id == SemIR::NameId::Vptr) {
      bool NonConstexprToClass = ToClass;
      CARBON_CHECK(NonConstexprToClass, "Only classes should have vptrs.");
      target.init_block->InsertHere();
      auto dest_id =
          AddInst<SemIR::ClassElementAccess>(context, value_loc_id,
                                             {.type_id = dest_field.type_id,
                                              .base_id = target.init_id,
                                              .index = SemIR::ElementIndex(i)});
      auto vtable_ptr_id = AddInst<SemIR::VtablePtr>(
          context, value_loc_id, {.type_id = dest_field.type_id});
      auto init_id =
          AddInst<SemIR::InitializeFrom>(context, value_loc_id,
                                         {.type_id = dest_field.type_id,
                                          .src_id = vtable_ptr_id,
                                          .dest_id = dest_id});
      new_block.Set(i, init_id);
      continue;
    }

    // Find the matching source field.
    auto src_field_index = i;
    if (src_type.fields_id != dest_type.fields_id) {
      if (auto lookup = src_field_indexes.Lookup(dest_field.name_id)) {
        src_field_index = lookup.value();
      } else {
        if (target.diagnose) {
          if (literal_elems_id.has_value()) {

Review comment (suggestion):
Hm. How would you feel about something like this:
```suggestion
      if constexpr (!ToClass) {
        CARBON_FATAL("Only classes should have vptrs.");
      }
```

Code after:

  // If we're forming an initializer, then we want an initializer for each
  // element. Otherwise, we want a value representation for each element.
  // Perform a final destination store if we're performing an in-place
  // initialization.
  bool is_init = target.is_initializer();
  ConversionTarget::Kind inner_kind =
      !is_init ? ConversionTarget::Value
      : SemIR::InitRepr::ForType(sem_ir, target.type_id).kind ==
              SemIR::InitRepr::InPlace
          ? ConversionTarget::FullInitializer
          : ConversionTarget::Initializer;

  // Initialize each element of the destination from the corresponding element
  // of the source.
  // TODO: Annotate diagnostics coming from here with the element index.
  auto new_block =
      literal_elems_id.has_value() && !dest_has_vptr
          ? SemIR::CopyOnWriteInstBlock(sem_ir, literal_elems_id)
          : SemIR::CopyOnWriteInstBlock(
                sem_ir, SemIR::CopyOnWriteInstBlock::UninitializedBlock{
                            dest_elem_fields.size()});
  for (auto [i, dest_field] : llvm::enumerate(dest_elem_fields)) {
    if (dest_field.name_id == SemIR::NameId::Vptr) {
      if constexpr (!ToClass) {
        CARBON_FATAL("Only classes should have vptrs.");
      }
      target.init_block->InsertHere();
      auto dest_id =
          AddInst<SemIR::ClassElementAccess>(context, value_loc_id,
                                             {.type_id = dest_field.type_id,
                                              .base_id = target.init_id,
                                              .index = SemIR::ElementIndex(i)});
      auto vtable_ptr_id = AddInst<SemIR::VtablePtr>(
          context, value_loc_id, {.type_id = dest_field.type_id});
      auto init_id =
          AddInst<SemIR::InitializeFrom>(context, value_loc_id,
                                         {.type_id = dest_field.type_id,
                                          .src_id = vtable_ptr_id,
                                          .dest_id = dest_id});
      new_block.Set(i, init_id);
      continue;
    }

    // Find the matching source field.
    auto src_field_index = i;
    if (src_type.fields_id != dest_type.fields_id) {
      if (auto lookup = src_field_indexes.Lookup(dest_field.name_id)) {
        src_field_index = lookup.value();
      } else {
        if (target.diagnose) {