Repository: goplus/gop
Pull request #595 (2020-09-13 04:04:55)

slice lit has only one element
fix: https://github.com/goplus/gop/issues/429

Previous pr：https://github.com/goplus/gop/pull/464 has a problem:

Use the following code to verify that there is a problem:

```
println([1]int{1}[0])
```


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

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

```diff
@@            Coverage Diff             @@
##           master     #595      +/-   ##
==========================================
- Coverage   90.33%   90.32%   -0.02%     
==========================================
  Files          41       41              
  Lines       10620    10623       +3     
==========================================
+ Hits         9594     9595       +1     
- Misses        737      738       +1     
- Partials      289      290       +1     
```


| [Impacted Files](https://codecov.io/gh/goplus/gop/pull/595?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [parser/parser.go](https://codecov.io/gh/goplus/gop/pull/595/diff?src=pr&el=tree#diff-cGFyc2VyL3BhcnNlci5nbw==) | `80.02% <50.00%> (-0.09%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goplus/gop/pull/595?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/595?src=pr&el=footer). Last update [4eb9e66...deee2ea](https://codecov.io/gh/goplus/gop/pull/595?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/parser/parser.go b/parser/parser.go
index 470f8ad7..30de4f76 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -702,9 +702,13 @@ func (p *parser) parseArrayTypeOrSliceLit(allowSliceLit bool) (expr ast.Expr, is
 
 	var elt ast.Expr
 	if allowSliceLit {
+		sliceLit := newSliceLit(lbrack, rbrack, len)
+		if p.tok == token.LBRACK { // idx of slice lit
+			return sliceLit, true
+		}
 		elt = p.tryType()
 		if elt == nil { // [a]
-			return newSliceLit(lbrack, rbrack, len), true
+			return sliceLit, true
 		}
 	} else {
 		elt = p.parseType()


Test patch:
diff --git a/parser/parser_test.go b/parser/parser_test.go
index 83b1676e..acbe74c1 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -252,3 +252,33 @@ func TestFromTestdata2(t *testing.T) {
 }
 
 // -----------------------------------------------------------------------------
+
+var fsTestArray = asttest.NewSingleFileFS("/foo", "bar.gop", `
+println([1][0])
+println([1,2][0])
+`)
+
+func TestArray(t *testing.T) {
+	fset := token.NewFileSet()
+	pkgs, err := ParseFSDir(fset, fsTestArray, "/foo", nil, 0)
+	if err != nil || len(pkgs) != 1 {
+		t.Fatal("ParseFSDir failed:", err, len(pkgs))
+	}
+	bar := pkgs["main"]
+	file := bar.Files["/foo/bar.gop"]
+	fmt.Println("Pkg:", file.Name)
+	for _, decl := range file.Decls {
+		fmt.Println("decl:", reflect.TypeOf(decl))
+		switch d := decl.(type) {
+		case *ast.GenDecl:
+			for _, spec := range d.Specs {
+				switch v := spec.(type) {
+				case *ast.ImportSpec:
+					fmt.Println(" - import:", v.Path.Value)
+				}
+			}
+		case *ast.FuncDecl:
+			fmt.Println(" - func:", d.Name.Name)
+		}
+	}
+}