Fix error messages for invalid array subscripts.
Previously the error for an out-of-range array subscript was off by one
when indicating the valid range. Fix this, and print a different message
for the case where the array is empty as this would not make sense for
the existing error message.
Change-Id: I2aba48c21559a47ce159d8dac2d0adc4fbcb310a
Reviewed-on: https://gn-review.googlesource.com/2860
Reviewed-by: Scott Graham <scottmg@google.com>
Commit-Queue: Scott Graham <scottmg@google.com>
diff --git a/tools/gn/parse_tree.cc b/tools/gn/parse_tree.cc
index 20f272b..e2201d4 100644
--- a/tools/gn/parse_tree.cc
+++ b/tools/gn/parse_tree.cc
@@ -269,12 +269,18 @@
"You gave me " + base::Int64ToString(index_int) + ".");
return false;
}
+ if (max_len == 0) {
+ *err = Err(index_->GetRange(), "Array subscript out of range.",
+ "You gave me " + base::Int64ToString(index_int) + " but the " +
+ "array has no elements.");
+ return false;
+ }
size_t index_sizet = static_cast<size_t>(index_int);
if (index_sizet >= max_len) {
*err = Err(index_->GetRange(), "Array subscript out of range.",
"You gave me " + base::Int64ToString(index_int) +
" but I was expecting something from 0 to " +
- base::NumberToString(max_len) + ", inclusive.");
+ base::NumberToString(max_len - 1) + ", inclusive.");
return false;
}