Skip to content

Commit f220359

Browse files
authored
Print special ids as their names and don't crash when dumping them (#6398)
1 parent 315b0ac commit f220359

File tree

10 files changed

+43
-11
lines changed

10 files changed

+43
-11
lines changed

toolchain/check/testdata/basics/raw_sem_ir/builtins.carbon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
// CHECK:STDOUT: generics: {}
2929
// CHECK:STDOUT: specifics: {}
3030
// CHECK:STDOUT: struct_type_fields:
31-
// CHECK:STDOUT: struct_type_fields0: {}
31+
// CHECK:STDOUT: struct_type_fields_empty: {}
3232
// CHECK:STDOUT: types:
3333
// CHECK:STDOUT: 'type(TypeType)':
3434
// CHECK:STDOUT: value_repr: {kind: copy, type: type(TypeType)}

toolchain/check/testdata/basics/raw_sem_ir/cpp_interop.carbon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn G(x: Cpp.X) {
7373
// CHECK:STDOUT: generics: {}
7474
// CHECK:STDOUT: specifics: {}
7575
// CHECK:STDOUT: struct_type_fields:
76-
// CHECK:STDOUT: struct_type_fields0: {}
76+
// CHECK:STDOUT: struct_type_fields_empty: {}
7777
// CHECK:STDOUT: struct_type_fields60000001:
7878
// CHECK:STDOUT: 0: {name_id: name5, type_inst_id: inst6000001F}
7979
// CHECK:STDOUT: struct_type_fields60000002:

toolchain/check/testdata/basics/raw_sem_ir/multifile.carbon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn B() {
4545
// CHECK:STDOUT: generics: {}
4646
// CHECK:STDOUT: specifics: {}
4747
// CHECK:STDOUT: struct_type_fields:
48-
// CHECK:STDOUT: struct_type_fields0: {}
48+
// CHECK:STDOUT: struct_type_fields_empty: {}
4949
// CHECK:STDOUT: types:
5050
// CHECK:STDOUT: 'type(TypeType)':
5151
// CHECK:STDOUT: value_repr: {kind: copy, type: type(TypeType)}
@@ -109,7 +109,7 @@ fn B() {
109109
// CHECK:STDOUT: generics: {}
110110
// CHECK:STDOUT: specifics: {}
111111
// CHECK:STDOUT: struct_type_fields:
112-
// CHECK:STDOUT: struct_type_fields0: {}
112+
// CHECK:STDOUT: struct_type_fields_empty: {}
113113
// CHECK:STDOUT: types:
114114
// CHECK:STDOUT: 'type(TypeType)':
115115
// CHECK:STDOUT: value_repr: {kind: copy, type: type(TypeType)}

toolchain/check/testdata/basics/raw_sem_ir/multifile_with_textual_ir.carbon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn B() {
4545
// CHECK:STDOUT: generics: {}
4646
// CHECK:STDOUT: specifics: {}
4747
// CHECK:STDOUT: struct_type_fields:
48-
// CHECK:STDOUT: struct_type_fields0: {}
48+
// CHECK:STDOUT: struct_type_fields_empty: {}
4949
// CHECK:STDOUT: types:
5050
// CHECK:STDOUT: 'type(TypeType)':
5151
// CHECK:STDOUT: value_repr: {kind: copy, type: type(TypeType)}
@@ -128,7 +128,7 @@ fn B() {
128128
// CHECK:STDOUT: generics: {}
129129
// CHECK:STDOUT: specifics: {}
130130
// CHECK:STDOUT: struct_type_fields:
131-
// CHECK:STDOUT: struct_type_fields0: {}
131+
// CHECK:STDOUT: struct_type_fields_empty: {}
132132
// CHECK:STDOUT: types:
133133
// CHECK:STDOUT: 'type(TypeType)':
134134
// CHECK:STDOUT: value_repr: {kind: copy, type: type(TypeType)}

toolchain/check/testdata/basics/raw_sem_ir/one_file.carbon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ fn Foo[T:! type](p: T*) -> (T*, ()) {
343343
// CHECK:STDOUT: specific60000017: {generic: generic60000001, args: inst_block6000006F}
344344
// CHECK:STDOUT: specific60000018: {generic: generic60000001, args: inst_block60000071}
345345
// CHECK:STDOUT: struct_type_fields:
346-
// CHECK:STDOUT: struct_type_fields0: {}
346+
// CHECK:STDOUT: struct_type_fields_empty: {}
347347
// CHECK:STDOUT: types:
348348
// CHECK:STDOUT: 'type(TypeType)':
349349
// CHECK:STDOUT: value_repr: {kind: copy, type: type(TypeType)}

toolchain/check/testdata/basics/raw_sem_ir/one_file_with_textual_ir.carbon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn Foo(n: ()) -> ((), ()) {
3636
// CHECK:STDOUT: generics: {}
3737
// CHECK:STDOUT: specifics: {}
3838
// CHECK:STDOUT: struct_type_fields:
39-
// CHECK:STDOUT: struct_type_fields0: {}
39+
// CHECK:STDOUT: struct_type_fields_empty: {}
4040
// CHECK:STDOUT: types:
4141
// CHECK:STDOUT: 'type(TypeType)':
4242
// CHECK:STDOUT: value_repr: {kind: copy, type: type(TypeType)}

toolchain/driver/testdata/stdin.carbon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
// CHECK:STDOUT: generics: {}
4141
// CHECK:STDOUT: specifics: {}
4242
// CHECK:STDOUT: struct_type_fields:
43-
// CHECK:STDOUT: struct_type_fields0: {}
43+
// CHECK:STDOUT: struct_type_fields_empty: {}
4444
// CHECK:STDOUT: types:
4545
// CHECK:STDOUT: 'type(TypeType)':
4646
// CHECK:STDOUT: value_repr: {kind: copy, type: type(TypeType)}

toolchain/sem_ir/dump.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ static auto DumpGenericSummary(const File& file, GenericId generic_id)
5353
static auto DumpInstSummary(const File& file, InstId inst_id) -> std::string {
5454
RawStringOstream out;
5555
out << inst_id;
56-
if (inst_id.has_value()) {
56+
if (inst_id.has_value() && inst_id != InstId::InitTombstone &&
57+
inst_id != InstId::ImplWitnessTablePlaceholder) {
5758
out << ": " << file.insts().Get(inst_id);
5859
}
5960
return out.TakeStr();
@@ -228,7 +229,7 @@ LLVM_DUMP_METHOD auto Dump(const File& file, InstBlockId inst_block_id)
228229
out << inst_block_id;
229230
if (inst_block_id.has_value()) {
230231
out << ":";
231-
auto inst_block = file.inst_blocks().Get(inst_block_id);
232+
auto inst_block = file.inst_blocks().GetOrEmpty(inst_block_id);
232233
for (auto inst_id : inst_block) {
233234
out << "\n - " << DumpInstSummary(file, inst_id);
234235
}

toolchain/sem_ir/ids.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ namespace Carbon::SemIR {
1515
auto InstId::Print(llvm::raw_ostream& out) const -> void {
1616
if (IsSingletonInstId(*this)) {
1717
out << Label << "(" << SingletonInstKinds[index] << ")";
18+
} else if (*this == InitTombstone) {
19+
out << Label << "(InitTombstone)";
20+
} else if (*this == ImplWitnessTablePlaceholder) {
21+
out << Label << "(ImplWitnessTablePlaceholder)";
1822
} else {
1923
IdBase::Print(out);
2024
}
@@ -207,6 +211,22 @@ auto InstBlockId::Print(llvm::raw_ostream& out) const -> void {
207211
}
208212
}
209213

214+
auto StructTypeFieldsId::Print(llvm::raw_ostream& out) const -> void {
215+
if (*this == Empty) {
216+
out << Label << "_empty";
217+
} else {
218+
IdBase::Print(out);
219+
}
220+
}
221+
222+
auto CustomLayoutId::Print(llvm::raw_ostream& out) const -> void {
223+
if (*this == Empty) {
224+
out << Label << "_empty";
225+
} else {
226+
IdBase::Print(out);
227+
}
228+
}
229+
210230
auto TypeId::Print(llvm::raw_ostream& out) const -> void {
211231
out << Label << "(";
212232
if (*this == TypeType::TypeId) {
@@ -240,6 +260,14 @@ auto LibraryNameId::Print(llvm::raw_ostream& out) const -> void {
240260
}
241261
}
242262

263+
auto RequireImplsBlockId::Print(llvm::raw_ostream& out) const -> void {
264+
if (*this == Empty) {
265+
out << Label << "_empty";
266+
} else {
267+
IdBase::Print(out);
268+
}
269+
}
270+
243271
auto LocId::Print(llvm::raw_ostream& out) const -> void {
244272
switch (kind()) {
245273
case Kind::None:

toolchain/sem_ir/ids.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ struct StructTypeFieldsId : public IdBase<StructTypeFieldsId> {
819819
static const StructTypeFieldsId Empty;
820820

821821
using IdBase::IdBase;
822+
auto Print(llvm::raw_ostream& out) const -> void;
822823
};
823824

824825
inline constexpr StructTypeFieldsId StructTypeFieldsId::Empty =
@@ -840,6 +841,7 @@ struct CustomLayoutId : public IdBase<CustomLayoutId> {
840841
static constexpr int FirstFieldIndex = 2;
841842

842843
using IdBase::IdBase;
844+
auto Print(llvm::raw_ostream& out) const -> void;
843845
};
844846

845847
inline constexpr CustomLayoutId CustomLayoutId::Empty = CustomLayoutId(0);
@@ -945,6 +947,7 @@ struct RequireImplsBlockId : public IdBase<RequireImplsBlockId> {
945947
static const RequireImplsBlockId Empty;
946948

947949
using IdBase::IdBase;
950+
auto Print(llvm::raw_ostream& out) const -> void;
948951
};
949952

950953
inline constexpr RequireImplsBlockId RequireImplsBlockId::Empty =

0 commit comments

Comments
 (0)