blob: 7a926cc482af8f041ad31d47365ba2b61f794fdf [file] [log] [blame]
Robert Griesemer3e119402016-12-16 16:28:30 -08001// errorcheck
2
3// Copyright 2016 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// Test basic restrictions on type aliases.
8
Robert Griesemer3e119402016-12-16 16:28:30 -08009package p
10
11import (
12 "reflect"
13 . "reflect"
14)
15
Robert Griesemerb2386df2017-01-11 11:24:35 -080016type T0 struct{}
17
Robert Griesemer3e119402016-12-16 16:28:30 -080018// Valid type alias declarations.
19
Robert Griesemerb2386df2017-01-11 11:24:35 -080020type _ = T0
21type _ = int
22type _ = struct{}
23type _ = reflect.Value
24type _ = Value
Robert Griesemer3e119402016-12-16 16:28:30 -080025
26type (
Robert Griesemerb2386df2017-01-11 11:24:35 -080027 A0 = T0
28 A1 = int
29 A2 = struct{}
30 A3 = reflect.Value
31 A4 = Value
32 A5 = Value
33
34 N0 A0
Robert Griesemer3e119402016-12-16 16:28:30 -080035)
36
Robert Griesemerb2386df2017-01-11 11:24:35 -080037// Methods can be declared on the original named type and the alias.
Ian Lance Taylor9259f302017-01-24 12:43:52 -080038func (T0) m1() {} // GCCGO_ERROR "previous"
Robert Griesemer7398c3c2022-09-26 21:27:20 -070039func (*T0) m1() {} // ERROR "method redeclared: T0\.m1|T0\.m1 already declared|redefinition of .m1."
40func (A0) m1() {} // ERROR "T0\.m1 already declared|redefinition of .m1."
41func (A0) m1() {} // ERROR "T0\.m1 already declared|redefinition of .m1."
Robert Griesemer5802cfd2017-01-13 17:23:01 -080042func (A0) m2() {}
Robert Griesemerb2386df2017-01-11 11:24:35 -080043
44// Type aliases and the original type name can be used interchangeably.
45var _ A0 = T0{}
46var _ T0 = A0{}
47
48// But aliases and original types cannot be used with new types based on them.
Robert Griesemer7d9802a2024-10-22 11:48:38 -070049var _ N0 = T0{} // ERROR "cannot use T0{} \(value of struct type T0\) as N0 value in variable declaration"
50var _ N0 = A0{} // ERROR "cannot use A0{} \(value of struct type A0\) as N0 value in variable declaration"
Robert Griesemerb2386df2017-01-11 11:24:35 -080051
52var _ A5 = Value{}
53
54var _ interface {
55 m1()
56 m2()
57} = T0{}
58
59var _ interface {
60 m1()
61 m2()
62} = A0{}
63
Robert Griesemer3e119402016-12-16 16:28:30 -080064func _() {
Robert Griesemerb2386df2017-01-11 11:24:35 -080065 type _ = T0
66 type _ = int
67 type _ = struct{}
68 type _ = reflect.Value
69 type _ = Value
Robert Griesemer3e119402016-12-16 16:28:30 -080070
71 type (
Robert Griesemerb2386df2017-01-11 11:24:35 -080072 A0 = T0
73 A1 = int
74 A2 = struct{}
75 A3 = reflect.Value
76 A4 = Value
77 A5 Value
78
79 N0 A0
Robert Griesemer3e119402016-12-16 16:28:30 -080080 )
Robert Griesemerb2386df2017-01-11 11:24:35 -080081
82 var _ A0 = T0{}
83 var _ T0 = A0{}
84
Robert Griesemer7d9802a2024-10-22 11:48:38 -070085 var _ N0 = T0{} // ERROR "cannot use T0{} \(value of struct type T0\) as N0 value in variable declaration"
86 var _ N0 = A0{} // ERROR "cannot use A0{} \(value of struct type A0\) as N0 value in variable declaration"
Robert Griesemerb2386df2017-01-11 11:24:35 -080087
Robert Griesemer7d9802a2024-10-22 11:48:38 -070088 var _ A5 = Value{} // ERROR "cannot use Value{} \(value of struct type reflect\.Value\) as A5 value in variable declaration"
Robert Griesemer3e119402016-12-16 16:28:30 -080089}
90
91// Invalid type alias declarations.
92
Robert Griesemer72ad2f42020-11-30 21:38:49 -080093type _ = reflect.ValueOf // ERROR "reflect.ValueOf .*is not a type|expected type"
Robert Griesemer3e119402016-12-16 16:28:30 -080094
Matthew Dempsky31c81502024-05-14 22:22:34 -070095func (A1) m() {} // ERROR "cannot define new methods on non-local type|may not define methods on non-local type"
Ian Lance Taylor9259f302017-01-24 12:43:52 -080096func (A2) m() {} // ERROR "invalid receiver type"
Matthew Dempsky31c81502024-05-14 22:22:34 -070097func (A3) m() {} // ERROR "cannot define new methods on non-local type|may not define methods on non-local type"
98func (A4) m() {} // ERROR "cannot define new methods on non-local type|may not define methods on non-local type"
Robert Griesemerb2386df2017-01-11 11:24:35 -080099
100type B1 = struct{}
101
Matthew Dempsky638f1122018-04-04 18:42:39 -0700102func (B1) m() {} // ERROR "invalid receiver type"
Robert Griesemer3e119402016-12-16 16:28:30 -0800103
104// TODO(gri) expand