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

Code before:
      auto [derived_vtable_entry_id, fn_id, specific_id] =
          DecomposeVirtualFunction(context.sem_ir(), base_vtable_entry_id,
                                   base_class_specific_id);
      const auto& fn = context.sem_ir().functions().Get(fn_id);
      const auto* i = llvm::find_if(
          vtable_contents, [&](SemIR::InstId override_fn_decl_id) -> bool {
            const auto& override_fn = context.functions().Get(
                context.insts()
                    .GetAs<SemIR::FunctionDecl>(override_fn_decl_id)
                    .function_id);
            return override_fn.virtual_modifier ==
                       SemIR::FunctionFields::VirtualModifier::Impl &&
                   override_fn.name_id == fn.name_id;
          });
      if (i != vtable_contents.end()) {
        auto override_fn_id =
            context.insts().GetAs<SemIR::FunctionDecl>(*i).function_id;
        implemented_impls.Insert(override_fn_id);
        auto& override_fn = context.functions().Get(override_fn_id);
        CheckFunctionTypeMatches(context, override_fn, fn, specific_id,
                                 /*check_syntax=*/false,
                                 /*check_self=*/false);
        derived_vtable_entry_id = build_specific_function(*i);
        override_fn.virtual_index = vtable.size();
        CARBON_CHECK(override_fn.virtual_index == fn.virtual_index);
      } else if (auto base_vtable_entry =
                     context.sem_ir().insts().TryGetAs<SemIR::SpecificFunction>(
                         base_vtable_entry_id)) {
        if (class_generic_id.has_value()) {
          derived_vtable_entry_id = GetOrAddInst<SemIR::SpecificFunction>(
              context, node_id,
              {.type_id = GetSingletonType(
                   context, SemIR::SpecificFunctionType::TypeInstId),
               .callee_id = base_vtable_entry->callee_id,
               .specific_id = base_vtable_entry->specific_id});
        }
      }
      vtable.push_back(derived_vtable_entry_id);
    }
  }

  for (auto inst_id : vtable_contents) {
    auto fn_decl = context.insts().GetAs<SemIR::FunctionDecl>(inst_id);
    auto& fn = context.functions().Get(fn_decl.function_id);
    if (fn.virtual_modifier != SemIR::FunctionFields::VirtualModifier::Impl) {
      fn.virtual_index = vtable.size();
      vtable.push_back(build_specific_function(inst_id));
    } else if (!implemented_impls.Lookup(fn_decl.function_id)) {
      CARBON_DIAGNOSTIC(ImplWithoutVirtualInBase, Error,
                        "impl without compatible virtual in base class");
      context.emitter().Emit(SemIR::LocId(inst_id), ImplWithoutVirtualInBase);

Review comment (suggestion):
```suggestion
      } else if (auto base_vtable_specific_function =
```

Code after:
      auto [derived_vtable_entry_id, derived_vtable_entry_const_id, fn_id,
            specific_id] =
          DecomposeVirtualFunction(context.sem_ir(), base_vtable_entry_id,
                                   base_class_specific_id);
      const auto& fn = context.sem_ir().functions().Get(fn_id);
      const auto* i = llvm::find_if(
          vtable_contents, [&](SemIR::InstId override_fn_decl_id) -> bool {
            const auto& override_fn = context.functions().Get(
                context.insts()
                    .GetAs<SemIR::FunctionDecl>(override_fn_decl_id)
                    .function_id);
            return override_fn.virtual_modifier ==
                       SemIR::FunctionFields::VirtualModifier::Impl &&
                   override_fn.name_id == fn.name_id;
          });
      if (i != vtable_contents.end()) {
        auto override_fn_id =
            context.insts().GetAs<SemIR::FunctionDecl>(*i).function_id;
        implemented_impls.Insert(override_fn_id);
        auto& override_fn = context.functions().Get(override_fn_id);
        CheckFunctionTypeMatches(context, override_fn, fn, specific_id,
                                 /*check_syntax=*/false,
                                 /*check_self=*/false);
        derived_vtable_entry_id = build_specific_function(*i);
        override_fn.virtual_index = vtable.size();
        CARBON_CHECK(override_fn.virtual_index == fn.virtual_index);
      } else if (auto base_vtable_specific_function =
                     context.sem_ir().insts().TryGetAs<SemIR::SpecificFunction>(
                         derived_vtable_entry_id)) {
        if (derived_vtable_entry_const_id.is_symbolic()) {
          // Create a new instruction here that is otherwise identical to
          // `derived_vtable_entry_id` but is dependent within the derived
          // class. This ensures we can `GetConstantValueInSpecific` for it
          // with the derived class's specific (when forming further derived
          // classes, lowering the vtable, etc).
          derived_vtable_entry_id = GetOrAddInst<SemIR::SpecificFunction>(
              context, node_id,
              {.type_id = GetSingletonType(
                   context, SemIR::SpecificFunctionType::TypeInstId),
               .callee_id = base_vtable_specific_function->callee_id,
               .specific_id = base_vtable_specific_function->specific_id});
        }
      }
      vtable.push_back(derived_vtable_entry_id);
    }
  }

  for (auto inst_id : vtable_contents) {
    auto fn_decl = context.insts().GetAs<SemIR::FunctionDecl>(inst_id);
    auto& fn = context.functions().Get(fn_decl.function_id);
    if (fn.virtual_modifier != SemIR::FunctionFields::VirtualModifier::Impl) {