Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/inst_fingerprinter.cpp (C++)

Code before:
  }

  // Add an instruction argument to the contents of the current instruction.
  auto AddWithKind(Inst::ArgAndKind arg) -> void {
    GetAddFn(arg.kind())(*this, arg.value());
  }

  // Ensure all the instructions on the todo list have fingerprints. To avoid a
  // re-lookup, returns the fingerprint of the first instruction on the todo
  // list, and requires the todo list to be non-empty.
  auto Run() -> uint64_t {
    CARBON_CHECK(!todo.empty());
    while (true) {
      const size_t init_size = todo.size();
      auto [next_sem_ir, next] = todo.back();

      sem_ir = next_sem_ir;
      contents.clear();

      if (!std::holds_alternative<InstId>(next)) {
        // Add the contents of the `next` instruction so they all contribute to
        // the `contents`.
        CARBON_KIND_SWITCH(next) {
          case CARBON_KIND(InstId _):
            // InstId is checked for above.
            CARBON_FATAL();
          case CARBON_KIND(ImplId impl_id):
            Add(impl_id);
            break;
          case CARBON_KIND(InstBlockId inst_block_id):
            Add(inst_block_id);
            break;
          case CARBON_KIND(CppOverloadSetId overload_set_id):
            Add(overload_set_id);
            break;
        }

        // If we didn't add any more work, then we have a fingerprint for the
        // `next` instruction, otherwise we wait until that work is completed.
        // If the `next` is the last thing in `todo`, we return the fingerprint.
        // Otherwise we would just discard it because we don't currently cache
        // the fingerprint for things other than `InstId`, but we really only
        // expect other `next` types to be at the bottom of the `todo` stack
        // since they are not added to `todo` during Run().
        if (todo.size() == init_size) {
          auto fingerprint = Finish();
          todo.pop_back();
          CARBON_CHECK(todo.empty(),
                       "A non-InstId was inserted into `todo` during Run()");
          return fingerprint;
        }

Review comment (suggestion):
I think it's a good habit to include strings for check messages, and it seems like the comment can be used here:

```suggestion
            CARBON_FATAL("InstId is checked for above.");
```

Code after:
  }

  // Add an instruction argument to the contents of the current instruction.
  auto AddWithKind(Inst::ArgAndKind arg) -> void {
    GetAddFn(arg.kind())(*this, arg.value());
  }

  // Ensure all the instructions on the todo list have fingerprints. To avoid a
  // re-lookup, returns the fingerprint of the first instruction on the todo
  // list, and requires the todo list to be non-empty.
  auto Run() -> uint64_t {
    CARBON_CHECK(!todo.empty());
    while (true) {
      const size_t init_size = todo.size();
      auto [next_sem_ir, next] = todo.back();

      sem_ir = next_sem_ir;
      contents.clear();

      if (!std::holds_alternative<InstId>(next)) {
        // Add the contents of the `next` instruction so they all contribute to
        // the `contents`.
        CARBON_KIND_SWITCH(next) {
          case CARBON_KIND(InstId _):
            CARBON_FATAL("InstId is checked for above.");
          case CARBON_KIND(ImplId impl_id):
            Add(impl_id);
            break;
          case CARBON_KIND(InstBlockId inst_block_id):
            Add(inst_block_id);
            break;
          case CARBON_KIND(CppOverloadSetId overload_set_id):
            Add(overload_set_id);
            break;
        }

        // If we didn't add any more work, then we have a fingerprint for the
        // `next` instruction, otherwise we wait until that work is completed.
        // If the `next` is the last thing in `todo`, we return the fingerprint.
        // Otherwise we would just discard it because we don't currently cache
        // the fingerprint for things other than `InstId`, but we really only
        // expect other `next` types to be at the bottom of the `todo` stack
        // since they are not added to `todo` during Run().
        if (todo.size() == init_size) {
          auto fingerprint = Finish();
          todo.pop_back();
          CARBON_CHECK(todo.empty(),
                       "A non-InstId was inserted into `todo` during Run()");
          return fingerprint;
        }