You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// This is how some assertions libraries check for equality// This particular implementation was taken from:// https://github.com/golang/mock/blob/0cdccf5f55d777b12c1ac5a93f607cdd1dbf5296/gomock/matchers.go#L104funcequal(x, yany) bool {
// In case, some value is nilifx==nil||y==nil {
returnreflect.DeepEqual(x, y)
}
xVal:=reflect.ValueOf(x)
yVal:=reflect.ValueOf(y)
ifxVal.Type().AssignableTo(yVal.Type()) {
x1ValConverted:=xVal.Convert(yVal.Type())
returnreflect.DeepEqual(x1ValConverted.Interface(), yVal.Interface())
}
returnfalse
}
funcTestContext(t*testing.T) {
background:=context.Background()
todo:=context.TODO()
// This probably should have never worked in <1.21// Mostly, this occurs when tests were carelessly written by declaring the// mock to expect context.TODO() but the call uses context.Background()// This is a trivial fix to make both use the same call either TODO or Backgroundif!equal(background, todo) {
t.Errorf("background: %T(%#v) is equel to TODO: %T(%#v)", background, background, todo, todo)
}
}
Initially I thought it was a breaking change, but as I dug in, it really seems like it only worked as an artifact of undocumented behavior.
What did you see instead?
It's potentially worth pointing out the corrected behavior of this edge case in the release notes. While this works in versions before 1.21, but only worked because type emptyCtx int and that both Background & TODO were variables assigned new values of emptyCtx. This wasn't documented and generally not expected behavior.
The text was updated successfully, but these errors were encountered:
That's absolutely true. I'm glad it was in a public repo to use as an example of how that library's equality could be (mis)used in previous versions of Go.
adonovan
changed the title
affected/package: context
context: Background and TODO may appear equal under some reflect-based notions of equivalence
Jun 23, 2023
Fixesgolang#60978
Change-Id: I3e4bd366dc30ac435698b8f17170695330034683
Reviewed-on: https://go-review.googlesource.com/c/go/+/505795
TryBot-Bypass: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Sameer Ajmani <sameer@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
This was introduced in 1.21rc1 & 1.21rc2. I bisected release-branch.go1.20 to release-branch.go1.21 and narrowed it down to this commit: 6e5c260
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://go.dev/play/p/eZIlVe7s-Or
What did you expect to see?
Initially I thought it was a breaking change, but as I dug in, it really seems like it only worked as an artifact of undocumented behavior.
What did you see instead?
It's potentially worth pointing out the corrected behavior of this edge case in the release notes. While this works in versions before 1.21, but only worked because
type emptyCtx int
and that both Background & TODO were variables assigned new values of emptyCtx. This wasn't documented and generally not expected behavior.The text was updated successfully, but these errors were encountered: