]> Cypherpunks repositories - gostls13.git/commitdiff
context: document that WithValue's key must be comparable
authorBrad Fitzpatrick <bradfitz@golang.org>
Sun, 10 Apr 2016 15:45:34 +0000 (15:45 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 11 Apr 2016 01:52:10 +0000 (01:52 +0000)
Also, check it and explode earlier, rather than cryptic failures later.

Change-Id: I319a425f60e2bc9d005a187fbdbd153faa96411c
Reviewed-on: https://go-review.googlesource.com/21799
Reviewed-by: Andrew Gerrand <adg@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Minux Ma <minux@golang.org>
src/context/context.go
src/context/context_test.go
src/go/build/deps_test.go

index 21dc8676bfe21b58d7a1555779575fcd90d52e84..c332e1f4432e9c7fa064cb2a0a698481fdee4903 100644 (file)
@@ -39,6 +39,7 @@ package context
 import (
        "errors"
        "fmt"
+       "reflect"
        "sync"
        "time"
 )
@@ -424,7 +425,12 @@ func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
 //
 // Use context Values only for request-scoped data that transits processes and
 // APIs, not for passing optional parameters to functions.
-func WithValue(parent Context, key interface{}, val interface{}) Context {
+//
+// The provided key must be comparable.
+func WithValue(parent Context, key, val interface{}) Context {
+       if !reflect.TypeOf(key).Comparable() {
+               panic("key is not comparable")
+       }
        return &valueCtx{parent, key, val}
 }
 
index 573470e0840949457f68922d0cd8bb7318429d33..0616704dd8eca9b663bfde69fe6833f9efeb23c6 100644 (file)
@@ -586,3 +586,16 @@ func TestCancelRemoves(t *testing.T) {
        cancel()
        checkChildren("after cancelling WithTimeout child", ctx, 0)
 }
+
+func TestWithValueChecksKey(t *testing.T) {
+       panicVal := recoveredValue(func() { WithValue(Background(), []byte("foo"), "bar") })
+       if panicVal == nil {
+               t.Error("expected panic")
+       }
+}
+
+func recoveredValue(fn func()) (v interface{}) {
+       defer func() { v = recover() }()
+       fn()
+       return
+}
index 8e2fd6e5843687a1e4153829c40a61d879f87a3d..f1d19bb50cc901c3ca100af5e8bc237580881e9f 100644 (file)
@@ -215,7 +215,7 @@ var pkgDeps = map[string][]string{
        "compress/gzip":            {"L4", "compress/flate"},
        "compress/lzw":             {"L4"},
        "compress/zlib":            {"L4", "compress/flate"},
-       "context":                  {"errors", "fmt", "sync", "time"},
+       "context":                  {"errors", "fmt", "reflect", "sync", "time"},
        "database/sql":             {"L4", "container/list", "database/sql/driver"},
        "database/sql/driver":      {"L4", "time"},
        "debug/dwarf":              {"L4"},