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

spec: empty vs absent struct field tag #15412

Closed
mdempsky opened this Issue Apr 22, 2016 · 7 comments

Comments

Projects
None yet
4 participants
@mdempsky
Member

mdempsky commented Apr 22, 2016

Go spec says

Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names, and identical types, and identical tags.

What does it mean for tags to be identical? In particular, is an absent tag identical to an empty tag?

cmd/compile rejects this code, but gotype and gccgo-4.8 accept it:

package p
var _ *struct{ x int } = (*struct{ x int "" })(nil)

Package reflect's StructField doesn't provide a way to distinguish absent from empty tags, but then it also agrees with cmd/compile that the above types are not assignable to each other: http://play.golang.org/p/QNISntvhWb

/cc @griesemer

@ianlancetaylor

This comment has been minimized.

Show comment
Hide comment
@ianlancetaylor

ianlancetaylor Apr 22, 2016

Contributor

See also #6858 .

Contributor

ianlancetaylor commented Apr 22, 2016

See also #6858 .

@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Apr 22, 2016

@griesemer griesemer self-assigned this Apr 22, 2016

@ianlancetaylor

This comment has been minimized.

Show comment
Hide comment
@ianlancetaylor

ianlancetaylor Apr 22, 2016

Contributor

Marking this as Go 1.7 because the compilers disagree. We should at least figure out what the correct behavior is.

gccgo has ignored empty field tags since the feature was added to the compiler in 2008.

Contributor

ianlancetaylor commented Apr 22, 2016

Marking this as Go 1.7 because the compilers disagree. We should at least figure out what the correct behavior is.

gccgo has ignored empty field tags since the feature was added to the compiler in 2008.

@griesemer

This comment has been minimized.

Show comment
Hide comment
@griesemer

griesemer Apr 22, 2016

Contributor

This, together with #6858 is on my list to be addressed soon - probably once we are frozen for 1.7 (changes to this are not going to make it into 1.7 either way, and we have lived with this for years now). I agree this needs to be resolved.

Contributor

griesemer commented Apr 22, 2016

This, together with #6858 is on my list to be addressed soon - probably once we are frozen for 1.7 (changes to this are not going to make it into 1.7 either way, and we have lived with this for years now). I agree this needs to be resolved.

@mdempsky

This comment has been minimized.

Show comment
Hide comment
@mdempsky

mdempsky Apr 22, 2016

Member

My 2c: We should treat empty and absent tags as identical. Package reflect's current API makes it difficult to extend to support distinguishing them: we can't add new fields to reflect.StructField without breaking backwards compatibility, so we would need to do something clunky like a FieldHasTag(i int) bool method to reflect.Type.

There also seems to be little value in distinguishing them. The conventional usage of struct tags involve key-value pairs, for which empty and absent tags can be treated identically anyway.

Member

mdempsky commented Apr 22, 2016

My 2c: We should treat empty and absent tags as identical. Package reflect's current API makes it difficult to extend to support distinguishing them: we can't add new fields to reflect.StructField without breaking backwards compatibility, so we would need to do something clunky like a FieldHasTag(i int) bool method to reflect.Type.

There also seems to be little value in distinguishing them. The conventional usage of struct tags involve key-value pairs, for which empty and absent tags can be treated identically anyway.

@griesemer

This comment has been minimized.

Show comment
Hide comment
@griesemer

griesemer Apr 22, 2016

Contributor

go/types doesn't distinguish between absent and empty tag strings. It would need an internal change of representation and probably an API change.

Contributor

griesemer commented Apr 22, 2016

go/types doesn't distinguish between absent and empty tag strings. It would need an internal change of representation and probably an API change.

@griesemer

This comment has been minimized.

Show comment
Hide comment
@griesemer

griesemer Apr 22, 2016

Contributor

Actually, the spec is already clear: See http://tip.golang.org/ref/spec#Struct_types, first example:

struct {
    x, y float64 ""  // an empty tag string is like an absent tag
    name string  "any string is permitted as a tag"
    _    [4]byte "ceci n'est pas un champ de structure"
}

Comment on first fields: an empty tag string is like an absent tag

I'll add an additional sentence to the prose.

Contributor

griesemer commented Apr 22, 2016

Actually, the spec is already clear: See http://tip.golang.org/ref/spec#Struct_types, first example:

struct {
    x, y float64 ""  // an empty tag string is like an absent tag
    name string  "any string is permitted as a tag"
    _    [4]byte "ceci n'est pas un champ de structure"
}

Comment on first fields: an empty tag string is like an absent tag

I'll add an additional sentence to the prose.

@gopherbot

This comment has been minimized.

Show comment
Hide comment
@gopherbot

gopherbot commented Apr 23, 2016

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

@gopherbot gopherbot closed this in 0436a89 Apr 25, 2016

@golang golang locked and limited conversation to collaborators Apr 25, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.