Просмотр исходного кода

Basic support for associated constant declarations (#3737)

Richard Smith 2 лет назад
Родитель
Сommit
8e956baca8
26 измененных файлов с 775 добавлено и 79 удалено
  1. 1 0
      toolchain/check/context.cpp
  2. 1 0
      toolchain/check/eval.cpp
  3. 72 7
      toolchain/check/handle_let.cpp
  4. 1 0
      toolchain/check/node_stack.h
  5. 40 0
      toolchain/check/testdata/interface/assoc_const.carbon
  6. 39 0
      toolchain/check/testdata/interface/fail_assoc_const_bad_default.carbon
  7. 26 0
      toolchain/check/testdata/interface/fail_assoc_const_not_binding.carbon
  8. 46 0
      toolchain/check/testdata/interface/fail_assoc_const_not_constant.carbon
  9. 26 0
      toolchain/check/testdata/interface/fail_assoc_const_template.carbon
  10. 24 6
      toolchain/check/testdata/interface/fail_member_lookup.carbon
  11. 52 0
      toolchain/check/testdata/interface/fail_todo_assoc_const_default.carbon
  12. 242 0
      toolchain/check/testdata/interface/fail_todo_import.carbon
  13. 101 38
      toolchain/check/testdata/interface/import.carbon
  14. 34 0
      toolchain/check/testdata/let/fail_missing_value.carbon
  15. 4 1
      toolchain/diagnostics/diagnostic_kind.def
  16. 6 0
      toolchain/lower/handle.cpp
  17. 0 8
      toolchain/parse/handle_let.cpp
  18. 7 3
      toolchain/parse/node_kind.def
  19. 5 1
      toolchain/parse/state.def
  20. 1 1
      toolchain/parse/testdata/let/fail_empty.carbon
  21. 16 9
      toolchain/parse/testdata/let/missing_value.carbon
  22. 6 2
      toolchain/parse/typed_nodes.h
  23. 3 0
      toolchain/sem_ir/file.cpp
  24. 12 3
      toolchain/sem_ir/formatter.cpp
  25. 1 0
      toolchain/sem_ir/inst_kind.def
  26. 9 0
      toolchain/sem_ir/typed_insts.h

+ 1 - 0
toolchain/check/context.cpp

@@ -819,6 +819,7 @@ class TypeCompleter {
       case SemIR::ArrayIndex::Kind:
       case SemIR::ArrayInit::Kind:
       case SemIR::Assign::Kind:
+      case SemIR::AssociatedConstantDecl::Kind:
       case SemIR::AssociatedEntity::Kind:
       case SemIR::BaseDecl::Kind:
       case SemIR::BindAlias::Kind:

+ 1 - 0
toolchain/check/eval.cpp

@@ -396,6 +396,7 @@ auto TryEvalInst(Context& context, SemIR::InstId inst_id, SemIR::Inst inst)
     // corresponding constant value.
     // TODO: This doesn't properly handle redeclarations. Consider adding a
     // corresponding `Value` inst for each of these cases.
+    case SemIR::AssociatedConstantDecl::Kind:
     case SemIR::BaseDecl::Kind:
     case SemIR::FieldDecl::Kind:
     case SemIR::FunctionDecl::Kind:

+ 72 - 7
toolchain/check/handle_let.cpp

@@ -4,8 +4,11 @@
 
 #include "toolchain/check/context.h"
 #include "toolchain/check/convert.h"
+#include "toolchain/check/interface.h"
 #include "toolchain/check/modifiers.h"
+#include "toolchain/diagnostics/diagnostic_emitter.h"
 #include "toolchain/sem_ir/inst.h"
+#include "toolchain/sem_ir/typed_insts.h"
 
 namespace Carbon::Check {
 
@@ -17,13 +20,54 @@ auto HandleLetIntroducer(Context& context, Parse::LetIntroducerId parse_node)
   return true;
 }
 
-auto HandleLetInitializer(Context& /*context*/,
-                          Parse::LetInitializerId /*parse_node*/) -> bool {
+auto HandleLetInitializer(Context& context, Parse::LetInitializerId parse_node)
+    -> bool {
+  context.node_stack().Push(parse_node);
   return true;
 }
 
+static auto BuildAssociatedConstantDecl(
+    Context& context, Parse::LetDeclId parse_node, SemIR::InstId pattern_id,
+    SemIR::ParseNodeAndInst pattern, SemIR::InterfaceId interface_id) -> void {
+  auto& interface_info = context.interfaces().Get(interface_id);
+
+  auto binding_pattern = pattern.inst.TryAs<SemIR::BindSymbolicName>();
+  if (!binding_pattern) {
+    CARBON_DIAGNOSTIC(ExpectedSymbolicBindingInAssociatedConstant, Error,
+                      "Pattern in associated constant declaration must be a "
+                      "single `:!` binding.");
+    context.emitter().Emit(pattern.parse_node,
+                           ExpectedSymbolicBindingInAssociatedConstant);
+    context.name_scopes().Get(interface_info.scope_id).has_error = true;
+    return;
+  }
+
+  // Replace the tentative BindName instruction with the associated constant
+  // declaration.
+  auto name_id =
+      context.bind_names().Get(binding_pattern->bind_name_id).name_id;
+  context.ReplaceInstBeforeConstantUse(
+      pattern_id, {parse_node, SemIR::AssociatedConstantDecl{
+                                   binding_pattern->type_id, name_id}});
+  auto decl_id = pattern_id;
+  context.inst_block_stack().AddInstId(decl_id);
+
+  // Add an associated entity name to the interface scope.
+  auto assoc_id = BuildAssociatedEntity(context, interface_id, decl_id);
+  auto name_context = context.decl_name_stack().MakeUnqualifiedName(
+      pattern.parse_node, name_id);
+  context.decl_name_stack().AddNameToLookup(name_context, assoc_id);
+}
+
 auto HandleLetDecl(Context& context, Parse::LetDeclId parse_node) -> bool {
-  auto value_id = context.node_stack().PopExpr();
+  // Pop the optional initializer.
+  std::optional<SemIR::InstId> value_id;
+  if (context.node_stack().PeekNextIs<Parse::NodeKind::LetInitializer>()) {
+    value_id = context.node_stack().PopExpr();
+    context.node_stack()
+        .PopAndDiscardSoloParseNode<Parse::NodeKind::LetInitializer>();
+  }
+
   if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
     return context.TODO(parse_node, "tuple pattern in let");
   }
@@ -52,10 +96,31 @@ auto HandleLetDecl(Context& context, Parse::LetDeclId parse_node) -> bool {
   }
   context.decl_state_stack().Pop(DeclState::Let);
 
-  // Convert the value to match the type of the pattern.
   auto pattern = context.insts().GetWithParseNode(pattern_id);
-  value_id = ConvertToValueOfType(context, parse_node, value_id,
-                                  pattern.inst.type_id());
+  auto interface_scope = context.GetCurrentScopeAs<SemIR::InterfaceDecl>();
+
+  if (value_id) {
+    // Convert the value to match the type of the pattern.
+    value_id = ConvertToValueOfType(context, parse_node, *value_id,
+                                    pattern.inst.type_id());
+  }
+
+  // At interface scope, we are forming an associated constant, which has
+  // different rules.
+  if (interface_scope) {
+    BuildAssociatedConstantDecl(context, parse_node, pattern_id, pattern,
+                                interface_scope->interface_id);
+    return true;
+  }
+
+  if (!value_id) {
+    CARBON_DIAGNOSTIC(
+        ExpectedInitializerAfterLet, Error,
+        "Expected `=`; `let` declaration must have an initializer.");
+    context.emitter().Emit(Parse::TokenOnly(parse_node),
+                           ExpectedInitializerAfterLet);
+    value_id = SemIR::InstId::BuiltinError;
+  }
 
   // Update the binding with its value and add it to the current block, after
   // the computation of the value.
@@ -63,7 +128,7 @@ auto HandleLetDecl(Context& context, Parse::LetDeclId parse_node) -> bool {
   auto bind_name = pattern.inst.As<SemIR::AnyBindName>();
   CARBON_CHECK(!bind_name.value_id.is_valid())
       << "Binding should not already have a value!";
-  bind_name.value_id = value_id;
+  bind_name.value_id = *value_id;
   pattern.inst = bind_name;
   context.ReplaceInstBeforeConstantUse(pattern_id, pattern);
   context.inst_block_stack().AddInstId(pattern_id);

+ 1 - 0
toolchain/check/node_stack.h

@@ -474,6 +474,7 @@ class NodeStack {
         case Parse::NodeKind::ImplicitParamListStart:
         case Parse::NodeKind::ImplIntroducer:
         case Parse::NodeKind::InterfaceIntroducer:
+        case Parse::NodeKind::LetInitializer:
         case Parse::NodeKind::LetIntroducer:
         case Parse::NodeKind::QualifiedName:
         case Parse::NodeKind::ReturnedModifier:

+ 40 - 0
toolchain/check/testdata/interface/assoc_const.carbon

@@ -0,0 +1,40 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// AUTOUPDATE
+
+interface I {
+  let T:! type;
+  let N:! i32;
+}
+
+// CHECK:STDOUT: --- assoc_const.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %.1: type = interface_type @I [template]
+// CHECK:STDOUT:   %.2: type = assoc_entity_type @I, type [template]
+// CHECK:STDOUT:   %.3: <associated type in I> = assoc_entity element0, @I.%T [template]
+// CHECK:STDOUT:   %.4: type = assoc_entity_type @I, i32 [template]
+// CHECK:STDOUT:   %.5: <associated i32 in I> = assoc_entity element1, @I.%N [template]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   package: <namespace> = namespace [template] {
+// CHECK:STDOUT:     .I = %I.decl
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %I.decl = interface_decl @I [template = constants.%.1] {}
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @I {
+// CHECK:STDOUT:   %T: type = assoc_const_decl T [template]
+// CHECK:STDOUT:   %.loc8: <associated type in I> = assoc_entity element0, %T [template = constants.%.3]
+// CHECK:STDOUT:   %N: i32 = assoc_const_decl N [template]
+// CHECK:STDOUT:   %.loc9: <associated i32 in I> = assoc_entity element1, %N [template = constants.%.5]
+// CHECK:STDOUT:
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   .T = %.loc8
+// CHECK:STDOUT:   .N = %.loc9
+// CHECK:STDOUT:   witness = (%T, %N)
+// CHECK:STDOUT: }
+// CHECK:STDOUT:

+ 39 - 0
toolchain/check/testdata/interface/fail_assoc_const_bad_default.carbon

@@ -0,0 +1,39 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// AUTOUPDATE
+
+interface I {
+  // CHECK:STDERR: fail_assoc_const_bad_default.carbon:[[@LINE+3]]:3: ERROR: Cannot implicitly convert from `i32` to `type`.
+  // CHECK:STDERR:   let T:! type = 42;
+  // CHECK:STDERR:   ^~~~~~~~~~~~~~~~~~
+  let T:! type = 42;
+}
+
+// CHECK:STDOUT: --- fail_assoc_const_bad_default.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %.1: type = interface_type @I [template]
+// CHECK:STDOUT:   %.2: i32 = int_literal 42 [template]
+// CHECK:STDOUT:   %.3: type = assoc_entity_type @I, type [template]
+// CHECK:STDOUT:   %.4: <associated type in I> = assoc_entity element0, @I.%T [template]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   package: <namespace> = namespace [template] {
+// CHECK:STDOUT:     .I = %I.decl
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %I.decl = interface_decl @I [template = constants.%.1] {}
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @I {
+// CHECK:STDOUT:   %.loc11_18: i32 = int_literal 42 [template = constants.%.2]
+// CHECK:STDOUT:   %T: type = assoc_const_decl T [template]
+// CHECK:STDOUT:   %.loc11_20: <associated type in I> = assoc_entity element0, %T [template = constants.%.4]
+// CHECK:STDOUT:
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   .T = %.loc11_20
+// CHECK:STDOUT:   witness = (%T)
+// CHECK:STDOUT: }
+// CHECK:STDOUT:

+ 26 - 0
toolchain/check/testdata/interface/fail_assoc_const_not_binding.carbon

@@ -0,0 +1,26 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// AUTOUPDATE
+
+interface I {
+  // CHECK:STDERR: fail_assoc_const_not_binding.carbon:[[@LINE+3]]:3: ERROR: Semantics TODO: `tuple pattern in let`.
+  // CHECK:STDERR:   let (T:! type, U:! type);
+  // CHECK:STDERR:   ^~~~~~~~~~~~~~~~~~~~~~~~~
+  let (T:! type, U:! type);
+}
+
+// CHECK:STDOUT: --- fail_assoc_const_not_binding.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %.1: type = interface_type @I [template]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {}
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @I {
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   witness = invalid
+// CHECK:STDOUT: }
+// CHECK:STDOUT:

+ 46 - 0
toolchain/check/testdata/interface/fail_assoc_const_not_constant.carbon

@@ -0,0 +1,46 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// AUTOUPDATE
+
+interface I {
+  // CHECK:STDERR: fail_assoc_const_not_constant.carbon:[[@LINE+3]]:7: ERROR: Pattern in associated constant declaration must be a single `:!` binding.
+  // CHECK:STDERR:   let a: i32;
+  // CHECK:STDERR:       ^
+  let a: i32;
+}
+
+// We shouldn't issue further errors on uses of the invalid name.
+alias UseA = I.a;
+
+// Ideally we would still diagnose this, but it's OK that we don't.
+alias UseOther = I.other;
+
+// CHECK:STDOUT: --- fail_assoc_const_not_constant.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %.1: type = interface_type @I [template]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   package: <namespace> = namespace [template] {
+// CHECK:STDOUT:     .I = %I.decl
+// CHECK:STDOUT:     .UseA = %UseA
+// CHECK:STDOUT:     .UseOther = %UseOther
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %I.decl = interface_decl @I [template = constants.%.1] {}
+// CHECK:STDOUT:   %I.ref.loc15: type = name_ref I, %I.decl [template = constants.%.1]
+// CHECK:STDOUT:   %a.ref: <error> = name_ref a, <error> [template = <error>]
+// CHECK:STDOUT:   %UseA: <error> = bind_alias UseA, <error> [template = <error>]
+// CHECK:STDOUT:   %I.ref.loc18: type = name_ref I, %I.decl [template = constants.%.1]
+// CHECK:STDOUT:   %other.ref: <error> = name_ref other, <error> [template = <error>]
+// CHECK:STDOUT:   %UseOther: <error> = bind_alias UseOther, <error> [template = <error>]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @I {
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   has_error
+// CHECK:STDOUT:   witness = ()
+// CHECK:STDOUT: }
+// CHECK:STDOUT:

+ 26 - 0
toolchain/check/testdata/interface/fail_assoc_const_template.carbon

@@ -0,0 +1,26 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// AUTOUPDATE
+
+interface I {
+  // CHECK:STDERR: fail_assoc_const_template.carbon:[[@LINE+3]]:7: ERROR: Semantics TODO: `HandleTemplate`.
+  // CHECK:STDERR:   let template T:! type;
+  // CHECK:STDERR:       ^~~~~~~~~~~~~~~~~
+  let template T:! type;
+}
+
+// CHECK:STDOUT: --- fail_assoc_const_template.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %.1: type = interface_type @I [template]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {}
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @I {
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   witness = invalid
+// CHECK:STDOUT: }
+// CHECK:STDOUT:

+ 24 - 6
toolchain/check/testdata/interface/fail_member_lookup.carbon

@@ -4,13 +4,22 @@
 //
 // AUTOUPDATE
 
-interface Interface { fn F(); }
+interface Interface {
+  fn F();
+
+  let T:! type;
+}
 
 fn F() {
   // CHECK:STDERR: fail_member_lookup.carbon:[[@LINE+3]]:3: ERROR: Value of type `<associated <function> in Interface>` is not callable.
   // CHECK:STDERR:   Interface.F();
   // CHECK:STDERR:   ^~~~~~~~~~~~
   Interface.F();
+
+  // CHECK:STDERR: fail_member_lookup.carbon:[[@LINE+3]]:10: ERROR: Cannot implicitly convert from `<associated type in Interface>` to `type`.
+  // CHECK:STDERR:   var v: Interface.T;
+  // CHECK:STDERR:          ^~~~~~~~~~~
+  var v: Interface.T;
 }
 
 // CHECK:STDOUT: --- fail_member_lookup.carbon
@@ -19,6 +28,8 @@ fn F() {
 // CHECK:STDOUT:   %.1: type = interface_type @Interface [template]
 // CHECK:STDOUT:   %.2: type = assoc_entity_type @Interface, <function> [template]
 // CHECK:STDOUT:   %.3: <associated <function> in Interface> = assoc_entity element0, @Interface.%F [template]
+// CHECK:STDOUT:   %.4: type = assoc_entity_type @Interface, type [template]
+// CHECK:STDOUT:   %.5: <associated type in Interface> = assoc_entity element1, @Interface.%T [template]
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: file {
@@ -32,19 +43,26 @@ fn F() {
 // CHECK:STDOUT:
 // CHECK:STDOUT: interface @Interface {
 // CHECK:STDOUT:   %F: <function> = fn_decl @F.1 [template] {}
-// CHECK:STDOUT:   %.loc7: <associated <function> in Interface> = assoc_entity element0, %F [template = constants.%.3]
+// CHECK:STDOUT:   %.loc8: <associated <function> in Interface> = assoc_entity element0, %F [template = constants.%.3]
+// CHECK:STDOUT:   %T: type = assoc_const_decl T [template]
+// CHECK:STDOUT:   %.loc10: <associated type in Interface> = assoc_entity element1, %T [template = constants.%.5]
 // CHECK:STDOUT:
 // CHECK:STDOUT: !members:
-// CHECK:STDOUT:   .F = %.loc7
-// CHECK:STDOUT:   witness = (%F)
+// CHECK:STDOUT:   .F = %.loc8
+// CHECK:STDOUT:   .T = %.loc10
+// CHECK:STDOUT:   witness = (%F, %T)
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: fn @F.1();
 // CHECK:STDOUT:
 // CHECK:STDOUT: fn @F.2() {
 // CHECK:STDOUT: !entry:
-// CHECK:STDOUT:   %Interface.ref: type = name_ref Interface, file.%Interface.decl [template = constants.%.1]
-// CHECK:STDOUT:   %F.ref: <associated <function> in Interface> = name_ref F, @Interface.%.loc7 [template = constants.%.3]
+// CHECK:STDOUT:   %Interface.ref.loc17: type = name_ref Interface, file.%Interface.decl [template = constants.%.1]
+// CHECK:STDOUT:   %F.ref: <associated <function> in Interface> = name_ref F, @Interface.%.loc8 [template = constants.%.3]
+// CHECK:STDOUT:   %Interface.ref.loc22: type = name_ref Interface, file.%Interface.decl [template = constants.%.1]
+// CHECK:STDOUT:   %T.ref: <associated type in Interface> = name_ref T, @Interface.%.loc10 [template = constants.%.5]
+// CHECK:STDOUT:   %v.var: ref <error> = var v
+// CHECK:STDOUT:   %v: ref <error> = bind_name v, %v.var
 // CHECK:STDOUT:   return
 // CHECK:STDOUT: }
 // CHECK:STDOUT:

+ 52 - 0
toolchain/check/testdata/interface/fail_todo_assoc_const_default.carbon

@@ -0,0 +1,52 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// AUTOUPDATE
+
+interface I {
+  // CHECK:STDERR: fail_todo_assoc_const_default.carbon:[[@LINE+3]]:3: ERROR: Semantics TODO: `interface modifier`.
+  // CHECK:STDERR:   default let T:! type = (i32, i32);
+  // CHECK:STDERR:   ^~~~~~~
+  default let T:! type = (i32, i32);
+  // CHECK:STDERR: fail_todo_assoc_const_default.carbon:[[@LINE+3]]:3: ERROR: Semantics TODO: `interface modifier`.
+  // CHECK:STDERR:   default let N:! i32 = 42;
+  // CHECK:STDERR:   ^~~~~~~
+  default let N:! i32 = 42;
+}
+
+// CHECK:STDOUT: --- fail_todo_assoc_const_default.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %.1: type = interface_type @I [template]
+// CHECK:STDOUT:   %.2: type = tuple_type (type, type) [template]
+// CHECK:STDOUT:   %.3: type = tuple_type (i32, i32) [template]
+// CHECK:STDOUT:   %.4: type = assoc_entity_type @I, type [template]
+// CHECK:STDOUT:   %.5: <associated type in I> = assoc_entity element0, @I.%T [template]
+// CHECK:STDOUT:   %.6: i32 = int_literal 42 [template]
+// CHECK:STDOUT:   %.7: type = assoc_entity_type @I, i32 [template]
+// CHECK:STDOUT:   %.8: <associated i32 in I> = assoc_entity element1, @I.%N [template]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   package: <namespace> = namespace [template] {
+// CHECK:STDOUT:     .I = %I.decl
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %I.decl = interface_decl @I [template = constants.%.1] {}
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @I {
+// CHECK:STDOUT:   %.loc11_35.1: (type, type) = tuple_literal (i32, i32)
+// CHECK:STDOUT:   %.loc11_35.2: type = converted %.loc11_35.1, constants.%.3 [template = constants.%.3]
+// CHECK:STDOUT:   %T: type = assoc_const_decl T [template]
+// CHECK:STDOUT:   %.loc11_36: <associated type in I> = assoc_entity element0, %T [template = constants.%.5]
+// CHECK:STDOUT:   %.loc15_25: i32 = int_literal 42 [template = constants.%.6]
+// CHECK:STDOUT:   %N: i32 = assoc_const_decl N [template]
+// CHECK:STDOUT:   %.loc15_27: <associated i32 in I> = assoc_entity element1, %N [template = constants.%.8]
+// CHECK:STDOUT:
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   .T = %.loc11_36
+// CHECK:STDOUT:   .N = %.loc15_27
+// CHECK:STDOUT:   witness = (%T, %N)
+// CHECK:STDOUT: }
+// CHECK:STDOUT:

+ 242 - 0
toolchain/check/testdata/interface/fail_todo_import.carbon

@@ -0,0 +1,242 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// AUTOUPDATE
+// CHECK:STDERR: b.carbon: ERROR: Semantics TODO: `TryResolveInst on AssociatedEntityType`.
+// CHECK:STDERR: b.carbon: ERROR: Semantics TODO: `TryResolveInst on AssociatedEntity`.
+// CHECK:STDERR: b.carbon: ERROR: Semantics TODO: `TryResolveInst on AssociatedEntityType`.
+// CHECK:STDERR: b.carbon: ERROR: Semantics TODO: `TryResolveInst on AssociatedEntity`.
+// CHECK:STDERR: b.carbon: ERROR: Semantics TODO: `TryResolveInst on AssociatedEntityType`.
+// CHECK:STDERR: b.carbon: ERROR: Semantics TODO: `TryResolveInst on AssociatedEntity`.
+// CHECK:STDERR: b.carbon: ERROR: Semantics TODO: `TryResolveInst on AssociatedEntityType`.
+// CHECK:STDERR: b.carbon: ERROR: Semantics TODO: `TryResolveInst on AssociatedEntity`.
+
+// --- a.carbon
+
+library "a" api;
+
+interface Empty {
+}
+
+interface Basic {
+  let T:! type;
+  fn F();
+}
+
+interface ForwardDeclared;
+
+interface ForwardDeclared {
+  let T:! type;
+  fn F();
+}
+
+var f_ref: {.f: ForwardDeclared};
+
+// --- b.carbon
+
+library "b" api;
+
+import library "a";
+
+fn UseEmpty(e: Empty) {}
+fn UseBasic(e: Basic) {}
+fn UseForwardDeclared(f: ForwardDeclared) {}
+
+alias UseBasicT = Basic.T;
+alias UseBasicF = Basic.F;
+
+alias UseForwardDeclaredT = ForwardDeclared.T;
+alias UseForwardDeclaredF = ForwardDeclared.F;
+
+var f: ForwardDeclared* = &f_ref.f;
+
+// CHECK:STDOUT: --- a.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %.1: type = interface_type @Empty [template]
+// CHECK:STDOUT:   %.2: type = interface_type @Basic [template]
+// CHECK:STDOUT:   %.3: type = assoc_entity_type @Basic, type [template]
+// CHECK:STDOUT:   %.4: <associated type in Basic> = assoc_entity element0, @Basic.%T [template]
+// CHECK:STDOUT:   %.5: type = assoc_entity_type @Basic, <function> [template]
+// CHECK:STDOUT:   %.6: <associated <function> in Basic> = assoc_entity element1, @Basic.%F [template]
+// CHECK:STDOUT:   %.7: type = interface_type @ForwardDeclared [template]
+// CHECK:STDOUT:   %.8: type = assoc_entity_type @ForwardDeclared, type [template]
+// CHECK:STDOUT:   %.9: <associated type in ForwardDeclared> = assoc_entity element0, @ForwardDeclared.%T [template]
+// CHECK:STDOUT:   %.10: type = assoc_entity_type @ForwardDeclared, <function> [template]
+// CHECK:STDOUT:   %.11: <associated <function> in ForwardDeclared> = assoc_entity element1, @ForwardDeclared.%F [template]
+// CHECK:STDOUT:   %.12: type = struct_type {.f: ForwardDeclared} [template]
+// CHECK:STDOUT:   %.13: type = tuple_type () [template]
+// CHECK:STDOUT:   %.14: type = struct_type {.f: ()} [template]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   package: <namespace> = namespace [template] {
+// CHECK:STDOUT:     .Empty = %Empty.decl
+// CHECK:STDOUT:     .Basic = %Basic.decl
+// CHECK:STDOUT:     .ForwardDeclared = %ForwardDeclared.decl.loc12
+// CHECK:STDOUT:     .f_ref = %f_ref
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %Empty.decl = interface_decl @Empty [template = constants.%.1] {}
+// CHECK:STDOUT:   %Basic.decl = interface_decl @Basic [template = constants.%.2] {}
+// CHECK:STDOUT:   %ForwardDeclared.decl.loc12 = interface_decl @ForwardDeclared [template = constants.%.7] {}
+// CHECK:STDOUT:   %ForwardDeclared.decl.loc14 = interface_decl @ForwardDeclared [template = constants.%.7] {}
+// CHECK:STDOUT:   %ForwardDeclared.ref: type = name_ref ForwardDeclared, %ForwardDeclared.decl.loc12 [template = constants.%.7]
+// CHECK:STDOUT:   %.loc19: type = struct_type {.f: ForwardDeclared} [template = constants.%.12]
+// CHECK:STDOUT:   %f_ref.var: ref {.f: ForwardDeclared} = var f_ref
+// CHECK:STDOUT:   %f_ref: ref {.f: ForwardDeclared} = bind_name f_ref, %f_ref.var
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @Empty {
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   witness = ()
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @Basic {
+// CHECK:STDOUT:   %T: type = assoc_const_decl T [template]
+// CHECK:STDOUT:   %.loc8: <associated type in Basic> = assoc_entity element0, %T [template = constants.%.4]
+// CHECK:STDOUT:   %F: <function> = fn_decl @F.1 [template] {}
+// CHECK:STDOUT:   %.loc9: <associated <function> in Basic> = assoc_entity element1, %F [template = constants.%.6]
+// CHECK:STDOUT:
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   .T = %.loc8
+// CHECK:STDOUT:   .F = %.loc9
+// CHECK:STDOUT:   witness = (%T, %F)
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @ForwardDeclared {
+// CHECK:STDOUT:   %T: type = assoc_const_decl T [template]
+// CHECK:STDOUT:   %.loc15: <associated type in ForwardDeclared> = assoc_entity element0, %T [template = constants.%.9]
+// CHECK:STDOUT:   %F: <function> = fn_decl @F.2 [template] {}
+// CHECK:STDOUT:   %.loc16: <associated <function> in ForwardDeclared> = assoc_entity element1, %F [template = constants.%.11]
+// CHECK:STDOUT:
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   .T = %.loc15
+// CHECK:STDOUT:   .F = %.loc16
+// CHECK:STDOUT:   witness = (%T, %F)
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @F.1();
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @F.2();
+// CHECK:STDOUT:
+// CHECK:STDOUT: --- b.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: constants {
+// CHECK:STDOUT:   %.1: type = interface_type @Empty [template]
+// CHECK:STDOUT:   %.2: type = tuple_type () [template]
+// CHECK:STDOUT:   %.3: type = interface_type @Basic [template]
+// CHECK:STDOUT:   %.4: type = interface_type @ForwardDeclared [template]
+// CHECK:STDOUT:   %.5: type = ptr_type ForwardDeclared [template]
+// CHECK:STDOUT:   %.6: type = struct_type {.f: ForwardDeclared} [template]
+// CHECK:STDOUT:   %.7: type = struct_type {.f: ()} [template]
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   package: <namespace> = namespace [template] {
+// CHECK:STDOUT:     .Empty = %import_ref.1
+// CHECK:STDOUT:     .Basic = %import_ref.2
+// CHECK:STDOUT:     .ForwardDeclared = %import_ref.3
+// CHECK:STDOUT:     .f_ref = %import_ref.4
+// CHECK:STDOUT:     .UseEmpty = %UseEmpty
+// CHECK:STDOUT:     .UseBasic = %UseBasic
+// CHECK:STDOUT:     .UseForwardDeclared = %UseForwardDeclared
+// CHECK:STDOUT:     .UseBasicT = %UseBasicT
+// CHECK:STDOUT:     .UseBasicF = %UseBasicF
+// CHECK:STDOUT:     .UseForwardDeclaredT = %UseForwardDeclaredT
+// CHECK:STDOUT:     .UseForwardDeclaredF = %UseForwardDeclaredF
+// CHECK:STDOUT:     .f = %f.loc16
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %import_ref.1: type = import_ref ir1, inst+1, used [template = constants.%.1]
+// CHECK:STDOUT:   %import_ref.2: type = import_ref ir1, inst+3, used [template = constants.%.3]
+// CHECK:STDOUT:   %import_ref.3: type = import_ref ir1, inst+13, used [template = constants.%.4]
+// CHECK:STDOUT:   %import_ref.4: ref {.f: ForwardDeclared} = import_ref ir1, inst+33, used
+// CHECK:STDOUT:   %UseEmpty: <function> = fn_decl @UseEmpty [template] {
+// CHECK:STDOUT:     %Empty.decl = interface_decl @Empty [template = constants.%.1] {}
+// CHECK:STDOUT:     %Empty.ref: type = name_ref Empty, %import_ref.1 [template = constants.%.1]
+// CHECK:STDOUT:     %e.loc6_13.1: Empty = param e
+// CHECK:STDOUT:     @UseEmpty.%e: Empty = bind_name e, %e.loc6_13.1
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %UseBasic: <function> = fn_decl @UseBasic [template] {
+// CHECK:STDOUT:     %Basic.decl = interface_decl @Basic [template = constants.%.3] {}
+// CHECK:STDOUT:     %Basic.ref.loc7: type = name_ref Basic, %import_ref.2 [template = constants.%.3]
+// CHECK:STDOUT:     %e.loc7_13.1: Basic = param e
+// CHECK:STDOUT:     @UseBasic.%e: Basic = bind_name e, %e.loc7_13.1
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %UseForwardDeclared: <function> = fn_decl @UseForwardDeclared [template] {
+// CHECK:STDOUT:     %ForwardDeclared.decl = interface_decl @ForwardDeclared [template = constants.%.4] {}
+// CHECK:STDOUT:     %ForwardDeclared.ref.loc8: type = name_ref ForwardDeclared, %import_ref.3 [template = constants.%.4]
+// CHECK:STDOUT:     %f.loc8_23.1: ForwardDeclared = param f
+// CHECK:STDOUT:     @UseForwardDeclared.%f: ForwardDeclared = bind_name f, %f.loc8_23.1
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %Basic.ref.loc10: type = name_ref Basic, %import_ref.2 [template = constants.%.3]
+// CHECK:STDOUT:   %T.ref.loc10: <error> = name_ref T, @Basic.%import_ref.2 [template = <error>]
+// CHECK:STDOUT:   %UseBasicT: <error> = bind_alias UseBasicT, @Basic.%import_ref.2 [template = <error>]
+// CHECK:STDOUT:   %Basic.ref.loc11: type = name_ref Basic, %import_ref.2 [template = constants.%.3]
+// CHECK:STDOUT:   %F.ref.loc11: <error> = name_ref F, @Basic.%import_ref.1 [template = <error>]
+// CHECK:STDOUT:   %UseBasicF: <error> = bind_alias UseBasicF, @Basic.%import_ref.1 [template = <error>]
+// CHECK:STDOUT:   %ForwardDeclared.ref.loc13: type = name_ref ForwardDeclared, %import_ref.3 [template = constants.%.4]
+// CHECK:STDOUT:   %T.ref.loc13: <error> = name_ref T, @ForwardDeclared.%import_ref.2 [template = <error>]
+// CHECK:STDOUT:   %UseForwardDeclaredT: <error> = bind_alias UseForwardDeclaredT, @ForwardDeclared.%import_ref.2 [template = <error>]
+// CHECK:STDOUT:   %ForwardDeclared.ref.loc14: type = name_ref ForwardDeclared, %import_ref.3 [template = constants.%.4]
+// CHECK:STDOUT:   %F.ref.loc14: <error> = name_ref F, @ForwardDeclared.%import_ref.1 [template = <error>]
+// CHECK:STDOUT:   %UseForwardDeclaredF: <error> = bind_alias UseForwardDeclaredF, @ForwardDeclared.%import_ref.1 [template = <error>]
+// CHECK:STDOUT:   %ForwardDeclared.ref.loc16: type = name_ref ForwardDeclared, %import_ref.3 [template = constants.%.4]
+// CHECK:STDOUT:   %.loc16: type = ptr_type ForwardDeclared [template = constants.%.5]
+// CHECK:STDOUT:   %f.var: ref ForwardDeclared* = var f
+// CHECK:STDOUT:   %f.loc16: ref ForwardDeclared* = bind_name f, %f.var
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @Empty {
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   witness = ()
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @Basic {
+// CHECK:STDOUT:   %import_ref.1: <error> = import_ref ir1, inst+11, used [template = <error>]
+// CHECK:STDOUT:   %import_ref.2: <error> = import_ref ir1, inst+7, used [template = <error>]
+// CHECK:STDOUT:   %import_ref.3 = import_ref ir1, inst+5, unused
+// CHECK:STDOUT:   %import_ref.4 = import_ref ir1, inst+9, unused
+// CHECK:STDOUT:
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   .F = %import_ref.1
+// CHECK:STDOUT:   .T = %import_ref.2
+// CHECK:STDOUT:   witness = (%import_ref.3, %import_ref.4)
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: interface @ForwardDeclared {
+// CHECK:STDOUT:   %import_ref.1: <error> = import_ref ir1, inst+22, used [template = <error>]
+// CHECK:STDOUT:   %import_ref.2: <error> = import_ref ir1, inst+18, used [template = <error>]
+// CHECK:STDOUT:   %import_ref.3 = import_ref ir1, inst+16, unused
+// CHECK:STDOUT:   %import_ref.4 = import_ref ir1, inst+20, unused
+// CHECK:STDOUT:
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   .F = %import_ref.1
+// CHECK:STDOUT:   .T = %import_ref.2
+// CHECK:STDOUT:   witness = (%import_ref.3, %import_ref.4)
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @UseEmpty(%e: Empty) {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   return
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @UseBasic(%e: Basic) {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   return
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @UseForwardDeclared(%f: ForwardDeclared) {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   return
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @__global_init() {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   %f_ref.ref: ref {.f: ForwardDeclared} = name_ref f_ref, file.%import_ref.4
+// CHECK:STDOUT:   %.loc16_33: ref ForwardDeclared = struct_access %f_ref.ref, element0
+// CHECK:STDOUT:   %.loc16_27: ForwardDeclared* = addr_of %.loc16_33
+// CHECK:STDOUT:   assign file.%f.var, %.loc16_27
+// CHECK:STDOUT:   return
+// CHECK:STDOUT: }
+// CHECK:STDOUT:

+ 101 - 38
toolchain/check/testdata/interface/import.carbon

@@ -11,9 +11,15 @@ library "a" api;
 interface Empty {
 }
 
+interface Basic {
+  let T:! type;
+  fn F();
+}
+
 interface ForwardDeclared;
 
 interface ForwardDeclared {
+  let T:! type;
   fn F();
 }
 
@@ -26,6 +32,7 @@ library "b" api;
 import library "a";
 
 fn UseEmpty(e: Empty) {}
+fn UseBasic(e: Basic) {}
 fn UseForwardDeclared(f: ForwardDeclared) {}
 
 var f: ForwardDeclared* = &f_ref.f;
@@ -34,25 +41,34 @@ var f: ForwardDeclared* = &f_ref.f;
 // CHECK:STDOUT:
 // CHECK:STDOUT: constants {
 // CHECK:STDOUT:   %.1: type = interface_type @Empty [template]
-// CHECK:STDOUT:   %.2: type = interface_type @ForwardDeclared [template]
-// CHECK:STDOUT:   %.3: type = assoc_entity_type @ForwardDeclared, <function> [template]
-// CHECK:STDOUT:   %.4: <associated <function> in ForwardDeclared> = assoc_entity element0, @ForwardDeclared.%F [template]
-// CHECK:STDOUT:   %.5: type = struct_type {.f: ForwardDeclared} [template]
-// CHECK:STDOUT:   %.6: type = tuple_type () [template]
-// CHECK:STDOUT:   %.7: type = struct_type {.f: ()} [template]
+// CHECK:STDOUT:   %.2: type = interface_type @Basic [template]
+// CHECK:STDOUT:   %.3: type = assoc_entity_type @Basic, type [template]
+// CHECK:STDOUT:   %.4: <associated type in Basic> = assoc_entity element0, @Basic.%T [template]
+// CHECK:STDOUT:   %.5: type = assoc_entity_type @Basic, <function> [template]
+// CHECK:STDOUT:   %.6: <associated <function> in Basic> = assoc_entity element1, @Basic.%F [template]
+// CHECK:STDOUT:   %.7: type = interface_type @ForwardDeclared [template]
+// CHECK:STDOUT:   %.8: type = assoc_entity_type @ForwardDeclared, type [template]
+// CHECK:STDOUT:   %.9: <associated type in ForwardDeclared> = assoc_entity element0, @ForwardDeclared.%T [template]
+// CHECK:STDOUT:   %.10: type = assoc_entity_type @ForwardDeclared, <function> [template]
+// CHECK:STDOUT:   %.11: <associated <function> in ForwardDeclared> = assoc_entity element1, @ForwardDeclared.%F [template]
+// CHECK:STDOUT:   %.12: type = struct_type {.f: ForwardDeclared} [template]
+// CHECK:STDOUT:   %.13: type = tuple_type () [template]
+// CHECK:STDOUT:   %.14: type = struct_type {.f: ()} [template]
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: file {
 // CHECK:STDOUT:   package: <namespace> = namespace [template] {
 // CHECK:STDOUT:     .Empty = %Empty.decl
-// CHECK:STDOUT:     .ForwardDeclared = %ForwardDeclared.decl.loc7
+// CHECK:STDOUT:     .Basic = %Basic.decl
+// CHECK:STDOUT:     .ForwardDeclared = %ForwardDeclared.decl.loc12
 // CHECK:STDOUT:     .f_ref = %f_ref
 // CHECK:STDOUT:   }
 // CHECK:STDOUT:   %Empty.decl = interface_decl @Empty [template = constants.%.1] {}
-// CHECK:STDOUT:   %ForwardDeclared.decl.loc7 = interface_decl @ForwardDeclared [template = constants.%.2] {}
-// CHECK:STDOUT:   %ForwardDeclared.decl.loc9 = interface_decl @ForwardDeclared [template = constants.%.2] {}
-// CHECK:STDOUT:   %ForwardDeclared.ref: type = name_ref ForwardDeclared, %ForwardDeclared.decl.loc7 [template = constants.%.2]
-// CHECK:STDOUT:   %.loc13: type = struct_type {.f: ForwardDeclared} [template = constants.%.5]
+// CHECK:STDOUT:   %Basic.decl = interface_decl @Basic [template = constants.%.2] {}
+// CHECK:STDOUT:   %ForwardDeclared.decl.loc12 = interface_decl @ForwardDeclared [template = constants.%.7] {}
+// CHECK:STDOUT:   %ForwardDeclared.decl.loc14 = interface_decl @ForwardDeclared [template = constants.%.7] {}
+// CHECK:STDOUT:   %ForwardDeclared.ref: type = name_ref ForwardDeclared, %ForwardDeclared.decl.loc12 [template = constants.%.7]
+// CHECK:STDOUT:   %.loc19: type = struct_type {.f: ForwardDeclared} [template = constants.%.12]
 // CHECK:STDOUT:   %f_ref.var: ref {.f: ForwardDeclared} = var f_ref
 // CHECK:STDOUT:   %f_ref: ref {.f: ForwardDeclared} = bind_name f_ref, %f_ref.var
 // CHECK:STDOUT: }
@@ -62,56 +78,83 @@ var f: ForwardDeclared* = &f_ref.f;
 // CHECK:STDOUT:   witness = ()
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
+// CHECK:STDOUT: interface @Basic {
+// CHECK:STDOUT:   %T: type = assoc_const_decl T [template]
+// CHECK:STDOUT:   %.loc8: <associated type in Basic> = assoc_entity element0, %T [template = constants.%.4]
+// CHECK:STDOUT:   %F: <function> = fn_decl @F.1 [template] {}
+// CHECK:STDOUT:   %.loc9: <associated <function> in Basic> = assoc_entity element1, %F [template = constants.%.6]
+// CHECK:STDOUT:
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   .T = %.loc8
+// CHECK:STDOUT:   .F = %.loc9
+// CHECK:STDOUT:   witness = (%T, %F)
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
 // CHECK:STDOUT: interface @ForwardDeclared {
-// CHECK:STDOUT:   %F: <function> = fn_decl @F [template] {}
-// CHECK:STDOUT:   %.loc10: <associated <function> in ForwardDeclared> = assoc_entity element0, %F [template = constants.%.4]
+// CHECK:STDOUT:   %T: type = assoc_const_decl T [template]
+// CHECK:STDOUT:   %.loc15: <associated type in ForwardDeclared> = assoc_entity element0, %T [template = constants.%.9]
+// CHECK:STDOUT:   %F: <function> = fn_decl @F.2 [template] {}
+// CHECK:STDOUT:   %.loc16: <associated <function> in ForwardDeclared> = assoc_entity element1, %F [template = constants.%.11]
 // CHECK:STDOUT:
 // CHECK:STDOUT: !members:
-// CHECK:STDOUT:   .F = %.loc10
-// CHECK:STDOUT:   witness = (%F)
+// CHECK:STDOUT:   .T = %.loc15
+// CHECK:STDOUT:   .F = %.loc16
+// CHECK:STDOUT:   witness = (%T, %F)
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
-// CHECK:STDOUT: fn @F();
+// CHECK:STDOUT: fn @F.1();
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @F.2();
 // CHECK:STDOUT:
 // CHECK:STDOUT: --- b.carbon
 // CHECK:STDOUT:
 // CHECK:STDOUT: constants {
 // CHECK:STDOUT:   %.1: type = interface_type @Empty [template]
 // CHECK:STDOUT:   %.2: type = tuple_type () [template]
-// CHECK:STDOUT:   %.3: type = interface_type @ForwardDeclared [template]
-// CHECK:STDOUT:   %.4: type = ptr_type ForwardDeclared [template]
-// CHECK:STDOUT:   %.5: type = struct_type {.f: ForwardDeclared} [template]
-// CHECK:STDOUT:   %.6: type = struct_type {.f: ()} [template]
+// CHECK:STDOUT:   %.3: type = interface_type @Basic [template]
+// CHECK:STDOUT:   %.4: type = interface_type @ForwardDeclared [template]
+// CHECK:STDOUT:   %.5: type = ptr_type ForwardDeclared [template]
+// CHECK:STDOUT:   %.6: type = struct_type {.f: ForwardDeclared} [template]
+// CHECK:STDOUT:   %.7: type = struct_type {.f: ()} [template]
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: file {
 // CHECK:STDOUT:   package: <namespace> = namespace [template] {
 // CHECK:STDOUT:     .Empty = %import_ref.1
-// CHECK:STDOUT:     .ForwardDeclared = %import_ref.2
-// CHECK:STDOUT:     .f_ref = %import_ref.3
+// CHECK:STDOUT:     .Basic = %import_ref.2
+// CHECK:STDOUT:     .ForwardDeclared = %import_ref.3
+// CHECK:STDOUT:     .f_ref = %import_ref.4
 // CHECK:STDOUT:     .UseEmpty = %UseEmpty
+// CHECK:STDOUT:     .UseBasic = %UseBasic
 // CHECK:STDOUT:     .UseForwardDeclared = %UseForwardDeclared
-// CHECK:STDOUT:     .f = %f.loc9
+// CHECK:STDOUT:     .f = %f.loc10
 // CHECK:STDOUT:   }
 // CHECK:STDOUT:   %import_ref.1: type = import_ref ir1, inst+1, used [template = constants.%.1]
 // CHECK:STDOUT:   %import_ref.2: type = import_ref ir1, inst+3, used [template = constants.%.3]
-// CHECK:STDOUT:   %import_ref.3: ref {.f: ForwardDeclared} = import_ref ir1, inst+19, used
+// CHECK:STDOUT:   %import_ref.3: type = import_ref ir1, inst+13, used [template = constants.%.4]
+// CHECK:STDOUT:   %import_ref.4: ref {.f: ForwardDeclared} = import_ref ir1, inst+33, used
 // CHECK:STDOUT:   %UseEmpty: <function> = fn_decl @UseEmpty [template] {
 // CHECK:STDOUT:     %Empty.decl = interface_decl @Empty [template = constants.%.1] {}
 // CHECK:STDOUT:     %Empty.ref: type = name_ref Empty, %import_ref.1 [template = constants.%.1]
 // CHECK:STDOUT:     %e.loc6_13.1: Empty = param e
 // CHECK:STDOUT:     @UseEmpty.%e: Empty = bind_name e, %e.loc6_13.1
 // CHECK:STDOUT:   }
+// CHECK:STDOUT:   %UseBasic: <function> = fn_decl @UseBasic [template] {
+// CHECK:STDOUT:     %Basic.decl = interface_decl @Basic [template = constants.%.3] {}
+// CHECK:STDOUT:     %Basic.ref: type = name_ref Basic, %import_ref.2 [template = constants.%.3]
+// CHECK:STDOUT:     %e.loc7_13.1: Basic = param e
+// CHECK:STDOUT:     @UseBasic.%e: Basic = bind_name e, %e.loc7_13.1
+// CHECK:STDOUT:   }
 // CHECK:STDOUT:   %UseForwardDeclared: <function> = fn_decl @UseForwardDeclared [template] {
-// CHECK:STDOUT:     %ForwardDeclared.decl = interface_decl @ForwardDeclared [template = constants.%.3] {}
-// CHECK:STDOUT:     %ForwardDeclared.ref.loc7: type = name_ref ForwardDeclared, %import_ref.2 [template = constants.%.3]
-// CHECK:STDOUT:     %f.loc7_23.1: ForwardDeclared = param f
-// CHECK:STDOUT:     @UseForwardDeclared.%f: ForwardDeclared = bind_name f, %f.loc7_23.1
+// CHECK:STDOUT:     %ForwardDeclared.decl = interface_decl @ForwardDeclared [template = constants.%.4] {}
+// CHECK:STDOUT:     %ForwardDeclared.ref.loc8: type = name_ref ForwardDeclared, %import_ref.3 [template = constants.%.4]
+// CHECK:STDOUT:     %f.loc8_23.1: ForwardDeclared = param f
+// CHECK:STDOUT:     @UseForwardDeclared.%f: ForwardDeclared = bind_name f, %f.loc8_23.1
 // CHECK:STDOUT:   }
-// CHECK:STDOUT:   %ForwardDeclared.ref.loc9: type = name_ref ForwardDeclared, %import_ref.2 [template = constants.%.3]
-// CHECK:STDOUT:   %.loc9: type = ptr_type ForwardDeclared [template = constants.%.4]
+// CHECK:STDOUT:   %ForwardDeclared.ref.loc10: type = name_ref ForwardDeclared, %import_ref.3 [template = constants.%.4]
+// CHECK:STDOUT:   %.loc10: type = ptr_type ForwardDeclared [template = constants.%.5]
 // CHECK:STDOUT:   %f.var: ref ForwardDeclared* = var f
-// CHECK:STDOUT:   %f.loc9: ref ForwardDeclared* = bind_name f, %f.var
+// CHECK:STDOUT:   %f.loc10: ref ForwardDeclared* = bind_name f, %f.var
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: interface @Empty {
@@ -119,13 +162,28 @@ var f: ForwardDeclared* = &f_ref.f;
 // CHECK:STDOUT:   witness = ()
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
+// CHECK:STDOUT: interface @Basic {
+// CHECK:STDOUT:   %import_ref.1 = import_ref ir1, inst+11, unused
+// CHECK:STDOUT:   %import_ref.2 = import_ref ir1, inst+7, unused
+// CHECK:STDOUT:   %import_ref.3 = import_ref ir1, inst+5, unused
+// CHECK:STDOUT:   %import_ref.4 = import_ref ir1, inst+9, unused
+// CHECK:STDOUT:
+// CHECK:STDOUT: !members:
+// CHECK:STDOUT:   .F = %import_ref.1
+// CHECK:STDOUT:   .T = %import_ref.2
+// CHECK:STDOUT:   witness = (%import_ref.3, %import_ref.4)
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
 // CHECK:STDOUT: interface @ForwardDeclared {
-// CHECK:STDOUT:   %import_ref.1 = import_ref ir1, inst+8, unused
-// CHECK:STDOUT:   %import_ref.2 = import_ref ir1, inst+6, unused
+// CHECK:STDOUT:   %import_ref.1 = import_ref ir1, inst+22, unused
+// CHECK:STDOUT:   %import_ref.2 = import_ref ir1, inst+18, unused
+// CHECK:STDOUT:   %import_ref.3 = import_ref ir1, inst+16, unused
+// CHECK:STDOUT:   %import_ref.4 = import_ref ir1, inst+20, unused
 // CHECK:STDOUT:
 // CHECK:STDOUT: !members:
 // CHECK:STDOUT:   .F = %import_ref.1
-// CHECK:STDOUT:   witness = (%import_ref.2)
+// CHECK:STDOUT:   .T = %import_ref.2
+// CHECK:STDOUT:   witness = (%import_ref.3, %import_ref.4)
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: fn @UseEmpty(%e: Empty) {
@@ -133,6 +191,11 @@ var f: ForwardDeclared* = &f_ref.f;
 // CHECK:STDOUT:   return
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
+// CHECK:STDOUT: fn @UseBasic(%e: Basic) {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   return
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
 // CHECK:STDOUT: fn @UseForwardDeclared(%f: ForwardDeclared) {
 // CHECK:STDOUT: !entry:
 // CHECK:STDOUT:   return
@@ -140,10 +203,10 @@ var f: ForwardDeclared* = &f_ref.f;
 // CHECK:STDOUT:
 // CHECK:STDOUT: fn @__global_init() {
 // CHECK:STDOUT: !entry:
-// CHECK:STDOUT:   %f_ref.ref: ref {.f: ForwardDeclared} = name_ref f_ref, file.%import_ref.3
-// CHECK:STDOUT:   %.loc9_33: ref ForwardDeclared = struct_access %f_ref.ref, element0
-// CHECK:STDOUT:   %.loc9_27: ForwardDeclared* = addr_of %.loc9_33
-// CHECK:STDOUT:   assign file.%f.var, %.loc9_27
+// CHECK:STDOUT:   %f_ref.ref: ref {.f: ForwardDeclared} = name_ref f_ref, file.%import_ref.4
+// CHECK:STDOUT:   %.loc10_33: ref ForwardDeclared = struct_access %f_ref.ref, element0
+// CHECK:STDOUT:   %.loc10_27: ForwardDeclared* = addr_of %.loc10_33
+// CHECK:STDOUT:   assign file.%f.var, %.loc10_27
 // CHECK:STDOUT:   return
 // CHECK:STDOUT: }
 // CHECK:STDOUT:

+ 34 - 0
toolchain/check/testdata/let/fail_missing_value.carbon

@@ -0,0 +1,34 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// AUTOUPDATE
+
+// CHECK:STDERR: fail_missing_value.carbon:[[@LINE+3]]:11: ERROR: Expected `=`; `let` declaration must have an initializer.
+// CHECK:STDERR: let n: i32;
+// CHECK:STDERR:           ^
+let n: i32;
+
+fn F() {
+  // CHECK:STDERR: fail_missing_value.carbon:[[@LINE+3]]:13: ERROR: Expected `=`; `let` declaration must have an initializer.
+  // CHECK:STDERR:   let n: i32;
+  // CHECK:STDERR:             ^
+  let n: i32;
+}
+
+// CHECK:STDOUT: --- fail_missing_value.carbon
+// CHECK:STDOUT:
+// CHECK:STDOUT: file {
+// CHECK:STDOUT:   package: <namespace> = namespace [template] {
+// CHECK:STDOUT:     .F = %F
+// CHECK:STDOUT:   }
+// CHECK:STDOUT:   %n: i32 = bind_name n, <error>
+// CHECK:STDOUT:   %F: <function> = fn_decl @F [template] {}
+// CHECK:STDOUT: }
+// CHECK:STDOUT:
+// CHECK:STDOUT: fn @F() {
+// CHECK:STDOUT: !entry:
+// CHECK:STDOUT:   %n: i32 = bind_name n, <error>
+// CHECK:STDOUT:   return
+// CHECK:STDOUT: }
+// CHECK:STDOUT:

+ 4 - 1
toolchain/diagnostics/diagnostic_kind.def

@@ -111,7 +111,6 @@ CARBON_DIAGNOSTIC_KIND(ExpectedElseAfterIf)
 CARBON_DIAGNOSTIC_KIND(ExpectedDeclName)
 CARBON_DIAGNOSTIC_KIND(ExpectedDeclSemi)
 CARBON_DIAGNOSTIC_KIND(ExpectedDeclSemiOrDefinition)
-CARBON_DIAGNOSTIC_KIND(ExpectedInitializerAfterLet)
 CARBON_DIAGNOSTIC_KIND(ParamsRequiredAfterImplicit)
 CARBON_DIAGNOSTIC_KIND(ParamsRequiredByIntroducer)
 CARBON_DIAGNOSTIC_KIND(ExpectedAfterBase)
@@ -184,6 +183,10 @@ CARBON_DIAGNOSTIC_KIND(ImplAsOutsideClass)
 CARBON_DIAGNOSTIC_KIND(ImplPreviousDefinition)
 CARBON_DIAGNOSTIC_KIND(ImplRedefinition)
 
+// Let declaration checking.
+CARBON_DIAGNOSTIC_KIND(ExpectedInitializerAfterLet)
+CARBON_DIAGNOSTIC_KIND(ExpectedSymbolicBindingInAssociatedConstant)
+
 CARBON_DIAGNOSTIC_KIND(AddrOfEphemeralRef)
 CARBON_DIAGNOSTIC_KIND(AddrOfNonRef)
 CARBON_DIAGNOSTIC_KIND(AddrOnNonSelfParam)

+ 6 - 0
toolchain/lower/handle.cpp

@@ -60,6 +60,12 @@ auto HandleAssign(FunctionContext& context, SemIR::InstId /*inst_id*/,
   context.FinishInit(storage_type_id, inst.lhs_id, inst.rhs_id);
 }
 
+auto HandleAssociatedConstantDecl(FunctionContext& /*context*/,
+                                  SemIR::InstId /*inst_id*/,
+                                  SemIR::AssociatedConstantDecl inst) -> void {
+  FatalErrorIfEncountered(inst);
+}
+
 auto HandleAssociatedEntity(FunctionContext& /*context*/,
                             SemIR::InstId /*inst_id*/,
                             SemIR::AssociatedEntity inst) -> void {

+ 0 - 8
toolchain/parse/handle_let.cpp

@@ -30,14 +30,6 @@ auto HandleLetAfterPattern(Context& context) -> void {
   if (auto equals = context.ConsumeIf(Lex::TokenKind::Equal)) {
     context.AddLeafNode(NodeKind::LetInitializer, *equals);
     context.PushState(State::Expr);
-  } else {
-    if (!state.has_error) {
-      CARBON_DIAGNOSTIC(
-          ExpectedInitializerAfterLet, Error,
-          "Expected `=`; `let` declaration must have an initializer.");
-      context.emitter().Emit(*context.position(), ExpectedInitializerAfterLet);
-    }
-    context.ReturnErrorOnState();
   }
 }
 

+ 7 - 3
toolchain/parse/node_kind.def

@@ -314,15 +314,19 @@ CARBON_PARSE_NODE_KIND_CHILD_COUNT(CompileTimeBindingPattern, 2, ColonExclaim)
 CARBON_PARSE_NODE_KIND_CHILD_COUNT(Addr, 1, Addr)
 CARBON_PARSE_NODE_KIND_CHILD_COUNT(Template, 1, Template)
 
-// `let`:
+// `let` declarations, including associated constant declarations:
 //   LetIntroducer
 //   _repeated_ _external_: modifier
 //   _external_: BindingPattern or TuplePattern
-//   LetInitializer
-//   _external_: expression
+//     LetInitializer
+//     _external_: expression
+//   _optional_
 // LetDecl
 //
 // Modifier keywords only appear for `let` declarations, not `let` statements.
+//
+// The LetInitializer and following expression are paired: either both will be
+// present, or neither will.
 CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetIntroducer, 0, Let)
 CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetInitializer, 0, Equal)
 CARBON_PARSE_NODE_KIND_BRACKET(LetDecl, LetIntroducer, CARBON_IF_VALID(Semi))

+ 5 - 1
toolchain/parse/state.def

@@ -1257,7 +1257,9 @@ CARBON_PARSE_STATE_VARIANTS2(VarFinish, Decl, For)
 //   3. LetFinish
 CARBON_PARSE_STATE(Let)
 
-// Handles `let` after the pattern, followed by an initializer.
+// Handles `let` after the pattern, optionally followed by an initializer. The
+// initializer is required except in an associated constant declaration, but
+// that is enforced by check.
 //
 // let ... = ...
 //         ^
@@ -1265,6 +1267,8 @@ CARBON_PARSE_STATE(Let)
 //         ^~~~~
 //   1. Expr
 //
+// let ... ;
+//        ^
 // let ... ??? ;
 //         ^~~
 //   (state done)

+ 1 - 1
toolchain/parse/testdata/let/fail_empty.carbon

@@ -16,6 +16,6 @@ let;
 // CHECK:STDOUT:         {kind: 'IdentifierName', text: ';', has_error: yes},
 // CHECK:STDOUT:         {kind: 'InvalidParse', text: ';', has_error: yes},
 // CHECK:STDOUT:       {kind: 'BindingPattern', text: ';', has_error: yes, subtree_size: 3},
-// CHECK:STDOUT:     {kind: 'LetDecl', text: ';', has_error: yes, subtree_size: 5},
+// CHECK:STDOUT:     {kind: 'LetDecl', text: ';', subtree_size: 5},
 // CHECK:STDOUT:     {kind: 'FileEnd', text: ''},
 // CHECK:STDOUT:   ]

+ 16 - 9
toolchain/parse/testdata/let/fail_missing_value.carbon → toolchain/parse/testdata/let/missing_value.carbon

@@ -4,26 +4,24 @@
 //
 // AUTOUPDATE
 
-// CHECK:STDERR: fail_missing_value.carbon:[[@LINE+3]]:11: ERROR: Expected `=`; `let` declaration must have an initializer.
-// CHECK:STDERR: let a: i32;
-// CHECK:STDERR:           ^
 let a: i32;
 
 fn F() {
-  // CHECK:STDERR: fail_missing_value.carbon:[[@LINE+3]]:13: ERROR: Expected `=`; `let` declaration must have an initializer.
-  // CHECK:STDERR:   let b: i32;
-  // CHECK:STDERR:             ^
   let b: i32;
 }
 
-// CHECK:STDOUT: - filename: fail_missing_value.carbon
+interface I {
+  let T:! type;
+}
+
+// CHECK:STDOUT: - filename: missing_value.carbon
 // CHECK:STDOUT:   parse_tree: [
 // CHECK:STDOUT:     {kind: 'FileStart', text: ''},
 // CHECK:STDOUT:       {kind: 'LetIntroducer', text: 'let'},
 // CHECK:STDOUT:         {kind: 'IdentifierName', text: 'a'},
 // CHECK:STDOUT:         {kind: 'IntTypeLiteral', text: 'i32'},
 // CHECK:STDOUT:       {kind: 'BindingPattern', text: ':', subtree_size: 3},
-// CHECK:STDOUT:     {kind: 'LetDecl', text: ';', has_error: yes, subtree_size: 5},
+// CHECK:STDOUT:     {kind: 'LetDecl', text: ';', subtree_size: 5},
 // CHECK:STDOUT:         {kind: 'FunctionIntroducer', text: 'fn'},
 // CHECK:STDOUT:         {kind: 'IdentifierName', text: 'F'},
 // CHECK:STDOUT:           {kind: 'TuplePatternStart', text: '('},
@@ -33,7 +31,16 @@ fn F() {
 // CHECK:STDOUT:           {kind: 'IdentifierName', text: 'b'},
 // CHECK:STDOUT:           {kind: 'IntTypeLiteral', text: 'i32'},
 // CHECK:STDOUT:         {kind: 'BindingPattern', text: ':', subtree_size: 3},
-// CHECK:STDOUT:       {kind: 'LetDecl', text: ';', has_error: yes, subtree_size: 5},
+// CHECK:STDOUT:       {kind: 'LetDecl', text: ';', subtree_size: 5},
 // CHECK:STDOUT:     {kind: 'FunctionDefinition', text: '}', subtree_size: 11},
+// CHECK:STDOUT:         {kind: 'InterfaceIntroducer', text: 'interface'},
+// CHECK:STDOUT:         {kind: 'IdentifierName', text: 'I'},
+// CHECK:STDOUT:       {kind: 'InterfaceDefinitionStart', text: '{', subtree_size: 3},
+// CHECK:STDOUT:         {kind: 'LetIntroducer', text: 'let'},
+// CHECK:STDOUT:           {kind: 'IdentifierName', text: 'T'},
+// CHECK:STDOUT:           {kind: 'TypeTypeLiteral', text: 'type'},
+// CHECK:STDOUT:         {kind: 'CompileTimeBindingPattern', text: ':!', subtree_size: 3},
+// CHECK:STDOUT:       {kind: 'LetDecl', text: ';', subtree_size: 5},
+// CHECK:STDOUT:     {kind: 'InterfaceDefinition', text: '}', subtree_size: 9},
 // CHECK:STDOUT:     {kind: 'FileEnd', text: ''},
 // CHECK:STDOUT:   ]

+ 6 - 2
toolchain/parse/typed_nodes.h

@@ -345,8 +345,12 @@ struct LetDecl {
   LetIntroducerId introducer;
   llvm::SmallVector<AnyModifierId> modifiers;
   AnyPatternId pattern;
-  LetInitializerId equals;
-  AnyExprId initializer;
+
+  struct Initializer {
+    LetInitializerId equals;
+    AnyExprId initializer;
+  };
+  std::optional<Initializer> initializer;
 };
 
 // `var` nodes

+ 3 - 0
toolchain/sem_ir/file.cpp

@@ -213,6 +213,7 @@ static auto GetTypePrecedence(InstKind kind) -> int {
     case ArrayIndex::Kind:
     case ArrayInit::Kind:
     case Assign::Kind:
+    case AssociatedConstantDecl::Kind:
     case AssociatedEntity::Kind:
     case BaseDecl::Kind:
     case BindName::Kind:
@@ -453,6 +454,7 @@ static auto StringifyTypeExprImpl(const SemIR::File& outer_sem_ir,
       case ArrayIndex::Kind:
       case ArrayInit::Kind:
       case Assign::Kind:
+      case AssociatedConstantDecl::Kind:
       case AssociatedEntity::Kind:
       case BaseDecl::Kind:
       case BindName::Kind:
@@ -569,6 +571,7 @@ auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory {
       case AddrOf::Kind:
       case AddrPattern::Kind:
       case ArrayType::Kind:
+      case AssociatedConstantDecl::Kind:
       case AssociatedEntity::Kind:
       case AssociatedEntityType::Kind:
       case BindSymbolicName::Kind:

+ 12 - 3
toolchain/sem_ir/formatter.cpp

@@ -469,9 +469,9 @@ class InstNamer {
           CollectNamesInBlock(scope_id, inst.As<AddrPattern>().inner_id);
           break;
         }
-        case SpliceBlock::Kind: {
-          CollectNamesInBlock(scope_id, inst.As<SpliceBlock>().block_id);
-          break;
+        case AssociatedConstantDecl::Kind: {
+          add_inst_name_id(inst.As<AssociatedConstantDecl>().name_id);
+          continue;
         }
         case BindAlias::Kind:
         case BindName::Kind:
@@ -536,6 +536,10 @@ class InstNamer {
           add_inst_name_id(inst.As<Param>().name_id);
           continue;
         }
+        case SpliceBlock::Kind: {
+          CollectNamesInBlock(scope_id, inst.As<SpliceBlock>().block_id);
+          break;
+        }
         case VarStorage::Kind: {
           add_inst_name_id(inst.As<VarStorage>().name_id, ".var");
           continue;
@@ -1203,6 +1207,11 @@ class Formatter {
   auto FormatArg(InstId id) -> void { FormatInstName(id); }
 
   auto FormatArg(InstBlockId id) -> void {
+    if (!id.is_valid()) {
+      out_ << "invalid";
+      return;
+    }
+
     out_ << '(';
     llvm::ListSeparator sep;
     for (auto inst_id : sem_ir_.inst_blocks().Get(id)) {

+ 1 - 0
toolchain/sem_ir/inst_kind.def

@@ -23,6 +23,7 @@ CARBON_SEM_IR_INST_KIND(ArrayIndex)
 CARBON_SEM_IR_INST_KIND(ArrayInit)
 CARBON_SEM_IR_INST_KIND(ArrayType)
 CARBON_SEM_IR_INST_KIND(Assign)
+CARBON_SEM_IR_INST_KIND(AssociatedConstantDecl)
 CARBON_SEM_IR_INST_KIND(AssociatedEntity)
 CARBON_SEM_IR_INST_KIND(AssociatedEntityType)
 CARBON_SEM_IR_INST_KIND(BaseDecl)

+ 9 - 0
toolchain/sem_ir/typed_insts.h

@@ -154,6 +154,15 @@ struct Assign {
   InstId rhs_id;
 };
 
+struct AssociatedConstantDecl {
+  static constexpr auto Kind =
+      InstKind::AssociatedConstantDecl.Define<Parse::NodeId>(
+          "assoc_const_decl");
+
+  TypeId type_id;
+  NameId name_id;
+};
+
 // An associated entity declared in an interface. This is either an associated
 // function or a non-function associated constant such as an associated type.
 // This represents the entity before impl lookup is performed, and identifies