Browse Source

Prevent returning struct containing compound literal slice

Jwaxy 2 weeks ago
parent
commit
4165e8e888
1 changed files with 14 additions and 0 deletions
  1. 14 0
      src/check_stmt.cpp

+ 14 - 0
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");
+					} 
+				}
+			}
 		}
 	}