Skip to content

Commit e1b14f5

Browse files
mdempskytoothrot
authored andcommitted
[release-branch.go1.18] cmd/compile: backport fix for #51840
This CL is a manual backport of CLs 403837 and 404914 to Go 1.18. CL 403837 was intended just as a simplification CL, but evidently it also fixed #51840. However, for backporting to Go 1.18, the existing logic needs to be preserved to support -G=0 mode (which still relies on Ntype). Fixes #51849. Change-Id: Ib060b0bc67ecf26de8a65d5b4d2f8a65cd547517 Reviewed-on: https://go-review.googlesource.com/c/go/+/405436 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent e46ac38 commit e1b14f5

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,23 @@ func assign(stmt ir.Node, lhs, rhs []ir.Node) {
127127

128128
checkLHS := func(i int, typ *types.Type) {
129129
lhs[i] = Resolve(lhs[i])
130-
if n := lhs[i]; typ != nil && ir.DeclaredBy(n, stmt) && n.Name().Ntype == nil {
131-
if typ.Kind() != types.TNIL {
130+
if base.Flag.G != 0 || base.Debug.Unified != 0 {
131+
// New logic added in CL 403837 for Go 1.19, which only has -G=3 and unified IR.
132+
if n := lhs[i]; typ != nil && ir.DeclaredBy(n, stmt) && n.Type() == nil {
133+
base.Assertf(typ.Kind() == types.TNIL, "unexpected untyped nil")
132134
n.SetType(defaultType(typ))
133-
} else {
134-
base.Errorf("use of untyped nil")
135+
}
136+
} else {
137+
// Original logic from Go 1.18, which is still needed for -G=0.
138+
if n := lhs[i]; typ != nil && ir.DeclaredBy(n, stmt) && n.Name().Ntype == nil {
139+
if typ.Kind() != types.TNIL {
140+
n.SetType(defaultType(typ))
141+
} else {
142+
base.Errorf("use of untyped nil")
143+
}
135144
}
136145
}
146+
137147
if lhs[i].Typecheck() == 0 {
138148
lhs[i] = AssignExpr(lhs[i])
139149
}

test/typeparam/issue51840.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// compile -G=3
2+
3+
// Copyright 2022 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 main
8+
9+
type Addr struct {
10+
hi uint64
11+
lo uint64
12+
z *byte
13+
}
14+
15+
func EqualMap[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool {
16+
for k, v1 := range m1 {
17+
if v2, ok := m2[k]; !ok || v1 != v2 {
18+
return false
19+
}
20+
}
21+
return true
22+
}
23+
24+
type Set[T comparable] map[T]struct{}
25+
26+
func NewSet[T comparable](items ...T) Set[T] {
27+
return nil
28+
}
29+
30+
func (s Set[T]) Equals(other Set[T]) bool {
31+
return EqualMap(s, other)
32+
}
33+
34+
func main() {
35+
NewSet[Addr](Addr{0, 0, nil})
36+
}

0 commit comments

Comments
 (0)