]> Cypherpunks repositories - gostls13.git/commit
go/types, types2: detect dupl. map keys in comp. literals with generic key type
authorRobert Griesemer <gri@golang.org>
Tue, 17 May 2022 00:43:38 +0000 (17:43 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 23 May 2022 22:10:24 +0000 (22:10 +0000)
commit2138124143e27f72e1ad8fdcee0405d8e8647c76
treea18385576fa22376c1fd55b46d841c6aad1e1f7f
parent3fa5ab0d83e1a27628dd997f3883eadd399679e1
go/types, types2: detect dupl. map keys in comp. literals with generic key type

For map composite literals where the key type is a suitably constrained
type parameter, the existing key duplicate detection mechanism doesn't
work when the keys are numeric values of different types but equal value.
For instance, given

        func _[P int64|float64]() {
                _ = map[P]string{0: "foo", 0.0: "bar"}
        }

the key values 0 and 0.0 have the same numeric value 0 but currently
are treated as different values int64(0) and float64(0.0). For any
valid instantiation of P, the keys will collide.

This CL changes the keyVal function to map numeric types to the
"smallest" numeric type in which a value can be represented. For
instance, float64(0.0) is mapped to int64(0). This ensures that
numerically equal values are always represented the same way so
that they can be detected as duplicates.

Fixes #51610.

Change-Id: I3eb71142bbe6b13453282a7f71ee48950e58ecbd
Reviewed-on: https://go-review.googlesource.com/c/go/+/406555
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue51610.go [new file with mode: 0644]
src/go/types/expr.go
src/go/types/testdata/fixedbugs/issue51610.go [new file with mode: 0644]