Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: parsePrimaryExpr fix LiteralValue check #1493

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions parser/_instance/instance5/cmd.gop
Original file line number Diff line number Diff line change
@@ -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}
}
115 changes: 115 additions & 0 deletions parser/_instance/instance5/parser.expect
Original file line number Diff line number Diff line change
@@ -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
18 changes: 2 additions & 16 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
}
Expand Down