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

Code before:
    if (auto value =
            eval_context.GetCompileTimeBindValue(bind_name.bind_index());
        value.has_value()) {
      return value;
    }
  }

  // The constant form of a symbolic binding is an idealized form of the
  // original, with no equivalent value.
  Phase phase = Phase::Concrete;
  bind.value_id = SemIR::InstId::None;
  if (!ReplaceTypeWithConstantValue(eval_context, inst_id, &bind, &phase) ||
      !ReplaceFieldWithConstantValue(eval_context, &bind,
                                     &SemIR::BindSymbolicName::entity_name_id,
                                     &phase)) {
    return SemIR::ConstantId::NotConstant;
  }
  // This correctly handles `Phase::UnknownDueToError`.
  return MakeConstantResult(eval_context.context(), bind, phase);
}

// Returns whether `const_id` is the same constant facet value as
// `facet_value_inst_id`.
static auto IsSameFacetValue(Context& context, SemIR::ConstantId const_id,
                             SemIR::InstId facet_value_inst_id) -> bool {
  const_id = GetCanonicalFacetOrTypeValue(context, const_id);
  return const_id == context.constant_values().Get(facet_value_inst_id);
}

// TODO: Convert this to an EvalConstantInst function. This will require
// providing a `GetConstantValue` overload for a requirement block.
template <>
auto TryEvalTypedInst<SemIR::WhereExpr>(EvalContext& eval_context,
                                        SemIR::InstId inst_id, SemIR::Inst inst)
    -> SemIR::ConstantId {
  auto typed_inst = inst.As<SemIR::WhereExpr>();

  Phase phase = Phase::Concrete;
  SemIR::FacetTypeInfo info;

  // Add the constraints from the `WhereExpr` instruction into `info`.
  if (typed_inst.requirements_id.has_value()) {
    auto insts = eval_context.inst_blocks().Get(typed_inst.requirements_id);
    for (auto inst_id : insts) {
      if (auto base =
              eval_context.insts().TryGetAs<SemIR::RequirementBaseFacetType>(
                  inst_id)) {
        if (base->base_type_inst_id == SemIR::ErrorInst::TypeInstId) {
          return SemIR::ErrorInst::ConstantId;
        }


Review comment (suggestion):
Similar to the above comment, perhaps use a different variable here since it's always set?

Or, you could perhaps even drop it, e.g.:

```
  return GetCanonicalFacetOrTypeValue(context, const_id) == context.constant_values().Get(facet_value_inst_id);
```

Code after:
    if (auto value =
            eval_context.GetCompileTimeBindValue(bind_name.bind_index());
        value.has_value()) {
      return value;
    }
  }

  // The constant form of a symbolic binding is an idealized form of the
  // original, with no equivalent value.
  Phase phase = Phase::Concrete;
  bind.value_id = SemIR::InstId::None;
  if (!ReplaceTypeWithConstantValue(eval_context, inst_id, &bind, &phase) ||
      !ReplaceFieldWithConstantValue(eval_context, &bind,
                                     &SemIR::BindSymbolicName::entity_name_id,
                                     &phase)) {
    return SemIR::ConstantId::NotConstant;
  }
  // This correctly handles `Phase::UnknownDueToError`.
  return MakeConstantResult(eval_context.context(), bind, phase);
}

// Returns whether `const_id` is the same constant facet value as
// `facet_value_inst_id`.
//
// Compares with the canonical facet value of `const_id`, dropping any `as type`
// conversions.
static auto IsSameFacetValue(Context& context, SemIR::ConstantId const_id,
                             SemIR::InstId facet_value_inst_id) -> bool {
  auto canon_const_id = GetCanonicalFacetOrTypeValue(context, const_id);
  return canon_const_id == context.constant_values().Get(facet_value_inst_id);
}

// TODO: Convert this to an EvalConstantInst function. This will require
// providing a `GetConstantValue` overload for a requirement block.
template <>
auto TryEvalTypedInst<SemIR::WhereExpr>(EvalContext& eval_context,
                                        SemIR::InstId inst_id, SemIR::Inst inst)
    -> SemIR::ConstantId {
  auto typed_inst = inst.As<SemIR::WhereExpr>();

  Phase phase = Phase::Concrete;
  SemIR::FacetTypeInfo info;

  // Add the constraints from the `WhereExpr` instruction into `info`.
  if (typed_inst.requirements_id.has_value()) {
    auto insts = eval_context.inst_blocks().Get(typed_inst.requirements_id);
    for (auto inst_id : insts) {
      if (auto base =
              eval_context.insts().TryGetAs<SemIR::RequirementBaseFacetType>(
                  inst_id)) {
        if (base->base_type_inst_id == SemIR::ErrorInst::TypeInstId) {