Skip to content

Commit

Permalink
compiler: allow slices of empty structs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkegel-fastly authored and ardnew committed Jun 21, 2022
1 parent 33d7110 commit 2eec946
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,11 @@ func (c *compilerContext) maxSliceSize(elementType llvm.Type) uint64 {
// Determine the maximum allowed size for a slice. The biggest possible
// pointer (starting from 0) would be maxPointerValue*sizeof(elementType) so
// divide by the element type to get the real maximum size.
maxSize := maxPointerValue / c.targetData.TypeAllocSize(elementType)
elementSize := c.targetData.TypeAllocSize(elementType)
if elementSize == 0 {
elementSize = 1
}
maxSize := maxPointerValue / elementSize

// len(slice) is an int. Make sure the length remains small enough to fit in
// an int.
Expand Down
3 changes: 3 additions & 0 deletions testdata/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func main() {
printslice("foo[1:2]", foo[1:2])
println("sum foo:", sum(foo))

// creating a slice of uncommon base type
assert(len(make([]struct{}, makeInt(4))) == 4)

// creating a slice with uncommon len, cap types
assert(len(make([]int, makeInt(2), makeInt(3))) == 2)
assert(len(make([]int, makeInt8(2), makeInt8(3))) == 2)
Expand Down

0 comments on commit 2eec946

Please sign in to comment.