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

Code before:
          eval_context.context(),
          SemIR::FacetType{.type_id = SemIR::TypeType::TypeId,
                           .facet_type_id = id},
          phase);
    }

    case SemIR::BuiltinFunctionKind::PrimitiveCopy: {
      return context.constant_values().Get(arg_ids[0]);
    }

    case SemIR::BuiltinFunctionKind::StringAt: {
      Phase phase = Phase::Concrete;
      auto str_id = GetConstantValue(eval_context, arg_ids[0], &phase);
      auto index_id = GetConstantValue(eval_context, arg_ids[1], &phase);

      if (phase != Phase::Concrete) {
        return MakeNonConstantResult(phase);
      }

      auto str_struct = eval_context.insts().GetAs<SemIR::StructValue>(str_id);
      auto elements = eval_context.inst_blocks().Get(str_struct.elements_id);
      CARBON_CHECK(elements.size() == 2, "String struct should have 2 fields.");

      auto ptr_const_id = eval_context.constant_values().Get(elements[0]);
      auto string_literal = eval_context.insts().GetAs<SemIR::StringLiteral>(
          eval_context.constant_values().GetInstId(ptr_const_id));

      const auto& string_value =
          eval_context.sem_ir().string_literal_values().Get(
              string_literal.string_literal_id);

      auto index_inst = eval_context.insts().GetAs<SemIR::IntValue>(index_id);
      const auto& index_val = eval_context.ints().Get(index_inst.int_id);

      if (index_val.isNegative()) {
        CARBON_DIAGNOSTIC(StringIndexNegative, Error,
                          "index `{0}` is negative.", TypedInt);
        context.emitter().Emit(
            loc_id, StringIndexNegative,
            {.type = eval_context.insts().Get(arg_ids[1]).type_id(),
             .value = index_val});
        return SemIR::ConstantId::NotConstant;
      }

      if (index_val.getActiveBits() > 64 ||
          index_val.getZExtValue() >= string_value.size()) {
        CARBON_DIAGNOSTIC(
            StringAtIndexOutOfBounds, Error,
            "string index `{0}` is out of bounds; string has length {1}.",
            TypedInt, unsigned);
        context.emitter().Emit(

Review comment (suggestion):
I think I'd combine these
```suggestion
      auto string_literal = eval_context.insts().GetAs<SemIR::StringLiteral>(
          eval_context.constant_values().GetConstantInstId(elements[0]));
```

Code after:
// Returns a constant for a call to a builtin function.
static auto MakeConstantForBuiltinCall(EvalContext& eval_context,
                                       SemIR::LocId loc_id, SemIR::Call call,
                                       SemIR::BuiltinFunctionKind builtin_kind,
                                       llvm::ArrayRef<SemIR::InstId> arg_ids,
                                       Phase phase) -> SemIR::ConstantId {
  auto& context = eval_context.context();
  switch (builtin_kind) {
    case SemIR::BuiltinFunctionKind::None:
      CARBON_FATAL("Not a builtin function.");

    case SemIR::BuiltinFunctionKind::NoOp:
    case SemIR::BuiltinFunctionKind::TypeDestroy: {
      // Return an empty tuple value.
      auto type_id = GetTupleType(eval_context.context(), {});
      return MakeConstantResult(
          eval_context.context(),
          SemIR::TupleValue{.type_id = type_id,
                            .elements_id = SemIR::InstBlockId::Empty},
          phase);
    }

    case SemIR::BuiltinFunctionKind::TypeCanDestroy: {
      CARBON_CHECK(arg_ids.empty());
      auto id = eval_context.facet_types().Add(
          {.builtin_constraint_mask =
               SemIR::BuiltinConstraintMask::TypeCanDestroy});
      return MakeConstantResult(
          eval_context.context(),
          SemIR::FacetType{.type_id = SemIR::TypeType::TypeId,
                           .facet_type_id = id},
          phase);
    }

    case SemIR::BuiltinFunctionKind::PrimitiveCopy: {
      return context.constant_values().Get(arg_ids[0]);
    }

    case SemIR::BuiltinFunctionKind::StringAt: {
      Phase phase = Phase::Concrete;
      auto str_id = GetConstantValue(eval_context, arg_ids[0], &phase);
      auto index_id = GetConstantValue(eval_context, arg_ids[1], &phase);

      if (phase != Phase::Concrete) {
        return MakeNonConstantResult(phase);
      }

      auto str_struct = eval_context.insts().GetAs<SemIR::StructValue>(str_id);
      auto elements = eval_context.inst_blocks().Get(str_struct.elements_id);
      // String struct has two fields: a pointer to the string data and the
      // length.