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

Code before:
  return ConstantEvalResult::Existing(
      context.constant_values().Get(inst.value_id));
}

auto EvalConstantInst(Context& context, SemIR::FacetAccessType inst)
    -> ConstantEvalResult {
  if (auto facet_value = context.insts().TryGetAs<SemIR::FacetValue>(
          inst.facet_value_inst_id)) {
    return ConstantEvalResult::Existing(
        context.constant_values().Get(facet_value->type_inst_id));
  }
  return ConstantEvalResult::NewSamePhase(inst);
}

auto EvalConstantInst(Context& context, SemIR::FacetValue inst)
    -> ConstantEvalResult {
  // A FacetValue that just wraps a BindSymbolicName without adding/removing any
  // witnesses is evaluated back to the BindSymbolicName itself.
  if (auto access =
          context.insts().TryGetAs<SemIR::FacetAccessType>(inst.type_inst_id)) {
    auto bind_id = access->facet_value_inst_id;
    auto bind = context.insts().Get(bind_id);
    if (bind.Is<SemIR::BindSymbolicName>()) {
      // If the FacetTypes are the same, then the FacetValue didn't add/remove
      // any witnesses.
      if (bind.type_id() == inst.type_id) {
        return ConstantEvalResult::Existing(
            context.constant_values().Get(bind_id));
      }
    }
  }

  return ConstantEvalResult::NewSamePhase(inst);
}

auto EvalConstantInst(Context& context, SemIR::InstId inst_id,
                      SemIR::FloatType inst) -> ConstantEvalResult {
  return ValidateFloatTypeAndSetKind(context, SemIR::LocId(inst_id), inst)
             ? ConstantEvalResult::NewSamePhase(inst)
             : ConstantEvalResult::Error;
}

auto EvalConstantInst(Context& /*context*/, SemIR::FunctionDecl inst)
    -> ConstantEvalResult {
  // A function declaration evaluates to a function object, which is an empty
  // object of function type.
  // TODO: Eventually we may need to handle captures here.
  return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
      .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
}


Review comment (suggestion):
Maybe bundle this together and reduce nesting, such as:

```suggestion
    // If the FacetTypes are the same, then the FacetValue didn't add/remove
    // any witnesses.
    if (auto bind = context.insts().TryGetAs<SemIR::BindSymbolicName>(bind_id); bind && bind.type_id() == inst.type_id) {
```

Code after:
    return ConstantEvalResult::Existing(
        context.constant_values().Get(facet_value->type_inst_id));
  }
  return ConstantEvalResult::NewSamePhase(inst);
}

auto EvalConstantInst(Context& context, SemIR::FacetValue inst)
    -> ConstantEvalResult {
  // A FacetValue that just wraps a BindSymbolicName without adding/removing any
  // witnesses is evaluated back to the BindSymbolicName itself.
  if (auto access =
          context.insts().TryGetAs<SemIR::FacetAccessType>(inst.type_inst_id)) {
    auto bind_id = access->facet_value_inst_id;
    auto bind = context.insts().TryGetAs<SemIR::BindSymbolicName>(bind_id);
    // If the FacetTypes are the same, then the FacetValue didn't add/remove
    // any witnesses.
    if (bind.has_value() && bind->type_id == inst.type_id) {
      return ConstantEvalResult::Existing(
          context.constant_values().Get(bind_id));
    }
  }

  return ConstantEvalResult::NewSamePhase(inst);
}

auto EvalConstantInst(Context& context, SemIR::InstId inst_id,
                      SemIR::FloatType inst) -> ConstantEvalResult {
  return ValidateFloatTypeAndSetKind(context, SemIR::LocId(inst_id), inst)
             ? ConstantEvalResult::NewSamePhase(inst)
             : ConstantEvalResult::Error;
}

auto EvalConstantInst(Context& /*context*/, SemIR::FunctionDecl inst)
    -> ConstantEvalResult {
  // A function declaration evaluates to a function object, which is an empty
  // object of function type.
  // TODO: Eventually we may need to handle captures here.
  return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
      .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
}

auto EvalConstantInst(Context& context, SemIR::InstId inst_id,
                      SemIR::LookupImplWitness inst) -> ConstantEvalResult {
  // The self value is canonicalized in order to produce a canonical
  // LookupImplWitness instruction. We save the non-canonical instruction as it
  // may be a concrete `FacetValue` that contains a concrete witness.
  auto non_canonical_query_self_inst_id = inst.query_self_inst_id;
  inst.query_self_inst_id =
      GetCanonicalizedFacetOrTypeValue(context, inst.query_self_inst_id);

  auto result = EvalLookupSingleImplWitness(