Repository: bytecodealliance/wasm-tools (rust)

Issue:
"print --name-unnamed" names block arguments
The following code is incorrectly round-tripped:

```
(module
  (func (result i32)
    i32.const 32
    f32.const 0.0
    block (param i32 f32) (result i32 f32)
    end
    drop
  )
)
```

The `cargo run -- print t.wasm --name-unnamed` output:

```
(module
  (type $#type0 (;0;) (func (result i32)))
  (type $#type1 (;1;) (func (param $#local0 i32) (param $#local1 f32) (result i32 f32)))
  (func $#func0 (;0;) (type $#type0) (result i32)
    i32.const 32
    f32.const 0x0p+0 (;=0;)
    block $#label0 (type $#type1) (param $#local0 i32) (param $#local1 f32) (result i32 f32)
    end
    drop
  )
)
```

The block parameters type definition are wrongfully names as locals.


Pull request:
Fix naming non-locals with `--name-unnamed`
This commit fixes an issue where the `--name-unnamed` argument to `wasm-tools print` would erroneously assign names to block params where names were not allowed.

Closes #1684

Patch:
diff --git a/crates/wasmprinter/src/lib.rs b/crates/wasmprinter/src/lib.rs
index f8e09f62..f8945d5b 100644
--- a/crates/wasmprinter/src/lib.rs
+++ b/crates/wasmprinter/src/lib.rs
@@ -919,7 +919,7 @@ impl Printer<'_, '_> {
         // we need to be careful to terminate previous param blocks and open
         // a new one if that's the case with a named parameter.
         for (i, param) in ty.params().iter().enumerate() {
-            params.start_local(names_for.unwrap_or(u32::MAX), i as u32, self, state)?;
+            params.start_local(names_for, i as u32, self, state)?;
             self.print_valtype(state, *param)?;
             params.end_local(self)?;
         }
@@ -1335,7 +1335,7 @@ impl Printer<'_, '_> {
                     self.newline(offset)?;
                     first = false;
                 }
-                locals.start_local(func_idx, params + local_idx, self, state)?;
+                locals.start_local(Some(func_idx), params + local_idx, self, state)?;
                 self.print_valtype(state, ty)?;
                 locals.end_local(self)?;
                 local_idx += 1;
@@ -2914,12 +2914,16 @@ impl NamedLocalPrinter {
 
     fn start_local(
         &mut self,
-        func: u32,
+        func: Option<u32>,
         local: u32,
         dst: &mut Printer,
         state: &State,
     ) -> Result<()> {
-        let name = state.core.local_names.index_to_name.get(&(func, local));
+        let name = state
+            .core
+            .local_names
+            .index_to_name
+            .get(&(func.unwrap_or(u32::MAX), local));
 
         // Named locals must be in their own group, so if we have a name we need
         // to terminate the previous group.
@@ -2949,7 +2953,7 @@ impl NamedLocalPrinter {
                 dst.result.write_str(" ")?;
                 self.end_group_after_local = true;
             }
-            None if dst.config.name_unnamed => {
+            None if dst.config.name_unnamed && func.is_some() => {
                 write!(dst.result, "$#local{local} ")?;
                 self.end_group_after_local = true;
             }


Test patch:
diff --git a/tests/cli/print-unnamed.wat b/tests/cli/print-unnamed.wat
index 33de1cbc..5a8819a3 100644
--- a/tests/cli/print-unnamed.wat
+++ b/tests/cli/print-unnamed.wat
@@ -31,4 +31,11 @@
       )
     )
   )
+
+  (func $foo
+    unreachable
+    block (param i32 f32) (result i32 f32)
+      unreachable
+    end
+  )
 )
diff --git a/tests/cli/print-unnamed.wat.stdout b/tests/cli/print-unnamed.wat.stdout
index ef3483ad..16f92910 100644
--- a/tests/cli/print-unnamed.wat.stdout
+++ b/tests/cli/print-unnamed.wat.stdout
@@ -1,5 +1,6 @@
 (module
   (type $#type0 (;0;) (func))
+  (type $#type1 (;1;) (func (param i32 f32) (result i32 f32)))
   (func $#func0 (;0;) (type $#type0)
     block $a
       block $#label1
@@ -26,4 +27,10 @@
       end
     end
   )
+  (func $foo (;1;) (type $#type0)
+    unreachable
+    block $#label0 (type $#type1) (param i32 f32) (result i32 f32)
+      unreachable
+    end
+  )
 )