bytecodealliance / wasm-tools

CLI and Rust libraries for low-level manipulation of WebAssembly modules
Apache License 2.0
1.3k stars 234 forks source link

wit-parser: the serialized function struct should reserve explicitID #1768

Closed Mossaka closed 3 weeks ago

Mossaka commented 3 weeks ago

Consider the following WIT file

package foo:foo;

interface conventions {
  // Identifiers with the same name as keywords are quoted.
  %bool: func();
}

world the-world {
  import conventions;
  export conventions;
}

The %bool function has a Token::ExplicitId and in parse_id() the symbol % is striped away, resulting the serialized Function struct that has difference between explicitID vs. ID, i.e.

"interfaces": [
    {
      "name": "conventions",
      "types": {},
      "functions": {
        "bool": {
          "name": "bool",
          "kind": "freestanding",
          "params": [],
          "results": [],
          "docs": {
            "contents": "Identifiers with the same name as keywords are quoted."
          }
        }
      },
      "package": 0
    }
  ],

which could be an issue for tools like https://github.com/ydnar/wasm-tools-go because turning the json back to wit is not possible anymore. cc @ydnar

alexcrichton commented 3 weeks ago

Could you expand on what you think the solution to this might be? Are you thinking it would be something like "name": { "raw": "bool" } or similar? To me this looks like the JSON is an accurate representation of the WIT and re-rendering as WIT would require the re-rendering process to understand WIT keywords to re-add % characters

alexcrichton commented 3 weeks ago

(or, alternatively, that's something that wasm-tools could possibly implement too: "ingest this JSON and output WIT"

Mossaka commented 3 weeks ago

yeah I realized a simple solution is to re-add % character back to the WIT keywords in wit-bindgen-go and in fact it's already doing so. So gonna close this issue now. Thanks!

ydnar commented 3 weeks ago

Isn't % encoding only in WIT? Eg the function would be named bool in the resolve struct (here and in Go)? The Go code generation handles collisions with Go keywords (including bool).