summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/type-checker.cc19
-rw-r--r--src/validator.cc2
2 files changed, 15 insertions, 6 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;