From 61c039227fd3d43128782bc6d4eb293378bebfaf Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Mon, 18 Sep 2023 17:59:40 +0200 Subject: [PATCH] Attempt to fix missing type inference for loop vars. Doesn't work because it loses track of `while foo/any := ...:` See #1815 --- src/compiler/type_check.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/compiler/type_check.cc b/src/compiler/type_check.cc index 62579da4f..25cff487b 100644 --- a/src/compiler/type_check.cc +++ b/src/compiler/type_check.cc @@ -251,7 +251,21 @@ class TypeChecker : public ReturningVisitor { } Type visit_While(While* node) { - auto condition_type = visit(node->condition()); + auto condition = node->condition(); + auto condition_type = visit(condition); + if (condition->is_AssignmentLocal()) { + // This is not allowed in normal Toit code. If we have an assignment, then + // it's because we moved a declaration out of the look. + // For example: + // `while chunk := read: ...` + // This became: + // chunk := ? + // while chunk = read: ... + auto local = condition->as_AssignmentLocal()->local(); + if (local->type().is_any()) { + local->set_type(condition_type); + } + } if (condition_type.is_none()) { report_error(node->condition()->range(), "Condition can't be 'none'"); } else if (condition_type != boolean_type_ &&