From d4e04e4e493aa4a99659361e28b22ecd51cceee3 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 27 Jul 2020 12:35:29 +0200 Subject: [PATCH] compiler: fix named string to []byte slice conversion This was missing a `.Underlying()` call to avoid testing the named type (but instead test for the underlying type). --- compiler/compiler.go | 2 +- testdata/string.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/compiler.go b/compiler/compiler.go index fe45eb3f..7b52cb66 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -2536,7 +2536,7 @@ func (b *builder) createConvert(typeFrom, typeTo types.Type, value llvm.Value, p return llvm.Value{}, b.makeError(pos, "todo: convert: basic non-integer type: "+typeFrom.String()+" -> "+typeTo.String()) case *types.Slice: - if basic, ok := typeFrom.(*types.Basic); !ok || basic.Info()&types.IsString == 0 { + if basic, ok := typeFrom.Underlying().(*types.Basic); !ok || basic.Info()&types.IsString == 0 { panic("can only convert from a string to a slice") } diff --git a/testdata/string.go b/testdata/string.go index 1c5ce630..7eb90173 100644 --- a/testdata/string.go +++ b/testdata/string.go @@ -17,8 +17,11 @@ func testRunesToString(r []rune) { println("string from runes:", string(r)) } +type myString string + func main() { testRangeString() testStringToRunes() testRunesToString([]rune{97, 98, 99, 252, 162, 8364, 66376, 176, 120}) + var _ = len([]byte(myString("foobar"))) // issue 1246 }