diff options
author | Robert Griesemer <gri@golang.org> | 2021-04-28 15:08:47 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2021-04-29 15:54:27 +0000 |
commit | 948a262455346c6035df6bd5dc36c5d4b6aca409 (patch) | |
tree | a9facf2b7f8fabf909e759fd43df250e57979212 /src/cmd/compile/internal/types2/testdata | |
parent | 12eaefead46d7ba10fce622f093d4c9b2989a5c0 (diff) | |
download | go-948a262455346c6035df6bd5dc36c5d4b6aca409.tar.gz go-948a262455346c6035df6bd5dc36c5d4b6aca409.zip |
cmd/compile/internal/types2: nest all test data under the testdata directory
This matches https://golang.org/cl/314829 for go/types.
Change-Id: If3d127af0557bb13d504581920ea03e39db0eb07
Reviewed-on: https://go-review.googlesource.com/c/go/+/314772
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types2/testdata')
105 files changed, 1777 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/types2/testdata/blank.src b/src/cmd/compile/internal/types2/testdata/check/blank.src index 6a2507f482..6a2507f482 100644 --- a/src/cmd/compile/internal/types2/testdata/blank.src +++ b/src/cmd/compile/internal/types2/testdata/check/blank.src diff --git a/src/cmd/compile/internal/types2/testdata/builtins.go2 b/src/cmd/compile/internal/types2/testdata/check/builtins.go2 index 3918d836b5..3918d836b5 100644 --- a/src/cmd/compile/internal/types2/testdata/builtins.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/builtins.go2 diff --git a/src/cmd/compile/internal/types2/testdata/builtins.src b/src/cmd/compile/internal/types2/testdata/check/builtins.src index 6d1f47129b..6d1f47129b 100644 --- a/src/cmd/compile/internal/types2/testdata/builtins.src +++ b/src/cmd/compile/internal/types2/testdata/check/builtins.src diff --git a/src/cmd/compile/internal/types2/testdata/chans.go2 b/src/cmd/compile/internal/types2/testdata/check/chans.go2 index fad2bcec9d..fad2bcec9d 100644 --- a/src/cmd/compile/internal/types2/testdata/chans.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/chans.go2 diff --git a/src/cmd/compile/internal/types2/testdata/const0.src b/src/cmd/compile/internal/types2/testdata/check/const0.src index 5608b1549b..5608b1549b 100644 --- a/src/cmd/compile/internal/types2/testdata/const0.src +++ b/src/cmd/compile/internal/types2/testdata/check/const0.src diff --git a/src/cmd/compile/internal/types2/testdata/const1.src b/src/cmd/compile/internal/types2/testdata/check/const1.src index 56b6bd4ca5..56b6bd4ca5 100644 --- a/src/cmd/compile/internal/types2/testdata/const1.src +++ b/src/cmd/compile/internal/types2/testdata/check/const1.src diff --git a/src/cmd/compile/internal/types2/testdata/constdecl.src b/src/cmd/compile/internal/types2/testdata/check/constdecl.src index cb155ab35d..cb155ab35d 100644 --- a/src/cmd/compile/internal/types2/testdata/constdecl.src +++ b/src/cmd/compile/internal/types2/testdata/check/constdecl.src diff --git a/src/cmd/compile/internal/types2/testdata/conversions.src b/src/cmd/compile/internal/types2/testdata/check/conversions.src index e1336c0456..e1336c0456 100644 --- a/src/cmd/compile/internal/types2/testdata/conversions.src +++ b/src/cmd/compile/internal/types2/testdata/check/conversions.src diff --git a/src/cmd/compile/internal/types2/testdata/conversions2.src b/src/cmd/compile/internal/types2/testdata/check/conversions2.src index 93a5f182fb..93a5f182fb 100644 --- a/src/cmd/compile/internal/types2/testdata/conversions2.src +++ b/src/cmd/compile/internal/types2/testdata/check/conversions2.src diff --git a/src/cmd/compile/internal/types2/testdata/cycles.src b/src/cmd/compile/internal/types2/testdata/check/cycles.src index b2ee8ecd5f..b2ee8ecd5f 100644 --- a/src/cmd/compile/internal/types2/testdata/cycles.src +++ b/src/cmd/compile/internal/types2/testdata/check/cycles.src diff --git a/src/cmd/compile/internal/types2/testdata/cycles1.src b/src/cmd/compile/internal/types2/testdata/check/cycles1.src index ae2b38ebec..ae2b38ebec 100644 --- a/src/cmd/compile/internal/types2/testdata/cycles1.src +++ b/src/cmd/compile/internal/types2/testdata/check/cycles1.src diff --git a/src/cmd/compile/internal/types2/testdata/cycles2.src b/src/cmd/compile/internal/types2/testdata/check/cycles2.src index 1a7f40ae4b..1a7f40ae4b 100644 --- a/src/cmd/compile/internal/types2/testdata/cycles2.src +++ b/src/cmd/compile/internal/types2/testdata/check/cycles2.src diff --git a/src/cmd/compile/internal/types2/testdata/cycles3.src b/src/cmd/compile/internal/types2/testdata/check/cycles3.src index 5e89b627f0..5e89b627f0 100644 --- a/src/cmd/compile/internal/types2/testdata/cycles3.src +++ b/src/cmd/compile/internal/types2/testdata/check/cycles3.src diff --git a/src/cmd/compile/internal/types2/testdata/cycles4.src b/src/cmd/compile/internal/types2/testdata/check/cycles4.src index 445babca68..445babca68 100644 --- a/src/cmd/compile/internal/types2/testdata/cycles4.src +++ b/src/cmd/compile/internal/types2/testdata/check/cycles4.src diff --git a/src/cmd/compile/internal/types2/testdata/cycles5.src b/src/cmd/compile/internal/types2/testdata/check/cycles5.src index 397adcce01..397adcce01 100644 --- a/src/cmd/compile/internal/types2/testdata/cycles5.src +++ b/src/cmd/compile/internal/types2/testdata/check/cycles5.src diff --git a/src/cmd/compile/internal/types2/testdata/decls0.src b/src/cmd/compile/internal/types2/testdata/check/decls0.src index e78d8867e0..e78d8867e0 100644 --- a/src/cmd/compile/internal/types2/testdata/decls0.src +++ b/src/cmd/compile/internal/types2/testdata/check/decls0.src diff --git a/src/cmd/compile/internal/types2/testdata/decls1.src b/src/cmd/compile/internal/types2/testdata/check/decls1.src index e6beb78358..e6beb78358 100644 --- a/src/cmd/compile/internal/types2/testdata/decls1.src +++ b/src/cmd/compile/internal/types2/testdata/check/decls1.src diff --git a/src/cmd/compile/internal/types2/testdata/decls2/decls2a.src b/src/cmd/compile/internal/types2/testdata/check/decls2/decls2a.src index d077db55dd..d077db55dd 100644 --- a/src/cmd/compile/internal/types2/testdata/decls2/decls2a.src +++ b/src/cmd/compile/internal/types2/testdata/check/decls2/decls2a.src diff --git a/src/cmd/compile/internal/types2/testdata/decls2/decls2b.src b/src/cmd/compile/internal/types2/testdata/check/decls2/decls2b.src index 7b3229cadc..7b3229cadc 100644 --- a/src/cmd/compile/internal/types2/testdata/decls2/decls2b.src +++ b/src/cmd/compile/internal/types2/testdata/check/decls2/decls2b.src diff --git a/src/cmd/compile/internal/types2/testdata/decls3.src b/src/cmd/compile/internal/types2/testdata/check/decls3.src index d7a0c444da..d7a0c444da 100644 --- a/src/cmd/compile/internal/types2/testdata/decls3.src +++ b/src/cmd/compile/internal/types2/testdata/check/decls3.src diff --git a/src/cmd/compile/internal/types2/testdata/decls4.src b/src/cmd/compile/internal/types2/testdata/check/decls4.src index eb08421bee..eb08421bee 100644 --- a/src/cmd/compile/internal/types2/testdata/decls4.src +++ b/src/cmd/compile/internal/types2/testdata/check/decls4.src diff --git a/src/cmd/compile/internal/types2/testdata/decls5.src b/src/cmd/compile/internal/types2/testdata/check/decls5.src index 88d31946da..88d31946da 100644 --- a/src/cmd/compile/internal/types2/testdata/decls5.src +++ b/src/cmd/compile/internal/types2/testdata/check/decls5.src diff --git a/src/cmd/compile/internal/types2/testdata/errors.src b/src/cmd/compile/internal/types2/testdata/check/errors.src index ff929217c4..ff929217c4 100644 --- a/src/cmd/compile/internal/types2/testdata/errors.src +++ b/src/cmd/compile/internal/types2/testdata/check/errors.src diff --git a/src/cmd/compile/internal/types2/testdata/expr0.src b/src/cmd/compile/internal/types2/testdata/check/expr0.src index 1aac726327..1aac726327 100644 --- a/src/cmd/compile/internal/types2/testdata/expr0.src +++ b/src/cmd/compile/internal/types2/testdata/check/expr0.src diff --git a/src/cmd/compile/internal/types2/testdata/expr1.src b/src/cmd/compile/internal/types2/testdata/check/expr1.src index 4ead815158..4ead815158 100644 --- a/src/cmd/compile/internal/types2/testdata/expr1.src +++ b/src/cmd/compile/internal/types2/testdata/check/expr1.src diff --git a/src/cmd/compile/internal/types2/testdata/expr2.src b/src/cmd/compile/internal/types2/testdata/check/expr2.src index 0c959e8011..0c959e8011 100644 --- a/src/cmd/compile/internal/types2/testdata/expr2.src +++ b/src/cmd/compile/internal/types2/testdata/check/expr2.src diff --git a/src/cmd/compile/internal/types2/testdata/expr3.src b/src/cmd/compile/internal/types2/testdata/check/expr3.src index eab3f72c4d..eab3f72c4d 100644 --- a/src/cmd/compile/internal/types2/testdata/expr3.src +++ b/src/cmd/compile/internal/types2/testdata/check/expr3.src diff --git a/src/cmd/compile/internal/types2/testdata/go1_12.src b/src/cmd/compile/internal/types2/testdata/check/go1_12.src index 75a602b8ff..75a602b8ff 100644 --- a/src/cmd/compile/internal/types2/testdata/go1_12.src +++ b/src/cmd/compile/internal/types2/testdata/check/go1_12.src diff --git a/src/cmd/compile/internal/types2/testdata/go1_13.src b/src/cmd/compile/internal/types2/testdata/check/go1_13.src index 93cb4c72a7..93cb4c72a7 100644 --- a/src/cmd/compile/internal/types2/testdata/go1_13.src +++ b/src/cmd/compile/internal/types2/testdata/check/go1_13.src diff --git a/src/cmd/compile/internal/types2/testdata/go1_8.src b/src/cmd/compile/internal/types2/testdata/check/go1_8.src index 0f3ba9443b..0f3ba9443b 100644 --- a/src/cmd/compile/internal/types2/testdata/go1_8.src +++ b/src/cmd/compile/internal/types2/testdata/check/go1_8.src diff --git a/src/cmd/compile/internal/types2/testdata/gotos.src b/src/cmd/compile/internal/types2/testdata/check/gotos.src index 069a94bbbf..069a94bbbf 100644 --- a/src/cmd/compile/internal/types2/testdata/gotos.src +++ b/src/cmd/compile/internal/types2/testdata/check/gotos.src diff --git a/src/cmd/compile/internal/types2/testdata/importC.src b/src/cmd/compile/internal/types2/testdata/check/importC.src index f55be2d5c5..f55be2d5c5 100644 --- a/src/cmd/compile/internal/types2/testdata/importC.src +++ b/src/cmd/compile/internal/types2/testdata/check/importC.src diff --git a/src/cmd/compile/internal/types2/testdata/importdecl0/importdecl0a.src b/src/cmd/compile/internal/types2/testdata/check/importdecl0/importdecl0a.src index 5ceb96e1fa..5ceb96e1fa 100644 --- a/src/cmd/compile/internal/types2/testdata/importdecl0/importdecl0a.src +++ b/src/cmd/compile/internal/types2/testdata/check/importdecl0/importdecl0a.src diff --git a/src/cmd/compile/internal/types2/testdata/importdecl0/importdecl0b.src b/src/cmd/compile/internal/types2/testdata/check/importdecl0/importdecl0b.src index 19b55aff76..19b55aff76 100644 --- a/src/cmd/compile/internal/types2/testdata/importdecl0/importdecl0b.src +++ b/src/cmd/compile/internal/types2/testdata/check/importdecl0/importdecl0b.src diff --git a/src/cmd/compile/internal/types2/testdata/importdecl1/importdecl1a.src b/src/cmd/compile/internal/types2/testdata/check/importdecl1/importdecl1a.src index d377c01638..d377c01638 100644 --- a/src/cmd/compile/internal/types2/testdata/importdecl1/importdecl1a.src +++ b/src/cmd/compile/internal/types2/testdata/check/importdecl1/importdecl1a.src diff --git a/src/cmd/compile/internal/types2/testdata/importdecl1/importdecl1b.src b/src/cmd/compile/internal/types2/testdata/check/importdecl1/importdecl1b.src index 43a7bcd753..43a7bcd753 100644 --- a/src/cmd/compile/internal/types2/testdata/importdecl1/importdecl1b.src +++ b/src/cmd/compile/internal/types2/testdata/check/importdecl1/importdecl1b.src diff --git a/src/cmd/compile/internal/types2/testdata/init0.src b/src/cmd/compile/internal/types2/testdata/check/init0.src index 6e8746afb6..6e8746afb6 100644 --- a/src/cmd/compile/internal/types2/testdata/init0.src +++ b/src/cmd/compile/internal/types2/testdata/check/init0.src diff --git a/src/cmd/compile/internal/types2/testdata/init1.src b/src/cmd/compile/internal/types2/testdata/check/init1.src index 39ca31466b..39ca31466b 100644 --- a/src/cmd/compile/internal/types2/testdata/init1.src +++ b/src/cmd/compile/internal/types2/testdata/check/init1.src diff --git a/src/cmd/compile/internal/types2/testdata/init2.src b/src/cmd/compile/internal/types2/testdata/check/init2.src index 614db6c949..614db6c949 100644 --- a/src/cmd/compile/internal/types2/testdata/init2.src +++ b/src/cmd/compile/internal/types2/testdata/check/init2.src diff --git a/src/cmd/compile/internal/types2/testdata/issue25008/issue25008a.src b/src/cmd/compile/internal/types2/testdata/check/issue25008/issue25008a.src index cf71ca10e4..cf71ca10e4 100644 --- a/src/cmd/compile/internal/types2/testdata/issue25008/issue25008a.src +++ b/src/cmd/compile/internal/types2/testdata/check/issue25008/issue25008a.src diff --git a/src/cmd/compile/internal/types2/testdata/issue25008/issue25008b.src b/src/cmd/compile/internal/types2/testdata/check/issue25008/issue25008b.src index f132b7fab3..f132b7fab3 100644 --- a/src/cmd/compile/internal/types2/testdata/issue25008/issue25008b.src +++ b/src/cmd/compile/internal/types2/testdata/check/issue25008/issue25008b.src diff --git a/src/cmd/compile/internal/types2/testdata/issues.go2 b/src/cmd/compile/internal/types2/testdata/check/issues.go2 index 1c73b5da92..1c73b5da92 100644 --- a/src/cmd/compile/internal/types2/testdata/issues.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/issues.go2 diff --git a/src/cmd/compile/internal/types2/testdata/issues.src b/src/cmd/compile/internal/types2/testdata/check/issues.src index 21aa208cc7..21aa208cc7 100644 --- a/src/cmd/compile/internal/types2/testdata/issues.src +++ b/src/cmd/compile/internal/types2/testdata/check/issues.src diff --git a/src/cmd/compile/internal/types2/testdata/labels.src b/src/cmd/compile/internal/types2/testdata/check/labels.src index 9f42406965..9f42406965 100644 --- a/src/cmd/compile/internal/types2/testdata/labels.src +++ b/src/cmd/compile/internal/types2/testdata/check/labels.src diff --git a/src/cmd/compile/internal/types2/testdata/linalg.go2 b/src/cmd/compile/internal/types2/testdata/check/linalg.go2 index 0d27603a58..0d27603a58 100644 --- a/src/cmd/compile/internal/types2/testdata/linalg.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/linalg.go2 diff --git a/src/cmd/compile/internal/types2/testdata/literals.src b/src/cmd/compile/internal/types2/testdata/check/literals.src index 494a465f48..494a465f48 100644 --- a/src/cmd/compile/internal/types2/testdata/literals.src +++ b/src/cmd/compile/internal/types2/testdata/check/literals.src diff --git a/src/cmd/compile/internal/types2/testdata/main.go2 b/src/cmd/compile/internal/types2/testdata/check/main.go2 index b7ddeaa1a8..b7ddeaa1a8 100644 --- a/src/cmd/compile/internal/types2/testdata/main.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/main.go2 diff --git a/src/cmd/compile/internal/types2/testdata/main.src b/src/cmd/compile/internal/types2/testdata/check/main.src index f892938d4a..f892938d4a 100644 --- a/src/cmd/compile/internal/types2/testdata/main.src +++ b/src/cmd/compile/internal/types2/testdata/check/main.src diff --git a/src/cmd/compile/internal/types2/testdata/map.go2 b/src/cmd/compile/internal/types2/testdata/check/map.go2 index 814d9539fd..814d9539fd 100644 --- a/src/cmd/compile/internal/types2/testdata/map.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/map.go2 diff --git a/src/cmd/compile/internal/types2/testdata/map2.go2 b/src/cmd/compile/internal/types2/testdata/check/map2.go2 index 2833445662..2833445662 100644 --- a/src/cmd/compile/internal/types2/testdata/map2.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/map2.go2 diff --git a/src/cmd/compile/internal/types2/testdata/methodsets.src b/src/cmd/compile/internal/types2/testdata/check/methodsets.src index 9fb10deb9a..9fb10deb9a 100644 --- a/src/cmd/compile/internal/types2/testdata/methodsets.src +++ b/src/cmd/compile/internal/types2/testdata/check/methodsets.src diff --git a/src/cmd/compile/internal/types2/testdata/mtypeparams.go2 b/src/cmd/compile/internal/types2/testdata/check/mtypeparams.go2 index c2f282bae1..c2f282bae1 100644 --- a/src/cmd/compile/internal/types2/testdata/mtypeparams.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/mtypeparams.go2 diff --git a/src/cmd/compile/internal/types2/testdata/shifts.src b/src/cmd/compile/internal/types2/testdata/check/shifts.src index 60db731cf4..60db731cf4 100644 --- a/src/cmd/compile/internal/types2/testdata/shifts.src +++ b/src/cmd/compile/internal/types2/testdata/check/shifts.src diff --git a/src/cmd/compile/internal/types2/testdata/slices.go2 b/src/cmd/compile/internal/types2/testdata/check/slices.go2 index 2bacd1c2aa..2bacd1c2aa 100644 --- a/src/cmd/compile/internal/types2/testdata/slices.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/slices.go2 diff --git a/src/cmd/compile/internal/types2/testdata/stmt0.src b/src/cmd/compile/internal/types2/testdata/check/stmt0.src index bedcbe5fce..bedcbe5fce 100644 --- a/src/cmd/compile/internal/types2/testdata/stmt0.src +++ b/src/cmd/compile/internal/types2/testdata/check/stmt0.src diff --git a/src/cmd/compile/internal/types2/testdata/stmt1.src b/src/cmd/compile/internal/types2/testdata/check/stmt1.src index f79f92058b..f79f92058b 100644 --- a/src/cmd/compile/internal/types2/testdata/stmt1.src +++ b/src/cmd/compile/internal/types2/testdata/check/stmt1.src diff --git a/src/cmd/compile/internal/types2/testdata/tinference.go2 b/src/cmd/compile/internal/types2/testdata/check/tinference.go2 index a53fde0a2a..a53fde0a2a 100644 --- a/src/cmd/compile/internal/types2/testdata/tinference.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/tinference.go2 diff --git a/src/cmd/compile/internal/types2/testdata/tmp.go2 b/src/cmd/compile/internal/types2/testdata/check/tmp.go2 index dae78caff8..dae78caff8 100644 --- a/src/cmd/compile/internal/types2/testdata/tmp.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/tmp.go2 diff --git a/src/cmd/compile/internal/types2/testdata/typeinst.go2 b/src/cmd/compile/internal/types2/testdata/check/typeinst.go2 index 0d628cb9d0..0d628cb9d0 100644 --- a/src/cmd/compile/internal/types2/testdata/typeinst.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/typeinst.go2 diff --git a/src/cmd/compile/internal/types2/testdata/typeinst2.go2 b/src/cmd/compile/internal/types2/testdata/check/typeinst2.go2 index 6e2104a515..6e2104a515 100644 --- a/src/cmd/compile/internal/types2/testdata/typeinst2.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/typeinst2.go2 diff --git a/src/cmd/compile/internal/types2/testdata/typeparams.go2 b/src/cmd/compile/internal/types2/testdata/check/typeparams.go2 index badda01105..badda01105 100644 --- a/src/cmd/compile/internal/types2/testdata/typeparams.go2 +++ b/src/cmd/compile/internal/types2/testdata/check/typeparams.go2 diff --git a/src/cmd/compile/internal/types2/testdata/vardecl.src b/src/cmd/compile/internal/types2/testdata/check/vardecl.src index 9e48cdf847..9e48cdf847 100644 --- a/src/cmd/compile/internal/types2/testdata/vardecl.src +++ b/src/cmd/compile/internal/types2/testdata/check/vardecl.src diff --git a/src/cmd/compile/internal/types2/testdata/examples/functions.go2 b/src/cmd/compile/internal/types2/testdata/examples/functions.go2 new file mode 100644 index 0000000000..0c2a408f02 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/examples/functions.go2 @@ -0,0 +1,215 @@ +// Copyright 2019 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. + +// This file shows some examples of type-parameterized functions. + +package p + +// Reverse is a generic function that takes a []T argument and +// reverses that slice in place. +func Reverse[T any](list []T) { + i := 0 + j := len(list)-1 + for i < j { + list[i], list[j] = list[j], list[i] + i++ + j-- + } +} + +func _() { + // Reverse can be called with an explicit type argument. + Reverse[int](nil) + Reverse[string]([]string{"foo", "bar"}) + Reverse[struct{x, y int}]([]struct{x, y int}{{1, 2}, {2, 3}, {3, 4}}) + + // Since the type parameter is used for an incoming argument, + // it can be inferred from the provided argument's type. + Reverse([]string{"foo", "bar"}) + Reverse([]struct{x, y int}{{1, 2}, {2, 3}, {3, 4}}) + + // But the incoming argument must have a type, even if it's a + // default type. An untyped nil won't work. + // Reverse(nil) // this won't type-check + + // A typed nil will work, though. + Reverse([]int(nil)) +} + +// Certain functions, such as the built-in `new` could be written using +// type parameters. +func new[T any]() *T { + var x T + return &x +} + +// When calling our own `new`, we need to pass the type parameter +// explicitly since there is no (value) argument from which the +// result type could be inferred. We don't try to infer the +// result type from the assignment to keep things simple and +// easy to understand. +var _ = new[int]() +var _ *float64 = new[float64]() // the result type is indeed *float64 + +// A function may have multiple type parameters, of course. +func foo[A, B, C any](a A, b []B, c *C) B { + // do something here + return b[0] +} + +// As before, we can pass type parameters explicitly. +var s = foo[int, string, float64](1, []string{"first"}, new[float64]()) + +// Or we can use type inference. +var _ float64 = foo(42, []float64{1.0}, &s) + +// Type inference works in a straight-forward manner even +// for variadic functions. +func variadic[A, B any](A, B, ...B) int + +// var _ = variadic(1) // ERROR not enough arguments +var _ = variadic(1, 2.3) +var _ = variadic(1, 2.3, 3.4, 4.5) +var _ = variadic[int, float64](1, 2.3, 3.4, 4) + +// Type inference also works in recursive function calls where +// the inferred type is the type parameter of the caller. +func f1[T any](x T) { + f1(x) +} + +func f2a[T any](x, y T) { + f2a(x, y) +} + +func f2b[T any](x, y T) { + f2b(y, x) +} + +func g2a[P, Q any](x P, y Q) { + g2a(x, y) +} + +func g2b[P, Q any](x P, y Q) { + g2b(y, x) +} + +// Here's an example of a recursive function call with variadic +// arguments and type inference inferring the type parameter of +// the caller (i.e., itself). +func max[T interface{ type int }](x ...T) T { + var x0 T + if len(x) > 0 { + x0 = x[0] + } + if len(x) > 1 { + x1 := max(x[1:]...) + if x1 > x0 { + return x1 + } + } + return x0 +} + +// When inferring channel types, the channel direction is ignored +// for the purpose of type inference. Once the type has been in- +// fered, the usual parameter passing rules are applied. +// Thus even if a type can be inferred successfully, the function +// call may not be valid. + +func fboth[T any](chan T) +func frecv[T any](<-chan T) +func fsend[T any](chan<- T) + +func _() { + var both chan int + var recv <-chan int + var send chan<-int + + fboth(both) + fboth(recv /* ERROR cannot use */ ) + fboth(send /* ERROR cannot use */ ) + + frecv(both) + frecv(recv) + frecv(send /* ERROR cannot use */ ) + + fsend(both) + fsend(recv /* ERROR cannot use */) + fsend(send) +} + +func ffboth[T any](func(chan T)) +func ffrecv[T any](func(<-chan T)) +func ffsend[T any](func(chan<- T)) + +func _() { + var both func(chan int) + var recv func(<-chan int) + var send func(chan<- int) + + ffboth(both) + ffboth(recv /* ERROR cannot use */ ) + ffboth(send /* ERROR cannot use */ ) + + ffrecv(both /* ERROR cannot use */ ) + ffrecv(recv) + ffrecv(send /* ERROR cannot use */ ) + + ffsend(both /* ERROR cannot use */ ) + ffsend(recv /* ERROR cannot use */ ) + ffsend(send) +} + +// When inferring elements of unnamed composite parameter types, +// if the arguments are defined types, use their underlying types. +// Even though the matching types are not exactly structurally the +// same (one is a type literal, the other a named type), because +// assignment is permitted, parameter passing is permitted as well, +// so type inference should be able to handle these cases well. + +func g1[T any]([]T) +func g2[T any]([]T, T) +func g3[T any](*T, ...T) + +func _() { + type intSlize []int + g1([]int{}) + g1(intSlize{}) + g2(nil, 0) + + type myString string + var s1 string + g3(nil, "1", myString("2"), "3") + g3(&s1, "1", myString /* ERROR does not match */ ("2"), "3") + _ = s1 + + type myStruct struct{x int} + var s2 myStruct + g3(nil, struct{x int}{}, myStruct{}) + g3(&s2, struct{x int}{}, myStruct{}) + g3(nil, myStruct{}, struct{x int}{}) + g3(&s2, myStruct{}, struct{x int}{}) +} + +// Here's a realistic example. + +func append[T any](s []T, t ...T) []T + +func _() { + var f func() + type Funcs []func() + var funcs Funcs + _ = append(funcs, f) +} + +// Generic type declarations cannot have empty type parameter lists +// (that would indicate a slice type). Thus, generic functions cannot +// have empty type parameter lists, either. This is a syntax error. + +func h[] /* ERROR empty type parameter list */ () + +func _() { + h[] /* ERROR operand */ () +} diff --git a/src/cmd/compile/internal/types2/testdata/examples/inference.go2 b/src/cmd/compile/internal/types2/testdata/examples/inference.go2 new file mode 100644 index 0000000000..b47ce75805 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/examples/inference.go2 @@ -0,0 +1,101 @@ +// Copyright 2021 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. + +// This file shows some examples of type inference. + +package p + +type Ordered interface { + type int, float64, string +} + +func min[T Ordered](x, y T) T + +func _() { + // min can be called with explicit instantiation. + _ = min[int](1, 2) + + // Alternatively, the type argument can be inferred from + // one of the arguments. Untyped arguments will be considered + // last. + var x int + _ = min(x, x) + _ = min(x, 1) + _ = min(x, 1.0) + _ = min(1, 2) + _ = min(1, 2.3 /* ERROR default type float64 .* does not match */ ) + + var y float64 + _ = min(1, y) + _ = min(1.2, y) + _ = min(1.2, 3.4) + _ = min(1.2, 3 /* ERROR default type int .* does not match */ ) + + var s string + _ = min(s, "foo") + _ = min("foo", "bar") +} + +func mixed[T1, T2, T3 any](T1, T2, T3) + +func _() { + // mixed can be called with explicit instantiation. + mixed[int, string, bool](0, "", false) + + // Alternatively, partial type arguments may be provided + // (from left to right), and the other may be inferred. + mixed[int, string](0, "", false) + mixed[int](0, "", false) + mixed(0, "", false) + + // Provided type arguments always take precedence over + // inferred types. + mixed[int, string](1.1 /* ERROR cannot use 1.1 */ , "", false) +} + +func related1[Slice interface{type []Elem}, Elem any](s Slice, e Elem) + +func _() { + // related1 can be called with explicit instantiation. + var si []int + related1[[]int, int](si, 0) + + // Alternatively, the 2nd type argument can be inferred + // from the first one through constraint type inference. + var ss []string + _ = related1[[]string] + related1[[]string](ss, "foo") + + // A type argument inferred from another explicitly provided + // type argument overrides whatever value argument type is given. + related1[[]string](ss, 0 /* ERROR cannot use 0 */ ) + + // A type argument may be inferred from a value argument + // and then help infer another type argument via constraint + // type inference. + related1(si, 0) + related1(si, "foo" /* ERROR cannot use "foo" */ ) +} + +func related2[Elem any, Slice interface{type []Elem}](e Elem, s Slice) + +func _() { + // related2 can be called with explicit instantiation. + var si []int + related2[int, []int](0, si) + + // Alternatively, the 2nd type argument can be inferred + // from the first one through constraint type inference. + var ss []string + _ = related2[string] + related2[string]("foo", ss) + + // A type argument may be inferred from a value argument + // and then help infer another type argument via constraint + // type inference. Untyped arguments are always considered + // last. + related2(1.2, []float64{}) + related2(1.0, []int{}) + related2( /* ERROR does not satisfy */ float64(1.0), []int{}) // TODO(gri) fix error position +} diff --git a/src/cmd/compile/internal/types2/testdata/examples/methods.go2 b/src/cmd/compile/internal/types2/testdata/examples/methods.go2 new file mode 100644 index 0000000000..76c6539e1b --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/examples/methods.go2 @@ -0,0 +1,96 @@ +// Copyright 2019 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. + +// This file shows some examples of methods on type-parameterized types. + +package p + +// Parameterized types may have methods. +type T1[A any] struct{ a A } + +// When declaring a method for a parameterized type, the "instantiated" +// receiver type acts as an implicit declaration of the type parameters +// for the receiver type. In the example below, method m1 on type T1 has +// the receiver type T1[A] which declares the type parameter A for use +// with this method. That is, within the method m1, A stands for the +// actual type argument provided to an instantiated T1. +func (t T1[A]) m1() A { return t.a } + +// For instance, if T1 is instantiated with the type int, the type +// parameter A in m1 assumes that type (int) as well and we can write +// code like this: +var x T1[int] +var _ int = x.m1() + +// Because the type parameter provided to a parameterized receiver type +// is declared through that receiver declaration, it must be an identifier. +// It cannot possibly be some other type because the receiver type is not +// instantiated with concrete types, it is standing for the parameterized +// receiver type. +func (t T1[[ /* ERROR must be an identifier */ ]int]) m2() {} + +// Note that using what looks like a predeclared identifier, say int, +// as type parameter in this situation is deceptive and considered bad +// style. In m3 below, int is the name of the local receiver type parameter +// and it shadows the predeclared identifier int which then cannot be used +// anymore as expected. +// This is no different from locally redelaring a predeclared identifier +// and usually should be avoided. There are some notable exceptions; e.g., +// sometimes it makes sense to use the identifier "copy" which happens to +// also be the name of a predeclared built-in function. +func (t T1[int]) m3() { var _ int = 42 /* ERROR cannot use 42 .* as int */ } + +// The names of the type parameters used in a parameterized receiver +// type don't have to match the type parameter names in the declaration +// of the type used for the receiver. In our example, even though T1 is +// declared with type parameter named A, methods using that receiver type +// are free to use their own name for that type parameter. That is, the +// name of type parameters is always local to the declaration where they +// are introduced. In our example we can write a method m2 and use the +// name X instead of A for the type parameter w/o any difference. +func (t T1[X]) m4() X { return t.a } + +// If the receiver type is parameterized, type parameters must always be +// provided: this simply follows from the general rule that a parameterized +// type must be instantiated before it can be used. A method receiver +// declaration using a parameterized receiver type is no exception. It is +// simply that such receiver type expressions perform two tasks simultaneously: +// they declare the (local) type parameters and then use them to instantiate +// the receiver type. Forgetting to provide a type parameter leads to an error. +func (t T1 /* ERROR generic type .* without instantiation */ ) m5() {} + +// However, sometimes we don't need the type parameter, and thus it is +// inconvenient to have to choose a name. Since the receiver type expression +// serves as a declaration for its type parameters, we are free to choose the +// blank identifier: +func (t T1[_]) m6() {} + +// Naturally, these rules apply to any number of type parameters on the receiver +// type. Here are some more complex examples. +type T2[A, B, C any] struct { + a A + b B + c C +} + +// Naming of the type parameters is local and has no semantic impact: +func (t T2[A, B, C]) m1() (A, B, C) { return t.a, t.b, t.c } +func (t T2[C, B, A]) m2() (C, B, A) { return t.a, t.b, t.c } +func (t T2[X, Y, Z]) m3() (X, Y, Z) { return t.a, t.b, t.c } + +// Type parameters may be left blank if they are not needed: +func (t T2[A, _, C]) m4() (A, C) { return t.a, t.c } +func (t T2[_, _, X]) m5() X { return t.c } +func (t T2[_, _, _]) m6() {} + +// As usual, blank names may be used for any object which we don't care about +// using later. For instance, we may write an unnamed method with a receiver +// that cannot be accessed: +func (_ T2[_, _, _]) _() int { return 42 } + +// Because a receiver parameter list is simply a parameter list, we can +// leave the receiver argument away for receiver types. +type T0 struct{} +func (T0) _() {} +func (T1[A]) _() {} diff --git a/src/cmd/compile/internal/types2/testdata/examples/types.go2 b/src/cmd/compile/internal/types2/testdata/examples/types.go2 new file mode 100644 index 0000000000..a7825ed2d9 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/examples/types.go2 @@ -0,0 +1,279 @@ +// Copyright 2019 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. + +// This file shows some examples of generic types. + +package p + +// List is just what it says - a slice of E elements. +type List[E any] []E + +// A generic (parameterized) type must always be instantiated +// before it can be used to designate the type of a variable +// (including a struct field, or function parameter); though +// for the latter cases, the provided type may be another type +// parameter. So: +var _ List[byte] = []byte{} + +// A generic binary tree might be declared as follows. +type Tree[E any] struct { + left, right *Tree[E] + payload E +} + +// A simple instantiation of Tree: +var root1 Tree[int] + +// The actual type parameter provided may be a generic type itself: +var root2 Tree[List[int]] + +// A couple of more complex examples. +// We don't need extra parentheses around the element type of the slices on +// the right (unlike when we use ()'s rather than []'s for type parameters). +var _ List[List[int]] = []List[int]{} +var _ List[List[List[Tree[int]]]] = []List[List[Tree[int]]]{} + +// Type parameters act like type aliases when used in generic types +// in the sense that we can "emulate" a specific type instantiation +// with type aliases. +type T1[P any] struct { + f P +} + +type T2[P any] struct { + f struct { + g P + } +} + +var x1 T1[struct{ g int }] +var x2 T2[int] + +func _() { + // This assignment is invalid because the types of x1, x2 are T1(...) + // and T2(...) respectively, which are two different defined types. + x1 = x2 // ERROR assignment + + // This assignment is valid because the types of x1.f and x2.f are + // both struct { g int }; the type parameters act like type aliases + // and their actual names don't come into play here. + x1.f = x2.f +} + +// We can verify this behavior using type aliases instead: +type T1a struct { + f A1 +} +type A1 = struct { g int } + +type T2a struct { + f struct { + g A2 + } +} +type A2 = int + +var x1a T1a +var x2a T2a + +func _() { + x1a = x2a // ERROR assignment + x1a.f = x2a.f +} + +// Another interesting corner case are generic types that don't use +// their type arguments. For instance: +type T[P any] struct{} + +var xint T[int] +var xbool T[bool] + +// Are these two variables of the same type? After all, their underlying +// types are identical. We consider them to be different because each type +// instantiation creates a new named type, in this case T<int> and T<bool> +// even if their underlying types are identical. This is sensible because +// we might still have methods that have different signatures or behave +// differently depending on the type arguments, and thus we can't possibly +// consider such types identical. Consequently: +func _() { + xint = xbool // ERROR assignment +} + +// Generic types cannot be used without instantiation. +var _ T // ERROR cannot use generic type T + +// In type context, generic (parameterized) types cannot be parenthesized before +// being instantiated. See also NOTES entry from 12/4/2019. +var _ (T /* ERROR cannot use generic type T */ )[ /* ERROR unexpected \[ */ int] + +// All types may be parameterized, including interfaces. +type I1[T any] interface{ + m1(T) +} + +// There is no such thing as a variadic generic type. +type _[T ... /* ERROR invalid use of ... */ interface{}] struct{} + +// Generic interfaces may be embedded as one would expect. +type I2 interface { + I1(int) // method! + I1[string] // embedded I1 +} + +func _() { + var x I2 + x.I1(0) + x.m1("foo") +} + +type I0 interface { + m0() +} + +type I3 interface { + I0 + I1[bool] + m(string) +} + +func _() { + var x I3 + x.m0() + x.m1(true) + x.m("foo") +} + +type _ struct { + ( /* ERROR cannot parenthesize */ int8) + ( /* ERROR cannot parenthesize */ *int16) + *( /* ERROR cannot parenthesize */ int32) + List[int] + + int8 /* ERROR int8 redeclared */ + * /* ERROR int16 redeclared */ int16 + List /* ERROR List redeclared */ [int] +} + +// It's possible to declare local types whose underlying types +// are type parameters. As with ordinary type definitions, the +// types underlying properties are "inherited" but the methods +// are not. +func _[T interface{ m(); type int }]() { + type L T + var x L + + // m is not defined on L (it is not "inherited" from + // its underlying type). + x.m /* ERROR x.m undefined */ () + + // But the properties of T, such that as that it supports + // the operations of the types given by its type bound, + // are also the properties of L. + x++ + _ = x - x + + // On the other hand, if we define a local alias for T, + // that alias stands for T as expected. + type A = T + var y A + y.m() + _ = y < 0 +} + +// As a special case, an explicit type argument may be omitted +// from a type parameter bound if the type bound expects exactly +// one type argument. In that case, the type argument is the +// respective type parameter to which the type bound applies. +// Note: We may not permit this syntactic sugar at first. +// Note: This is now disabled. All examples below are adjusted. +type Adder[T any] interface { + Add(T) T +} + +// We don't need to explicitly instantiate the Adder bound +// if we have exactly one type parameter. +func Sum[T Adder[T]](list []T) T { + var sum T + for _, x := range list { + sum = sum.Add(x) + } + return sum +} + +// Valid and invalid variations. +type B0 interface {} +type B1[_ any] interface{} +type B2[_, _ any] interface{} + +func _[T1 B0]() +func _[T1 B1[T1]]() +func _[T1 B2 /* ERROR cannot use generic type .* without instantiation */ ]() + +func _[T1, T2 B0]() +func _[T1 B1[T1], T2 B1[T2]]() +func _[T1, T2 B2 /* ERROR cannot use generic type .* without instantiation */ ]() + +func _[T1 B0, T2 B1[T2]]() // here B1 applies to T2 + +// When the type argument is left away, the type bound is +// instantiated for each type parameter with that type +// parameter. +// Note: We may not permit this syntactic sugar at first. +func _[A Adder[A], B Adder[B], C Adder[A]]() { + var a A // A's type bound is Adder[A] + a = a.Add(a) + var b B // B's type bound is Adder[B] + b = b.Add(b) + var c C // C's type bound is Adder[A] + a = c.Add(a) +} + +// The type of variables (incl. parameters and return values) cannot +// be an interface with type constraints or be/embed comparable. +type I interface { + type int +} + +var ( + _ interface /* ERROR contains type constraints */ {type int} + _ I /* ERROR contains type constraints */ +) + +func _(I /* ERROR contains type constraints */ ) +func _(x, y, z I /* ERROR contains type constraints */ ) +func _() I /* ERROR contains type constraints */ + +func _() { + var _ I /* ERROR contains type constraints */ +} + +type C interface { + comparable +} + +var _ comparable /* ERROR comparable */ +var _ C /* ERROR comparable */ + +func _(_ comparable /* ERROR comparable */ , _ C /* ERROR comparable */ ) + +func _() { + var _ comparable /* ERROR comparable */ + var _ C /* ERROR comparable */ +} + +// Type parameters are never const types, i.e., it's +// not possible to declare a constant of type parameter type. +// (If a type list contains just a single const type, we could +// allow it, but such type lists don't make much sense in the +// first place.) +func _[T interface { type int, float64 }]() { + // not valid + const _ = T /* ERROR not constant */ (0) + const _ T /* ERROR invalid constant type T */ = 1 + + // valid + var _ = T(0) + var _ T = 1 + _ = T(0) +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue20583.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue20583.src new file mode 100644 index 0000000000..85f11ecd38 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue20583.src @@ -0,0 +1,12 @@ +// Copyright 2020 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 issue20583 +const ( + _ = 6e886451608 /* ERROR malformed constant */ /2 + _ = 6e886451608i /* ERROR malformed constant */ /2 + _ = 0 * 1e+1000000000 // ERROR malformed constant + x = 1e100000000 + _ = x*x*x*x*x*x* /* ERROR not representable */ x +) diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue23203a.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue23203a.src new file mode 100644 index 0000000000..48cb5889cd --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue23203a.src @@ -0,0 +1,14 @@ +// Copyright 2018 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 main + +import "unsafe" + +type T struct{} + +func (T) m1() {} +func (T) m2([unsafe.Sizeof(T.m1)]int) {} + +func main() {} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue23203b.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue23203b.src new file mode 100644 index 0000000000..638ec6c5ce --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue23203b.src @@ -0,0 +1,14 @@ +// Copyright 2018 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 main + +import "unsafe" + +type T struct{} + +func (T) m2([unsafe.Sizeof(T.m1)]int) {} +func (T) m1() {} + +func main() {} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue26390.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue26390.src new file mode 100644 index 0000000000..b8e67e9bdd --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue26390.src @@ -0,0 +1,11 @@ +// Copyright 2018 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 issue26390 + +type A = T + +func (t *T) m() *A { return t } + +type T struct{} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue28251.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue28251.src new file mode 100644 index 0000000000..ef5e61df47 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue28251.src @@ -0,0 +1,65 @@ +// Copyright 2018 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. + +// This file contains test cases for various forms of +// method receiver declarations, per the spec clarification +// https://golang.org/cl/142757. + +package issue28251 + +// test case from issue28251 +type T struct{} + +type T0 = *T + +func (T0) m() {} + +func _() { (&T{}).m() } + +// various alternative forms +type ( + T1 = (((T))) +) + +func ((*(T1))) m1() {} +func _() { (T{}).m2() } +func _() { (&T{}).m2() } + +type ( + T2 = (((T3))) + T3 = T +) + +func (T2) m2() {} +func _() { (T{}).m2() } +func _() { (&T{}).m2() } + +type ( + T4 = ((*(T5))) + T5 = T +) + +func (T4) m4() {} +func _() { (T{}).m4 /* ERROR "cannot call pointer method m4 on T" */ () } +func _() { (&T{}).m4() } + +type ( + T6 = (((T7))) + T7 = (*(T8)) + T8 = T +) + +func (T6) m6() {} +func _() { (T{}).m6 /* ERROR "cannot call pointer method m6 on T" */ () } +func _() { (&T{}).m6() } + +type ( + T9 = *T10 + T10 = *T11 + T11 = T +) + +func (T9 /* ERROR invalid receiver type \*\*T */ ) m9() {} +func _() { (T{}).m9 /* ERROR has no field or method m9 */ () } +func _() { (&T{}).m9 /* ERROR has no field or method m9 */ () } diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39634.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39634.go2 new file mode 100644 index 0000000000..2c1299feb0 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39634.go2 @@ -0,0 +1,91 @@ +// Copyright 2020 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. + +// Examples adjusted to match new [T any] syntax for type parameters. +// Also, previously permitted empty type parameter lists and instantiations +// are now syntax errors. + +package p + +// crash 1 +type nt1[_ any]interface{g /* ERROR undeclared name */ } +type ph1[e nt1[e],g(d /* ERROR undeclared name */ )]s /* ERROR undeclared name */ +func(*ph1[e,e /* ERROR redeclared */ ])h(d /* ERROR undeclared name */ ) + +// crash 2 +// Disabled: empty []'s are now syntax errors. This example leads to too many follow-on errors. +// type Numeric2 interface{t2 /* ERROR not a type */ } +// func t2[T Numeric2](s[]T){0 /* ERROR not a type */ []{s /* ERROR cannot index */ [0][0]}} + +// crash 3 +type t3 *interface{ t3.p /* ERROR no field or method p */ } + +// crash 4 +type Numeric4 interface{t4 /* ERROR not a type */ } +func t4[T Numeric4](s[]T){if( /* ERROR non-boolean */ 0){*s /* ERROR cannot indirect */ [0]}} + +// crash 7 +type foo7 interface { bar() } +type x7[A any] struct{ foo7 } +func main7() { var _ foo7 = x7[int]{} } + +// crash 8 +type foo8[A any] interface { type A } +func bar8[A foo8[A]](a A) {} +func main8() {} + +// crash 9 +type foo9[A any] interface { type foo9 /* ERROR interface contains type constraints */ [A] } +func _() { var _ = new(foo9 /* ERROR interface contains type constraints */ [int]) } + +// crash 12 +var u /* ERROR cycle */ , i [func /* ERROR used as value */ /* ERROR used as value */ (u, c /* ERROR undeclared */ /* ERROR undeclared */ ) {}(0, len /* ERROR must be called */ /* ERROR must be called */ )]c /* ERROR undeclared */ /* ERROR undeclared */ + +// crash 15 +func y15() { var a /* ERROR declared but not used */ interface{ p() } = G15[string]{} } +type G15[X any] s /* ERROR undeclared name */ +func (G15 /* ERROR generic type .* without instantiation */ ) p() + +// crash 16 +type Foo16[T any] r16 /* ERROR not a type */ +func r16[T any]() Foo16[Foo16[T]] + +// crash 17 +type Y17 interface{ c() } +type Z17 interface { + c() Y17 + Y17 /* ERROR duplicate method */ +} +func F17[T Z17](T) + +// crash 18 +type o18[T any] []func(_ o18[[]_ /* ERROR cannot use _ */ ]) + +// crash 19 +type Z19 [][[]Z19{}[0][0]]c19 /* ERROR undeclared */ + +// crash 20 +type Z20 /* ERROR illegal cycle */ interface{ Z20 } +func F20[t Z20]() { F20(t /* ERROR invalid composite literal type */ {}) } + +// crash 21 +type Z21 /* ERROR illegal cycle */ interface{ Z21 } +func F21[T Z21]() { ( /* ERROR not used */ F21[Z21]) } + +// crash 24 +type T24[P any] P +func (r T24[P]) m() { T24 /* ERROR without instantiation */ .m() } + +// crash 25 +type T25[A any] int +func (t T25[A]) m1() {} +var x T25 /* ERROR without instantiation */ .m1 + +// crash 26 +type T26 = interface{ F26[ /* ERROR cannot have type parameters */ Z any]() } +func F26[Z any]() T26 { return F26 /* ERROR without instantiation */ /* ERROR missing method */ [] /* ERROR operand */ } + +// crash 27 +func e27[T any]() interface{ x27 /* ERROR not a type */ } +func x27() { e27( /* ERROR cannot infer T */ ) }
\ No newline at end of file diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39664.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39664.go2 new file mode 100644 index 0000000000..3b3ec56980 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39664.go2 @@ -0,0 +1,15 @@ +// Copyright 2020 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 + +type T[_ any] struct {} + +func (T /* ERROR instantiation */ ) m() + +func _() { + var x interface { m() } + x = T[int]{} + _ = x +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39680.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39680.go2 new file mode 100644 index 0000000000..9bc26f3546 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39680.go2 @@ -0,0 +1,27 @@ +// Copyright 2020 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 "fmt" + +// Minimal test case. +func _[T interface{type T}](x T) T{ + return x +} + +// Test case from issue. +type constr[T any] interface { + type T +} + +func Print[T constr[T]](s []T) { + for _, v := range s { + fmt.Print(v) + } +} + +func f() { + Print([]string{"Hello, ", "playground\n"}) +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39693.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39693.go2 new file mode 100644 index 0000000000..316ab1982e --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39693.go2 @@ -0,0 +1,14 @@ +// Copyright 2020 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 + +type Number interface { + int /* ERROR int is not an interface */ + float64 /* ERROR float64 is not an interface */ +} + +func Add[T Number](a, b T) T { + return a /* ERROR not defined */ + b +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39699.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39699.go2 new file mode 100644 index 0000000000..75491e7e26 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39699.go2 @@ -0,0 +1,29 @@ +// Copyright 2020 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 + +type T0 interface{ +} + +type T1 interface{ + type int +} + +type T2 interface{ + comparable +} + +type T3 interface { + T0 + T1 + T2 +} + +func _() { + _ = T0(0) + _ = T1 /* ERROR cannot use interface T1 in conversion */ (1) + _ = T2 /* ERROR cannot use interface T2 in conversion */ (2) + _ = T3 /* ERROR cannot use interface T3 in conversion */ (3) +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39711.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39711.go2 new file mode 100644 index 0000000000..df621a4c17 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39711.go2 @@ -0,0 +1,11 @@ +// Copyright 2020 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 + +// Do not report a duplicate type error for this type list. +// (Check types after interfaces have been completed.) +type _ interface { + type interface{ Error() string }, interface{ String() string } +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39723.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39723.go2 new file mode 100644 index 0000000000..55464e6b77 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39723.go2 @@ -0,0 +1,9 @@ +// Copyright 2020 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 + +// A constraint must be an interface; it cannot +// be a type parameter, for instance. +func _[A interface{ type interface{} }, B A /* ERROR not an interface */ ]() diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39725.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39725.go2 new file mode 100644 index 0000000000..e19b6770bf --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39725.go2 @@ -0,0 +1,16 @@ +// Copyright 2020 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 + +func f1[T1, T2 any](T1, T2, struct{a T1; b T2}) +func _() { + f1(42, string("foo"), struct /* ERROR does not match inferred type struct\{a int; b string\} */ {a, b int}{}) +} + +// simplified test case from issue +func f2[T any](_ []T, _ func(T)) +func _() { + f2([]string{}, func /* ERROR does not match inferred type func\(string\) */ (f []byte) {}) +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39754.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39754.go2 new file mode 100644 index 0000000000..f70b8d0ce0 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39754.go2 @@ -0,0 +1,23 @@ +// Copyright 2020 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
+
+type Optional[T any] struct {}
+
+func (_ Optional[T]) Val() (T, bool)
+
+type Box[T any] interface {
+ Val() (T, bool)
+}
+
+func f[V interface{}, A, B Box[V]]() {}
+
+func _() {
+ f[int, Optional[int], Optional[int]]()
+ _ = f[int, Optional[int], Optional /* ERROR does not satisfy Box */ [string]]
+ // TODO(gri) Provide better position information here.
+ // See TODO in call.go, Checker.arguments.
+ f[int, Optional[int], Optional[string]]( /* ERROR does not satisfy Box */ )
+}
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39755.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39755.go2 new file mode 100644 index 0000000000..b7ab68818e --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39755.go2 @@ -0,0 +1,23 @@ +// Copyright 2020 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 + +func _[T interface{type map[string]int}](x T) { + _ = x == nil +} + +// simplified test case from issue + +type PathParamsConstraint interface { + type map[string]string, []struct{key, value string} +} + +type PathParams[T PathParamsConstraint] struct { + t T +} + +func (pp *PathParams[T]) IsNil() bool { + return pp.t == nil // this must succeed +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39768.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39768.go2 new file mode 100644 index 0000000000..abac141d7f --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39768.go2 @@ -0,0 +1,20 @@ +// Copyright 2020 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 + +type T[P any] P +type A = T +var x A[int] +var _ A /* ERROR cannot use generic type */ + +type B = T[int] +var y B = x +var _ B /* ERROR not a generic type */ [int] + +// test case from issue + +type Vector[T any] []T +type VectorAlias = Vector +var v Vector[int] diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39938.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39938.go2 new file mode 100644 index 0000000000..76e7e369ca --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39938.go2 @@ -0,0 +1,50 @@ +// Copyright 2020 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. + +// Check "infinite expansion" cycle errors across instantiated types. + +package p + +type E0[P any] P +type E1[P any] *P +type E2[P any] struct{ P } +type E3[P any] struct{ *P } + +type T0 /* ERROR illegal cycle */ struct { + _ E0[T0] +} + +type T0_ /* ERROR illegal cycle */ struct { + E0[T0_] +} + +type T1 struct { + _ E1[T1] +} + +type T2 /* ERROR illegal cycle */ struct { + _ E2[T2] +} + +type T3 struct { + _ E3[T3] +} + +// some more complex cases + +type T4 /* ERROR illegal cycle */ struct { + _ E0[E2[T4]] +} + +type T5 struct { + _ E0[E2[E0[E1[E2[[10]T5]]]]] +} + +type T6 /* ERROR illegal cycle */ struct { + _ E0[[10]E2[E0[E2[E2[T6]]]]] +} + +type T7 struct { + _ E0[[]E2[E0[E2[E2[T6]]]]] +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39948.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39948.go2 new file mode 100644 index 0000000000..c2b460902c --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39948.go2 @@ -0,0 +1,9 @@ +// Copyright 2020 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 + +type T[P any] interface{ + P // ERROR P is a type parameter, not an interface +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39976.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39976.go2 new file mode 100644 index 0000000000..3db4eae012 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39976.go2 @@ -0,0 +1,16 @@ +// Copyright 2020 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 + +type policy[K, V any] interface{} +type LRU[K, V any] struct{} + +func NewCache[K, V any](p policy[K, V]) + +func _() { + var lru LRU[int, string] + NewCache[int, string](&lru) + NewCache(& /* ERROR does not match policy\[K, V\] \(cannot infer K and V\) */ lru) +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39982.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39982.go2 new file mode 100644 index 0000000000..9810b6386a --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue39982.go2 @@ -0,0 +1,36 @@ +// Copyright 2020 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 + +type ( + T[_ any] struct{} + S[_ any] struct { + data T[*T[int]] + } +) + +func _() { + _ = S[int]{ + data: T[*T[int]]{}, + } +} + +// full test case from issue + +type ( + Element[TElem any] struct{} + + entry[K comparable] struct{} + + Cache[K comparable] struct { + data map[K]*Element[*entry[K]] + } +) + +func _() { + _ = Cache[int]{ + data: make(map[int](*Element[*entry[int]])), + } +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40038.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40038.go2 new file mode 100644 index 0000000000..8948d61caa --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40038.go2 @@ -0,0 +1,15 @@ +// Copyright 2020 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 + +type A[T any] int + +func (A[T]) m(A[T]) + +func f[P interface{m(P)}]() + +func _() { + _ = f[A[int]] +}
\ No newline at end of file diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40056.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40056.go2 new file mode 100644 index 0000000000..747aab49dd --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40056.go2 @@ -0,0 +1,15 @@ +// Copyright 2020 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 + +func _() { + NewS( /* ERROR cannot infer T */ ) .M() +} + +type S struct {} + +func NewS[T any]() *S + +func (_ *S /* ERROR S is not a generic type */ [T]) M() diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40057.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40057.go2 new file mode 100644 index 0000000000..fdc8fb1c00 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40057.go2 @@ -0,0 +1,17 @@ +// Copyright 2020 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 + +func _() { + var x interface{} + switch t := x.(type) { + case S /* ERROR cannot use generic type */ : + t.m() + } +} + +type S[T any] struct {} + +func (_ S[T]) m() diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40301.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40301.go2 new file mode 100644 index 0000000000..5d97855f8a --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40301.go2 @@ -0,0 +1,12 @@ +// Copyright 2020 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 "unsafe" + +func _[T any](x T) { + _ = unsafe /* ERROR undefined */ .Alignof(x) + _ = unsafe /* ERROR undefined */ .Sizeof(x) +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40684.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40684.go2 new file mode 100644 index 0000000000..0269c3a62c --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue40684.go2 @@ -0,0 +1,15 @@ +// Copyright 2020 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 + +type T[_ any] int + +func f[_ any]() +func g[_, _ any]() + +func _() { + _ = f[T /* ERROR without instantiation */ ] + _ = g[T /* ERROR without instantiation */ , T /* ERROR without instantiation */ ] +}
\ No newline at end of file diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue41124.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue41124.go2 new file mode 100644 index 0000000000..61f766bcbd --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue41124.go2 @@ -0,0 +1,91 @@ +// Copyright 2020 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 + +// Test case from issue. + +type Nat interface { + type Zero, Succ +} + +type Zero struct{} +type Succ struct{ + Nat // ERROR interface contains type constraints +} + +// Struct tests. + +type I1 interface { + comparable +} + +type I2 interface { + type int +} + +type I3 interface { + I1 + I2 +} + +type _ struct { + f I1 // ERROR interface is .* comparable +} + +type _ struct { + comparable // ERROR interface is .* comparable +} + +type _ struct{ + I1 // ERROR interface is .* comparable +} + +type _ struct{ + I2 // ERROR interface contains type constraints +} + +type _ struct{ + I3 // ERROR interface contains type constraints +} + +// General composite types. + +type ( + _ [10]I1 // ERROR interface is .* comparable + _ [10]I2 // ERROR interface contains type constraints + + _ []I1 // ERROR interface is .* comparable + _ []I2 // ERROR interface contains type constraints + + _ *I3 // ERROR interface contains type constraints + _ map[I1 /* ERROR interface is .* comparable */ ]I2 // ERROR interface contains type constraints + _ chan I3 // ERROR interface contains type constraints + _ func(I1 /* ERROR interface is .* comparable */ ) + _ func() I2 // ERROR interface contains type constraints +) + +// Other cases. + +var _ = [...]I3 /* ERROR interface contains type constraints */ {} + +func _(x interface{}) { + _ = x.(I3 /* ERROR interface contains type constraints */ ) +} + +type T1[_ any] struct{} +type T3[_, _, _ any] struct{} +var _ T1[I2 /* ERROR interface contains type constraints */ ] +var _ T3[int, I2 /* ERROR interface contains type constraints */ , float32] + +func f1[_ any]() int +var _ = f1[I2 /* ERROR interface contains type constraints */ ]() +func f3[_, _, _ any]() int +var _ = f3[int, I2 /* ERROR interface contains type constraints */ , float32]() + +func _(x interface{}) { + switch x.(type) { + case I2 /* ERROR interface contains type constraints */ : + } +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42695.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42695.src new file mode 100644 index 0000000000..d0d6200969 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42695.src @@ -0,0 +1,17 @@ +// Copyright 2020 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 issue42695 + +const _ = 6e5518446744 // ERROR malformed constant +const _ uint8 = 6e5518446744 // ERROR malformed constant + +var _ = 6e5518446744 // ERROR malformed constant +var _ uint8 = 6e5518446744 // ERROR malformed constant + +func f(x int) int { + return x + 6e5518446744 // ERROR malformed constant +} + +var _ = f(6e5518446744 /* ERROR malformed constant */ ) diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42758.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42758.go2 new file mode 100644 index 0000000000..698cb8a16b --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42758.go2 @@ -0,0 +1,33 @@ +// Copyright 2020 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 + +func _[T any](x interface{}){ + switch x.(type) { + case T: // ok to use a type parameter + case int: + } + + switch x.(type) { + case T: + case T /* ERROR duplicate case */ : + } +} + +type constraint interface { + type int +} + +func _[T constraint](x interface{}){ + switch x.(type) { + case T: // ok to use a type parameter even if type list contains int + case int: + } +} + +func _(x constraint /* ERROR contains type constraints */ ) { + switch x /* ERROR contains type constraints */ .(type) { + } +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42987.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42987.src new file mode 100644 index 0000000000..8aa3544272 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42987.src @@ -0,0 +1,8 @@ +// Copyright 2021 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. + +// Check that there is only one error (no follow-on errors). + +package p +var _ = [ /* ERROR invalid use of .* array */ ...]byte("foo") diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43087.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43087.src new file mode 100644 index 0000000000..85d4450139 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43087.src @@ -0,0 +1,43 @@ +// Copyright 2021 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 + +func _() { + a, b, b /* ERROR b repeated on left side of := */ := 1, 2, 3 + _ = a + _ = b +} + +func _() { + a, _, _ := 1, 2, 3 // multiple _'s ok + _ = a +} + +func _() { + var b int + a, b, b /* ERROR b repeated on left side of := */ := 1, 2, 3 + _ = a + _ = b +} + +func _() { + var a []int + a /* ERROR non-name .* on left side of := */ [0], b := 1, 2 + _ = a + _ = b +} + +func _() { + var a int + a, a /* ERROR a repeated on left side of := */ := 1, 2 + _ = a +} + +func _() { + var a, b int + a, b := /* ERROR no new variables on left side of := */ 1, 2 + _ = a + _ = b +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43110.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43110.src new file mode 100644 index 0000000000..4a46945239 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43110.src @@ -0,0 +1,43 @@ +// Copyright 2020 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 + +type P *struct{} + +func _() { + // want an error even if the switch is empty + var a struct{ _ func() } + switch a /* ERROR cannot switch on a */ { + } + + switch a /* ERROR cannot switch on a */ { + case a: // no follow-on error here + } + + // this is ok because f can be compared to nil + var f func() + switch f { + } + + switch f { + case nil: + } + + switch (func())(nil) { + case nil: + } + + switch (func())(nil) { + case f /* ERROR cannot compare */ : + } + + switch nil /* ERROR use of untyped nil in switch expression */ { + } + + // this is ok + switch P(nil) { + case P(nil): + } +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43124.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43124.src new file mode 100644 index 0000000000..7e48c2211b --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43124.src @@ -0,0 +1,16 @@ +// Copyright 2020 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 + +var _ = int(0 /* ERROR invalid use of \.\.\. in type conversion */ ...) + +// test case from issue + +type M []string + +var ( + x = []string{"a", "b"} + _ = M(x /* ERROR invalid use of \.\.\. in type conversion */ ...) +) diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43125.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43125.src new file mode 100644 index 0000000000..c2bd970e25 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43125.src @@ -0,0 +1,8 @@ +// Copyright 2020 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 + +var _ = new(- /* ERROR not a type */ 1) +var _ = new(1 /* ERROR not a type */ + 1) diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43190.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43190.src new file mode 100644 index 0000000000..ae42719ad7 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43190.src @@ -0,0 +1,19 @@ +// Copyright 2020 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 ; // ERROR missing import path +import +var /* ERROR missing import path */ _ int +import .; // ERROR missing import path + +import () +import (.) // ERROR missing import path +import ( + "fmt" + . +) // ERROR missing import path + +var _ = fmt.Println // avoid imported but not used error diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue44688.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue44688.go2 new file mode 100644 index 0000000000..512bfcc922 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue44688.go2 @@ -0,0 +1,83 @@ +// Copyright 2021 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 + +type A1[T any] struct{} + +func (*A1[T]) m1(T) {} + +type A2[T any] interface { + m2(T) +} + +type B1[T any] struct { + filler int + *A1[T] + A2[T] +} + +type B2[T any] interface { + A2[T] +} + +type C[T any] struct { + filler1 int + filler2 int + B1[T] +} + +type D[T any] struct { + filler1 int + filler2 int + filler3 int + C[T] +} + +func _() { + // calling embedded methods + var b1 B1[string] + + b1.A1.m1("") + b1.m1("") + + b1.A2.m2("") + b1.m2("") + + var b2 B2[string] + b2.m2("") + + // a deeper nesting + var d D[string] + d.m1("") + d.m2("") + + // calling method expressions + m1x := B1[string].m1 + m1x(b1, "") + m2x := B2[string].m2 + m2x(b2, "") + + // calling method values + m1v := b1.m1 + m1v("") + m2v := b1.m2 + m2v("") + b2v := b2.m2 + b2v("") +} + +// actual test case from issue + +type A[T any] struct{} + +func (*A[T]) f(T) {} + +type B[T any] struct{ A[T] } + +func _() { + var b B[string] + b.A.f("") + b.f("") +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue44799.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue44799.go2 new file mode 100644 index 0000000000..9e528a7475 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue44799.go2 @@ -0,0 +1,19 @@ +// Copyright 2021 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 main + +func Map[F, T any](s []F, f func(F) T) []T { return nil } + +func Reduce[Elem1, Elem2 any](s []Elem1, initializer Elem2, f func(Elem2, Elem1) Elem2) Elem2 { var x Elem2; return x } + +func main() { + var s []int + var f1 func(int) float64 + var f2 func(float64, int) float64 + _ = Map[int](s, f1) + _ = Map(s, f1) + _ = Reduce[int](s, 0, f2) + _ = Reduce(s, 0, f2) +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue45548.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue45548.go2 new file mode 100644 index 0000000000..b1e42497e8 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue45548.go2 @@ -0,0 +1,13 @@ +// Copyright 2021 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 + +func f[F interface{type *Q}, G interface{type *R}, Q, R any](q Q, r R) {} + +func _() { + f[*float64, *int](1, 2) + f[*float64](1, 2) + f(1, 2) +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue45635.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue45635.go2 new file mode 100644 index 0000000000..65662cdc76 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue45635.go2 @@ -0,0 +1,32 @@ +// Copyright 2021 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 main + +func main() { + some /* ERROR "undeclared name" */ [int, int]() +} + +type N[T any] struct{} + +var _ N[] /* ERROR expecting type */ + +type I interface { + type map[int]int, []int +} + +func _[T I](i, j int) { + var m map[int]int + _ = m[i, j /* ERROR more than one index */ ] + + var a [3]int + _ = a[i, j /* ERROR more than one index */ ] + + var s []int + _ = s[i, j /* ERROR more than one index */ ] + + var t T + // TODO(gri) fix multiple error below + _ = t[i, j /* ERROR more than one index */ /* ERROR more than one index */ ] +} diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue6977.src b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue6977.src new file mode 100644 index 0000000000..8f4e9ba2b2 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue6977.src @@ -0,0 +1,82 @@ +// Copyright 2019 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" + +// Alan's initial report. + +type I interface { f(); String() string } +type J interface { g(); String() string } + +type IJ1 = interface { I; J } +type IJ2 = interface { f(); g(); String() string } + +var _ = (*IJ1)(nil) == (*IJ2)(nil) // static assert that IJ1 and IJ2 are identical types + +// The canonical example. + +type ReadWriteCloser interface { io.ReadCloser; io.WriteCloser } + +// Some more cases. + +type M interface { m() } +type M32 interface { m() int32 } +type M64 interface { m() int64 } + +type U1 interface { m() } +type U2 interface { m(); M } +type U3 interface { M; m() } +type U4 interface { M; M; M } +type U5 interface { U1; U2; U3; U4 } + +type U6 interface { m(); m /* ERROR duplicate method */ () } +type U7 interface { M32 /* ERROR duplicate method */ ; m() } +type U8 interface { m(); M32 /* ERROR duplicate method */ } +type U9 interface { M32; M64 /* ERROR duplicate method */ } + +// Verify that repeated embedding of the same interface(s) +// eliminates duplicate methods early (rather than at the +// end) to prevent exponential memory and time use. +// Without early elimination, computing T29 may take dozens +// of minutes. +type ( + T0 interface { m() } + T1 interface { T0; T0 } + T2 interface { T1; T1 } + T3 interface { T2; T2 } + T4 interface { T3; T3 } + T5 interface { T4; T4 } + T6 interface { T5; T5 } + T7 interface { T6; T6 } + T8 interface { T7; T7 } + T9 interface { T8; T8 } + + T10 interface { T9; T9 } + T11 interface { T10; T10 } + T12 interface { T11; T11 } + T13 interface { T12; T12 } + T14 interface { T13; T13 } + T15 interface { T14; T14 } + T16 interface { T15; T15 } + T17 interface { T16; T16 } + T18 interface { T17; T17 } + T19 interface { T18; T18 } + + T20 interface { T19; T19 } + T21 interface { T20; T20 } + T22 interface { T21; T21 } + T23 interface { T22; T22 } + T24 interface { T23; T23 } + T25 interface { T24; T24 } + T26 interface { T25; T25 } + T27 interface { T26; T26 } + T28 interface { T27; T27 } + T29 interface { T28; T28 } +) + +// Verify that m is present. +var x T29 +var _ = x.m |