Skip to content

Commit 7f4a946

Browse files
committed
cmd/compile: prevent irgen crashing for empty local declaration stmt
Updates #47631 Fixes #49611 Change-Id: Ib4a4466038e0d4a9aa9380d7909f29f7d15c6c69 Reviewed-on: https://go-review.googlesource.com/c/go/+/364314 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 7c50ef6 commit 7f4a946

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
4646
n.SetTypecheck(1)
4747
return n
4848
case *syntax.DeclStmt:
49-
if _, ok := stmt.DeclList[0].(*syntax.TypeDecl); ok && g.topFuncIsGeneric {
50-
// TODO: remove this restriction. See issue 47631.
51-
base.ErrorfAt(g.pos(stmt), "type declarations inside generic functions are not currently supported")
49+
if g.topFuncIsGeneric && len(stmt.DeclList) > 0 {
50+
if _, ok := stmt.DeclList[0].(*syntax.TypeDecl); ok {
51+
// TODO: remove this restriction. See issue 47631.
52+
base.ErrorfAt(g.pos(stmt), "type declarations inside generic functions are not currently supported")
53+
}
5254
}
5355
n := ir.NewBlockStmt(g.pos(stmt), nil)
5456
g.decls(&n.List, stmt.DeclList)

test/fixedbugs/issue49611.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile
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+
func f() {
10+
var ()
11+
}

test/typeparam/issue49611.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile -G=3
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+
func f[T any]() {
10+
var ()
11+
}

0 commit comments

Comments
 (0)