From 3fd8c70b572aa1a2e8989c2dedf0baa228b50b0d Mon Sep 17 00:00:00 2001 From: "Soni L." Date: Mon, 23 Sep 2024 17:55:21 -0300 Subject: [PATCH] Fix error message for ref.is_null (#2471) Fixes #2453 in a bit of a silly way. (Conveniently, we already have tests for this, but nobody noticed they were broken.) --- src/type-checker.cc | 23 +++++++++++------------ test/spec/ref_is_null.txt | 3 ++- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/type-checker.cc b/src/type-checker.cc index 5424f1322e..37412e15e5 100644 --- a/src/type-checker.cc +++ b/src/type-checker.cc @@ -30,7 +30,14 @@ std::string TypesToString(const TypeVector& types, } for (size_t i = 0; i < types.size(); ++i) { - result += types[i].GetName(); + Type ty = types[i]; + // NOTE: Reference (and GetName) is also used by (e.g.) objdump, which does + // not apply validation. do this here so as to not break that. + if (ty == Type::Reference && ty.GetReferenceIndex() == kInvalidIndex) { + result += "reference"; + } else { + result += types[i].GetName(); + } if (i < types.size() - 1) { result += ", "; } @@ -812,18 +819,10 @@ Result TypeChecker::OnRefNullExpr(Type type) { Result TypeChecker::OnRefIsNullExpr() { Type type; Result result = PeekType(0, &type); - if (!(type == Type::Any || type.IsRef())) { - TypeVector actual; - if (Succeeded(result)) { - actual.push_back(type); - } - std::string message = - "type mismatch in ref.is_null, expected reference but got " + - TypesToString(actual); - PrintError("%s", message.c_str()); - result = Result::Error; + if (!type.IsRef()) { + type = Type::Reference; } - result |= DropTypes(1); + result |= PopAndCheck1Type(type, "ref.is_null"); PushType(Type::I32); return result; } diff --git a/test/spec/ref_is_null.txt b/test/spec/ref_is_null.txt index 0a6744a835..45765058d7 100644 --- a/test/spec/ref_is_null.txt +++ b/test/spec/ref_is_null.txt @@ -4,9 +4,10 @@ init(externref:1) => deinit() => out/test/spec/ref_is_null.wast:52: assert_invalid passed: - out/test/spec/ref_is_null/ref_is_null.1.wasm:000001b: error: type mismatch in ref.is_null, expected reference but got [i32] + out/test/spec/ref_is_null/ref_is_null.1.wasm:000001b: error: type mismatch in ref.is_null, expected [reference] but got [i32] 000001b: error: OnRefIsNullExpr callback failed out/test/spec/ref_is_null.wast:56: assert_invalid passed: + out/test/spec/ref_is_null/ref_is_null.2.wasm:0000018: error: type mismatch in ref.is_null, expected [reference] but got [] 0000018: error: OnRefIsNullExpr callback failed 16/16 tests passed. ;;; STDOUT ;;)