Skip to content

Commit 7af24c7

Browse files
dkegel-fastlydeadprogram
authored andcommitted
compiler: allow slices of empty structs.
Fixes #2749
1 parent 3dc1e40 commit 7af24c7

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

compiler/compiler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,11 @@ func (c *compilerContext) maxSliceSize(elementType llvm.Type) uint64 {
15541554
// Determine the maximum allowed size for a slice. The biggest possible
15551555
// pointer (starting from 0) would be maxPointerValue*sizeof(elementType) so
15561556
// divide by the element type to get the real maximum size.
1557-
maxSize := maxPointerValue / c.targetData.TypeAllocSize(elementType)
1557+
elementSize := c.targetData.TypeAllocSize(elementType)
1558+
if elementSize == 0 {
1559+
elementSize = 1
1560+
}
1561+
maxSize := maxPointerValue / elementSize
15581562

15591563
// len(slice) is an int. Make sure the length remains small enough to fit in
15601564
// an int.

testdata/slice.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ func main() {
1919
printslice("foo[1:2]", foo[1:2])
2020
println("sum foo:", sum(foo))
2121

22+
// creating a slice of uncommon base type
23+
assert(len(make([]struct{}, makeInt(4))) == 4)
24+
2225
// creating a slice with uncommon len, cap types
2326
assert(len(make([]int, makeInt(2), makeInt(3))) == 2)
2427
assert(len(make([]int, makeInt8(2), makeInt8(3))) == 2)

0 commit comments

Comments
 (0)