diff options
author | Robert Griesemer <gri@golang.org> | 2022-01-05 15:46:31 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2022-01-06 21:38:59 +0000 |
commit | 61014f00f24df8b144d9d235fe3e25ff64b96521 (patch) | |
tree | 9eaa26bbf277f58527faa5999c94c10e6d25da1e /test | |
parent | 2bfa6ef63d3cfa89f46cc5f6708c1078f15fb875 (diff) | |
download | go-61014f00f24df8b144d9d235fe3e25ff64b96521.tar.gz go-61014f00f24df8b144d9d235fe3e25ff64b96521.zip |
go/types, types2: implement field access for struct structural constraints
This change implements field the access p.f where the type of p
is a type parameter with a structural constraint that is a struct
with a field f. This is only the fix for the type checker. The
compiler will need a separate CL.
This makes the behavior consistent with the fact that we can
write struct composite literals for type parameters with a
struct structural type.
For #50417.
For #50233.
Change-Id: I87d07e016f97cbf19c45cde19165eae3ec0bad2b
Reviewed-on: https://go-review.googlesource.com/c/go/+/375795
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/typeparam/issue50417.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/test/typeparam/issue50417.go b/test/typeparam/issue50417.go new file mode 100644 index 0000000000..bf6ac424c5 --- /dev/null +++ b/test/typeparam/issue50417.go @@ -0,0 +1,70 @@ +// run + +// Copyright 2022 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() {} + +type Sf struct { + f int +} + +func f0[P Sf](p P) { + _ = p.f + p.f = 0 +} + +func f0t[P ~struct{ f int }](p P) { + _ = p.f + p.f = 0 +} + +// TODO(danscales) enable once the compiler is fixed +// var _ = f0[Sf] +// var _ = f0t[Sf] + +func f1[P interface { + Sf + m() +}](p P) { + _ = p.f + p.f = 0 + p.m() +} + +type Sm struct{} + +func (Sm) m() {} + +type Sfm struct { + f int +} + +func (Sfm) m() {} + +func f2[P interface { + Sfm + m() +}](p P) { + _ = p.f + p.f = 0 + p.m() +} + +// TODO(danscales) enable once the compiler is fixed +// var _ = f2[Sfm] + +// special case: structural type is a named pointer type + +type PSfm *Sfm + +func f3[P interface{ PSfm }](p P) { + _ = p.f + p.f = 0 +} + +// TODO(danscales) enable once the compiler is fixed +// var _ = f3[PSfm] |