From b7e19b75d0eabc5a0b12c31044473262b134ea90 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Tue, 14 Feb 2023 15:49:36 +0100 Subject: [PATCH] Improve error reporting for define and function definition. --- src/compiler/parse_global.c | 18 ++++++++++++++++++ .../distinct/distinct_parse_errors.c3 | 4 ++++ test/test_suite/functions/missing_fn.c3 | 1 + 3 files changed, 23 insertions(+) create mode 100644 test/test_suite/distinct/distinct_parse_errors.c3 create mode 100644 test/test_suite/functions/missing_fn.c3 diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index f58eb2ea8..581c45987 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -1136,6 +1136,12 @@ static inline Decl *parse_global_declaration(ParseContext *c) } if (!parse_decl_initializer(c, decl, true)) return poisoned_decl; } + else if (!decl->attributes && tok_is(c, TOKEN_LPAREN) && !threadlocal) + { + // Guess we forgot `fn`? + sema_error_at(type->span, "This looks like the beginning of a function declaration but it's missing the initial `fn`. Did you forget it?"); + return poisoned_decl; + } CONSUME_EOS_OR_RET(poisoned_decl); Attr **attributes = decl->attributes; if (attributes) @@ -1928,6 +1934,18 @@ static inline Decl *parse_define_ident(ParseContext *c) if (!params) return poisoned_decl; decl->define_decl.generic_params = params; } + else if (!tok_is(c, TOKEN_EOS) && decl->define_decl.ident == kw_distinct) + { + if (token_is_any_type(c->tok)) + { + SEMA_ERROR(decl, "A type name alias must start with an uppercase letter."); + } + else + { + sema_error_at(decl->define_decl.span, "'distinct' can only be used with types."); + } + return poisoned_decl; + } RANGE_EXTEND_PREV(decl); CONSUME_EOS_OR_RET(poisoned_decl); return decl; diff --git a/test/test_suite/distinct/distinct_parse_errors.c3 b/test/test_suite/distinct/distinct_parse_errors.c3 new file mode 100644 index 000000000..7f28662fc --- /dev/null +++ b/test/test_suite/distinct/distinct_parse_errors.c3 @@ -0,0 +1,4 @@ +import std::io; + +define foo = distinct x; // #error: can only be used with types +define bar = distinct int; // #error: type name alias must start with an uppercase diff --git a/test/test_suite/functions/missing_fn.c3 b/test/test_suite/functions/missing_fn.c3 new file mode 100644 index 000000000..29f684c8c --- /dev/null +++ b/test/test_suite/functions/missing_fn.c3 @@ -0,0 +1 @@ +void foo(int x) {} // #error: This looks like the beginning of a function declaration \ No newline at end of file