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

Code before:
          [&] {
            CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error,
                              "{0:field|variable} has incomplete type {1}",
                              BoolAsSelect, SemIR::TypeId);
            return context.emitter().Build(type_node, IncompleteTypeInVarDecl,
                                           parent_class_decl.has_value(),
                                           cast_type_id);
          },
          [&] {
            CARBON_DIAGNOSTIC(AbstractTypeInVarDecl, Error,
                              "{0:field|variable} has abstract type {1}",
                              BoolAsSelect, SemIR::TypeId);
            return context.emitter().Build(type_node, AbstractTypeInVarDecl,
                                           parent_class_decl.has_value(),
                                           cast_type_id);
          });
      if (parent_class_decl) {
        CARBON_CHECK(context_node_kind == Parse::NodeKind::VariableIntroducer,
                     "`returned var` at class scope");
        auto& class_info = context.classes().Get(parent_class_decl->class_id);
        auto field_type_id = context.GetUnboundElementType(
            class_info.self_type_id, cast_type_id);
        auto field_id = context.AddInst<SemIR::FieldDecl>(
            binding_id, {.type_id = field_type_id,
                         .name_id = name_id,
                         .index = SemIR::ElementIndex(-1)});
        context.field_decls_stack().AppendToTop(field_id);

        // Add a corresponding field to the object representation of the class.
        context.node_stack().Push(node_id, field_id);
        break;
      }

      SemIR::InstId value_id = SemIR::InstId::Invalid;
      if (context_node_kind == Parse::NodeKind::ReturnedModifier) {
        // TODO: Should we check this for the `var` as a whole, rather than for
        // the name binding?
        value_id =
            CheckReturnedVar(context, context.node_stack().PeekNodeId(),
                             name_node, name_id, type_node, cast_type_id);
      } else {
        value_id = context.AddInst<SemIR::VarStorage>(
            name_node, {.type_id = cast_type_id, .name_id = name_id});
      }
      auto bind_id = context.AddInst(make_bind_name(cast_type_id, value_id));
      push_bind_name(bind_id);

      if (context_node_kind == Parse::NodeKind::ReturnedModifier) {
        RegisterReturnedVar(context, bind_id);
      }
      break;

Review comment (suggestion):
Please add an `ElementIndex::Invalid` for use here, for consistency with our other invalid index values.
```suggestion
                         .index = SemIR::ElementIndex::Invalid});
```

Code after:
          [&] {
            CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error,
                              "{0:field|variable} has incomplete type {1}",
                              BoolAsSelect, SemIR::TypeId);
            return context.emitter().Build(type_node, IncompleteTypeInVarDecl,
                                           parent_class_decl.has_value(),
                                           cast_type_id);
          },
          [&] {
            CARBON_DIAGNOSTIC(AbstractTypeInVarDecl, Error,
                              "{0:field|variable} has abstract type {1}",
                              BoolAsSelect, SemIR::TypeId);
            return context.emitter().Build(type_node, AbstractTypeInVarDecl,
                                           parent_class_decl.has_value(),
                                           cast_type_id);
          });
      if (parent_class_decl) {
        CARBON_CHECK(context_node_kind == Parse::NodeKind::VariableIntroducer,
                     "`returned var` at class scope");
        auto& class_info = context.classes().Get(parent_class_decl->class_id);
        auto field_type_id = context.GetUnboundElementType(
            class_info.self_type_id, cast_type_id);
        auto field_id = context.AddInst<SemIR::FieldDecl>(
            binding_id, {.type_id = field_type_id,
                         .name_id = name_id,
                         .index = SemIR::ElementIndex::Invalid});
        context.field_decls_stack().AppendToTop(field_id);

        context.node_stack().Push(node_id, field_id);
        break;
      }

      SemIR::InstId value_id = SemIR::InstId::Invalid;
      if (context_node_kind == Parse::NodeKind::ReturnedModifier) {
        // TODO: Should we check this for the `var` as a whole, rather than for
        // the name binding?
        value_id =
            CheckReturnedVar(context, context.node_stack().PeekNodeId(),
                             name_node, name_id, type_node, cast_type_id);
      } else {
        value_id = context.AddInst<SemIR::VarStorage>(
            name_node, {.type_id = cast_type_id, .name_id = name_id});
      }
      auto bind_id = context.AddInst(make_bind_name(cast_type_id, value_id));
      push_bind_name(bind_id);

      if (context_node_kind == Parse::NodeKind::ReturnedModifier) {
        RegisterReturnedVar(context, bind_id);
      }
      break;
    }