Skip to content

Commit 11ada63

Browse files
authored
Include type names in error messages from building (#4517)
Instead of just reporting the type index that causes an error when building types, report the name of the responsible type when parsing the text format.
1 parent 6810b60 commit 11ada63

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/wasm/wasm-s-parser.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,16 @@ void SExpressionWasmBuilder::preParseHeapTypes(Element& module) {
936936

937937
auto result = builder.build();
938938
if (auto* err = result.getError()) {
939-
Fatal() << "Invalid type: " << err->reason << " at index " << err->index;
939+
// Find the name to provide a better error message.
940+
std::stringstream msg;
941+
msg << "Invalid type: " << err->reason;
942+
for (auto& [name, index] : typeIndices) {
943+
if (index == err->index) {
944+
Fatal() << msg.str() << " at type $" << name;
945+
}
946+
}
947+
// No name, just report the index.
948+
Fatal() << msg.str() << " at index " << err->index;
940949
}
941950
types = *result;
942951

test/lit/parse-bad-supertype.wast

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
;; Test that an invalid supertype results in a useful error message
2+
3+
;; RUN: not wasm-opt %s -all --nominal 2>&1 | filecheck %s
4+
5+
;; CHECK: Fatal: Invalid type: Heap type has an invalid supertype at type $sub
6+
(module
7+
(type $super (struct_subtype i32 data))
8+
(type $sub (struct_subtype i64 $super))
9+
)

0 commit comments

Comments
 (0)