compiler: support any int type in slice indexes

Make sure the compiler will correctly compile indexes of type uint64,
for example.
This commit is contained in:
Ayke van Laethem
2018-11-14 14:41:40 +01:00
parent ee7c276493
commit 9bddaae04a
3 changed files with 104 additions and 22 deletions
+46
View File
@@ -10,6 +10,46 @@ func main() {
printslice("foo[1:2]", foo[1:2])
println("sum foo:", sum(foo))
// indexing into a slice with uncommon index types
assert(foo[int(2)] == 4)
assert(foo[int8(2)] == 4)
assert(foo[int16(2)] == 4)
assert(foo[int32(2)] == 4)
assert(foo[int64(2)] == 4)
assert(foo[uint(2)] == 4)
assert(foo[uint8(2)] == 4)
assert(foo[uint16(2)] == 4)
assert(foo[uint32(2)] == 4)
assert(foo[uint64(2)] == 4)
assert(foo[uintptr(2)] == 4)
// slicing with uncommon low, high types
assert(len(foo[int(1):int(3)]) == 2)
assert(len(foo[int8(1):int8(3)]) == 2)
assert(len(foo[int16(1):int16(3)]) == 2)
assert(len(foo[int32(1):int32(3)]) == 2)
assert(len(foo[int64(1):int64(3)]) == 2)
assert(len(foo[uint(1):uint(3)]) == 2)
assert(len(foo[uint8(1):uint8(3)]) == 2)
assert(len(foo[uint16(1):uint16(3)]) == 2)
assert(len(foo[uint32(1):uint32(3)]) == 2)
assert(len(foo[uint64(1):uint64(3)]) == 2)
assert(len(foo[uintptr(1):uintptr(3)]) == 2)
// slicing an array with uncommon low, high types
arr := [4]int{1, 2, 4, 5}
assert(len(arr[int(1):int(3)]) == 2)
assert(len(arr[int8(1):int8(3)]) == 2)
assert(len(arr[int16(1):int16(3)]) == 2)
assert(len(arr[int32(1):int32(3)]) == 2)
assert(len(arr[int64(1):int64(3)]) == 2)
assert(len(arr[uint(1):uint(3)]) == 2)
assert(len(arr[uint8(1):uint8(3)]) == 2)
assert(len(arr[uint16(1):uint16(3)]) == 2)
assert(len(arr[uint32(1):uint32(3)]) == 2)
assert(len(arr[uint64(1):uint64(3)]) == 2)
assert(len(arr[uintptr(1):uintptr(3)]) == 2)
// copy
println("copy foo -> bar:", copy(bar, foo))
printslice("bar", bar)
@@ -53,3 +93,9 @@ func sum(l []int) int {
}
return sum
}
func assert(ok bool) {
if !ok {
panic("assert failed")
}
}