diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 0bf4eba62..a37b27dc7 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -5002,22 +5002,21 @@ static inline bool sema_expr_analyse_typeof(Context *context, Expr *expr) static inline bool sema_expr_analyse_failable(Context *context, Type *to, Expr *expr) { Expr *inner = expr->failable_expr; + + // Syntactic sugar: Foo! => Foo({})! + if (inner->expr_kind == EXPR_TYPEINFO) + { + TypeInfo *type = inner->type_expr; + *inner = (Expr) { .expr_kind = EXPR_COMPOUND_LITERAL, .span = inner->span }; + inner->expr_compound_literal.type_info = type; + Expr *initializer_list = expr_new(EXPR_INITIALIZER_LIST, inner->span); + initializer_list->initializer_expr.init_type = INITIALIZER_UNKNOWN; + inner->expr_compound_literal.initializer = initializer_list; + } + if (!sema_analyse_expr(context, NULL, inner)) return false; expr->pure = inner->pure; expr->constant = inner->constant; - if (inner->expr_kind == EXPR_TYPEINFO) - { - TypeInfo *inner_type_info = inner->type_expr; - if (inner_type_info->type->type_kind != TYPE_ERRTYPE) - { - SEMA_ERROR(inner, "This must be an error type."); - return false; - } - inner->expr_kind = EXPR_COMPOUND_LITERAL; - inner->expr_compound_literal.type_info = inner_type_info; - inner->expr_compound_literal.initializer = NULL; - expr_set_type(inner, inner_type_info->type); - } if (inner->failable) { SEMA_ERROR(inner, "The inner expression is already a failable."); diff --git a/test/test_suite/errors/error_throw.c3 b/test/test_suite/errors/error_throw.c3 new file mode 100644 index 000000000..fcc408ebb --- /dev/null +++ b/test/test_suite/errors/error_throw.c3 @@ -0,0 +1,11 @@ +module foo; + +error Blurg +{ +} + +func void main() +{ + int! i = Blurg!; + int! j = Blurg({})!; +} \ No newline at end of file