]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: remove duplicate const logic from typecheck
authorMatthew Dempsky <mdempsky@google.com>
Mon, 7 Mar 2022 16:54:28 +0000 (08:54 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 7 Mar 2022 18:17:40 +0000 (18:17 +0000)
Now that we always use types2 to validate user source code, we can
remove the constSet logic from typecheck for detecting duplicate
expression switch cases and duplicate map literal keys. This logic is
redundant with types2, and currently causes unified IR to report
inappropriate duplicate constant errors that only appear after type
substitution.

Updates #42758.

Change-Id: I51ee2c5106eec9abf40eba2480dc52603c68ba21
Reviewed-on: https://go-review.googlesource.com/c/go/+/390474
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/noder/reader.go
src/cmd/compile/internal/typecheck/const.go
src/cmd/compile/internal/typecheck/expr.go
src/cmd/compile/internal/typecheck/stmt.go
test/typeparam/issue42758.go [new file with mode: 0644]

index 2b8134a02c54497ac2709313f5ef7ece4571df98..004630236d6880a9491c07a08ddc43359947613e 100644 (file)
@@ -26,12 +26,6 @@ import (
        "cmd/internal/src"
 )
 
-// TODO(mdempsky): Suppress duplicate type/const errors that can arise
-// during typecheck due to naive type substitution (e.g., see #42758).
-// I anticipate these will be handled as a consequence of adding
-// dictionaries support, so it's probably not important to focus on
-// this until after that's done.
-
 type pkgReader struct {
        pkgbits.PkgDecoder
 
index fbe7c02c49821eb587ea97bf5e27b9f507e3bff6..3be3b8059f7861f39c6be581e5891a1c3bfe2375 100644 (file)
@@ -16,7 +16,6 @@ import (
        "cmd/compile/internal/base"
        "cmd/compile/internal/ir"
        "cmd/compile/internal/types"
-       "cmd/internal/src"
 )
 
 func roundFloat(v constant.Value, sz int64) constant.Value {
@@ -773,94 +772,6 @@ func anyCallOrChan(n ir.Node) bool {
        })
 }
 
-// A constSet represents a set of Go constant expressions.
-type constSet struct {
-       m map[constSetKey]src.XPos
-}
-
-type constSetKey struct {
-       typ *types.Type
-       val interface{}
-}
-
-// add adds constant expression n to s. If a constant expression of
-// equal value and identical type has already been added, then add
-// reports an error about the duplicate value.
-//
-// pos provides position information for where expression n occurred
-// (in case n does not have its own position information). what and
-// where are used in the error message.
-//
-// n must not be an untyped constant.
-func (s *constSet) add(pos src.XPos, n ir.Node, what, where string) {
-       if conv := n; conv.Op() == ir.OCONVIFACE {
-               conv := conv.(*ir.ConvExpr)
-               if conv.Implicit() {
-                       n = conv.X
-               }
-       }
-
-       if !ir.IsConstNode(n) || n.Type() == nil {
-               return
-       }
-       if n.Type().IsUntyped() {
-               base.Fatalf("%v is untyped", n)
-       }
-
-       // Consts are only duplicates if they have the same value and
-       // identical types.
-       //
-       // In general, we have to use types.Identical to test type
-       // identity, because == gives false negatives for anonymous
-       // types and the byte/uint8 and rune/int32 builtin type
-       // aliases.  However, this is not a problem here, because
-       // constant expressions are always untyped or have a named
-       // type, and we explicitly handle the builtin type aliases
-       // below.
-       //
-       // This approach may need to be revisited though if we fix
-       // #21866 by treating all type aliases like byte/uint8 and
-       // rune/int32.
-
-       typ := n.Type()
-       switch typ {
-       case types.ByteType:
-               typ = types.Types[types.TUINT8]
-       case types.RuneType:
-               typ = types.Types[types.TINT32]
-       }
-       k := constSetKey{typ, ir.ConstValue(n)}
-
-       if ir.HasUniquePos(n) {
-               pos = n.Pos()
-       }
-
-       if s.m == nil {
-               s.m = make(map[constSetKey]src.XPos)
-       }
-
-       if prevPos, isDup := s.m[k]; isDup {
-               base.ErrorfAt(pos, "duplicate %s %s in %s\n\tprevious %s at %v",
-                       what, nodeAndVal(n), where,
-                       what, base.FmtPos(prevPos))
-       } else {
-               s.m[k] = pos
-       }
-}
-
-// nodeAndVal reports both an expression and its constant value, if
-// the latter is non-obvious.
-//
-// TODO(mdempsky): This could probably be a fmt.go flag.
-func nodeAndVal(n ir.Node) string {
-       show := fmt.Sprint(n)
-       val := ir.ConstValue(n)
-       if s := fmt.Sprintf("%#v", val); show != s {
-               show += " (value " + s + ")"
-       }
-       return show
-}
-
 // evalunsafe evaluates a package unsafe operation and returns the result.
 func evalunsafe(n ir.Node) int64 {
        switch n.Op() {
index 0fe8f916965bed16498a3c62b4c40c803245371a..e2b95b27c6a93621f1c2d87e49e02db5e73f0cee 100644 (file)
@@ -245,7 +245,6 @@ func tcCompLit(n *ir.CompLitExpr) (res ir.Node) {
                n.Len = length
 
        case types.TMAP:
-               var cs constSet
                for i3, l := range n.List {
                        ir.SetPos(l)
                        if l.Op() != ir.OKEY {
@@ -259,7 +258,6 @@ func tcCompLit(n *ir.CompLitExpr) (res ir.Node) {
                        r = pushtype(r, t.Key())
                        r = Expr(r)
                        l.Key = AssignConv(r, t.Key(), "map key")
-                       cs.add(base.Pos, l.Key, "key", "map literal")
 
                        r = l.Value
                        r = pushtype(r, t.Elem())
index f2660075073388447f2630b39e3021e9e132a9e2..b2fba315e751f0c332093cfdd81aea7268d952e2 100644 (file)
@@ -519,7 +519,6 @@ func tcSwitchExpr(n *ir.SwitchStmt) {
        }
 
        var defCase ir.Node
-       var cs constSet
        for _, ncase := range n.Cases {
                ls := ncase.List
                if len(ls) == 0 { // default:
@@ -554,16 +553,6 @@ func tcSwitchExpr(n *ir.SwitchStmt) {
                                        }
                                }
                        }
-
-                       // Don't check for duplicate bools. Although the spec allows it,
-                       // (1) the compiler hasn't checked it in the past, so compatibility mandates it, and
-                       // (2) it would disallow useful things like
-                       //       case GOARCH == "arm" && GOARM == "5":
-                       //       case GOARCH == "arm":
-                       //     which would both evaluate to false for non-ARM compiles.
-                       if !n1.Type().IsBoolean() {
-                               cs.add(ncase.Pos(), n1, "case", "switch")
-                       }
                }
 
                Stmts(ncase.Body)
diff --git a/test/typeparam/issue42758.go b/test/typeparam/issue42758.go
new file mode 100644 (file)
index 0000000..25fb85f
--- /dev/null
@@ -0,0 +1,19 @@
+// 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 F[T, U int]() interface{} {
+       switch interface{}(nil) {
+       case int(0), T(0), U(0):
+       }
+
+       return map[interface{}]int{int(0): 0, T(0): 0, U(0): 0}
+}
+
+func main() {
+       F[int, int]()
+}