| 1 |
// Copyright 2024 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 |
func _[P int]() { |
| 8 |
type A = P |
| 9 |
_ = A(0) // don't crash with this conversion |
| 10 |
} |
| 11 |
|
| 12 |
func _[P []int]() { |
| 13 |
type A = P |
| 14 |
_ = make(A, 10) // don't report an error for A |
| 15 |
} |
| 16 |
|
| 17 |
func _[P string]() { |
| 18 |
var t []byte |
| 19 |
type A = P |
| 20 |
var s A |
| 21 |
copy(t, s) // don't report an error for s |
| 22 |
} |
| 23 |
|
| 24 |
func _[P map[int]int]() { |
| 25 |
type A = P |
| 26 |
var m A |
| 27 |
clear(m) // don't report an error for m |
| 28 |
} |
| 29 |
|
| 30 |
type S1 struct { |
| 31 |
x int "S1.x" |
| 32 |
} |
| 33 |
|
| 34 |
type S2 struct { |
| 35 |
x int "S2.x" |
| 36 |
} |
| 37 |
|
| 38 |
func _[P1 S1, P2 S2]() { |
| 39 |
type A = P1 |
| 40 |
var p A |
| 41 |
_ = P2(p) // conversion must be valid |
| 42 |
} |
| 43 |
|
| 44 |
func _[P1 S1, P2 S2]() { |
| 45 |
var p P1 |
| 46 |
type A = P2 |
| 47 |
_ = A(p) // conversion must be valid |
| 48 |
} |
| 49 |
|
| 50 |
func _[P int | string]() { |
| 51 |
var p P |
| 52 |
type A = int |
| 53 |
// preserve target type name A in error messages when using Alias types |
| 54 |
// (test are run with and without Alias types enabled, so we need to |
| 55 |
// keep both A and int in the error message) |
| 56 |
_ = A(p /* ERRORx `cannot convert string \(in P\) to type (A|int)` */) |
| 57 |
} |
| 58 |
|
| 59 |
func _[P struct{ x int }]() { |
| 60 |
var x struct{ x int } |
| 61 |
type A = P |
| 62 |
var _ A = x // assignment must be valid |
| 63 |
} |
| 64 |
|
| 65 |
func _[P struct{ x int }]() { |
| 66 |
type A = P |
| 67 |
var x A |
| 68 |
var _ struct{ x int } = x // assignment must be valid |
| 69 |
} |
| 70 |
|
| 71 |
func _[P []int | struct{}]() { |
| 72 |
type A = []int |
| 73 |
var a A |
| 74 |
var p P |
| 75 |
// preserve target type name A in error messages when using Alias types |
| 76 |
a = p // ERRORx `cannot assign struct{} \(in P\) to (A|\[\]int)` |
| 77 |
_ = a |
| 78 |
} |
| 79 |
|
| 80 |
func _[P any]() { |
| 81 |
type A = P |
| 82 |
var x A |
| 83 |
// keep "constrained by" for aliased type parameters in error messages |
| 84 |
var _ int = x // ERRORx `cannot use x \(variable of type (A|P) constrained by any\) as int value in variable declaration` |
| 85 |
} |
| 86 |
|
| 87 |
// Test case for go.dev/issue/67540. |
| 88 |
func _() { |
| 89 |
type ( |
| 90 |
S struct{} |
| 91 |
A = *S |
| 92 |
T S |
| 93 |
) |
| 94 |
var p A |
| 95 |
_ = (*T)(p) |
| 96 |
} |