From 2c7f2f82dfbb319410071680f689a181e84b4794 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 3 Dec 2023 18:38:42 +0200 Subject: [PATCH] Add error message for foo().bar() (#402) --- self_hosted/typecheck.jou | 6 ++++++ src/typecheck.c | 8 ++++++++ tests/other_errors/func_then_method.jou | 11 +++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/other_errors/func_then_method.jou diff --git a/self_hosted/typecheck.jou b/self_hosted/typecheck.jou index 546b6e1a..ac27970f 100644 --- a/self_hosted/typecheck.jou +++ b/self_hosted/typecheck.jou @@ -833,6 +833,12 @@ class Stage3TypeChecker: ) fail(call->location, message) + if call->method_call_self != NULL and not call->uses_arrow_operator: + snprintf( + message, sizeof message, + "cannot take address of %%s, needed for calling the %s() method", call->name) + ensure_can_take_address(call->method_call_self, message) + signature_string = signature->to_string(False, False) expected = signature->nargs diff --git a/src/typecheck.c b/src/typecheck.c index 2d326972..684317b9 100644 --- a/src/typecheck.c +++ b/src/typecheck.c @@ -1028,6 +1028,14 @@ static ExpressionTypes *typecheck_expression(FileTypes *ft, const AstExpression case AST_EXPR_CALL_METHOD: temptype = typecheck_expression_not_void(ft, expr->data.methodcall.obj)->type; result = typecheck_function_or_method_call(ft, &expr->data.methodcall.call, temptype, expr->location); + + char tmp[500]; + snprintf( + tmp, sizeof tmp, + "cannot take address of %%s, needed for calling the %s() method", + expr->data.methodcall.call.calledname); + ensure_can_take_address(expr->data.methodcall.obj, tmp); + if (!result) return NULL; break; diff --git a/tests/other_errors/func_then_method.jou b/tests/other_errors/func_then_method.jou new file mode 100644 index 00000000..6e8ba735 --- /dev/null +++ b/tests/other_errors/func_then_method.jou @@ -0,0 +1,11 @@ +import "stdlib/io.jou" + +class Foo: + def do_stuff(self) -> void: + printf("Doing stuff!\n") + +def make_foo() -> Foo: + return Foo{} + +def bar() -> void: + make_foo().do_stuff() # Error: cannot take address of a function call, needed for calling the do_stuff() method