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

encoding/binary: fix NativeEndian == BigEndian || NativeEndian == LittleEndian #67026

Closed
dolmen opened this issue Apr 24, 2024 · 6 comments
Closed

Comments

@dolmen
Copy link
Contributor

dolmen commented Apr 24, 2024

This is a follow-up to #57237.

In comment #57237 (comment) @robpike implied that the value of binary.NativeEndian should be comparable with == to the binary.BigEndian and binary.LittleEndian values.

Unfortunately in the current implementation added in Go 1.21, comparisons with both values fail because the type of binary.NativeEndian is different of both values because of the use of an embedded type:

type nativeEndian struct {
        bigEndian
}

Also, it makes no sense to have a specific name (NativeEndian) for binary.NativeEndian.String(). It should be either BigEndian or LittleEndian (or any weired concrete endianness added in the future).

Proposed fix

Define the type of binary.NativeEndian using a type alias instead of type embedding.

type nativeEndian = bigEndian

var NativeEndian nativeEndian

Go version

go version go1.22.2 darwin/arm64

What did you do?

https://go.dev/play/p/niinanpbVuC

func TestEqual(t *testing.T) {
	nbOK := 0
	for _, bo := range []binary.ByteOrder{
		binary.BigEndian,
		binary.LittleEndian,
	} {
		t.Logf("Native == %v: %v", bo, binary.NativeEndian == bo)
		if binary.NativeEndian == bo {
			nbOK++
		}
	}
	if nbOK != 1 {
		t.Error("1 equal expected, got", nbOK)
	}
}

What did you see happen?

=== RUN   TestEqual
    prog_test.go:14: NativeEndian == BigEndian: false
    prog_test.go:14: NativeEndian == LittleEndian: false
    prog_test.go:20: 1 equal expected, got 0
--- FAIL: TestEqual (0.00s)
FAIL

What did you expect to see?

=== RUN   TestEqual
    compile_test.go:27: NativeEndian == BigEndian: false
    compile_test.go:27: NativeEndian == LittleEndian: true
--- PASS: TestEqual (0.00s)
PASS
@seankhliao
Copy link
Member

As they are different types, equality isn't possible (nor is it generally expected of unexported types).

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 24, 2024
@dolmen
Copy link
Contributor Author

dolmen commented Apr 24, 2024

@seankhliao My point is that they should not be different types, but instead defined using a type alias. For this reason I'm asking this issue to be reopened.

Cc: @tklauser #57237 (comment)

@gopherbot
Copy link

Change https://go.dev/cl/581655 mentions this issue: encoding/binary: fix NativeEndian to support == with Little/BigEndian

@dolmen
Copy link
Contributor Author

dolmen commented Apr 24, 2024

@seankhliao Please review CL581655 which fixes this issue.

@dolmen
Copy link
Contributor Author

dolmen commented Apr 24, 2024

Cc: @randall77 because #57237 (comment)

@dolmen
Copy link
Contributor Author

dolmen commented Apr 25, 2024

I realize that my TestEqual in the description is flawed: the bo is of type binary.ByteOrder (an interface) so the comparaison happens at runtime.

However for binary.NativeEndian == binary.BigEndian, the type are resolved at compile time and a mismatch is detected like with binary.LittleEndian == binary.BigEndian:

invalid operation: binary.LittleEndian == binary.BigEndian (mismatched types binary.littleEndian and binary.bigEndian)

So this issue deserved to be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants