aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJwaxy <98110672+jwaxy@users.noreply.github.com>2025-09-27 13:53:04 +0300
committerGitHub <noreply@github.com>2025-09-27 13:53:04 +0300
commit4165e8e8888f4894c039c0a55cf67c24fae83af0 (patch)
tree011f2ef2bfd7c5fd4732567903a9b23967e3d62f /src
parenta7af6055b00e6f6d47d454ad91591abad9e1ea2d (diff)
Prevent returning struct containing compound literal slice
Diffstat (limited to 'src')
-rw-r--r--src/check_stmt.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp
index 62cfc256a..f509267fb 100644
--- a/src/check_stmt.cpp
+++ b/src/check_stmt.cpp
@@ -2671,6 +2671,20 @@ gb_internal void check_return_stmt(CheckerContext *ctx, Ast *node) {
ERROR_BLOCK();
unsafe_return_error(o, "a compound literal of a slice");
error_line("\tNote: A constant slice value will use the memory of the current stack frame\n");
+ } else if (expr->kind == Ast_CompoundLit && is_type_struct(o.type)) {
+ ast_node(cl, CompoundLit, expr);
+ for (Ast *elem : cl->elems) {
+ if (elem->kind == Ast_FieldValue) {
+ ast_node(fv, FieldValue, elem);
+ if (fv->value->kind == Ast_CompoundLit && is_type_slice(entity_of_node(fv->field)->type)) {
+ ast_node(sl, CompoundLit, fv->value);
+ if (sl->elems.count == 0) {
+ continue;
+ }
+ unsafe_return_error(o, "a compound literal containing a slice");
+ }
+ }
+ }
}
}