diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 97eb426b4..4288b9e41 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -653,7 +653,15 @@ CheckedError Parser::ParseField(StructDef &struct_def) { "default values currently only supported for scalars in tables"); ECHECK(ParseSingleValue(field->value)); } - if (IsFloat(field->value.type.base_type)) { + if (type.enum_def && + !type.enum_def->is_union && + !type.enum_def->attributes.Lookup("bit_flags") && + !type.enum_def->ReverseLookup(static_cast( + StringToInt(field->value.constant.c_str())))) { + return Error("default value of " + field->value.constant + " for field " + + name + " is not part of enum " + type.enum_def->name); + } + if (IsFloat(type.base_type)) { if (!strpbrk(field->value.constant.c_str(), ".eE")) field->value.constant += ".0"; } @@ -700,15 +708,15 @@ CheckedError Parser::ParseField(StructDef &struct_def) { return Error("can't deprecate fields in a struct"); field->required = field->attributes.Lookup("required") != nullptr; if (field->required && - (struct_def.fixed || IsScalar(field->value.type.base_type))) + (struct_def.fixed || IsScalar(type.base_type))) return Error("only non-scalar fields in tables may be 'required'"); field->key = field->attributes.Lookup("key") != nullptr; if (field->key) { if (struct_def.has_key) return Error("only one field may be set as 'key'"); struct_def.has_key = true; - if (!IsScalar(field->value.type.base_type)) { + if (!IsScalar(type.base_type)) { field->required = true; - if (field->value.type.base_type != BASE_TYPE_STRING) + if (type.base_type != BASE_TYPE_STRING) return Error("'key' field must be string or scalar type"); } } @@ -729,8 +737,7 @@ CheckedError Parser::ParseField(StructDef &struct_def) { if (nested->type.base_type != BASE_TYPE_STRING) return Error( "nested_flatbuffer attribute must be a string (the root type)"); - if (field->value.type.base_type != BASE_TYPE_VECTOR || - field->value.type.element != BASE_TYPE_UCHAR) + if (type.base_type != BASE_TYPE_VECTOR || type.element != BASE_TYPE_UCHAR) return Error( "nested_flatbuffer attribute may only apply to a vector of ubyte"); // This will cause an error if the root type of the nested flatbuffer @@ -746,8 +753,8 @@ CheckedError Parser::ParseField(StructDef &struct_def) { if (field->attributes.Lookup("flexbuffer")) { field->flexbuffer = true; uses_flexbuffers_ = true; - if (field->value.type.base_type != BASE_TYPE_VECTOR || - field->value.type.element != BASE_TYPE_UCHAR) + if (type.base_type != BASE_TYPE_VECTOR || + type.element != BASE_TYPE_UCHAR) return Error("flexbuffer attribute may only apply to a vector of ubyte"); } diff --git a/tests/test.cpp b/tests/test.cpp index 3b7e3d5b0..1102273fa 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1179,6 +1179,8 @@ void ErrorTest() { TestError("table X { Y:int; Y:int; }", "field already"); TestError("table Y {} table X { Y:int; }", "same as table"); TestError("struct X { Y:string; }", "only scalar"); + TestError("table X { Y:string = 1; }", "default values"); + TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum"); TestError("struct X { Y:int (deprecated); }", "deprecate"); TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }", "missing type field"); @@ -1968,7 +1970,7 @@ int main(int /*argc*/, const char * /*argv*/ []) { EndianSwapTest(); JsonDefaultTest(); - + FlexBuffersTest(); if (!testing_fails) {