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

Code before:
      auto index_id = GetConstantValue(eval_context, arg_ids[1], &phase);
      //If either isnt constant, we can't evaluate it at compile time
      if (!str_id.has_value() || !index_id.has_value()) {
        return MakeNonConstantResult(phase);
      }

      auto str_struct = eval_context.insts().TryGetAs<SemIR::StructValue>(str_id);
      if(!str_struct) {
        return MakeNonConstantResult(phase);
      }

      auto elements = eval_context.inst_blocks().Get(str_struct->elements_id);
      if (elements.size() != 2) {
        return MakeNonConstantResult(phase);
      }

      auto ptr_const_id = eval_context.constant_values().Get(elements[0]);
      auto string_literal = eval_context.insts().TryGetAs<SemIR::StringLiteral>(
        eval_context.constant_values().GetInstId(ptr_const_id));
      if (!string_literal){
        return MakeNonConstantResult(phase);
      }
      
      auto string_value = eval_context.sem_ir().string_literal_values().Get(
        string_literal->string_literal_id);
      //Get index value
      auto index_inst = eval_context.insts().TryGetAs<SemIR::IntValue>(index_id);
      if (!index_inst) {
        CARBON_CHECK(phase != Phase::Concrete, "Concrete constant integer should be a literal");
        return MakeNonConstantResult(phase);
      }

      const auto& index_val = eval_context.ints().Get(index_inst->int_id);

      //Check bounds
      if (index_val.isNegative()){
        CARBON_DIAGNOSTIC(ArrayIndexNegative, Error, "index `{0}` is negative",
                          TypedInt);
        eval_context.emitter().Emit(
          eval_context.GetDiagnosticLoc(index_id), ArrayIndexNegative,
          {.type = eval_context.insts().Get(index_id).type_id(),
          .value = index_val});
        return SemIR::ErrorInst::ConstantId;
      }

      //Check for out of bounds
      if (index_val.getActiveBits() > 64 ||
          index_val.getZExtValue() >= string_value.size()) {
        CARBON_DIAGNOSTIC(ArrayIndexOutOfBounds, Error,
                          "string index `{0}` is past the end of the string",
                          TypedInt);

Review comment (suggestion):
```suggestion
```
This comment just says what the code says

Code after:
          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.
      CARBON_CHECK(elements.size() == 2, "String struct should have 2 fields.");

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

      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(StringAtIndexNegative, Error,
                          "index `{0}` is negative.", TypedInt);
        context.emitter().Emit(