From a745171e6b30394b661a040d04e8807b4bd0c7da Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Mon, 17 Aug 2020 21:59:07 -0700 Subject: [PATCH] cmd/compile: fix SSA type comparison A typo in the conversion code caused comparisons of SSA types to report CMPeq when they were not in fact equal. Fixes #40837 Change-Id: I0627eee51d524a585908b34a4590bc533c8415fc Reviewed-on: https://go-review.googlesource.com/c/go/+/248781 Run-TryBot: Keith Randall TryBot-Result: Gobot Gobot Reviewed-by: Cuong Manh Le Reviewed-by: Emmanuel Odeke --- src/cmd/compile/internal/types/type.go | 3 ++- src/cmd/compile/internal/types/type_test.go | 28 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/cmd/compile/internal/types/type_test.go diff --git a/src/cmd/compile/internal/types/type.go b/src/cmd/compile/internal/types/type.go index 3b7b31c5d6..91b54b43d4 100644 --- a/src/cmd/compile/internal/types/type.go +++ b/src/cmd/compile/internal/types/type.go @@ -131,6 +131,7 @@ type Type struct { // TPTR: Ptr // TARRAY: *Array // TSLICE: Slice + // TSSA: string Extra interface{} // Width is the width of this Type in bytes. @@ -1026,7 +1027,7 @@ func (t *Type) cmp(x *Type) Cmp { case TSSA: tname := t.Extra.(string) - xname := t.Extra.(string) + xname := x.Extra.(string) // desire fast sorting, not pretty sorting. if len(tname) == len(xname) { if tname == xname { diff --git a/src/cmd/compile/internal/types/type_test.go b/src/cmd/compile/internal/types/type_test.go new file mode 100644 index 0000000000..fe3f380b21 --- /dev/null +++ b/src/cmd/compile/internal/types/type_test.go @@ -0,0 +1,28 @@ +// Copyright 2020 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 types_test + +import ( + "cmd/compile/internal/types" + "testing" +) + +func TestSSACompare(t *testing.T) { + a := []*types.Type{ + types.TypeInvalid, + types.TypeMem, + types.TypeFlags, + types.TypeVoid, + types.TypeInt128, + } + for _, x := range a { + for _, y := range a { + c := x.Compare(y) + if x == y && c != types.CMPeq || x != y && c == types.CMPeq { + t.Errorf("%s compare %s == %d\n", x.Extra, y.Extra, c) + } + } + } +}