diff --git a/context/context.go b/context/context.go index e3c5345d..9cc1ae43 100644 --- a/context/context.go +++ b/context/context.go @@ -142,27 +142,28 @@ var Canceled = errors.New("context canceled") // deadline passes. var DeadlineExceeded = errors.New("context deadline exceeded") -// An emptyCtx is never canceled, has no values, and has no deadline. +// An emptyCtx is never canceled, has no values, and has no deadline. It is not +// struct{}, since vars of this type must have distinct addresses. type emptyCtx int -func (emptyCtx) Deadline() (deadline time.Time, ok bool) { +func (*emptyCtx) Deadline() (deadline time.Time, ok bool) { return } -func (emptyCtx) Done() <-chan struct{} { +func (*emptyCtx) Done() <-chan struct{} { return nil } -func (emptyCtx) Err() error { +func (*emptyCtx) Err() error { return nil } -func (emptyCtx) Value(key interface{}) interface{} { +func (*emptyCtx) Value(key interface{}) interface{} { return nil } -func (n emptyCtx) String() string { - switch n { +func (e *emptyCtx) String() string { + switch e { case background: return "context.Background" case todo: @@ -171,9 +172,9 @@ func (n emptyCtx) String() string { return "unknown empty Context" } -const ( - background emptyCtx = 1 - todo emptyCtx = 2 +var ( + background = new(emptyCtx) + todo = new(emptyCtx) ) // Background returns a non-nil, empty Context. It is never canceled, has no diff --git a/context/context_test.go b/context/context_test.go index c1a4de5f..82d2494a 100644 --- a/context/context_test.go +++ b/context/context_test.go @@ -365,7 +365,7 @@ func TestAllocs(t *testing.T) { c := WithValue(bg, k1, nil) c.Value(k1) }, - limit: 1, + limit: 3, gccgoLimit: 3, }, {