compiler: support creating slices with uncommon initial len/cap types
Этот коммит содержится в:
родитель
9181f2d4ce
коммит
8cb7b583d8
2 изменённых файлов: 27 добавлений и 0 удалений
|
@ -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}, "")
|
||||
}
|
||||
|
||||
|
|
13
testdata/slice.go
предоставленный
13
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)
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче