diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 46000b232..eff0935ed 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -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)) { diff --git a/test/test_suite/errors/missing_bang.c3 b/test/test_suite/errors/missing_bang.c3 new file mode 100644 index 000000000..93d41fa64 --- /dev/null +++ b/test/test_suite/errors/missing_bang.c3 @@ -0,0 +1,7 @@ + +fault Bob { FOO } + +fn int! test() +{ + return Bob.FOO; // #error: You need to add a trailing +} \ No newline at end of file diff --git a/test/test_suite2/errors/missing_bang.c3 b/test/test_suite2/errors/missing_bang.c3 new file mode 100644 index 000000000..93d41fa64 --- /dev/null +++ b/test/test_suite2/errors/missing_bang.c3 @@ -0,0 +1,7 @@ + +fault Bob { FOO } + +fn int! test() +{ + return Bob.FOO; // #error: You need to add a trailing +} \ No newline at end of file