Skip to content

WebAssembly if...else doc doesn't mention result statements which seem to be required if both branches have an explicit return #28013

Open
@fyndor

Description

MDN URL

https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Control_flow/if...else

What specific section or headline is this issue about?

No response

What information was incorrect, unhelpful, or incomplete?

In testing the if...else statement. Consider the following WAT:

(func $if_example (param $a i32) (param $b i32) (result i32)
  local.get $a
  local.get $b
  i32.gt_s
  (if
    (then
      local.get $a
      return
    )
    (else
      local.get $b
      return
    )
  )
)

This will fail to compile. It appears that if you have an if/else combo, and both return, then you need a (result i32) inside the if block to appease the compiler. This seems to be because they have values on the stack, and otherwise the compiler expects no values on the stack. I feel like this optional, and sometimes necessary result statement should be mentioned on this page as it is a requirement to fix certain issues.

What did you expect to see?

Text mentioning that when you have values on the stack (including with return statements) in if branches, a result statement is required.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details

Metadata

Assignees

No one assigned

    Labels

    Content:wasmWebAssembly docseffort: mediumThis task is a medium effort.help wantedIf you know something about this topic, we would love your help!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions