From f44dff6f59782a33ebaa33761643fbbfd6a29cb6 Mon Sep 17 00:00:00 2001 From: Konstantin Yegupov Date: Thu, 3 Jan 2019 08:02:44 +0000 Subject: [PATCH] compiler: allow slicing arrays of a named type --- compiler/compiler.go | 2 +- testdata/slice.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler/compiler.go b/compiler/compiler.go index 4834c19c..9be4f527 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -2334,7 +2334,7 @@ func (c *Compiler) parseExpr(frame *Frame, expr ssa.Value) (llvm.Value, error) { switch typ := expr.X.Type().Underlying().(type) { case *types.Pointer: // pointer to array // slice an array - length := typ.Elem().(*types.Array).Len() + length := typ.Elem().Underlying().(*types.Array).Len() llvmLen := llvm.ConstInt(c.uintptrType, uint64(length), false) if high.IsNil() { high = llvmLen diff --git a/testdata/slice.go b/testdata/slice.go index ad59f7ee..cf0fd216 100644 --- a/testdata/slice.go +++ b/testdata/slice.go @@ -1,5 +1,7 @@ package main +type MySlice [32]byte + func main() { l := 5 foo := []int{1, 2, 4, 5} @@ -89,6 +91,12 @@ func main() { print(" ", n) } println() + + // Verify the fix in https://github.com/aykevl/tinygo/pull/119 + var unnamed [32]byte + var named MySlice + assert(len(unnamed[:]) == 32) + assert(len(named[:]) == 32) } func printslice(name string, s []int) {