From a4fa41b49d8f489a2d36fe000e4bdfcb3c2afbd4 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 26 Dec 2019 02:25:44 +0100 Subject: [PATCH] compiler: don't crash when encountering types.Invalid This commit fixes a crash when trying to compile the following (invalid) code: package main import "unsafe" func main() { } type Foo struct { x DoesNotExist } const foo = unsafe.Sizeof(Foo{}) This commit fixes this situation. The result is a regular error message, indicating that DoesNotExist is not defined. --- compiler/sizes.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/sizes.go b/compiler/sizes.go index 7f2b87d3..3f6cdbc3 100644 --- a/compiler/sizes.go +++ b/compiler/sizes.go @@ -110,6 +110,9 @@ func (s *StdSizes) Sizeof(T types.Type) int64 { if k == types.UnsafePointer { return s.PtrSize } + if k == types.Invalid { + return 0 // only relevant when there is a type error somewhere + } panic("unknown basic type: " + t.String()) case *types.Array: n := t.Len()