Repository: carbon-language/carbon-lang
File: toolchain/lower/mangler.cpp (C++)

Code before:
    -> void {
  // Maintain a stack of names for delayed rendering of interface impls.
  struct NameEntry {
    SemIR::NameScopeId name_scope_id;
    SemIR::SpecificId specific_id;

    // The prefix emitted before this name component. If '\0', no prefix will be
    // emitted.
    // - Namespace components are separated by '.'.
    // - The two components of an interface are separated by ':'.
    char prefix;
  };
  llvm::SmallVector<NameEntry> names_to_render;
  names_to_render.push_back({.name_scope_id = name_scope_id,
                             .specific_id = SemIR::SpecificId::None,
                             .prefix = '.'});
  while (!names_to_render.empty()) {
    auto [name_scope_id, specific_id, prefix] = names_to_render.pop_back_val();
    if (prefix) {
      os << prefix;
    }
    if (!name_scope_id.has_value()) {
      continue;
    }
    if (name_scope_id == SemIR::NameScopeId::Package) {
      auto package_id = sem_ir().package_id();
      if (auto ident_id = package_id.AsIdentifierId(); ident_id.has_value()) {
        os << sem_ir().identifiers().Get(ident_id);
      } else {
        // TODO: Handle name conflicts between special package names and raw
        // identifier package names. Note that any change here will also require
        // a change to namespace mangling for imported packages.
        os << package_id.AsSpecialName();
      }
      continue;
    }
    const auto& name_scope = sem_ir().name_scopes().Get(name_scope_id);
    CARBON_KIND_SWITCH(sem_ir().insts().Get(name_scope.inst_id())) {
      case CARBON_KIND(SemIR::ImplDecl impl_decl): {
        const auto& impl = sem_ir().impls().Get(impl_decl.impl_id);

        auto facet_type = insts().GetAs<SemIR::FacetType>(
            constant_values().GetConstantInstId(impl.constraint_id));

        auto identified_facet_type_id =
            sem_ir().identified_facet_types().Lookup(
                {.facet_type_id = facet_type.facet_type_id,
                 .self_const_id =
                     sem_ir().constant_values().Get(impl.self_id)});
        CARBON_CHECK(identified_facet_type_id.has_value(),
                     "ImplDecl with unidentified facet type constraint");

Review comment (suggestion):
```suggestion
      // TODO: Include something in the mangling to identify the scope for a
      // function-local class, function, or similar. We may need to number
      // these within the enclosing function, as their name need not be unique.
      continue;
```

Code after:
    -> void {
  // Maintain a stack of names for delayed rendering of interface impls.
  struct NameEntry {
    SemIR::NameScopeId name_scope_id;
    SemIR::SpecificId specific_id;

    // The prefix emitted before this name component. If '\0', no prefix will be
    // emitted.
    // - Namespace components are separated by '.'.
    // - The two components of an interface are separated by ':'.
    char prefix;
  };
  llvm::SmallVector<NameEntry> names_to_render;
  names_to_render.push_back({.name_scope_id = name_scope_id,
                             .specific_id = SemIR::SpecificId::None,
                             .prefix = '.'});
  while (!names_to_render.empty()) {
    auto [name_scope_id, specific_id, prefix] = names_to_render.pop_back_val();
    if (prefix) {
      os << prefix;
    }
    if (!name_scope_id.has_value()) {
      // TODO: Include something in the mangling to identify the scope for a
      // function-local class, function, or similar. We may need to number
      // these within the enclosing function, as their name need not be unique.
      continue;
    }
    if (name_scope_id == SemIR::NameScopeId::Package) {
      auto package_id = sem_ir().package_id();
      if (auto ident_id = package_id.AsIdentifierId(); ident_id.has_value()) {
        os << sem_ir().identifiers().Get(ident_id);
      } else {
        // TODO: Handle name conflicts between special package names and raw
        // identifier package names. Note that any change here will also require
        // a change to namespace mangling for imported packages.
        os << package_id.AsSpecialName();
      }
      continue;
    }
    const auto& name_scope = sem_ir().name_scopes().Get(name_scope_id);
    CARBON_KIND_SWITCH(sem_ir().insts().Get(name_scope.inst_id())) {
      case CARBON_KIND(SemIR::ImplDecl impl_decl): {
        const auto& impl = sem_ir().impls().Get(impl_decl.impl_id);

        auto facet_type = insts().GetAs<SemIR::FacetType>(
            constant_values().GetConstantInstId(impl.constraint_id));

        auto identified_facet_type_id =
            sem_ir().identified_facet_types().Lookup(
                {.facet_type_id = facet_type.facet_type_id,
                 .self_const_id =