Skip to content

Commit

Permalink
Region: Rename built-in try_close() -> is_closed
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Nov 12, 2024
1 parent bb216a4 commit 625d7f5
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ set_property(TEST invalid_not_bridge.frank PROPERTY WILL_FAIL true)
set_property(TEST region_bad_1.frank PROPERTY WILL_FAIL true)
set_property(TEST region_bad_2.frank PROPERTY WILL_FAIL true)
set_property(TEST fail_cross_region_ref.frank PROPERTY WILL_FAIL true)
set_property(TEST region_close_fail_1.frank PROPERTY WILL_FAIL true)
2 changes: 1 addition & 1 deletion docs/builtin.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Forces the given region to close by invalidating all local references into to re

This function will also correct all dirty LRCs.

#### `try_close(reg)`
#### `is_closed(reg)`

Checks if the given region can be closed. If the LRC is dirty or subreagions are open, it will correct all dirty LRCs.

Expand Down
2 changes: 1 addition & 1 deletion src/rt/core/builtin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ namespace rt::core
close_function_impl(frame, args, true);
return std::nullopt;
});
add_builtin("try_close", [](auto frame, auto args) {
add_builtin("is_closed", [](auto frame, auto args) {
auto result = close_function_impl(frame, args, false);
auto result_obj = rt::get_bool(result);
// The return will be linked to the frame by the interpreter, but the RC
Expand Down
5 changes: 5 additions & 0 deletions src/rt/objects/region.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ namespace rt::objects
Region::is_ancestor(dst_reg, to_close_reg));
if (invalidate)
{
if (e.key == PrototypeField)
{
ui::error("Can't close the region due to this prototype", e);
}

auto old = src->set(e.key, nullptr);
assert(old == dst);
add_reference(src, nullptr);
Expand Down
4 changes: 2 additions & 2 deletions tests/regions/region_close_1.frank
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ lref2 = r2.b.c

# r2.LRC is now 3, from: r2, lref1, lref2

# Fail `try_close()`
if try_close(r2):
# Fail `is_closed()`
if is_closed(r2):
unreachable()

# Force close
Expand Down
4 changes: 2 additions & 2 deletions tests/regions/region_close_2.frank
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ r1.r2.b = {}
# r2.LRC is now 0
# r2.LRC will be 1, from the value on the stack

# Succeed `try_close()`
if try_close(r1.r2):
# Succeed `is_closed()`
if is_closed(r1.r2):
pass()
else:
unreachable()
Expand Down
4 changes: 2 additions & 2 deletions tests/regions/region_close_3.frank
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ lref2 = r2.b.c
freeze(r2.b)
r2 = None

# Succeed `try_close()` after LRC cleaning
if try_close(r1.r2):
# Succeed `is_closed()` after LRC cleaning
if is_closed(r1.r2):
pass()
else:
unreachable()
Expand Down
2 changes: 1 addition & 1 deletion tests/regions/region_close_4.frank
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ r0.r1.r2.r3.a = {}
lref = r0.r1.r2.r3.a

# Fail try close, since r3 is open
if try_close(r0.r1):
if is_closed(r0.r1):
unreachable()

# Force close
Expand Down
4 changes: 2 additions & 2 deletions tests/regions/region_close_5.frank
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Helper to close the region while a ref is on the stack
def close_and_get(r1):
# Check that the region is currently open
if try_close(r1.r2):
if is_closed(r1.r2):
unreachable()

# Force close it
close(r1.r2)

Expand Down
11 changes: 11 additions & 0 deletions tests/regions/region_close_fail_1.frank
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Construct regions
r1 = Region()

# Create a local string to share the `String` prototype
local_string = "Hello Local"

# This also moves the `String` prototype
r1.something = "Important string"

# This should fail, since implicit freezing is disabled
close(r1)

0 comments on commit 625d7f5

Please sign in to comment.