Improve error reporting for define and function definition.

This commit is contained in:
Christoffer Lerno
2023-02-14 15:49:36 +01:00
parent 61e26d8188
commit b7e19b75d0
3 changed files with 23 additions and 0 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -0,0 +1 @@
void foo(int x) {} // #error: This looks like the beginning of a function declaration