| 1 |
// Copyright 2023 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 |
import ( |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
) |
| 11 |
|
| 12 |
func g[T any](...T) {} |
| 13 |
|
| 14 |
// Interface and non-interface types do not match. |
| 15 |
func _() { |
| 16 |
var file *os.File |
| 17 |
g(file, io /* ERROR "type io.Writer of io.Discard does not match inferred type *os.File for T" */ .Discard) |
| 18 |
g(file, os.Stdout) |
| 19 |
} |
| 20 |
|
| 21 |
func _() { |
| 22 |
var a *os.File |
| 23 |
var b any |
| 24 |
g(a, a) |
| 25 |
g(a, b /* ERROR "type any of b does not match inferred type *os.File for T" */) |
| 26 |
} |
| 27 |
|
| 28 |
var writer interface { |
| 29 |
Write(p []byte) (n int, err error) |
| 30 |
} |
| 31 |
|
| 32 |
func _() { |
| 33 |
var file *os.File |
| 34 |
g(file, writer /* ERROR "type interface{Write(p []byte) (n int, err error)} of writer does not match inferred type *os.File for T" */) |
| 35 |
g(writer, file /* ERROR "type *os.File of file does not match inferred type interface{Write(p []byte) (n int, err error)} for T" */) |
| 36 |
} |
| 37 |
|
| 38 |
// Different named interface types do not match. |
| 39 |
func _() { |
| 40 |
g(io.ReadWriter(nil), io.ReadWriter(nil)) |
| 41 |
g(io.ReadWriter(nil), io /* ERROR "does not match" */ .Writer(nil)) |
| 42 |
g(io.Writer(nil), io /* ERROR "does not match" */ .ReadWriter(nil)) |
| 43 |
} |
| 44 |
|
| 45 |
// Named and unnamed interface types match if they have the same methods. |
| 46 |
func _() { |
| 47 |
g(io.Writer(nil), writer) |
| 48 |
g(io.ReadWriter(nil), writer /* ERROR "does not match" */ ) |
| 49 |
} |
| 50 |
|
| 51 |
// There must be no order dependency for named and unnamed interfaces. |
| 52 |
func f[T interface{ m(T) }](a, b T) {} |
| 53 |
|
| 54 |
type F interface { |
| 55 |
m(F) |
| 56 |
} |
| 57 |
|
| 58 |
func _() { |
| 59 |
var i F |
| 60 |
var j interface { |
| 61 |
m(F) |
| 62 |
} |
| 63 |
|
| 64 |
// order doesn't matter |
| 65 |
f(i, j) |
| 66 |
f(j, i) |
| 67 |
} |