Skip to content

[GC] Add Type::IsDefaultable and use that to do more validation #3456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class Type {
bool isRtt() const;
bool isStruct() const;
bool isArray() const;
bool isDefaultable() const;

private:
template<bool (Type::*pred)() const> bool hasPredicate() {
Expand Down
7 changes: 7 additions & 0 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ bool Type::isStruct() const { return isRef() && getHeapType().isStruct(); }

bool Type::isArray() const { return isRef() && getHeapType().isArray(); }

bool Type::isDefaultable() const {
// A variable can get a default value if its type is concrete (unreachable
// and none have no values, hence no default), and if it's a reference, it
// must be nullable.
return isConcrete() && (!isRef() || isNullable());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do RTTs have default values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, and binaryen gives them the natural default value (rtt.canon). I admit I can't find an explanation of that on the GC docs, though. I opened WebAssembly/gc#175

}

bool Type::operator<(const Type& other) const {
if (*this == other) {
return false;
Expand Down
9 changes: 3 additions & 6 deletions src/wasm/wasm-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2294,8 +2294,7 @@ void FunctionValidator::visitStructNew(StructNew* curr) {
"struct.new_with_default should have no operands");
// All the fields must be defaultable.
for (const auto& field : fields) {
// TODO: add type.isDefaultable()?
shouldBeTrue(!field.type.isRef() || field.type.isNullable(),
shouldBeTrue(field.type.isDefaultable(),
field,
"struct.new_with_default value type must be defaultable");
}
Expand Down Expand Up @@ -2368,8 +2367,7 @@ void FunctionValidator::visitArrayNew(ArrayNew* curr) {
shouldBeTrue(
!curr->init, curr, "array.new_with_default should have no init");
// The element must be defaultable.
// TODO: add type.isDefaultable()?
shouldBeTrue(!element.type.isRef() || element.type.isNullable(),
shouldBeTrue(element.type.isDefaultable(),
element,
"array.new_with_default value type must be defaultable");
} else {
Expand Down Expand Up @@ -2439,8 +2437,7 @@ void FunctionValidator::visitFunction(Function* curr) {
}
for (const auto& var : curr->vars) {
features |= var.getFeatures();
shouldBeTrue(var.isConcrete(), curr, "vars must be concretely typed");
// TODO: check for nullability
shouldBeTrue(var.isDefaultable(), curr, "vars must be defaultable");
}
shouldBeTrue(features <= getModule()->features,
curr->name,
Expand Down