From 7f4a946fa26f2ffdc14c354f2f1cc193ab5d0e90 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 16 Nov 2021 21:59:15 +0700 Subject: [PATCH] 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 Run-TryBot: Cuong Manh Le TryBot-Result: Go Bot Reviewed-by: Keith Randall --- src/cmd/compile/internal/noder/stmt.go | 8 +++++--- test/fixedbugs/issue49611.go | 11 +++++++++++ test/typeparam/issue49611.go | 11 +++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/fixedbugs/issue49611.go create mode 100644 test/typeparam/issue49611.go diff --git a/src/cmd/compile/internal/noder/stmt.go b/src/cmd/compile/internal/noder/stmt.go index e329a59156..1e996b95c4 100644 --- a/src/cmd/compile/internal/noder/stmt.go +++ b/src/cmd/compile/internal/noder/stmt.go @@ -46,9 +46,11 @@ func (g *irgen) stmt(stmt syntax.Stmt) ir.Node { n.SetTypecheck(1) return n case *syntax.DeclStmt: - if _, ok := stmt.DeclList[0].(*syntax.TypeDecl); ok && g.topFuncIsGeneric { - // TODO: remove this restriction. See issue 47631. - base.ErrorfAt(g.pos(stmt), "type declarations inside generic functions are not currently supported") + if g.topFuncIsGeneric && len(stmt.DeclList) > 0 { + if _, ok := stmt.DeclList[0].(*syntax.TypeDecl); ok { + // TODO: remove this restriction. See issue 47631. + base.ErrorfAt(g.pos(stmt), "type declarations inside generic functions are not currently supported") + } } n := ir.NewBlockStmt(g.pos(stmt), nil) g.decls(&n.List, stmt.DeclList) diff --git a/test/fixedbugs/issue49611.go b/test/fixedbugs/issue49611.go new file mode 100644 index 0000000000..b40ad58649 --- /dev/null +++ b/test/fixedbugs/issue49611.go @@ -0,0 +1,11 @@ +// compile + +// Copyright 2021 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 p + +func f() { + var () +} diff --git a/test/typeparam/issue49611.go b/test/typeparam/issue49611.go new file mode 100644 index 0000000000..96c651e2b5 --- /dev/null +++ b/test/typeparam/issue49611.go @@ -0,0 +1,11 @@ +// compile -G=3 + +// Copyright 2021 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 p + +func f[T any]() { + var () +} -- 2.48.1