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

Code before:
  const auto& symbolic =
      context.constant_values().GetSymbolicConstant(const_id);
  // Fast early reject before doing more expensive operations.
  if (symbolic.dependence != SemIR::ConstantDependence::PeriodSelf) {
    return false;
  }
  return IsPeriodSelf(context, symbolic.inst_id);
}

auto IsPeriodSelf(Context& context, SemIR::InstId inst_id) -> bool {
  // Unwrap the `FacetAccessType` instruction, which we get when the `.Self` is
  // converted to `type`.
  if (auto facet_access_type =
          context.insts().TryGetAs<SemIR::FacetAccessType>(inst_id)) {
    inst_id = facet_access_type->facet_value_inst_id;
  }
  if (auto bind_symbolic_name =
          context.insts().TryGetAs<SemIR::BindSymbolicName>(inst_id)) {
    const auto& bind_name =
        context.entity_names().Get(bind_symbolic_name->entity_name_id);
    return bind_name.name_id == SemIR::NameId::PeriodSelf;
  }
  return false;
}

// If `inst_id` is an `ImplWitnessAccess` into `.Self`, return it. Otherwise,
// returns `nullopt`.
static auto TryGetImplWitnessAccessOfPeriodSelf(Context& context,
                                                SemIR::InstId inst_id)
    -> std::optional<SemIR::ImplWitnessAccess> {
  auto lhs_access = context.insts().TryGetAs<SemIR::ImplWitnessAccess>(inst_id);
  if (!lhs_access) {
    return std::nullopt;
  }
  auto lhs_lookup = context.insts().TryGetAs<SemIR::LookupImplWitness>(
      lhs_access->witness_id);
  if (!lhs_lookup) {
    return std::nullopt;
  }
  if (!IsPeriodSelf(context, lhs_lookup->query_self_inst_id)) {
    return std::nullopt;
  }
  return lhs_access;
}

// To be used for substituting into the RHS of a rewrite constraint.
//
// It will substitute any `ImplWitnessAccess` into `.Self` (a reference to an
// associated constant) with the RHS of another rewrite constraint that writes
// to the same associated constant. For example:
// ```

Review comment (suggestion):
```suggestion
// If `inst_id` is the ID of an `ImplWitnessAccess` into `.Self`, return it. Otherwise,
```

(As written it sounds like the function returns the argument, which would be a little surprising.)

Code after:
  }
  const auto& symbolic =
      context.constant_values().GetSymbolicConstant(const_id);
  // Fast early reject before doing more expensive operations.
  if (symbolic.dependence != SemIR::ConstantDependence::PeriodSelf) {
    return false;
  }
  return IsPeriodSelf(context, symbolic.inst_id);
}

auto IsPeriodSelf(Context& context, SemIR::InstId inst_id) -> bool {
  // Unwrap the `FacetAccessType` instruction, which we get when the `.Self` is
  // converted to `type`.
  if (auto facet_access_type =
          context.insts().TryGetAs<SemIR::FacetAccessType>(inst_id)) {
    inst_id = facet_access_type->facet_value_inst_id;
  }
  if (auto bind_symbolic_name =
          context.insts().TryGetAs<SemIR::BindSymbolicName>(inst_id)) {
    const auto& bind_name =
        context.entity_names().Get(bind_symbolic_name->entity_name_id);
    return bind_name.name_id == SemIR::NameId::PeriodSelf;
  }
  return false;
}

// If `inst_id` is the ID of an `ImplWitnessAccess` into `.Self`, return it.
// Otherwise, returns `nullopt`.
static auto TryGetImplWitnessAccessOfPeriodSelf(Context& context,
                                                SemIR::InstId inst_id)
    -> std::optional<SemIR::ImplWitnessAccess> {
  auto lhs_access = context.insts().TryGetAs<SemIR::ImplWitnessAccess>(inst_id);
  if (!lhs_access) {
    return std::nullopt;
  }
  auto lhs_lookup = context.insts().TryGetAs<SemIR::LookupImplWitness>(
      lhs_access->witness_id);
  if (!lhs_lookup) {
    return std::nullopt;
  }
  if (!IsPeriodSelf(context, lhs_lookup->query_self_inst_id)) {
    return std::nullopt;
  }
  return lhs_access;
}

// To be used for substituting into the RHS of a rewrite constraint.
//
// It will substitute any `ImplWitnessAccess` into `.Self` (a reference to an
// associated constant) with the RHS of another rewrite constraint that writes
// to the same associated constant. For example: