Skip to content

cmd/compile: clarify prose/examples regarding struct identity when embedded types match equally named non-embedded fields #69472

Closed

Description

@leabit

Go version

go version go1.23.0 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/leabit/.cache/go-build'
GOENV='/home/leabit/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/leabit/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/leabit/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/leabit/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.0.linux-amd64'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/leabit/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.0.linux-amd64/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/leabit/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/leabit/goprojects/exeexe/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1849554432=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Golang specification for type identity states the following for the struct types:

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. Non-exported field names from different packages are always different.

Thus, it has 4 requirements:

  1. same sequence of fields
  2. same corresponding names
  3. identical types of corresponding fields
  4. identical tags of corresponding fields

However, there are cases related to embedding aliases of unnamed types where, despite meeting all four previous requirements, the compiler still fails to compile.

I will explain the problem through few examples. The following notes holds for all examples:

  • three packages are used: pac1, pac2 and main
  • declared alias types in pac and pac2 are unnamed types struct{} (thus, they are identical)
  • struct tags aren't used, so they are ignored

(1) This example successfully compiles (as expected). M1 and M2 have the same sequence of fields. Names, even thought not explicitly defined, are indirectly given by embedding and thus they are same (MyType1). Types of the fields (field MyType1) are also same (struct{}).

pac1:

package pac1

type MyType1 = struct{}

pac2:

package pac2

type MyType1 = struct{}

main:

package main

import (
	"exeexe/pac1"
	"exeexe/pac2"
)

type M1 struct {
	pac1.MyType1
}

type M2 struct {
	pac2.MyType1
}

func main() {
	_ = M2(M1{})
}

(2) In this example MyType1 = struct{} is replaced with MyType2 = struct{} in pac2. Also, pac2.MyType2 is now embedded in M2 (instead of pac2.MyType1) in main package. As expected, it doesn't compile. Even thought the types of the fields are the same, the names are not (MyType1 != MyType2), thus requirement 2 doesn't hold.

pac1:

package pac1

type MyType1 = struct{}

pac2:

package pac2

type MyType2 = struct{}

main:

package main

import (
	"exeexe/pac1"
	"exeexe/pac2"
)

type M1 struct {
	pac1.MyType1
}

type M2 struct {
	pac2.MyType2
}

func main() {
	_ = M2(M1{})
}

(3) This example shows the actual problem. The only difference compared to the previous example is that embedded field (pac2.MyType2) is now replaced by non-embedded (ordinary) field with the name MyType1 (type is the same). Since specification for type identity doesn't make a distinct between embedded and ordinary field in the struct this should be valid and compile (all requirements are met).

pac1:

package pac1

type MyType1 = struct{}

pac2:

package pac2

type MyType2 = struct{}

main:

package main

import (
	"exeexe/pac1"
	"exeexe/pac2"
)

type M1 struct {
	pac1.MyType1
}

type M2 struct {
	MyType1 pac2.MyType2
}

func main() {
	_ = M2(M1{})
}

(4) This example just shows that even when MyType1 pac2.MyType2 is replaced with MyType1 pac1.MyType1 in M2 (so just the name is added), it still doesn't compile.

pac1:

package pac1

type MyType1 = struct{}

pac2:

package pac2

type MyType2 = struct{}

main:

package main

import (
	"exeexe/pac1"
)

type M1 struct {
	pac1.MyType1
}

type M2 struct {
	MyType1 pac1.MyType1
}

func main() {
	_ = M2(M1{})
}

What did you see happen?

Inconsistency between specification and implementation

What did you expect to see?

Since I am following specification, I expect examples (3) and (4) to compile, or to modify the specification to address the difference.

Activity

  1. zigo101 commented on Sep 14, 2024

    @zigo101
    Contributor

    A clever finding.

    BTW, it looks your code doesn't prove type identity, but conversion rules. Though I believe that your point is described well and it applies to conversions too.

  2. zigo101 commented on Sep 14, 2024

    @zigo101
    Contributor

    The code is used to prove type identity:

    package main
    
    type MyType1 = struct{}
    
    type M1 = struct {
    	MyType1
    }
    
    type M2 = struct {
    	MyType1 MyType1
    }
    
    func main() {
    	indentify := any(M1{}) == any(M2{})
    	println(indentify) // false
    }
    
  3. leabit commented on Sep 14, 2024

    @leabit
    Author

    @zigo101 Right, I just used conversion as a quick way to check type identity equality. According to specification if two types are identical, values of these types can be assigned to each other. Further, if they are assignable, conversion is possible.

  4. leabit commented on Sep 14, 2024

    @leabit
    Author
  5. cuonglm commented on Sep 14, 2024

    @cuonglm
    Member

    As I understand the spec, in case of (3) and (4), M1 and M2 does not have the same sequence of fields, because MyType1 of M1 is an embedded field, while MyType1 of M2 is not. These fields have the same name but are different fields.

  6. leabit commented on Sep 14, 2024

    @leabit
    Author

    BTW, if I use a @zigo101 way of checking type identity, then even the first example fails (it prints false). However, the conversion is successful (as already stated).

    @cuonglm Specification doesn't make a distinction between embedded and ordinary fields. From spec:

    Promoted fields act like ordinary fields of a struct except that they cannot be used as field names in composite literals of the struct.

    Even if you are right, then the first example with indentify := any(M1{}) == any(M2{}) must print true.

  7. zigo101 commented on Sep 14, 2024

    @zigo101
    Contributor

    The problem shown in the first example should be implementation bug in my understanding.

  8. cuonglm commented on Sep 14, 2024

    @cuonglm
    Member

    Even if you are right, then the first example with indentify := any(M1{}) == any(M2{}) must print true.

    I don't get it.

    You can't do M1{} == M2{} because they are distincted types, thus any(M1{}) == any(M2{}) should print false IMHO.

  9. cuonglm commented on Sep 14, 2024

    @cuonglm
    Member

    Specification doesn't make a distinction between embedded and ordinary fields. From spec:

    Don't that your quote show the difference? "act like" does not mean they are the same fields, causing M1 and M2 don't have the same sequence of fields

  10. zigo101 commented on Sep 14, 2024

    @zigo101
    Contributor

    Aha, @cuonglm is right here. M1 and M2 and not alias declarations. So they are distinct types. No problem is shown in example 1.

    [edit] When they are alias declarations, then M1 and M2 does denote the same type: https://go.dev/play/p/nR3-nts_kc7

  11. leabit commented on Sep 14, 2024

    @leabit
    Author

    Then how is conversion possible?

  12. cuonglm commented on Sep 14, 2024

    @cuonglm
    Member

    Then how is conversion possible?

    It's the same manner with converting from a float to a int, following a set of rules, which you can read here: https://go.dev/ref/spec#Conversions

  13. zigo101 commented on Sep 14, 2024

    @zigo101
    Contributor

    Because in example 1, M1 and M2 shares the same underlying type.

    The point (inaccuracy in spec) mentioned by @leabit can still be demonstrated in this example: #69472 (comment)

  14. cuonglm commented on Sep 14, 2024

    @cuonglm
    Member

    Because in example 1, M1 and M2 shares the same underlying type.

    The point (inaccuracy in spec) mentioned by @leabit can still be demonstrated in this example: #69472 (comment)

    As I explained above, M1 and M2 don't have the same sequence of fields. If you laid them out literally, M1 is:

    struct{main.MyType1}
    

    while M2 is:

    struct{MyType1 main.MyType1}
    

    They don't satisfy the type identity definition in spec:

    two types are identical if their underlying type literals are structurally equivalent; that is, they have the same literal structure and corresponding components have identical types

  15. 22 remaining items

  16. added this to the Go1.24 milestone on Sep 25, 2024
  17. changed the title [-]cmd/compile: inconsistency between specification and implementation related to struct type identity when struct contains embedded alias on unnamed type[/-] [+]cmd/compile: clarify prose/examples regarding struct identity when embedded types match equally named non-embedded fields[/+] on Sep 30, 2024
  18. added
    DocumentationIssues describing a change to documentation.
    and removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    compiler/runtimeIssues related to the Go compiler and/or runtime.
    on Sep 30, 2024
  19. griesemer commented on Sep 30, 2024

    @griesemer
    Contributor

    It's not clear to me what this issue has to do with alias or unnamed types: if I understand correctly, the issue is about whether two struct fields are identical if one of them is embedded and the other one is not but they appear in the same "spot" in the struct, have the same name, same type, and same tag.

    It is the intent of the spec that two corresponding fields are not identical if one of them is embedded and the other one is not. I agree that this doesn't seem to be spelled out explicitly. A suitable example or perhaps some prose fine tuning should address that.

    The implementation is very clear though: for two structs to be identical, two otherwise matching fields must be both embedded or both not embedded.

  20. gopherbot commented on Sep 30, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/616697 mentions this issue: spec: clarify that identical structs must have identical field embeddings

  21. leabit commented on Oct 1, 2024

    @leabit
    Author

    @griesemer

    It's not clear to me what this issue has to do with alias or unnamed types

    Named types are different without any further check, so issue only makes sense when unnamed types are used.

    First example would fail to compile in case pac1.MyType1 and pac2.MyType2 are defined as follows:

    pac1:

    package pac1
    
    type MyType1 struct{} // defined (named) type
    

    pac2:

    package pac2
    
    type MyType1 struct{} // defined (named) type
    

    main:

    package main
    
    import (
    	"exeexe/pac1"
    	"exeexe/pac2"
    )
    
    type M1 struct {
    	pac1.MyType1
    }
    
    type M2 struct {
    	pac2.MyType1
    }
    
    func main() {
    	_ = M2(M1{})
    }
    

    Types of the field in M1 in M2 are not the same (unlike in case of unnamed types and aliases where they are the same and the program successfully compiles).

    if I understand correctly, the issue is about whether two struct fields are identical if one of them is embedded and the other one is not but they appear in the same "spot" in the struct, have the same name, same type, and same tag.

    Yes, you did understand it correctly.

  22. griesemer commented on Oct 1, 2024

    @griesemer
    Contributor

    Thanks, @leabit for confirming. Please comment on the CL if you believe it is not sufficient, thanks.

  23. added a commit that references this issue on Oct 2, 2024
    aa42d7b
  24. locked and limited conversation to collaborators on Oct 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

DocumentationIssues describing a change to documentation.FrozenDueToAgecompiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions