From 48b368b01fa1f4f9a4380722f03b35d449a09871 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 20 Apr 2021 10:55:59 -0700 Subject: [PATCH] cmd/compile/internal/types2: avoid follow-on errors for invalid [...] array Fixes #42987. Change-Id: Iaaa46e1f79525cd1e418c1a81a6414d11f8120b5 Reviewed-on: https://go-review.googlesource.com/c/go/+/311889 Trust: Robert Griesemer Reviewed-by: Robert Findley --- src/cmd/compile/internal/types2/api_test.go | 2 +- src/cmd/compile/internal/types2/fixedbugs/issue42987.src | 8 ++++++++ src/cmd/compile/internal/types2/typexpr.go | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 src/cmd/compile/internal/types2/fixedbugs/issue42987.src diff --git a/src/cmd/compile/internal/types2/api_test.go b/src/cmd/compile/internal/types2/api_test.go index 68048f28d3..c90f2e7510 100644 --- a/src/cmd/compile/internal/types2/api_test.go +++ b/src/cmd/compile/internal/types2/api_test.go @@ -326,7 +326,7 @@ func TestTypesInfo(t *testing.T) { {brokenPkg + `x2; func _() { var a, b string; type x struct {f string}; z := &x{f: a, f: b,}}`, `b`, `string`}, {brokenPkg + `x3; var x = panic("");`, `panic`, `func(interface{})`}, {`package x4; func _() { panic("") }`, `panic`, `func(interface{})`}, - {brokenPkg + `x5; func _() { var x map[string][...]int; x = map[string][...]int{"": {1,2,3}} }`, `x`, `map[string][-1]int`}, + {brokenPkg + `x5; func _() { var x map[string][...]int; x = map[string][...]int{"": {1,2,3}} }`, `x`, `map[string]invalid type`}, // parameterized functions {genericPkg + `p0; func f[T any](T); var _ = f[int]`, `f`, `func[T₁ interface{}](T₁)`}, diff --git a/src/cmd/compile/internal/types2/fixedbugs/issue42987.src b/src/cmd/compile/internal/types2/fixedbugs/issue42987.src new file mode 100644 index 0000000000..8aa3544272 --- /dev/null +++ b/src/cmd/compile/internal/types2/fixedbugs/issue42987.src @@ -0,0 +1,8 @@ +// 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. + +// Check that there is only one error (no follow-on errors). + +package p +var _ = [ /* ERROR invalid use of .* array */ ...]byte("foo") diff --git a/src/cmd/compile/internal/types2/typexpr.go b/src/cmd/compile/internal/types2/typexpr.go index 61b290c075..e64d804c30 100644 --- a/src/cmd/compile/internal/types2/typexpr.go +++ b/src/cmd/compile/internal/types2/typexpr.go @@ -518,7 +518,10 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) { typ.len = -1 } typ.elem = check.varType(e.Elem) - return typ + if typ.len >= 0 { + return typ + } + // report error if we encountered [...] case *syntax.SliceType: typ := new(Slice) -- 2.48.1