Repository: bytecodealliance/wasm-tools
Pull request #2109 (2025-03-21 02:38:09)

wast.rs: add explicit --assert for test-folded/no-test-folded
This PR gives the `wast` subcommand a command-line `--assert` to configure whether to test the folded-instructions printer. Currently this is set more implicitly (`!self.features.features().legacy_exceptions()`).


Patch:
diff --git a/src/bin/wasm-tools/wast.rs b/src/bin/wasm-tools/wast.rs
index f97deef8..aac09494 100644
--- a/src/bin/wasm-tools/wast.rs
+++ b/src/bin/wasm-tools/wast.rs
@@ -238,18 +238,19 @@ impl Opts {
 
     /// Tests whether `assert` is enabled.
     fn assert(&self, assert: Assert) -> bool {
-        self.assert
-            .iter()
-            .flat_map(|v| v)
-            .any(|a| match (*a, assert) {
-                // if explicitly requested, always enable
-                (a, b) if a == b => true,
-                // exclude some assertions from the default
-                (Assert::Default, Assert::SnapshotFolded) => false,
-                // otherwise allow all other assertions when default is enabled
-                (Assert::Default, _) => true,
-                _ => false,
-            })
+        let mut enabled = false;
+        for &a in self.assert.iter().flat_map(|v| v) {
+            match (a, assert) {
+                // if explicitly requested, enable
+                (a, b) if a == b => enabled = true,
+                // default enables almost all assertions
+                (Assert::Default, b) if b != Assert::SnapshotFolded => enabled = true,
+                // NoTestFolded disables TestFolded
+                (Assert::NoTestFolded, Assert::TestFolded) => enabled = false,
+                _ => (),
+            }
+        }
+        enabled
     }
 
     /// Returns whether `error`, from the validator, matches the expected error
@@ -274,12 +275,8 @@ impl Opts {
         }
 
         // Test that we can print these bytes with instructions in folded form.
-        //
-        // Note though that the folded form doesn't support legacy exceptions,
-        // so if that's enabled disable testing automatically.
         let mut folded_string = String::new();
-        let test_folded = !self.features.features().legacy_exceptions();
-        if test_folded {
+        if self.assert(Assert::TestFolded) {
             let mut folding_printer = wasmprinter::Config::new();
             folding_printer.fold_instructions(true);
             folding_printer
@@ -300,7 +297,7 @@ impl Opts {
             self.binary_compare(&binary2, contents)
                 .context("failed to compare original `wat` with roundtrip `wat`")?;
 
-            if test_folded {
+            if self.assert(Assert::TestFolded) {
                 let binary2f = wat::parse_str(&folded_string)
                     .context("failed to parse folded `wat` from `wasmprinter`")?;
                 self.binary_compare(&binary2f, contents)
@@ -563,6 +560,8 @@ enum Assert {
     SnapshotPrint,
     SnapshotJson,
     SnapshotFolded,
+    TestFolded,
+    NoTestFolded,
 }
 
 fn parse_asserts(arg: &str) -> Result<Vec<Assert>> {
@@ -576,6 +575,8 @@ fn parse_asserts(arg: &str) -> Result<Vec<Assert>> {
             "snapshot-print" => ret.push(Assert::SnapshotPrint),
             "snapshot-json" => ret.push(Assert::SnapshotJson),
             "snapshot-folded" => ret.push(Assert::SnapshotFolded),
+            "test-folded" => ret.push(Assert::TestFolded),
+            "no-test-folded" => ret.push(Assert::NoTestFolded),
             other => bail!("unknown assertion: {other}"),
         }
     }


Test patch:
diff --git a/tests/cli/legacy-exceptions/legacy-exceptions.wast b/tests/cli/legacy-exceptions/legacy-exceptions.wast
index 0ef594cc..37dc0816 100644
--- a/tests/cli/legacy-exceptions/legacy-exceptions.wast
+++ b/tests/cli/legacy-exceptions/legacy-exceptions.wast
@@ -1,6 +1,5 @@
-;; RUN: wast --assert default --snapshot tests/snapshots % -f legacy-exceptions
+;; RUN: wast --assert default,no-test-folded --snapshot tests/snapshots % -f legacy-exceptions
 
-;; --enable-legacy-exceptions
 (module
   (type (;0;) (func))
   (func (;0;) (type 0)
diff --git a/tests/cli/legacy-exceptions/rethrow.wast b/tests/cli/legacy-exceptions/rethrow.wast
index 5fc5ce82..ef8aaa7a 100644
--- a/tests/cli/legacy-exceptions/rethrow.wast
+++ b/tests/cli/legacy-exceptions/rethrow.wast
@@ -1,4 +1,4 @@
-;; RUN: wast --assert default --snapshot tests/snapshots % -f legacy-exceptions
+;; RUN: wast --assert default,no-test-folded --snapshot tests/snapshots % -f legacy-exceptions
 
 ;; Test rethrow instruction.
 
diff --git a/tests/cli/legacy-exceptions/throw.wast b/tests/cli/legacy-exceptions/throw.wast
index 96dcd6f8..0fbb300e 100644
--- a/tests/cli/legacy-exceptions/throw.wast
+++ b/tests/cli/legacy-exceptions/throw.wast
@@ -1,4 +1,4 @@
-;; RUN: wast --assert default --snapshot tests/snapshots % -f legacy-exceptions
+;; RUN: wast --assert default,no-test-folded --snapshot tests/snapshots % -f legacy-exceptions
 
 ;; Test throw instruction.
 
diff --git a/tests/cli/legacy-exceptions/try_catch.wast b/tests/cli/legacy-exceptions/try_catch.wast
index 50b89fe3..b7ab9287 100644
--- a/tests/cli/legacy-exceptions/try_catch.wast
+++ b/tests/cli/legacy-exceptions/try_catch.wast
@@ -1,4 +1,4 @@
-;; RUN: wast --assert default --snapshot tests/snapshots % -f legacy-exceptions
+;; RUN: wast --assert default,no-test-folded --snapshot tests/snapshots % -f legacy-exceptions
 
 ;; Test try-catch blocks.
 
diff --git a/tests/cli/legacy-exceptions/try_delegate.wast b/tests/cli/legacy-exceptions/try_delegate.wast
index cb5e0cf3..81830e94 100644
--- a/tests/cli/legacy-exceptions/try_delegate.wast
+++ b/tests/cli/legacy-exceptions/try_delegate.wast
@@ -1,4 +1,4 @@
-;; RUN: wast --assert default --snapshot tests/snapshots % -f legacy-exceptions
+;; RUN: wast --assert default,no-test-folded --snapshot tests/snapshots % -f legacy-exceptions
 
 ;; Test try-delegate blocks.
 
diff --git a/tests/snapshots/cli/legacy-exceptions/legacy-exceptions.wast.json b/tests/snapshots/cli/legacy-exceptions/legacy-exceptions.wast.json
index d088bbc3..557040e1 100644
--- a/tests/snapshots/cli/legacy-exceptions/legacy-exceptions.wast.json
+++ b/tests/snapshots/cli/legacy-exceptions/legacy-exceptions.wast.json
@@ -3,7 +3,7 @@
   "commands": [
     {
       "type": "module",
-      "line": 4,
+      "line": 3,
       "filename": "legacy-exceptions.0.wasm",
       "module_type": "binary"
     }