From abb5fd19ca12f51b6a0298d5fe8d019bd12445af Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Tue, 1 Feb 2022 17:59:47 -0500 Subject: Defer: Pop elements off the defer-stack when returning We restore the count later, but this fix makes it so that the compiler doesn't get stuck in an infinite loop when you try to compile the following code: ``` fn main(): int { defer return 5; return 1; } ``` --- src/generator.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/generator.c b/src/generator.c index 403bf4a..7de9d32 100644 --- a/src/generator.c +++ b/src/generator.c @@ -251,8 +251,10 @@ void generate_statement(Node *stmt, FILE *out) fprintf(out, " push rax\n"); // Save the return value // Run all the defer statements - for (int i = defer_stack_count - 1; i >= 0; i--) - generate_statement(defer_stack[i], out); + i64 old_count = defer_stack_count; + while (defer_stack_count > 0) + generate_statement(defer_stack[--defer_stack_count], out); + defer_stack_count = old_count; // TODO: Only do this if we have local variables fprintf(out, " pop rax\n"); -- cgit v1.2.3