|
| 1 | +// Copyright 2022 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package p |
| 6 | + |
| 7 | +// The first example from the issue. |
| 8 | +type Numeric interface { |
| 9 | + ~int | ~int8 | ~int16 | ~int32 | ~int64 |
| 10 | +} |
| 11 | + |
| 12 | +// numericAbs matches numeric types with an Abs method. |
| 13 | +type numericAbs[T Numeric] interface { |
| 14 | + ~struct{ Value T } |
| 15 | + Abs() T |
| 16 | +} |
| 17 | + |
| 18 | +// AbsDifference computes the absolute value of the difference of |
| 19 | +// a and b, where the absolute value is determined by the Abs method. |
| 20 | +func absDifference[T numericAbs[T /* ERROR T does not implement Numeric */]](a, b T) T { |
| 21 | + // TODO: the error below should probably be positioned on the '-'. |
| 22 | + d := a /* ERROR "invalid operation: operator - not defined" */ .Value - b.Value |
| 23 | + return d.Abs() |
| 24 | +} |
| 25 | + |
| 26 | +// The second example from the issue. |
| 27 | +type T[P int] struct{ f P } |
| 28 | + |
| 29 | +func _[P T[P /* ERROR "P does not implement int" */ ]]() {} |
| 30 | + |
| 31 | +// Additional tests |
| 32 | +func _[P T[T /* ERROR "T\[P\] does not implement int" */ [P /* ERROR "P does not implement int" */ ]]]() {} |
| 33 | +func _[P T[Q /* ERROR "Q does not implement int" */ ], Q T[P /* ERROR "P does not implement int" */ ]]() {} |
| 34 | +func _[P T[Q], Q int]() {} |
| 35 | + |
| 36 | +type C[P comparable] struct{ f P } |
| 37 | +func _[P C[C[P]]]() {} |
| 38 | +func _[P C[C /* ERROR "C\[Q\] does not implement comparable" */ [Q /* ERROR "Q does not implement comparable" */]], Q func()]() {} |
| 39 | +func _[P [10]C[P]]() {} |
| 40 | +func _[P struct{ f C[C[P]]}]() {} |
0 commit comments