Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling in "+=" and similar operators #406

Merged
merged 10 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions self_hosted/typecheck.jou
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def can_cast_explicitly(from: Type*, to: Type*) -> bool:
# of types. We cannot use printf() style functions because the arguments can be in
# any order.
def fail_with_implicit_cast_error(location: Location, template: byte*, from: Type*, to: Type*) -> void:
assert template != NULL

n = 0
for i = 0; template[i] != '\0'; i++:
if template[i] == '<':
Expand Down Expand Up @@ -1167,13 +1169,19 @@ class Stage3TypeChecker:
) -> void:
ensure_can_take_address(target, "cannot assign to %s")
target_types = self->do_expression(target)
value_types = self->do_expression(value)

t = check_binop(op_expr_kind, location, target_types, value_types)
temp_value_types = ExpressionTypes{ expression = target, original_type = t }

error_template: byte[200]
strcpy(error_template, op_description)
strcat(error_template, " produced a value of type <from> which cannot be assigned back to <to>")
temp_value_types.do_implicit_cast(target_types->original_type, location, error_template)

# TODO: this logic doesn't make much sense
self->do_expression_and_implicit_cast(value, target_types->original_type, error_template)
# I think it is currently impossible to cast target.
# If this assert fails, we probably need a new error message.
assert target_types->implicit_cast_type == NULL

def do_statement(self, statement: AstStatement*) -> void:
if statement->kind == AstStatementKind::ExpressionStatement:
Expand Down
33 changes: 22 additions & 11 deletions src/typecheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ any order.
static noreturn void fail_with_implicit_cast_error(
Location location, const char *template, const Type *from, const Type *to)
{
assert(template);

List(char) msg = {0};
while(*template){
if (!strncmp(template, "FROM", 4)) {
Expand Down Expand Up @@ -1203,23 +1205,32 @@ static void typecheck_statement(FileTypes *ft, const AstStatement *stmt)
const AstExpression *targetexpr = &stmt->data.assignment.target;
const AstExpression *valueexpr = &stmt->data.assignment.value;

ensure_can_take_address(targetexpr, "cannot assign to %s");

enum AstExpressionKind op;
const char *opname;

switch(stmt->kind) {
case AST_STMT_INPLACE_ADD: opname = "addition"; break;
case AST_STMT_INPLACE_SUB: opname = "subtraction"; break;
case AST_STMT_INPLACE_MUL: opname = "multiplication"; break;
case AST_STMT_INPLACE_DIV: opname = "division"; break;
case AST_STMT_INPLACE_MOD: opname = "modulo"; break;
case AST_STMT_INPLACE_ADD: op = AST_EXPR_ADD; opname = "addition"; break;
case AST_STMT_INPLACE_SUB: op = AST_EXPR_SUB; opname = "subtraction"; break;
case AST_STMT_INPLACE_MUL: op = AST_EXPR_MUL; opname = "multiplication"; break;
case AST_STMT_INPLACE_DIV: op = AST_EXPR_DIV; opname = "division"; break;
case AST_STMT_INPLACE_MOD: op = AST_EXPR_MOD; opname = "modulo"; break;
default: assert(0);
}

char errmsg[500];
sprintf(errmsg, "%s produced a value of type FROM which cannot be assigned back to TO", opname);
ensure_can_take_address(targetexpr, "cannot assign to %s");
ExpressionTypes *targettypes = typecheck_expression_not_void(ft, targetexpr);
ExpressionTypes *valuetypes = typecheck_expression_not_void(ft, valueexpr);

const Type *t = check_binop(op, stmt->location, targettypes, valuetypes);
ExpressionTypes tempvalue_types = { .expr = targetexpr, .type = t };

char msg[500];
snprintf(msg, sizeof msg, "%s produced a value of type FROM which cannot be assigned back to TO", opname);
do_implicit_cast(&tempvalue_types, targettypes->type, stmt->location, msg);

const ExpressionTypes *targettypes = typecheck_expression_not_void(ft, targetexpr);
typecheck_expression_with_implicit_cast(ft, valueexpr, targettypes->type, errmsg);
// I think it is currently impossible to cast target.
// If this assert fails, we probably need to add another error message for it.
assert(!targettypes->implicit_cast_type);
break;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/wrong_type/inplace_add_str_int.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def foo(s: byte*) -> void:
# The "Jou way" would be: s = &s[5]
s += 5 # Error: wrong types: cannot add byte* and int
3 changes: 3 additions & 0 deletions tests/wrong_type/inplace_mul_str_str.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def foo() -> void:
x = "hello"
x *= "world" # Error: wrong types: cannot multiply byte* and byte*