]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: fix silly logic error in commonUnder
authorRobert Griesemer <gri@golang.org>
Wed, 19 Mar 2025 16:38:51 +0000 (09:38 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 19 Mar 2025 17:15:42 +0000 (10:15 -0700)
Fixes #72936.

Change-Id: I79ed8d559c8565fa960b974f8c1207ee442f4c26
Reviewed-on: https://go-review.googlesource.com/c/go/+/659256
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/under.go
src/go/types/under.go
src/internal/types/testdata/fixedbugs/issue72936.go [new file with mode: 0644]

index 846788a2107d97251c9d73ccd43da1b15d7b21e1..9e5334b7245edcfdca0cefd6f4e5b9b35975aad8 100644 (file)
@@ -112,11 +112,13 @@ func commonUnder(t Type, cond func(t, u Type) *typeError) (Type, *typeError) {
                                }
                                // If we have different channel directions, keep the restricted one
                                // and complain if they conflict.
-                               if chu.dir == SendRecv {
-                                       ct, cu = t, u // switch to current, possibly restricted channel
-                               } else if chu.dir != ch.dir {
+                               switch {
+                               case chu.dir == ch.dir:
+                                       // nothing to do
+                               case chu.dir == SendRecv:
+                                       ct, cu = t, u // switch to restricted channel
+                               case ch.dir != SendRecv:
                                        return bad("channels %s and %s have conflicting directions", ct, t)
-
                                }
                                return true
                        }
index 8d87e24237f105c1a45701c53d7434bf64fb5e5d..2c09c491348db76d9a269ab58aa73d4eb14913cf 100644 (file)
@@ -115,11 +115,13 @@ func commonUnder(t Type, cond func(t, u Type) *typeError) (Type, *typeError) {
                                }
                                // If we have different channel directions, keep the restricted one
                                // and complain if they conflict.
-                               if chu.dir == SendRecv {
-                                       ct, cu = t, u // switch to current, possibly restricted channel
-                               } else if chu.dir != ch.dir {
+                               switch {
+                               case chu.dir == ch.dir:
+                                       // nothing to do
+                               case chu.dir == SendRecv:
+                                       ct, cu = t, u // switch to restricted channel
+                               case ch.dir != SendRecv:
                                        return bad("channels %s and %s have conflicting directions", ct, t)
-
                                }
                                return true
                        }
diff --git a/src/internal/types/testdata/fixedbugs/issue72936.go b/src/internal/types/testdata/fixedbugs/issue72936.go
new file mode 100644 (file)
index 0000000..eb4942c
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2025 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 _[C chan<- int | chan int](c C)   { c <- 0 }
+func _[C chan int | chan<- int](c C)   { c <- 0 }
+func _[C <-chan int | chan<- int](c C) { c <- /* ERROR "receive-only channel <-chan int" */ 0 }
+
+func _[C <-chan int | chan int](c C)   { <-c }
+func _[C chan int | <-chan int](c C)   { <-c }
+func _[C chan<- int | <-chan int](c C) { <-c /* ERROR "send-only channel chan<- int" */ }
+
+// from issue report
+
+func send[C interface{ ~chan<- V | ~chan V }, V any](c C, v V) {
+       c <- v
+}
+
+func receive[C interface{ ~<-chan V | ~chan V }, V any](c C) V {
+       return <-c
+}