Skip to content

Commit c1473ac

Browse files
committed
wit/bindgen: demote bool as a variant or result GC shape
This fixes #284.
1 parent a1d6d88 commit c1473ac

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

wit/bindgen/abi.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"go.bytecodealliance.org/wit"
77
)
88

9-
// variantShape returns the type with the greatest size.
9+
// variantShape returns the type with the greatest size that is not a bool.
1010
// If there are multiple types with the same size, it returns
1111
// the first type that contains a pointer.
1212
func variantShape(types []wit.Type) wit.Type {
@@ -19,6 +19,12 @@ func variantShape(types []wit.Type) wit.Type {
1919
return -1
2020
case a.Size() < b.Size():
2121
return 1
22+
case !isBool(a) && isBool(b):
23+
// bool cannot be used as variant shape
24+
// See https://github.com/bytecodealliance/go-modules/issues/284
25+
return -1
26+
case isBool(a) && !isBool(b):
27+
return 1
2228
case wit.HasPointer(a) && !wit.HasPointer(b):
2329
return -1
2430
case !wit.HasPointer(a) && wit.HasPointer(b):
@@ -30,6 +36,16 @@ func variantShape(types []wit.Type) wit.Type {
3036
return types[0]
3137
}
3238

39+
func isBool(t wit.TypeDefKind) bool {
40+
switch t := t.(type) {
41+
case wit.Bool:
42+
return true
43+
case *wit.TypeDef:
44+
return isBool(t.Root().Kind)
45+
}
46+
return false
47+
}
48+
3349
// variantAlign returns the type with the largest alignment.
3450
func variantAlign(types []wit.Type) wit.Type {
3551
if len(types) == 0 {

0 commit comments

Comments
 (0)