commit 03a6b516d3c3395f6e80a58396b66f740fc2ceba
parent 974918dca34966951efd639c251073f8d91d1baa
Author: Sebastian <sebastian@sebsite.pw>
Date: Thu, 5 Jan 2023 18:00:32 -0500
scope: fix abort when scope has wrong class
Previously, a labelled break or continue which didn't act on a loop
resulted in harec aborting:
$ cat t.ha
export fn main() void = :blk {
break :blk;
};
$ hare build t.ha
Error: /home/sebastian/.local/bin/harec: exited with signal 6
hare build: build failed
This now errors as intended:
$ hare build t.ha
Error /tmp/t.ha:2:14: No eligible loop for operation
Error: /home/sebastian/.local/bin/harec: exited with status 1
hare build: build failed
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
2 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/src/scope.c b/src/scope.c
@@ -50,6 +50,9 @@ scope_lookup_ancestor(struct scope *scope,
if (scope && class != scope->class) {
assert(scope->class == SCOPE_COMPOUND);
scope = scope->parent;
+ if (scope->class != class) {
+ return NULL;
+ }
}
return scope;
diff --git a/tests/12-loops.ha b/tests/12-loops.ha
@@ -95,6 +95,7 @@ fn label() void = {
assert(compile("fn test() void = { for (true) { break :bar; }; };") as exited != EXIT_SUCCESS);
assert(compile("fn test() void = { break :bar; };") as exited != EXIT_SUCCESS);
assert(compile("fn test() void = { :foo for (true) { :foo for (true) void; } ; };") as exited != EXIT_SUCCESS);
+ assert(compile("fn test() void = :foo { break :foo; };") as exited != EXIT_SUCCESS);
};
type abool = bool;