From df6221ed921293de74d22bd37061f0a9860046da Mon Sep 17 00:00:00 2001 From: visualfc Date: Thu, 26 Oct 2023 09:39:14 +0800 Subject: [PATCH 1/2] parser: parsePrimaryExpr fix LiteralValue check --- parser/parser.go | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index 6a9100d9d..5914f55bd 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -2068,23 +2068,9 @@ func (p *parser) checkExpr(x ast.Expr) ast.Expr { return x } -// isTypeName reports whether x is a (qualified) TypeName. -func isTypeName(x ast.Expr) bool { - switch t := x.(type) { - case *ast.BadExpr: - case *ast.Ident: - case *ast.SelectorExpr: - _, isIdent := t.X.(*ast.Ident) - return isIdent - default: - return false // all other nodes are not type names - } - return true -} - // isLiteralType reports whether x is a legal composite literal type. func isLiteralType(x ast.Expr) bool { - switch t := x.(type) { + switch t := unparen(x).(type) { case *ast.BadExpr: case *ast.Ident: case *ast.IndexExpr: @@ -2188,7 +2174,7 @@ L: case token.LBRACE: // { if allowCmd && p.isCmd(x) { // println {...} x = p.parseCallOrConversion(p.checkExprOrType(x), true) - } else if isLiteralType(x) && (p.exprLev >= 0 || !isTypeName(x)) { + } else if isLiteralType(x) && p.exprLev >= 0 { if lhs { p.resolve(x) } From 3363ba1cc35b08585e2a7bbee25b9c0ecad966a6 Mon Sep 17 00:00:00 2001 From: visualfc Date: Thu, 26 Oct 2023 09:45:45 +0800 Subject: [PATCH 2/2] parser: add test _instance/instance5 --- parser/_instance/instance5/cmd.gop | 6 ++ parser/_instance/instance5/parser.expect | 115 +++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 parser/_instance/instance5/cmd.gop create mode 100644 parser/_instance/instance5/parser.expect diff --git a/parser/_instance/instance5/cmd.gop b/parser/_instance/instance5/cmd.gop new file mode 100644 index 000000000..b693b560a --- /dev/null +++ b/parser/_instance/instance5/cmd.gop @@ -0,0 +1,6 @@ +var a [2]int +if 0 < a[0] { + println a + println T[int]{1} + println (T[int]){a:1,b:2} +} diff --git a/parser/_instance/instance5/parser.expect b/parser/_instance/instance5/parser.expect new file mode 100644 index 000000000..1dc92068c --- /dev/null +++ b/parser/_instance/instance5/parser.expect @@ -0,0 +1,115 @@ +package main + +file cmd.gop +noEntrypoint +ast.GenDecl: + Tok: var + Specs: + ast.ValueSpec: + Names: + ast.Ident: + Name: a + Type: + ast.ArrayType: + Len: + ast.BasicLit: + Kind: INT + Value: 2 + Elt: + ast.Ident: + Name: int +ast.FuncDecl: + Name: + ast.Ident: + Name: main + Type: + ast.FuncType: + Params: + ast.FieldList: + Body: + ast.BlockStmt: + List: + ast.IfStmt: + Cond: + ast.BinaryExpr: + X: + ast.BasicLit: + Kind: INT + Value: 0 + Op: < + Y: + ast.IndexExpr: + X: + ast.Ident: + Name: a + Index: + ast.BasicLit: + Kind: INT + Value: 0 + Body: + ast.BlockStmt: + List: + ast.ExprStmt: + X: + ast.CallExpr: + Fun: + ast.Ident: + Name: println + Args: + ast.Ident: + Name: a + ast.ExprStmt: + X: + ast.CallExpr: + Fun: + ast.Ident: + Name: println + Args: + ast.CompositeLit: + Type: + ast.IndexExpr: + X: + ast.Ident: + Name: T + Index: + ast.Ident: + Name: int + Elts: + ast.BasicLit: + Kind: INT + Value: 1 + ast.ExprStmt: + X: + ast.CallExpr: + Fun: + ast.Ident: + Name: println + Args: + ast.CompositeLit: + Type: + ast.ParenExpr: + X: + ast.IndexExpr: + X: + ast.Ident: + Name: T + Index: + ast.Ident: + Name: int + Elts: + ast.KeyValueExpr: + Key: + ast.Ident: + Name: a + Value: + ast.BasicLit: + Kind: INT + Value: 1 + ast.KeyValueExpr: + Key: + ast.Ident: + Name: b + Value: + ast.BasicLit: + Kind: INT + Value: 2