Skip to content

Commit 1d0256a

Browse files
committed
cmd/compile: do not add invalid key to constSet
After CL 272654, the compiler now use go/constant.Value to represent constant nodes. That makes ir.ConstantValue requires node type to correctly return value for untyped int node. But untyped int node can have nil type after typechecked, e.g: using int value as key for map[string]int, that makes the compiler crashes. To fix it, just don't add the invalid key to constSet, since when it's not important to report duplicated keys when they aren't valid. For #43311 Fixes #44432 Change-Id: I44d8f2b95f5cb339e77e8a705a94bcb16e62beb9 Reviewed-on: https://go-review.googlesource.com/c/go/+/294034 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 4b8b2c5 commit 1d0256a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/cmd/compile/internal/typecheck/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ func (s *constSet) add(pos src.XPos, n ir.Node, what, where string) {
794794
}
795795
}
796796

797-
if !ir.IsConstNode(n) {
797+
if !ir.IsConstNode(n) || n.Type() == nil {
798798
return
799799
}
800800
if n.Type().IsUntyped() {

test/fixedbugs/issue44432.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// errorcheck -G=0 -d=panic
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
var m = map[string]int{
10+
"a": 1,
11+
1: 1, // ERROR "cannot use 1.*as type string in map key"
12+
2: 2, // ERROR "cannot use 2.*as type string in map key"
13+
}

0 commit comments

Comments
 (0)