From 622a1ffd7236529d9774498742810ce1102b009f Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 4 Sep 2017 19:53:01 +0200 Subject: pkg/compiler: prohibit bitfields of size 0 They don't work the way C bitfields work. So this will lead to confusion at least. --- pkg/compiler/testdata/errors.txt | 2 +- pkg/compiler/types.go | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) (limited to 'pkg') diff --git a/pkg/compiler/testdata/errors.txt b/pkg/compiler/testdata/errors.txt index 512d615c8..c339dcb80 100644 --- a/pkg/compiler/testdata/errors.txt +++ b/pkg/compiler/testdata/errors.txt @@ -128,7 +128,7 @@ resource inout[int32] ### resource uses reserved name inout bar() s3 { - f1 int8:0 + f1 int8:0 ### bitfields of size 0 are not supported f2 int8:1 f3 int8:7 f4 int8:8 diff --git a/pkg/compiler/types.go b/pkg/compiler/types.go index 035a47165..29ef4dbf5 100644 --- a/pkg/compiler/types.go +++ b/pkg/compiler/types.go @@ -539,10 +539,19 @@ var typeArgBase = namedArg{ Names: []string{"int8", "int16", "int32", "int64", "int16be", "int32be", "int64be", "intptr"}, AllowColon: true, Check: func(comp *compiler, t *ast.Type) { - size, _ := comp.parseIntType(t.Ident) - if t.Value2 > size*8 { - comp.error(t.Pos2, "bitfield of size %v is too large for base type of size %v", - t.Value2, size*8) + if t.HasColon { + if t.Value2 == 0 { + // This was not supported historically + // and does not work the way C bitfields of size 0 work. + // We could allow this, but then we need to make + // this work the way C bitfields work. + comp.error(t.Pos2, "bitfields of size 0 are not supported") + } + size, _ := comp.parseIntType(t.Ident) + if t.Value2 > size*8 { + comp.error(t.Pos2, "bitfield of size %v is too large for base type of size %v", + t.Value2, size*8) + } } }, }, -- cgit mrf-deployment