Skip to content

Commit

Permalink
chore: emit more reasonable error message when using incomplete type …
Browse files Browse the repository at this point in the history
…in struct (google#7678)

Co-authored-by: Derek Bailey <derekbailey@google.com>
  • Loading branch information
2 people authored and Wen Sun committed Dec 25, 2022
1 parent f7282a5 commit 3eb45e5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/flatbuffers/idl.h
Expand Up @@ -473,6 +473,10 @@ inline bool IsStruct(const Type &type) {
return type.base_type == BASE_TYPE_STRUCT && type.struct_def->fixed;
}

inline bool IsIncompleteStruct(const Type &type) {
return type.base_type == BASE_TYPE_STRUCT && type.struct_def->predecl;
}

inline bool IsTable(const Type &type) {
return type.base_type == BASE_TYPE_STRUCT && !type.struct_def->fixed;
}
Expand Down
6 changes: 6 additions & 0 deletions src/idl_parser.cpp
Expand Up @@ -918,6 +918,12 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
ECHECK(ParseType(type));

if (struct_def.fixed) {
if (IsIncompleteStruct(type) ||
(IsArray(type) && IsIncompleteStruct(type.VectorType()))) {
std::string type_name = IsArray(type) ? type.VectorType().struct_def->name : type.struct_def->name;
return Error(std::string("Incomplete type in struct is not allowed, type name: ") + type_name);
}

auto valid = IsScalar(type.base_type) || IsStruct(type);
if (!valid && IsArray(type)) {
const auto &elem_type = type.VectorType();
Expand Down

0 comments on commit 3eb45e5

Please sign in to comment.