|
| 1 | +// Copyright 2014 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 | +// This file contains tests for the bool checker. |
| 6 | + |
| 7 | +//go:build go1.18 |
| 8 | + |
| 9 | +package typeparams |
| 10 | + |
| 11 | +type T[P interface{ ~int }] struct { |
| 12 | + a P |
| 13 | +} |
| 14 | + |
| 15 | +func (t T[P]) Foo() int { return int(t.a) } |
| 16 | + |
| 17 | +type FT[P any] func() P |
| 18 | + |
| 19 | +func Sink[Elem any]() chan Elem { |
| 20 | + return make(chan Elem) |
| 21 | +} |
| 22 | + |
| 23 | +func RedundantConditions[P interface{ int }]() { |
| 24 | + type _f[P1 any] func() P1 |
| 25 | + |
| 26 | + var f, g _f[P] |
| 27 | + if f() == 0 || f() == 0 { // OK f might have side effects |
| 28 | + } |
| 29 | + var t T[P] |
| 30 | + _ = t.Foo() == 2 || t.Foo() == 2 // OK Foo might have side effects |
| 31 | + if v, w := f(), g(); v == w || v == w { // want `redundant or: v == w \|\| v == w` |
| 32 | + } |
| 33 | + |
| 34 | + // error messages present type params correctly. |
| 35 | + _ = t == T[P]{2} || t == T[P]{2} // want `redundant or: t == T\[P\]\{2\} \|\| t == T\[P\]\{2\}` |
| 36 | + _ = FT[P](f) == nil || FT[P](f) == nil // want `redundant or: FT\[P\]\(f\) == nil \|\| FT\[P\]\(f\) == nil` |
| 37 | + _ = (func() P)(f) == nil || (func() P)(f) == nil // want `redundant or: \(func\(\) P\)\(f\) == nil \|\| \(func\(\) P\)\(f\) == nil` |
| 38 | + |
| 39 | + var tint T[int] |
| 40 | + var fint _f[int] |
| 41 | + _ = tint == T[int]{2} || tint == T[int]{2} // want `redundant or: tint == T\[int\]\{2\} \|\| tint\ == T\[int\]\{2\}` |
| 42 | + _ = FT[int](fint) == nil || FT[int](fint) == nil // want `redundant or: FT\[int\]\(fint\) == nil \|\| FT\[int\]\(fint\) == nil` |
| 43 | + _ = (func() int)(fint) == nil || (func() int)(fint) == nil // want `redundant or: \(func\(\) int\)\(fint\) == nil \|\| \(func\(\) int\)\(fint\) == nil` |
| 44 | + |
| 45 | + c := Sink[P]() |
| 46 | + _ = 0 == <-c || 0 == <-c // OK subsequent receives may yield different values |
| 47 | + for i, j := <-c, <-c; i == j || i == j; i, j = <-c, <-c { // want `redundant or: i == j \|\| i == j` |
| 48 | + } |
| 49 | + |
| 50 | + var i, j P |
| 51 | + _ = i == 1 || j+1 == i || i == 1 // want `redundant or: i == 1 \|\| i == 1` |
| 52 | + _ = i == 1 || f() == 1 || i == 1 // OK f may alter i as a side effect |
| 53 | + _ = f() == 1 || i == 1 || i == 1 // want `redundant or: i == 1 \|\| i == 1` |
| 54 | +} |
| 55 | + |
| 56 | +func SuspectConditions[P interface{ ~int }, S interface{ ~string }]() { |
| 57 | + var i, j P |
| 58 | + _ = i == 0 || i == 1 // OK |
| 59 | + _ = i+3 != 7 || j+5 == 0 || i+3 != 9 // want `suspect or: i\+3 != 7 \|\| i\+3 != 9` |
| 60 | + |
| 61 | + var s S |
| 62 | + _ = s != "one" || s != "the other" // want `suspect or: s != .one. \|\| s != .the other.` |
| 63 | +} |
0 commit comments