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

Code before:
  // Other instructions are imported in a single phase (once their dependencies
  // are all imported).
  CARBON_CHECK(!const_id.has_value());

  auto inst_constant_id = resolver.import_constant_values().Get(inst_id);
  if (!inst_constant_id.is_constant()) {
    // TODO: Import of non-constant BindNames happens when importing `let`
    // declarations.
    CARBON_CHECK(resolver.import_insts().Is<SemIR::BindName>(inst_id),
                 "TryResolveInst on non-constant instruction {0}", inst_id);
    return ResolveResult::Done(SemIR::ConstantId::NotConstant);
  }

  // Import the canonical constant value instruction for `inst_id` directly. We
  // don't try to import the non-canonical `inst_id`.
  auto constant_inst_id =
      resolver.import_constant_values().GetInstId(inst_constant_id);
  CARBON_DCHECK(resolver.import_constant_values().GetConstantInstId(
                    constant_inst_id) == constant_inst_id,
                "Constant value of constant instruction should refer to "
                "the same instruction");

  if (SemIR::IsSingletonInstId(constant_inst_id)) {
    // Constants for builtins can be directly copied.
    return ResolveResult::Done(
        resolver.local_constant_values().Get(constant_inst_id));
  }

  auto untyped_constant_inst =
      resolver.import_insts().GetWithAttachedType(constant_inst_id);
  CARBON_KIND_SWITCH(untyped_constant_inst) {
    case CARBON_KIND(SemIR::AdaptDecl inst): {
      return TryResolveTypedInst(resolver, inst, constant_inst_id);
    }
    case CARBON_KIND(SemIR::AddrPattern inst): {
      return TryResolveTypedInst(resolver, inst, constant_inst_id);
    }
    case CARBON_KIND(SemIR::ArrayType inst): {
      return TryResolveTypedInst(resolver, inst);
    }
    case CARBON_KIND(SemIR::AssociatedEntity inst): {
      return TryResolveTypedInst(resolver, inst);
    }
    case CARBON_KIND(SemIR::AssociatedEntityType inst): {
      return TryResolveTypedInst(resolver, inst);
    }
    case CARBON_KIND(SemIR::BaseDecl inst): {
      return TryResolveTypedInst(resolver, inst, constant_inst_id);
    }
    case CARBON_KIND(SemIR::BindAlias inst): {
      return TryResolveTypedInst(resolver, inst);

Review comment (suggestion):
To be sure, does this need to be done after the above, or does it change the result to do it before all other logic (before GetWithAttachedType, as in the base version)? I ask just because singletons are a little special, and I'm a little surprised if the indirections are needed for it.

Code after:
      break;
  }

  // Other instructions are imported in a single phase (once their dependencies
  // are all imported).
  CARBON_CHECK(!const_id.has_value());

  auto inst_constant_id = resolver.import_constant_values().Get(inst_id);
  if (!inst_constant_id.is_constant()) {
    // TODO: Import of non-constant BindNames happens when importing `let`
    // declarations.
    CARBON_CHECK(resolver.import_insts().Is<SemIR::BindName>(inst_id),
                 "TryResolveInst on non-constant instruction {0}", inst_id);
    return ResolveResult::Done(SemIR::ConstantId::NotConstant);
  }

  // Import the canonical constant value instruction for `inst_id` directly. We
  // don't try to import the non-canonical `inst_id`.
  auto constant_inst_id =
      resolver.import_constant_values().GetInstId(inst_constant_id);
  CARBON_DCHECK(resolver.import_constant_values().GetConstantInstId(
                    constant_inst_id) == constant_inst_id,
                "Constant value of constant instruction should refer to "
                "the same instruction");

  if (SemIR::IsSingletonInstId(constant_inst_id)) {
    // Constants for builtins can be directly copied.
    return ResolveResult::Done(
        resolver.local_constant_values().Get(constant_inst_id));
  }

  auto untyped_constant_inst =
      resolver.import_insts().GetWithAttachedType(constant_inst_id);
  CARBON_KIND_SWITCH(untyped_constant_inst) {
    case CARBON_KIND(SemIR::AdaptDecl inst): {
      return TryResolveTypedInst(resolver, inst, constant_inst_id);
    }
    case CARBON_KIND(SemIR::AddrPattern inst): {
      return TryResolveTypedInst(resolver, inst, constant_inst_id);
    }
    case CARBON_KIND(SemIR::ArrayType inst): {
      return TryResolveTypedInst(resolver, inst);
    }
    case CARBON_KIND(SemIR::AssociatedEntity inst): {
      return TryResolveTypedInst(resolver, inst);
    }
    case CARBON_KIND(SemIR::AssociatedEntityType inst): {
      return TryResolveTypedInst(resolver, inst);
    }
    case CARBON_KIND(SemIR::BaseDecl inst): {
      return TryResolveTypedInst(resolver, inst, constant_inst_id);