From 9594f99766f9326f336a0307e1887e1317f18b48 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 27 Feb 2024 21:14:37 -0800 Subject: [PATCH] Improve error reporting for field types with invalid dimensions. Bug: issue #939 Test: new test case --- compiler/array-helpers.cpp | 19 +++++++++++-------- compiler/name-resolution.cpp | 7 +++++-- .../fail-array-decl-with-zero-enum-value.sp | 9 +++++++++ .../fail-array-decl-with-zero-enum-value.txt | 1 + 4 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 tests/compile-only/fail-array-decl-with-zero-enum-value.sp create mode 100644 tests/compile-only/fail-array-decl-with-zero-enum-value.txt diff --git a/compiler/array-helpers.cpp b/compiler/array-helpers.cpp index fd515db7..f22ca6b9 100644 --- a/compiler/array-helpers.cpp +++ b/compiler/array-helpers.cpp @@ -41,7 +41,7 @@ class ArrayTypeResolver bool Resolve(); private: - void ResolveSize(); + bool ResolveSize(); bool ResolveDimExprs(); void PrepareDimArray(); void ResolveRank(int rank, Expr* init); @@ -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); @@ -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 @@ -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. @@ -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 @@ -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 diff --git a/compiler/name-resolution.cpp b/compiler/name-resolution.cpp index 56519fcf..cb1e4793 100644 --- a/compiler/name-resolution.cpp +++ b/compiler/name-resolution.cpp @@ -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); @@ -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_) { diff --git a/tests/compile-only/fail-array-decl-with-zero-enum-value.sp b/tests/compile-only/fail-array-decl-with-zero-enum-value.sp new file mode 100644 index 00000000..c6883989 --- /dev/null +++ b/tests/compile-only/fail-array-decl-with-zero-enum-value.sp @@ -0,0 +1,9 @@ +enum Pl0000 +{ + Pl_MAX +} + +enum struct Array +{ + int value[Pl_MAX]; +} diff --git a/tests/compile-only/fail-array-decl-with-zero-enum-value.txt b/tests/compile-only/fail-array-decl-with-zero-enum-value.txt new file mode 100644 index 00000000..3f01e1b8 --- /dev/null +++ b/tests/compile-only/fail-array-decl-with-zero-enum-value.txt @@ -0,0 +1 @@ +(8) : error 009: invalid array size (negative, zero or out of bounds)