summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/type-checker.cc19
-rw-r--r--src/validator.cc2
-rw-r--r--test/exceptions/rethrow-not-in-catch.txt3
-rw-r--r--test/exceptions/rethrow-to-wrong-block.txt2
4 files changed, 16 insertions, 10 deletions
diff --git a/src/type-checker.cc b/src/type-checker.cc
index 2881d391..70a1d659 100644
--- a/src/type-checker.cc
+++ b/src/type-checker.cc
@@ -463,10 +463,21 @@ Result TypeChecker::OnRethrow(Index depth) {
Label* label;
CHECK_RESULT(GetLabel(depth, &label));
if (label->label_type != LabelType::Catch) {
- // TODO(karlschimpf) Make this error more readable (readers
- // typically think in terms of try/catch clauses, not all blocks).
- PrintError("invalid rethrow depth: %" PRIindex " (max %" PRIzd ")", depth,
- label_stack_.size() - 1);
+ std::string candidates;
+ size_t last = label_stack_.size() - 1;
+ for (size_t i = 0; i < label_stack_.size(); ++i) {
+ if (label_stack_[last - i].label_type == LabelType::Catch) {
+ if (!candidates.empty())
+ candidates.append(", ");
+ candidates.append(std::to_string(i));
+ }
+ }
+ if (candidates.empty()) {
+ PrintError("Rethrow not in try catch block");
+ } else {
+ PrintError("invalid rethrow depth: %" PRIindex " (catches: %s)", depth,
+ candidates.c_str());
+ }
result = Result::Error;
}
CHECK_RESULT(SetUnreachable());
diff --git a/src/validator.cc b/src/validator.cc
index 2a984224..ab635719 100644
--- a/src/validator.cc
+++ b/src/validator.cc
@@ -548,8 +548,6 @@ void Validator::CheckExpr(const Expr* expr) {
break;
case ExprType::Rethrow:
- if (try_contexts_.empty() || try_contexts_.back().catch_ == nullptr)
- PrintError(&expr->loc, "Rethrow not in try catch block");
typechecker_.OnRethrow(expr->As<RethrowExpr>()->var.index);
break;
diff --git a/test/exceptions/rethrow-not-in-catch.txt b/test/exceptions/rethrow-not-in-catch.txt
index 008764db..17cfb02c 100644
--- a/test/exceptions/rethrow-not-in-catch.txt
+++ b/test/exceptions/rethrow-not-in-catch.txt
@@ -17,7 +17,4 @@
out/test/exceptions/rethrow-not-in-catch.txt:9:8: Rethrow not in try catch block
(rethrow $try1)
^^^^^^^^^^^^^
-out/test/exceptions/rethrow-not-in-catch.txt:9:8: invalid rethrow depth: 0 (max 1)
- (rethrow $try1)
- ^^^^^^^^^^^^^
;;; STDERR ;;)
diff --git a/test/exceptions/rethrow-to-wrong-block.txt b/test/exceptions/rethrow-to-wrong-block.txt
index 7d80ce4d..75c4cace 100644
--- a/test/exceptions/rethrow-to-wrong-block.txt
+++ b/test/exceptions/rethrow-to-wrong-block.txt
@@ -14,7 +14,7 @@
)
)
(;; STDERR ;;;
-out/test/exceptions/rethrow-to-wrong-block.txt:10:12: invalid rethrow depth: 1 (max 2)
+out/test/exceptions/rethrow-to-wrong-block.txt:10:12: invalid rethrow depth: 1 (catches: 0)
(rethrow $b)
^^^^^^^^^^
;;; STDERR ;;)