| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2023 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
// Smoke test for reverse type inference. |
| 8 |
// The type checker has more expansive tests. |
| 9 |
|
| 10 |
package main |
| 11 |
|
| 12 |
func main() { |
| 13 |
var f1 func(int) int |
| 14 |
f1 = g1 |
| 15 |
if f1(1) != g1(1) { |
| 16 |
panic(1) |
| 17 |
} |
| 18 |
|
| 19 |
var f2 func(int) string = g2 |
| 20 |
if f2(2) != "" { |
| 21 |
panic(2) |
| 22 |
} |
| 23 |
|
| 24 |
if g3(g1, 3) != g1(3) { |
| 25 |
panic(3) |
| 26 |
} |
| 27 |
|
| 28 |
if g4(g2, 4) != "" { |
| 29 |
panic(4) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func g1[P any](x P) P { return x } |
| 34 |
func g2[P, Q any](x P) Q { var q Q; return q } |
| 35 |
|
| 36 |
func g3(f1 func(int) int, x int) int { return f1(x) } |
| 37 |
func g4(f2 func(int) string, x int) string { return f2(x) } |