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

Code before:
  CARBON_CHECK(!context.types().Is<SemIR::PatternType>(scrutinee_type_id),
               "Type is already a pattern type");
  if (scrutinee_type_id == SemIR::ErrorInst::TypeId) {
    return SemIR::ErrorInst::TypeId;
  }
  return GetTypeImpl<SemIR::PatternType>(
      context, context.types().GetInstId(scrutinee_type_id));
}

auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
                           SemIR::TypeInstId element_type_id) -> SemIR::TypeId {
  return GetTypeImpl<SemIR::UnboundElementType>(context, class_type_id,
                                                element_type_id);
}

auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
    -> SemIR::InstId {
  if (inst_id == SemIR::ErrorInst::InstId) {
    return inst_id;
  }

  CARBON_CHECK(
      context.types().IsFacetType(context.insts().Get(inst_id).type_id()),
      "{0}", context.insts().Get(inst_id).type_id());

  inst_id = context.constant_values().GetConstantInstId(inst_id);

  if (auto access = context.insts().TryGetAs<SemIR::FacetAccessType>(inst_id)) {
    inst_id = access->facet_value_inst_id;
  }

  return inst_id;
}

auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::ConstantId const_id)
    -> SemIR::ConstantId {
  return context.constant_values().Get(GetCanonicalFacetOrTypeValue(
      context, context.constant_values().GetInstId(const_id)));
}

}  // namespace Carbon::Check


Review comment (suggestion):
Had you considered using a different variable for this, to make clear it's never related to the original value past this?

e.g. (would need changes below):

```suggestion
  SemIR::InstId constant_inst_id = context.constant_values().GetConstantInstId(inst_id);
```

Code after:
  CARBON_CHECK(!context.types().Is<SemIR::PatternType>(scrutinee_type_id),
               "Type is already a pattern type");
  if (scrutinee_type_id == SemIR::ErrorInst::TypeId) {
    return SemIR::ErrorInst::TypeId;
  }
  return GetTypeImpl<SemIR::PatternType>(
      context, context.types().GetInstId(scrutinee_type_id));
}

auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
                           SemIR::TypeInstId element_type_id) -> SemIR::TypeId {
  return GetTypeImpl<SemIR::UnboundElementType>(context, class_type_id,
                                                element_type_id);
}

auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
    -> SemIR::InstId {
  if (inst_id == SemIR::ErrorInst::InstId) {
    return inst_id;
  }

  CARBON_CHECK(
      context.types().IsFacetType(context.insts().Get(inst_id).type_id()),
      "{0}", context.insts().Get(inst_id).type_id());

  auto const_inst_id = context.constant_values().GetConstantInstId(inst_id);

  if (auto access =
          context.insts().TryGetAs<SemIR::FacetAccessType>(const_inst_id)) {
    return access->facet_value_inst_id;
  }

  return const_inst_id;
}

auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::ConstantId const_id)
    -> SemIR::ConstantId {
  return context.constant_values().Get(GetCanonicalFacetOrTypeValue(
      context, context.constant_values().GetInstId(const_id)));
}

}  // namespace Carbon::Check