Skip to content

Confusing error message for ambiguous syntax, gofmt removes the fix #1071

@gopherbot

Description

@gopherbot

by consalus:

GOOS=darwin
GOARCH=amd64
hg identify: e785d68bf901+ tip

This is probably working as intended, but I think the error message is a bit misleading,
and one
of two fixes is reverted by gofmt, so it seems like something could be improved
somewhere..

With the following code: 

package main

import (
    "container/vector"
)


func main() {
    var vec vector.Vector
    for _, _ = range []interface{}(vec) {
    }
}

The compiler gives the error:

testcase.go:10: syntax error: unexpected {, expecting {

That is a bit confusing.
As an attempt at a fix (based on the part of the spec that talks about the syntactical
ambiguity there), I change the for loop to:

for _, _ = range ([]interface{}(vec)) {
}

Not unreasonable, and it compiles without error now. However, when I run gofmt, the
parens 
get removed, and I get the compile error again.
Of course, the solution is to put the parenthesis around the type only, making it:

for _, _ = range ([]interface{})(vec) {
}

However, it's a confusing process to get from the first version of the code to this
correct one.
Perhaps gofmt should not remove parenthesis if it can determine they are resolving an
ambiguity, or move them to the minimal scope required to keep it unambiguous. 
Or, failing that, having the syntax error recommended that the type be in parens would
also probably be better.

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