Skip to content

specification: composite literal example invalid after gofmt #1243

@peterGo

Description

@peterGo
A parsing ambiguity arises when a composite literal using the TypeName form of the
LiteralType appears between the keyword and the opening brace of the block of an
"if", "for", or "switch" statement, because the braces
surrounding the expressions in the literal are confused with those introducing the block
of statements. To resolve the ambiguity in this rare case, the composite literal must
appear within parentheses.

   if x == (T{a,b,c}[i]) { ... }
   if (x == T{a,b,c}[i]) { ... }

Composite literals, The Go Programming Language Specification.
http://golang.org/doc/go_spec.html#Composite_literals

The second example is invalid because it does not survive gofmt.

package main

type T [3]int

func main() {
    var a, b, c, i, x int
    if (x == T{a, b, c}[i]) {
    }
}

After gofmt -w,

package main

type T [3]int

func main() {
    var a, b, c, i, x int
    if x == T{a, b, c}[i] {
    }
}

6g error:
7: syntax error: unexpected }, expecting := or = or comma

hg id 4c76cc96e113 tip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions