Improve error message #543

This commit is contained in:
Christoffer Lerno
2022-08-28 00:58:49 +02:00
committed by Christoffer Lerno
parent c9ae0779e7
commit 5386b6fe50
3 changed files with 23 additions and 0 deletions

View File

@@ -8082,6 +8082,15 @@ bool sema_analyse_expr_rhs(SemaContext *context, Type *to, Expr *expr, bool allo
assert(allow_failable);
}
if (!sema_analyse_inferred_expr(context, to, expr)) return false;
if (to && allow_failable && to->canonical != expr->type->canonical && expr->type->canonical->type_kind == TYPE_FAULTTYPE)
{
Type *canonical = type_flatten_distinct(to);
if (canonical != type_anyerr && canonical->type_kind != TYPE_FAULTTYPE && expr->expr_kind == EXPR_CONST)
{
sema_error_at_after(expr->span, "You need to add a trailing '!' here to make this an optional.");
return false;
}
}
if (to && !cast_implicit(expr, to)) return false;
if (!allow_failable && IS_OPTIONAL(expr))
{

View File

@@ -0,0 +1,7 @@
fault Bob { FOO }
fn int! test()
{
return Bob.FOO; // #error: You need to add a trailing
}

View File

@@ -0,0 +1,7 @@
fault Bob { FOO }
fn int! test()
{
return Bob.FOO; // #error: You need to add a trailing
}