Skip to content

Commit

Permalink
Improve error reporting for field types with invalid dimensions.
Browse files Browse the repository at this point in the history
Bug: issue #939
Test: new test case
  • Loading branch information
dvander committed Feb 28, 2024
1 parent 3164e53 commit 9594f99
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
19 changes: 11 additions & 8 deletions compiler/array-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ArrayTypeResolver
bool Resolve();

private:
void ResolveSize();
bool ResolveSize();
bool ResolveDimExprs();
void PrepareDimArray();
void ResolveRank(int rank, Expr* init);
Expand Down Expand Up @@ -92,7 +92,7 @@ ArrayTypeResolver::ArrayTypeResolver(Semantics* sema, const token_pos_t& pos, ty
}

bool ArrayTypeResolver::Resolve() {
ResolveSize();
bool resolved_size = ResolveSize();

assert(!type_->resolved_array);

Expand All @@ -104,12 +104,12 @@ bool ArrayTypeResolver::Resolve() {
auto types = CompileContext::get().types();
type_->type = types->defineArray(type_->type, type_->dim_vec());
type_->resolved_array = true;
return true;
return resolved_size;
}

void ArrayTypeResolver::ResolveSize() {
bool ArrayTypeResolver::ResolveSize() {
if (!type_->has_postdims)
return;
return true;

// If the array has old-style dimensions, we analyze them now. This is
// technically a violation of the normal parsing order. The experimental
Expand All @@ -120,14 +120,14 @@ void ArrayTypeResolver::ResolveSize() {
// all usable symbols are already entered, and their types will not
// change between binding and later analysis.
if (!ResolveDimExprs())
return;
return false;

PrepareDimArray();

// If this is an implicit dynamic array (old syntax), the initializer
// cannot be used for computing a size.
if (decl_ && decl_->implicit_dynamic_array())
return;
return true;

// Traverse the initializer if present. For arguments, initializers
// don't participate in determining the fixed size.
Expand All @@ -137,7 +137,7 @@ void ArrayTypeResolver::ResolveSize() {
// If we hit errors resolving the type, don't bother using any values
// we computed along the way.
if (!errors_.ok())
return;
return false;
}

// Any kind of indeterminate status gets forced back to 0. Semantic
Expand Down Expand Up @@ -171,10 +171,13 @@ void ArrayTypeResolver::ResolveSize() {
// as the last dimension is filled.
} else if (vclass_ == sLOCAL) {
report(pos_, 159);
return false;
} else {
report(pos_, 183);
return false;
}
}
return true;
}

void
Expand Down
7 changes: 5 additions & 2 deletions compiler/name-resolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,10 @@ bool EnumStructDecl::EnterNames(SemaContext& sc) {
}

if (field->type_info().numdim()) {
ResolveArrayType(sc.sema(), field->pos(), &field->mutable_type_info(), sENUMFIELD);
if (!ResolveArrayType(sc.sema(), field->pos(), &field->mutable_type_info(),
sENUMFIELD)) {
continue;
}

if (field->type_info().numdim() > 1) {
error(field->pos(), 65);
Expand All @@ -1030,7 +1033,7 @@ bool EnumStructDecl::EnterNames(SemaContext& sc) {
position += size;
}

if (!position)
if (fields_.empty())
report(pos_, 119) << name_;

for (const auto& decl : methods_) {
Expand Down
9 changes: 9 additions & 0 deletions tests/compile-only/fail-array-decl-with-zero-enum-value.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enum Pl0000
{
Pl_MAX
}

enum struct Array
{
int value[Pl_MAX];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(8) : error 009: invalid array size (negative, zero or out of bounds)

0 comments on commit 9594f99

Please sign in to comment.