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

cmd/compile: fail to capture invalid field name in struct initializer #15311

Closed
mikioh opened this issue Apr 15, 2016 · 4 comments
Closed

cmd/compile: fail to capture invalid field name in struct initializer #15311

mikioh opened this issue Apr 15, 2016 · 4 comments
Milestone

Comments

@mikioh
Copy link
Contributor

mikioh commented Apr 15, 2016

The following snippet can compile with tip,

package main

type T struct {
    toInt    map[string]int
    toString map[int]string
}

var t = T{
    foo.toInt:    make(map[string]int),
    bar.toString: make(map[int]string),
}

func main() {}

though, go1.4 fails with the following:

./typ.go:9: invalid field name foo.toInt in struct initializer
./typ.go:10: invalid field name bar.toString in struct initializer
@mikioh mikioh added this to the Go1.7 milestone Apr 15, 2016
@bradfitz
Copy link
Contributor

Nice find! Indeed, it's a recent regression:

bradfitz@laptop ~$ cat x.go
package main

import "fmt"

type T struct {
        toInt    map[string]int
        toString map[int]string
}

var t = T{
        foo.toInt:    make(map[string]int),
        bar.toString: make(map[int]string),
}

func main() {
        fmt.Printf("Got: %#v\n", t)
}

bradfitz@laptop ~$ GOROOT=$HOME/go1.4 go run x.go
# command-line-arguments
./x.go:11: invalid field name foo.toInt in struct initializer
./x.go:12: invalid field name bar.toString in struct initializer

bradfitz@laptop ~$ GOROOT=$HOME/go1.5 go run x.go
# command-line-arguments
./x.go:11: invalid field name foo.toInt in struct initializer
./x.go:12: invalid field name bar.toString in struct initializer

bradfitz@laptop ~$ GOROOT=$HOME/go1.6 go run x.go
# command-line-arguments
./x.go:11: invalid field name foo.toInt in struct initializer
./x.go:12: invalid field name bar.toString in struct initializer

bradfitz@laptop ~$ GOROOT=$HOME/go go run x.go
Got: main.T{toInt:map[string]int{}, toString:map[int]string{}}

/cc @josharian @mdempsky @griesemer

@mdempsky
Copy link
Member

Regression introduced by 5f525ca.

/cc @ianlancetaylor

@mdempsky mdempsky self-assigned this Apr 16, 2016
@mdempsky
Copy link
Member

mdempsky commented Apr 16, 2016

This is a little tricky, because the parser has already resolved (or tried to resolve) names, including package-qualified names as well as dot-imported names. In particular, I'm not sure off hand how to best handle:

package p

import (
    "errors"
    . "unsafe"
)

type T struct {
    New     int
    Pointer int
}

var t = T{
    errors.New: 1,  // ERROR
    Pointer: 2,     // ok
}

because at this point errors.New and Pointer have both been parsed as references to the imported ONAME and OTYPE Nodes (respectively). I'm not sure there's a way to distinguish whether the name was qualified or not.

Relatedly, I discovered that cmd/compile erroneously accepts

 package p
 import . "fmt"
 type T struct { Println int }
 var t = T{ Println: 0 }

even though the fmt import isn't actually used. The parser is misrecognizing the "Println: 0" as a use of fmt.Println and marking fmt as used.

(gccgo-4.8 also accepts it; gotype correctly rejects it.)

@gopherbot
Copy link

CL https://golang.org/cl/22162 mentions this issue.

@golang golang locked and limited conversation to collaborators Apr 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants