diff --git a/releasenotes.md b/releasenotes.md index 547d244b8..03588ba83 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -152,6 +152,7 @@ - Updates to how variadics are implemented. - Fixes to shift checks. - Fixes to string parsing. +- Bug when rethrowing an optional from a macro which didn't return an optional. - Fixed issues with ranged cases. - Disallow trailing ',' in function parameter list. - Fixed errors on flexible array slices. diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index f3528d999..97089df64 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -6207,6 +6207,12 @@ static inline bool sema_expr_analyse_rethrow(SemaContext *context, Expr *expr) if (context->active_scope.flags & (SCOPE_EXPR_BLOCK | SCOPE_MACRO)) { + TypeInfoId rtype = context->active_scope.flags & SCOPE_MACRO ? context->current_macro->func_decl.signature.rtype : 0; + if (rtype && !type_is_optional(typeinfotype(rtype))) + { + RETURN_SEMA_ERROR(expr, "Rethrow is only allowed in macros with an optional or inferred return type. " + "Did you mean to use '!!' instead?"); + } vec_add(context->returns, NULL); expr->rethrow_expr.in_block = context->block_exit_ref; expr->rethrow_expr.cleanup = context_get_defers(context, context->active_scope.defer_last, context->block_return_defer, false); diff --git a/src/version.h b/src/version.h index fd076ba90..1feb92ae2 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.4.562" \ No newline at end of file +#define COMPILER_VERSION "0.4.563" \ No newline at end of file diff --git a/test/test_suite/errors/rethrow_macro.c3 b/test/test_suite/errors/rethrow_macro.c3 new file mode 100644 index 000000000..f2d9ec3d8 --- /dev/null +++ b/test/test_suite/errors/rethrow_macro.c3 @@ -0,0 +1,14 @@ +module testing; +import std::io; + +macro char[] read(src, allocator, n) +{ + char* data = allocator.alloc(n)!; // #error: Rethrow is only allowed in macros + src.read_all(data[:n])!; +} + +fn void main() +{ + ByteReader br; + read(br.as_stream(), mem::temp(), 10); +} \ No newline at end of file