Skip to content

Commit d1eb76f

Browse files
Added built-in support for unsafe.SliceData
1 parent 7a7beaa commit d1eb76f

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

compiler/expressions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,12 @@ func (fc *funcContext) translateBuiltin(name string, sig *types.Signature, args
10581058
case "Offsetof":
10591059
sel, _ := fc.selectionOf(astutil.RemoveParens(args[0]).(*ast.SelectorExpr))
10601060
return fc.formatExpr("%d", typesutil.OffsetOf(sizes32, sel))
1061+
case "SliceData":
1062+
// SliceData returns nil if the slice is nil, otherwise returns a pointer to the first index of the array, &s[0].
1063+
// If the slice is empty (cap == 0), it returns an "unspecified memory address" or in our case, a pointer to the empty array.
1064+
t := fc.typeOf(args[0]).Underlying().(*types.Slice)
1065+
elemPtrType := types.NewPointer(t.Elem())
1066+
return fc.formatExpr("(%1e === %2s.nil) ? %3s.nil : $indexPtr(%1e.$array, %1e.$offset, %3s)", args[0], fc.typeName(t), fc.typeName(elemPtrType))
10611067
default:
10621068
panic(fmt.Sprintf("Unhandled builtin: %s\n", name))
10631069
}

0 commit comments

Comments
 (0)