From f4916c653e84854f004d53172bf43bfdf0df1747 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 12 Jan 2021 07:08:49 -0800 Subject: [PATCH] Implement module type subtyping in wasm-smith At this point we've got all the metadata required for this! --- crates/wasm-smith/src/lib.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/crates/wasm-smith/src/lib.rs b/crates/wasm-smith/src/lib.rs index 39f20c5da9..74872072f3 100644 --- a/crates/wasm-smith/src/lib.rs +++ b/crates/wasm-smith/src/lib.rs @@ -1857,22 +1857,25 @@ where // https://github.com/WebAssembly/module-linking/blob/master/proposals/module-linking/Subtyping.md fn is_subtype_instance(&self, a: &InstanceType, b: &InstanceType) -> bool { - b.exports - .iter() - .all(|(b_name, b_ty)| match a.exports.get(b_name) { - Some(a_ty) => self.is_subtype(a_ty, b_ty), - None => false, - }) + self.is_set_subtypes(&a.exports, &b.exports) } // https://github.com/WebAssembly/module-linking/blob/master/proposals/module-linking/Subtyping.md - // - // FIXME currently we require both modules have zero imports, but that's not - // what we actually want. Waiting for module-linking#17 to settle first. fn is_subtype_module(&self, a: &ModuleType, b: &ModuleType) -> bool { - self.is_subtype_instance(&a.exports, &b.exports) - && a.imports.len() == 0 - && b.imports.len() == 0 + self.is_set_subtypes(&a.exports.exports, &b.exports.exports) + && self.is_set_subtypes(&b.import_types, &a.import_types) + } + + // https://github.com/WebAssembly/module-linking/blob/master/proposals/module-linking/Subtyping.md + fn is_set_subtypes( + &self, + a: &indexmap::IndexMap, + b: &indexmap::IndexMap, + ) -> bool { + b.iter().all(|(b_name, b_ty)| match a.get(b_name) { + Some(a_ty) => self.is_subtype(a_ty, b_ty), + None => false, + }) } }