| 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 |
// Field accesses through type parameters are disabled |
| 6 |
// until we have a more thorough understanding of the |
| 7 |
// implications on the spec. See issue #51576. |
| 8 |
|
| 9 |
package p |
| 10 |
|
| 11 |
// The first example from the issue. |
| 12 |
type Numeric interface { |
| 13 |
~int | ~int8 | ~int16 | ~int32 | ~int64 |
| 14 |
} |
| 15 |
|
| 16 |
// numericAbs matches numeric types with an Abs method. |
| 17 |
type numericAbs[T Numeric] interface { |
| 18 |
~struct{ Value T } |
| 19 |
Abs() T |
| 20 |
} |
| 21 |
|
| 22 |
// AbsDifference computes the absolute value of the difference of |
| 23 |
// a and b, where the absolute value is determined by the Abs method. |
| 24 |
func absDifference[T numericAbs[T /* ERROR "T does not satisfy Numeric" */]](a, b T) T { |
| 25 |
// Field accesses are not permitted for now. Keep an error so |
| 26 |
// we can find and fix this code once the situation changes. |
| 27 |
return a.Value // ERROR "a.Value undefined" |
| 28 |
// TODO: The error below should probably be positioned on the '-'. |
| 29 |
// d := a /* ERROR "invalid operation: operator - not defined" */ .Value - b.Value |
| 30 |
// return d.Abs() |
| 31 |
} |
| 32 |
|
| 33 |
// The second example from the issue. |
| 34 |
type T[P int] struct{ f P } |
| 35 |
|
| 36 |
func _[P T[P /* ERROR "P does not satisfy int" */ ]]() {} |
| 37 |
|
| 38 |
// Additional tests |
| 39 |
func _[P T[T /* ERROR "T[P] does not satisfy int" */ [P /* ERROR "P does not satisfy int" */ ]]]() {} |
| 40 |
func _[P T[Q /* ERROR "Q does not satisfy int" */ ], Q T[P /* ERROR "P does not satisfy int" */ ]]() {} |
| 41 |
func _[P T[Q], Q int]() {} |
| 42 |
|
| 43 |
type C[P comparable] struct{ f P } |
| 44 |
func _[P C[C[P]]]() {} |
| 45 |
func _[P C[C /* ERROR "C[Q] does not satisfy comparable" */ [Q /* ERROR "Q does not satisfy comparable" */]], Q func()]() {} |
| 46 |
func _[P [10]C[P]]() {} |
| 47 |
func _[P struct{ f C[C[P]]}]() {} |