diff --git a/releasenotes.md b/releasenotes.md index 58a88f511..3a7e72cb5 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -44,6 +44,7 @@ - Usage of @noreturn macro is type-checked as if it returns #1913. - Bug when indexing into a constant array at compile time. - Fixing various issues around shifts, like `z <<= { 1, 2 }`. +- `return (any)&foo` would not be reported as an escaping variable if `foo` was a pointer or slice. ### Stdlib changes - Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter. diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index 7143c808c..c7e00001a 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -569,10 +569,12 @@ INLINE bool sema_check_not_stack_variable_escape(SemaContext *context, Expr *exp { Expr *outer = expr; expr = sema_dive_into_expression(expr); + bool allow_pointer = false; // We only want && and & if (expr->expr_kind == EXPR_SUBSCRIPT_ADDR) { expr = exprptr(expr->subscript_expr.expr); + allow_pointer = true; goto CHECK_ACCESS; } if (expr->expr_kind != EXPR_UNARY) return true; @@ -598,7 +600,8 @@ CHECK_ACCESS: case TYPE_POINTER: case TYPE_SLICE: // &foo[2] is fine if foo is a pointer or slice. - return true; + if (allow_pointer) return true; + break; default: break; }