A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix handling of `if`s without `else`s and subtyping (#14334)
Nick Fitzgerald0675528cca48
diff --git a/crates/cranelift/src/translate/code_translator.rs b/crates/cranelift/src/translate/code_translator.rsindex e86340ec2e..c0c37e96cf 100644--- a/crates/cranelift/src/translate/code_translator.rs+++ b/crates/cranelift/src/translate/code_translator.rs@@ -295,7 +295,17 @@ pub fn translate_operator(let next_block = builder.create_block();let (params, results) = blocktype_params_results(validator, *blockty)?;- let (destination, else_data) = if params.clone().eq(results.clone()) {++ // We don't need an `else` if every parameter is a subtype of the+ // result.+ let resources = validator.resources();+ let else_is_optional = params.len() == results.len()+ && params+ .clone()+ .zip(results.clone())+ .all(|(param, result)| resources.is_subtype(param, result));++ let (destination, else_data) = if else_is_optional {// It is possible there is no `else` block, so we will only// allocate a block for it if/when we find the `else`. For now,// we if the condition isn't true, then we jump directly to thediff --git a/tests/misc_testsuite/gc/issue-14333.wast b/tests/misc_testsuite/gc/issue-14333.wastnew file mode 100644index 0000000000..3fca0fface--- /dev/null+++ b/tests/misc_testsuite/gc/issue-14333.wast@@ -0,0 +1,57 @@+;;! gc = true++;; An `if` needs no `else` when each param is a subtype of its result.++(module+ (type $sup (sub (struct (field i32))))+ (type $sub (sub $sup (struct (field i32) (field i32))))++ (func (export "no-else") (param i32) (result i32)+ (struct.new $sub (i32.const 7) (i32.const 8))+ (local.get 0)+ if (param (ref $sub)) (result (ref $sup))+ drop+ (struct.new $sup (i32.const 100))+ end+ (struct.get $sup 0))++ (func (export "no-else-i31") (param i32) (result i32)+ (ref.i31 (i32.const 33))+ (local.get 0)+ if (param i31ref) (result anyref)+ end+ (ref.cast i31ref)+ (i31.get_s))++ (func (export "with-else") (param i32) (result i32)+ (struct.new $sub (i32.const 7) (i32.const 8))+ (local.get 0)+ if (param (ref $sub)) (result (ref $sup))+ drop+ (struct.new $sup (i32.const 1))+ else+ drop+ (struct.new $sup (i32.const 2))+ end+ (struct.get $sup 0))++ (func (export "else-required") (param i32) (result i32)+ (struct.new $sup (i32.const 5))+ (local.get 0)+ if (param anyref) (result (ref $sup))+ (ref.cast (ref $sup))+ else+ drop+ (struct.new $sup (i32.const 6))+ end+ (struct.get $sup 0))+)++(assert_return (invoke "no-else" (i32.const 0)) (i32.const 7))+(assert_return (invoke "no-else" (i32.const 1)) (i32.const 100))+(assert_return (invoke "no-else-i31" (i32.const 0)) (i32.const 33))+(assert_return (invoke "no-else-i31" (i32.const 1)) (i32.const 33))+(assert_return (invoke "with-else" (i32.const 0)) (i32.const 2))+(assert_return (invoke "with-else" (i32.const 1)) (i32.const 1))+(assert_return (invoke "else-required" (i32.const 0)) (i32.const 6))+(assert_return (invoke "else-required" (i32.const 1)) (i32.const 5))