diff --git a/compiler/compiler.go b/compiler/compiler.go index 83527c31..f31e9481 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -2420,6 +2420,20 @@ func (c *Compiler) parseExpr(frame *Frame, expr ssa.Value) (llvm.Value, error) { // Bounds checking. if !frame.fn.IsNoBounds() { + if sliceLen.Type().IntTypeWidth() < c.uintptrType.IntTypeWidth() { + if expr.Len.Type().(*types.Basic).Info()&types.IsUnsigned != 0 { + sliceLen = c.builder.CreateZExt(sliceLen, c.uintptrType, "") + } else { + sliceLen = c.builder.CreateSExt(sliceLen, c.uintptrType, "") + } + } + if sliceCap.Type().IntTypeWidth() < c.uintptrType.IntTypeWidth() { + if expr.Cap.Type().(*types.Basic).Info()&types.IsUnsigned != 0 { + sliceCap = c.builder.CreateZExt(sliceCap, c.uintptrType, "") + } else { + sliceCap = c.builder.CreateSExt(sliceCap, c.uintptrType, "") + } + } c.createRuntimeCall("sliceBoundsCheckMake", []llvm.Value{sliceLen, sliceCap}, "") } diff --git a/testdata/slice.go b/testdata/slice.go index 6237a015..ad59f7ee 100644 --- a/testdata/slice.go +++ b/testdata/slice.go @@ -10,6 +10,19 @@ func main() { printslice("foo[1:2]", foo[1:2]) println("sum foo:", sum(foo)) + // creating a slice with uncommon len, cap types + assert(len(make([]int, int(2), int(3))) == 2) + assert(len(make([]int, int8(2), int8(3))) == 2) + assert(len(make([]int, int16(2), int16(3))) == 2) + assert(len(make([]int, int32(2), int32(3))) == 2) + assert(len(make([]int, int64(2), int64(3))) == 2) + assert(len(make([]int, uint(2), uint(3))) == 2) + assert(len(make([]int, uint8(2), uint8(3))) == 2) + assert(len(make([]int, uint16(2), uint16(3))) == 2) + assert(len(make([]int, uint32(2), uint32(3))) == 2) + assert(len(make([]int, uint64(2), uint64(3))) == 2) + assert(len(make([]int, uintptr(2), uintptr(3))) == 2) + // indexing into a slice with uncommon index types assert(foo[int(2)] == 4) assert(foo[int8(2)] == 4)