diff --git a/pkg/dart2wasm/lib/code_generator.dart b/pkg/dart2wasm/lib/code_generator.dart index 38e5219354c..5c3886ed6c4 100644 --- a/pkg/dart2wasm/lib/code_generator.dart +++ b/pkg/dart2wasm/lib/code_generator.dart @@ -3256,6 +3256,9 @@ class TearOffCodeGenerator extends AstCodeGenerator { b.pushObjectHeaderFields(info); b.local_get(paramLocals[0]); // `this` as context + // The closure requires a struct value so box `this` if necessary. + translator.convertType(b, paramLocals[0].type, + struct.fields[FieldIndex.closureContext].type.unpacked); translator.globals.readGlobal(b, closure.vtable); types.makeType(this, functionType); b.struct_new(struct); diff --git a/tests/web/wasm/boxed_value_tearoff_test.dart b/tests/web/wasm/boxed_value_tearoff_test.dart new file mode 100644 index 00000000000..f1be75ba0bc --- /dev/null +++ b/tests/web/wasm/boxed_value_tearoff_test.dart @@ -0,0 +1,10 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:expect/expect.dart'; + +void main() { + final tearoff = (-4).abs; + Expect.equals(tearoff(), 4); +}