From 5386b6fe50543d5d8c57c77037ccf3ed2e4e1b13 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 28 Aug 2022 00:58:49 +0200 Subject: [PATCH] Improve error message #543 --- src/compiler/sema_expr.c | 9 +++++++++ test/test_suite/errors/missing_bang.c3 | 7 +++++++ test/test_suite2/errors/missing_bang.c3 | 7 +++++++ 3 files changed, 23 insertions(+) create mode 100644 test/test_suite/errors/missing_bang.c3 create mode 100644 test/test_suite2/errors/missing_bang.c3 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