@@ -104,13 +104,11 @@ func mustFormatGolden(path string, in []struct{ Name, Data string }) {
104104
105105var now = time .Date (2009 , time .November , 10 , 23 , 00 , 00 , 00 , time .UTC )
106106
107- // TODO(≥go1.18): Define a generic function that boxes a value on the heap.
108- func newInt (n int ) * int { return & n }
107+ func newT [T any ](t T ) * T { return & t }
109108
110109type Stringer string
111110
112- func newStringer (s string ) fmt.Stringer { return (* Stringer )(& s ) }
113- func (s Stringer ) String () string { return string (s ) }
111+ func (s Stringer ) String () string { return string (s ) }
114112
115113type test struct {
116114 label string // Test name
@@ -319,29 +317,29 @@ func comparerTests() []test {
319317 reason : "unexported fields result in a panic by default" ,
320318 }, {
321319 label : label + "/PointerStructEqual" ,
322- x : & struct { A * int }{newInt (4 )},
323- y : & struct { A * int }{newInt (4 )},
320+ x : & struct { A * int }{newT [ int ] (4 )},
321+ y : & struct { A * int }{newT [ int ] (4 )},
324322 wantEqual : true ,
325323 reason : "comparison of pointer to struct with equal A field" ,
326324 }, {
327325 label : label + "/PointerStructInequal" ,
328- x : & struct { A * int }{newInt (4 )},
329- y : & struct { A * int }{newInt (5 )},
326+ x : & struct { A * int }{newT [ int ] (4 )},
327+ y : & struct { A * int }{newT [ int ] (5 )},
330328 wantEqual : false ,
331329 reason : "comparison of pointer to struct with inequal A field" ,
332330 }, {
333331 label : label + "/PointerStructTrueComparer" ,
334- x : & struct { A * int }{newInt (4 )},
335- y : & struct { A * int }{newInt (5 )},
332+ x : & struct { A * int }{newT [ int ] (4 )},
333+ y : & struct { A * int }{newT [ int ] (5 )},
336334 opts : []cmp.Option {
337335 cmp .Comparer (func (x , y int ) bool { return true }),
338336 },
339337 wantEqual : true ,
340338 reason : "comparison of pointer to struct with inequal A field, but treated as equal with always equal comparer" ,
341339 }, {
342340 label : label + "/PointerStructNonNilComparer" ,
343- x : & struct { A * int }{newInt (4 )},
344- y : & struct { A * int }{newInt (5 )},
341+ x : & struct { A * int }{newT [ int ] (4 )},
342+ y : & struct { A * int }{newT [ int ] (5 )},
345343 opts : []cmp.Option {
346344 cmp .Comparer (func (x , y * int ) bool { return x != nil && y != nil }),
347345 },
@@ -909,35 +907,35 @@ func reporterTests() []test {
909907 reason : "reporter should display the qualified type name to disambiguate between the two values" ,
910908 }, {
911909 label : label + "/AmbiguousPointer" ,
912- x : newInt (0 ),
913- y : newInt (0 ),
910+ x : newT [ int ] (0 ),
911+ y : newT [ int ] (0 ),
914912 opts : []cmp.Option {
915913 cmp .Comparer (func (x , y * int ) bool { return x == y }),
916914 },
917915 wantEqual : false ,
918916 reason : "reporter should display the address to disambiguate between the two values" ,
919917 }, {
920918 label : label + "/AmbiguousPointerStruct" ,
921- x : struct { I * int }{newInt (0 )},
922- y : struct { I * int }{newInt (0 )},
919+ x : struct { I * int }{newT [ int ] (0 )},
920+ y : struct { I * int }{newT [ int ] (0 )},
923921 opts : []cmp.Option {
924922 cmp .Comparer (func (x , y * int ) bool { return x == y }),
925923 },
926924 wantEqual : false ,
927925 reason : "reporter should display the address to disambiguate between the two struct fields" ,
928926 }, {
929927 label : label + "/AmbiguousPointerSlice" ,
930- x : []* int {newInt (0 )},
931- y : []* int {newInt (0 )},
928+ x : []* int {newT [ int ] (0 )},
929+ y : []* int {newT [ int ] (0 )},
932930 opts : []cmp.Option {
933931 cmp .Comparer (func (x , y * int ) bool { return x == y }),
934932 },
935933 wantEqual : false ,
936934 reason : "reporter should display the address to disambiguate between the two slice elements" ,
937935 }, {
938936 label : label + "/AmbiguousPointerMap" ,
939- x : map [string ]* int {"zero" : newInt (0 )},
940- y : map [string ]* int {"zero" : newInt (0 )},
937+ x : map [string ]* int {"zero" : newT [ int ] (0 )},
938+ y : map [string ]* int {"zero" : newT [ int ] (0 )},
941939 opts : []cmp.Option {
942940 cmp .Comparer (func (x , y * int ) bool { return x == y }),
943941 },
@@ -946,25 +944,25 @@ func reporterTests() []test {
946944 }, {
947945 label : label + "/AmbiguousStringer" ,
948946 x : Stringer ("hello" ),
949- y : newStringer ("hello" ),
947+ y : newT [ Stringer ] ("hello" ),
950948 wantEqual : false ,
951949 reason : "reporter should avoid calling String to disambiguate between the two values" ,
952950 }, {
953951 label : label + "/AmbiguousStringerStruct" ,
954952 x : struct { S fmt.Stringer }{Stringer ("hello" )},
955- y : struct { S fmt.Stringer }{newStringer ("hello" )},
953+ y : struct { S fmt.Stringer }{newT [ Stringer ] ("hello" )},
956954 wantEqual : false ,
957955 reason : "reporter should avoid calling String to disambiguate between the two struct fields" ,
958956 }, {
959957 label : label + "/AmbiguousStringerSlice" ,
960958 x : []fmt.Stringer {Stringer ("hello" )},
961- y : []fmt.Stringer {newStringer ("hello" )},
959+ y : []fmt.Stringer {newT [ Stringer ] ("hello" )},
962960 wantEqual : false ,
963961 reason : "reporter should avoid calling String to disambiguate between the two slice elements" ,
964962 }, {
965963 label : label + "/AmbiguousStringerMap" ,
966964 x : map [string ]fmt.Stringer {"zero" : Stringer ("hello" )},
967- y : map [string ]fmt.Stringer {"zero" : newStringer ("hello" )},
965+ y : map [string ]fmt.Stringer {"zero" : newT [ Stringer ] ("hello" )},
968966 wantEqual : false ,
969967 reason : "reporter should avoid calling String to disambiguate between the two map values" ,
970968 }, {
@@ -984,15 +982,15 @@ func reporterTests() []test {
984982 foo1.Bar {"fizz" }: "buzz" ,
985983 },
986984 y : map [interface {}]string {
987- newStringer ("hello" ): "goodbye" ,
988- foo2.Bar {"fizz" }: "buzz" ,
985+ newT [ Stringer ] ("hello" ): "goodbye" ,
986+ foo2.Bar {"fizz" }: "buzz" ,
989987 },
990988 wantEqual : false ,
991989 reason : "reporter should avoid calling String to disambiguate between the two map keys" ,
992990 }, {
993991 label : label + "/NonAmbiguousStringerMapKey" ,
994992 x : map [interface {}]string {Stringer ("hello" ): "goodbye" },
995- y : map [interface {}]string {newStringer ("fizz" ): "buzz" },
993+ y : map [interface {}]string {newT [ Stringer ] ("fizz" ): "buzz" },
996994 wantEqual : false ,
997995 reason : "reporter should call String as there is no ambiguity between the two map keys" ,
998996 }, {
@@ -2594,7 +2592,7 @@ func project1Tests() []test {
25942592 Target : "corporation" ,
25952593 Immutable : & ts.GoatImmutable {
25962594 ID : "southbay" ,
2597- State : (* pb .Goat_States )(newInt (5 )),
2595+ State : (* pb .Goat_States )(newT [ int ] (5 )),
25982596 Started : now ,
25992597 },
26002598 },
@@ -2622,7 +2620,7 @@ func project1Tests() []test {
26222620 Immutable : & ts.EagleImmutable {
26232621 ID : "eagleID" ,
26242622 Birthday : now ,
2625- MissingCall : (* pb .Eagle_MissingCalls )(newInt (55 )),
2623+ MissingCall : (* pb .Eagle_MissingCalls )(newT [ int ] (55 )),
26262624 },
26272625 }
26282626 }
@@ -2671,7 +2669,7 @@ func project1Tests() []test {
26712669 x : func () ts.Eagle {
26722670 eg := createEagle ()
26732671 eg .Dreamers [1 ].Animal [0 ].(ts.Goat ).Immutable .ID = "southbay2"
2674- eg .Dreamers [1 ].Animal [0 ].(ts.Goat ).Immutable .State = (* pb .Goat_States )(newInt (6 ))
2672+ eg .Dreamers [1 ].Animal [0 ].(ts.Goat ).Immutable .State = (* pb .Goat_States )(newT [ int ] (6 ))
26752673 eg .Slaps [0 ].Immutable .MildSlap = false
26762674 return eg
26772675 }(),
0 commit comments