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

Print special ids as their names and don't crash when dumping them (#6398)

Dana Jansens 5 месяцев назад
Родитель
Сommit
f220359a9f

+ 1 - 1
toolchain/check/testdata/basics/raw_sem_ir/builtins.carbon

@@ -28,7 +28,7 @@
 // CHECK:STDOUT:   generics:        {}
 // CHECK:STDOUT:   specifics:       {}
 // CHECK:STDOUT:   struct_type_fields:
-// CHECK:STDOUT:     struct_type_fields0: {}
+// CHECK:STDOUT:     struct_type_fields_empty: {}
 // CHECK:STDOUT:   types:
 // CHECK:STDOUT:     'type(TypeType)':
 // CHECK:STDOUT:       value_repr:      {kind: copy, type: type(TypeType)}

+ 1 - 1
toolchain/check/testdata/basics/raw_sem_ir/cpp_interop.carbon

@@ -73,7 +73,7 @@ fn G(x: Cpp.X) {
 // CHECK:STDOUT:   generics:        {}
 // CHECK:STDOUT:   specifics:       {}
 // CHECK:STDOUT:   struct_type_fields:
-// CHECK:STDOUT:     struct_type_fields0: {}
+// CHECK:STDOUT:     struct_type_fields_empty: {}
 // CHECK:STDOUT:     struct_type_fields60000001:
 // CHECK:STDOUT:       0:               {name_id: name5, type_inst_id: inst6000001F}
 // CHECK:STDOUT:     struct_type_fields60000002:

+ 2 - 2
toolchain/check/testdata/basics/raw_sem_ir/multifile.carbon

@@ -45,7 +45,7 @@ fn B() {
 // CHECK:STDOUT:   generics:        {}
 // CHECK:STDOUT:   specifics:       {}
 // CHECK:STDOUT:   struct_type_fields:
-// CHECK:STDOUT:     struct_type_fields0: {}
+// CHECK:STDOUT:     struct_type_fields_empty: {}
 // CHECK:STDOUT:   types:
 // CHECK:STDOUT:     'type(TypeType)':
 // CHECK:STDOUT:       value_repr:      {kind: copy, type: type(TypeType)}
@@ -109,7 +109,7 @@ fn B() {
 // CHECK:STDOUT:   generics:        {}
 // CHECK:STDOUT:   specifics:       {}
 // CHECK:STDOUT:   struct_type_fields:
-// CHECK:STDOUT:     struct_type_fields0: {}
+// CHECK:STDOUT:     struct_type_fields_empty: {}
 // CHECK:STDOUT:   types:
 // CHECK:STDOUT:     'type(TypeType)':
 // CHECK:STDOUT:       value_repr:      {kind: copy, type: type(TypeType)}

+ 2 - 2
toolchain/check/testdata/basics/raw_sem_ir/multifile_with_textual_ir.carbon

@@ -45,7 +45,7 @@ fn B() {
 // CHECK:STDOUT:   generics:        {}
 // CHECK:STDOUT:   specifics:       {}
 // CHECK:STDOUT:   struct_type_fields:
-// CHECK:STDOUT:     struct_type_fields0: {}
+// CHECK:STDOUT:     struct_type_fields_empty: {}
 // CHECK:STDOUT:   types:
 // CHECK:STDOUT:     'type(TypeType)':
 // CHECK:STDOUT:       value_repr:      {kind: copy, type: type(TypeType)}
@@ -128,7 +128,7 @@ fn B() {
 // CHECK:STDOUT:   generics:        {}
 // CHECK:STDOUT:   specifics:       {}
 // CHECK:STDOUT:   struct_type_fields:
-// CHECK:STDOUT:     struct_type_fields0: {}
+// CHECK:STDOUT:     struct_type_fields_empty: {}
 // CHECK:STDOUT:   types:
 // CHECK:STDOUT:     'type(TypeType)':
 // CHECK:STDOUT:       value_repr:      {kind: copy, type: type(TypeType)}

+ 1 - 1
toolchain/check/testdata/basics/raw_sem_ir/one_file.carbon

@@ -343,7 +343,7 @@ fn Foo[T:! type](p: T*) -> (T*, ()) {
 // CHECK:STDOUT:     specific60000017: {generic: generic60000001, args: inst_block6000006F}
 // CHECK:STDOUT:     specific60000018: {generic: generic60000001, args: inst_block60000071}
 // CHECK:STDOUT:   struct_type_fields:
-// CHECK:STDOUT:     struct_type_fields0: {}
+// CHECK:STDOUT:     struct_type_fields_empty: {}
 // CHECK:STDOUT:   types:
 // CHECK:STDOUT:     'type(TypeType)':
 // CHECK:STDOUT:       value_repr:      {kind: copy, type: type(TypeType)}

+ 1 - 1
toolchain/check/testdata/basics/raw_sem_ir/one_file_with_textual_ir.carbon

@@ -36,7 +36,7 @@ fn Foo(n: ()) -> ((), ()) {
 // CHECK:STDOUT:   generics:        {}
 // CHECK:STDOUT:   specifics:       {}
 // CHECK:STDOUT:   struct_type_fields:
-// CHECK:STDOUT:     struct_type_fields0: {}
+// CHECK:STDOUT:     struct_type_fields_empty: {}
 // CHECK:STDOUT:   types:
 // CHECK:STDOUT:     'type(TypeType)':
 // CHECK:STDOUT:       value_repr:      {kind: copy, type: type(TypeType)}

+ 1 - 1
toolchain/driver/testdata/stdin.carbon

@@ -40,7 +40,7 @@
 // CHECK:STDOUT:   generics:        {}
 // CHECK:STDOUT:   specifics:       {}
 // CHECK:STDOUT:   struct_type_fields:
-// CHECK:STDOUT:     struct_type_fields0: {}
+// CHECK:STDOUT:     struct_type_fields_empty: {}
 // CHECK:STDOUT:   types:
 // CHECK:STDOUT:     'type(TypeType)':
 // CHECK:STDOUT:       value_repr:      {kind: copy, type: type(TypeType)}

+ 3 - 2
toolchain/sem_ir/dump.cpp

@@ -53,7 +53,8 @@ static auto DumpGenericSummary(const File& file, GenericId generic_id)
 static auto DumpInstSummary(const File& file, InstId inst_id) -> std::string {
   RawStringOstream out;
   out << inst_id;
-  if (inst_id.has_value()) {
+  if (inst_id.has_value() && inst_id != InstId::InitTombstone &&
+      inst_id != InstId::ImplWitnessTablePlaceholder) {
     out << ": " << file.insts().Get(inst_id);
   }
   return out.TakeStr();
@@ -228,7 +229,7 @@ LLVM_DUMP_METHOD auto Dump(const File& file, InstBlockId inst_block_id)
   out << inst_block_id;
   if (inst_block_id.has_value()) {
     out << ":";
-    auto inst_block = file.inst_blocks().Get(inst_block_id);
+    auto inst_block = file.inst_blocks().GetOrEmpty(inst_block_id);
     for (auto inst_id : inst_block) {
       out << "\n  - " << DumpInstSummary(file, inst_id);
     }

+ 28 - 0
toolchain/sem_ir/ids.cpp

@@ -15,6 +15,10 @@ namespace Carbon::SemIR {
 auto InstId::Print(llvm::raw_ostream& out) const -> void {
   if (IsSingletonInstId(*this)) {
     out << Label << "(" << SingletonInstKinds[index] << ")";
+  } else if (*this == InitTombstone) {
+    out << Label << "(InitTombstone)";
+  } else if (*this == ImplWitnessTablePlaceholder) {
+    out << Label << "(ImplWitnessTablePlaceholder)";
   } else {
     IdBase::Print(out);
   }
@@ -207,6 +211,22 @@ auto InstBlockId::Print(llvm::raw_ostream& out) const -> void {
   }
 }
 
+auto StructTypeFieldsId::Print(llvm::raw_ostream& out) const -> void {
+  if (*this == Empty) {
+    out << Label << "_empty";
+  } else {
+    IdBase::Print(out);
+  }
+}
+
+auto CustomLayoutId::Print(llvm::raw_ostream& out) const -> void {
+  if (*this == Empty) {
+    out << Label << "_empty";
+  } else {
+    IdBase::Print(out);
+  }
+}
+
 auto TypeId::Print(llvm::raw_ostream& out) const -> void {
   out << Label << "(";
   if (*this == TypeType::TypeId) {
@@ -240,6 +260,14 @@ auto LibraryNameId::Print(llvm::raw_ostream& out) const -> void {
   }
 }
 
+auto RequireImplsBlockId::Print(llvm::raw_ostream& out) const -> void {
+  if (*this == Empty) {
+    out << Label << "_empty";
+  } else {
+    IdBase::Print(out);
+  }
+}
+
 auto LocId::Print(llvm::raw_ostream& out) const -> void {
   switch (kind()) {
     case Kind::None:

+ 3 - 0
toolchain/sem_ir/ids.h

@@ -819,6 +819,7 @@ struct StructTypeFieldsId : public IdBase<StructTypeFieldsId> {
   static const StructTypeFieldsId Empty;
 
   using IdBase::IdBase;
+  auto Print(llvm::raw_ostream& out) const -> void;
 };
 
 inline constexpr StructTypeFieldsId StructTypeFieldsId::Empty =
@@ -840,6 +841,7 @@ struct CustomLayoutId : public IdBase<CustomLayoutId> {
   static constexpr int FirstFieldIndex = 2;
 
   using IdBase::IdBase;
+  auto Print(llvm::raw_ostream& out) const -> void;
 };
 
 inline constexpr CustomLayoutId CustomLayoutId::Empty = CustomLayoutId(0);
@@ -945,6 +947,7 @@ struct RequireImplsBlockId : public IdBase<RequireImplsBlockId> {
   static const RequireImplsBlockId Empty;
 
   using IdBase::IdBase;
+  auto Print(llvm::raw_ostream& out) const -> void;
 };
 
 inline constexpr RequireImplsBlockId RequireImplsBlockId::Empty =