Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/inst.h (C/C++)

Code before:
  template <typename InstT>
  auto TryGetAs(InstId inst_id) const -> std::optional<InstT> {
    return Get(inst_id).TryAs<InstT>();
  }

  // Use `Get()` when the instruction type is known.
  template <typename InstT, typename KnownInstT>
  auto TryGetAs(KnownInstId<KnownInstT> inst_id) const = delete;

  // Returns the requested instruction as the specified type, if it is valid and
  // of that type. Otherwise returns nullopt.
  template <typename InstT>
  auto TryGetAsIfValid(InstId inst_id) const -> std::optional<InstT> {
    if (!inst_id.has_value()) {
      return std::nullopt;
    }
    return TryGetAs<InstT>(inst_id);
  }

  // Returns the `KnownInstId` form of `inst_id`. Requires a matching
  // instruction type.
  template <typename InstT>
  auto GetAsKnownInstId(InstId inst_id) const -> KnownInstId<InstT> {
    CARBON_CHECK(Is<InstT>(inst_id), "Casting inst {0} to wrong kind {1}",
                 Get(inst_id), Internal::InstLikeTypeInfo<InstT>::DebugName());
    return KnownInstId<InstT>::UnsafeMake(inst_id);
  }

  template <typename InstT>
  struct GetAsWithIdResult {
    KnownInstId<InstT> inst_id;
    InstT inst;
  };

  // Returns the requested instruction, which is known to have the specified
  // type, along with the original `InstId`, encoding the work of checking its
  // type in a `KnownInstId`.
  template <typename InstT>
  auto GetAsWithId(InstId inst_id) const -> GetAsWithIdResult<InstT> {
    auto inst = GetAs<InstT>(inst_id);
    return {.inst_id = KnownInstId<InstT>::UnsafeMake(inst_id), .inst = inst};
  }

  // Returns the requested instruction, if it is of that type, along with the
  // original `InstId`, encoding the work of checking its type in a
  // `KnownInstId`.
  template <typename InstT>
  auto TryGetAsWithId(InstId inst_id) const
      -> std::optional<GetAsWithIdResult<InstT>> {
    auto inst = TryGetAs<InstT>(inst_id);
    if (!inst) {

Review comment (suggestion):
nit: I would consider implementing this function as `return GetAsWithId(inst_id).inst_id` as this performs the same checks and does the same work, but avoids introducing another UnsafeMake

Code after:
  template <typename InstT>
  auto TryGetAs(InstId inst_id) const -> std::optional<InstT> {
    return Get(inst_id).TryAs<InstT>();
  }

  // Use `Get()` when the instruction type is known.
  template <typename InstT, typename KnownInstT>
  auto TryGetAs(KnownInstId<KnownInstT> inst_id) const = delete;

  // Returns the requested instruction as the specified type, if it is valid and
  // of that type. Otherwise returns nullopt.
  template <typename InstT>
  auto TryGetAsIfValid(InstId inst_id) const -> std::optional<InstT> {
    if (!inst_id.has_value()) {
      return std::nullopt;
    }
    return TryGetAs<InstT>(inst_id);
  }

  // Returns the `KnownInstId` form of `inst_id`. Requires a matching
  // instruction type.
  template <typename InstT>
  auto GetAsKnownInstId(InstId inst_id) const -> KnownInstId<InstT> {
    CARBON_CHECK(Is<InstT>(inst_id), "Casting inst {0} to wrong kind {1}",
                 Get(inst_id), Internal::InstLikeTypeInfo<InstT>::DebugName());
    return KnownInstId<InstT>::UnsafeMake(inst_id);
  }

  template <typename InstT>
  struct GetAsWithIdResult {
    KnownInstId<InstT> inst_id;
    InstT inst;
  };

  // Returns the requested instruction, if it is of that type, along with the
  // original `InstId`, encoding the work of checking its type in a
  // `KnownInstId`.
  template <typename InstT>
  auto TryGetAsWithId(InstId inst_id) const
      -> std::optional<GetAsWithIdResult<InstT>> {
    auto inst = TryGetAs<InstT>(inst_id);
    if (!inst) {
      return std::nullopt;
    }
    return {
        {.inst_id = KnownInstId<InstT>::UnsafeMake(inst_id), .inst = *inst}};
  }

  // Attempts to convert the given instruction to the type that contains
  // `member`. If it can be converted, the instruction ID and instruction are
  // replaced by the unwrapped value of that member, and the converted wrapper