Repository: goplus/gop
Pull request #523 (2020-08-03 23:30:41)

cl: fix array index bug #521

codecov-commenter: # [Codecov](https://codecov.io/gh/goplus/gop/pull/523?src=pr&el=h1) Report
> Merging [#523](https://codecov.io/gh/goplus/gop/pull/523?src=pr&el=desc) into [master](https://codecov.io/gh/goplus/gop/commit/6e8cd12d117150135210a1d65104a9a3130214b9&el=desc) will **decrease** coverage by `0.05%`.
> The diff coverage is `55.55%`.

[![Impacted file tree graph](https://codecov.io/gh/goplus/gop/pull/523/graphs/tree.svg?width=650&height=150&src=pr&token=4qln9Wk3Pn)](https://codecov.io/gh/goplus/gop/pull/523?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #523      +/-   ##
==========================================
- Coverage   89.43%   89.38%   -0.06%     
==========================================
  Files          40       40              
  Lines        9819     9828       +9     
==========================================
+ Hits         8782     8785       +3     
- Misses        757      761       +4     
- Partials      280      282       +2     
```


| [Impacted Files](https://codecov.io/gh/goplus/gop/pull/523?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [cl/expr.go](https://codecov.io/gh/goplus/gop/pull/523/diff?src=pr&el=tree#diff-Y2wvZXhwci5nbw==) | `83.70% <55.55%> (-0.34%)` | :arrow_down: |
| [exec/bytecode/goinstr.go](https://codecov.io/gh/goplus/gop/pull/523/diff?src=pr&el=tree#diff-ZXhlYy9ieXRlY29kZS9nb2luc3RyLmdv) | `92.87% <0.00%> (-0.53%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goplus/gop/pull/523?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goplus/gop/pull/523?src=pr&el=footer). Last update [6e8cd12...bfd9502](https://codecov.io/gh/goplus/gop/pull/523?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/cl/expr.go b/cl/expr.go
index f982945b..6589d12d 100644
--- a/cl/expr.go
+++ b/cl/expr.go
@@ -721,6 +721,14 @@ func compileIndexExprLHS(ctx *blockCtx, v *ast.IndexExpr, mode compileMode) {
 
 	typ := ctx.infer.Get(-1).(iValue).Type()
 	typElem := typ.Elem()
+	if typ.Kind() == reflect.Ptr {
+		if typElem.Kind() != reflect.Array {
+			logPanic(ctx, v, `type %v does not support indexing`, typ)
+		}
+		typ = typElem
+		typElem = typElem.Elem()
+	}
+
 	if cons, ok := val.(*constVal); ok {
 		cons.bound(typElem, ctx.out)
 	} else if t := val.(iValue).Type(); t != typElem {
@@ -821,6 +829,12 @@ func compileIndexExpr(ctx *blockCtx, v *ast.IndexExpr) func() { // x[i]
 	x := ctx.infer.Get(-1)
 	typ := x.(iValue).Type()
 	kind = typ.Kind()
+	if kind == reflect.Ptr {
+		typ = typ.Elem()
+		if kind = typ.Kind(); kind != reflect.Array {
+			logPanic(ctx, v, `type *%v does not support indexing`, typ)
+		}
+	}
 	if kind == reflect.String {
 		typElem = exec.TyByte
 	} else {


Test patch:
diff --git a/cl/expr_test.go b/cl/expr_test.go
index b849c54a..73a3b20e 100644
--- a/cl/expr_test.go
+++ b/cl/expr_test.go
@@ -46,7 +46,7 @@ func TestNew(t *testing.T) {
 	)
 }
 
-func _TestNew2(t *testing.T) {
+func TestNew2(t *testing.T) {
 	cltest.Expect(t, `
 		a := new([2]int)
 		a[0] = 2
@@ -54,9 +54,40 @@ func _TestNew2(t *testing.T) {
 		`,
 		"a: 2\n",
 	)
+	cltest.Expect(t, `
+		a := new([2]float64)
+		a[0] = 1.1
+		println("a:", a[0])
+		`,
+		"a: 1.1\n",
+	)
+	cltest.Expect(t, `
+		a := new([2]string)
+		a[0] = "gop"
+		println("a:", a[0])
+		`,
+		"a: gop\n",
+	)
 }
 
-// -----------------------------------------------------------------------------
+func TestBadIndex(t *testing.T) {
+	cltest.Expect(t, `
+		a := new(int)
+		println(a[0])
+		`,
+		"",
+		nil,
+	)
+	cltest.Expect(t, `
+		a := new(int)
+		a[0] = 2
+		`,
+		"",
+		nil,
+	)
+}
+
+// -------------------`----------------------------------------------------------
 
 func TestAutoProperty(t *testing.T) {
 	script := `