Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/types: assertion failure: Sizeof Basic T is typed #20237

Closed
josharian opened this issue May 4, 2017 · 1 comment
Closed

go/types: assertion failure: Sizeof Basic T is typed #20237

josharian opened this issue May 4, 2017 · 1 comment
Milestone

Comments

@josharian
Copy link
Contributor

The following test, which is identical to TestIssue16902 aside from src, fails.

func TestShift(t *testing.T) {
	const src = `
package p

const x = 1>>1`
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "x.go", src, 0)
	if err != nil {
		t.Fatal(err)
	}
	info := types.Info{Types: make(map[ast.Expr]types.TypeAndValue)}
	conf := types.Config{
		Importer: importer.Default(),
		Sizes:    &types.StdSizes{WordSize: 8, MaxAlign: 8},
	}
	_, err = conf.Check("x", fset, []*ast.File{f}, &info)
	if err != nil {
		t.Fatal(err)
	}
	for _, tv := range info.Types {
		_ = conf.Sizes.Sizeof(tv.Type)
		_ = conf.Sizes.Alignof(tv.Type)
	}
}

Result:

panic: assertion failed [recovered]
	panic: assertion failed

goroutine 5 [running]:
testing.tRunner.func1(0xc4200de0f0)
	/Users/josh/go/tip/src/testing/testing.go:712 +0x256
panic(0x1291ac0, 0x1334970)
	/Users/josh/go/tip/src/runtime/panic.go:489 +0x259
go/types.assert(0x1468400)
	/Users/josh/go/tip/src/go/types/errors.go:18 +0x4c
go/types.(*StdSizes).Sizeof(0xc420014430, 0x14684e0, 0x147cd80, 0xc420058b40)
	/Users/josh/go/tip/src/go/types/sizes.go:123 +0x39d
go/types_test.TestZeroShiftOne(0xc4200de0f0)
	/Users/josh/go/tip/src/go/types/sizes_test.go:131 +0x485
testing.tRunner(0xc4200de0f0, 0x130c348)
	/Users/josh/go/tip/src/testing/testing.go:747 +0xd0
created by testing.(*T).Run
	/Users/josh/go/tip/src/testing/testing.go:789 +0x2de
exit status 2
FAIL	go/types	0.010s

The relevant assertion is near the beginning of StdSizes.SizeOf:

func (s *StdSizes) Sizeof(T Type) int64 {
	switch t := T.Underlying().(type) {
	case *Basic:
		assert(isTyped(T))  // HERE
		k := t.kind

When fixed, we should also test larger shifts, like 0>>1090, which currently produce a different error.

@griesemer

Found by go-fuzz.

@josharian josharian added this to the Go1.9 milestone May 4, 2017
@griesemer
Copy link
Contributor

@josharian FWIW, you wrote the old test when you fixed #16902... :-)

I don't believe there's anything wrong here: go/types.Sizes.Sizeof is supposed to be called with a typed value; so in this case the test is simply wrong/not applicable.

Trying a large shift count (0 >> 1080) after disabling the assertion (or removing the explicit calls to Sizeof) leads to the error

invalid operation: invalid shift count 1080 (untyped int constant)

which is perhaps not great, but ok: we don't support shift counts over a certain size.

Closing.

@golang golang locked and limited conversation to collaborators May 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants