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