Detect when a slice on the stack is accidentally returned from a function.

This commit is contained in:
Christoffer Lerno
2025-06-30 15:56:19 +02:00
parent e1a125e326
commit 9299c78747
3 changed files with 17 additions and 0 deletions

View File

@@ -108,6 +108,7 @@
- Add deprecation for `@param foo "abc"`.
- Add `--header-output` and `header-output` options for controlling header output folder.
- Generic faults is disallowed.
- Detect when a slice on the stack is accidentally returned from a function.
### Fixes
- Assert triggered when casting from `int[2]` to `uint[2]` #2115

View File

@@ -600,6 +600,13 @@ 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;
if (expr_is_const_slice(expr) && expr->const_expr.slice_init)
{
RETURN_SEMA_ERROR(outer, "A slice literal is backed by a stack allocated array which will be invalid once the function returns. "
"However, you can place the literal in a global or 'static' variable and safely return that value as long "
"as the caller of the function won't modify the slice.");
}
// We only want && and &
if (expr->expr_kind == EXPR_SUBSCRIPT_ADDR)
{

View File

@@ -0,0 +1,9 @@
import std;
fn int[] hello()
{
return { 1, 2 }; // #error: will be invalid
}
fn void main()
{
hello();
}