From eaa4b1a6e4d9f9bec10257a548d25b2425a83ee5 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 6 Jun 2023 16:25:26 -0700 Subject: [PATCH] cmd/compile/internal/types2: add missing tests from go/types Add 3 tests that exist in go/types but that were not ported to types2. Change-Id: I34d219f605f9ae66e8b4439c3dfe93cfa0ec9524 Reviewed-on: https://go-review.googlesource.com/c/go/+/501304 Reviewed-by: Robert Findley Reviewed-by: Robert Griesemer Auto-Submit: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot --- src/cmd/compile/internal/types2/check_test.go | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/cmd/compile/internal/types2/check_test.go b/src/cmd/compile/internal/types2/check_test.go index b149ae3908..8cb3000501 100644 --- a/src/cmd/compile/internal/types2/check_test.go +++ b/src/cmd/compile/internal/types2/check_test.go @@ -317,7 +317,32 @@ func TestManual(t *testing.T) { } } -// TODO(gri) go/types has extra TestLongConstants and TestIndexRepresentability tests +func TestLongConstants(t *testing.T) { + format := `package longconst; const _ = %s /* ERROR "constant overflow" */; const _ = %s // ERROR "excessively long constant"` + src := fmt.Sprintf(format, strings.Repeat("1", 9999), strings.Repeat("1", 10001)) + testFiles(t, []string{"longconst.go"}, [][]byte{[]byte(src)}, 0, false) +} + +func withSizes(sizes Sizes) func(*Config) { + return func(cfg *Config) { + cfg.Sizes = sizes + } +} + +// TestIndexRepresentability tests that constant index operands must +// be representable as int even if they already have a type that can +// represent larger values. +func TestIndexRepresentability(t *testing.T) { + const src = `package index; var s []byte; var _ = s[int64 /* ERRORx "int64\\(1\\) << 40 \\(.*\\) overflows int" */ (1) << 40]` + testFiles(t, []string{"index.go"}, [][]byte{[]byte(src)}, 0, false, withSizes(&StdSizes{4, 4})) +} + +func TestIssue47243_TypedRHS(t *testing.T) { + // The RHS of the shift expression below overflows uint on 32bit platforms, + // but this is OK as it is explicitly typed. + const src = `package issue47243; var a uint64; var _ = a << uint64(4294967296)` // uint64(1<<32) + testFiles(t, []string{"p.go"}, [][]byte{[]byte(src)}, 0, false, withSizes(&StdSizes{4, 4})) +} func TestCheck(t *testing.T) { DefPredeclaredTestFuncs() -- 2.50.0