From 6eb28aeafc177ae91ae704c3e4f2c75b9f27b092 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 22 Jul 2024 22:52:10 +0100 Subject: Check to see if people are return a slice of a local fixed array from a procedure --- src/check_stmt.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/check_stmt.cpp') diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 6c3570cc8..76b6d3f40 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -2517,7 +2517,7 @@ gb_internal void check_return_stmt(CheckerContext *ctx, Ast *node) { Entity *e = entity_of_node(x); if (is_entity_local_variable(e)) { unsafe_return_error(o, "the address of a local variable"); - } else if(x->kind == Ast_CompoundLit) { + } else if (x->kind == Ast_CompoundLit) { unsafe_return_error(o, "the address of a compound literal"); } else if (x->kind == Ast_IndexExpr) { Entity *f = entity_of_node(x->IndexExpr.expr); @@ -2532,6 +2532,14 @@ gb_internal void check_return_stmt(CheckerContext *ctx, Ast *node) { unsafe_return_error(o, "the address of an indexed variable", f->type); } } + } else if (expr->kind == Ast_SliceExpr) { + Ast *x = unparen_expr(expr->SliceExpr.expr); + Entity *e = entity_of_node(x); + if (is_entity_local_variable(e) && is_type_array(e->type)) { + unsafe_return_error(o, "a slice of a local variable"); + } else if (x->kind == Ast_CompoundLit) { + unsafe_return_error(o, "a slice of a compound literal"); + } } else if (o.mode == Addressing_Constant && is_type_slice(o.type)) { ERROR_BLOCK(); unsafe_return_error(o, "a compound literal of a slice"); -- cgit v1.2.3