From dd06dfa5bab3e70986399358a1e79085bcbd6f8c Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Mon, 18 Nov 2024 15:53:05 +0100 Subject: [PATCH] Fix issue with resolved try-unwrap in defer. --- releasenotes.md | 1 + resources/castrules.md | 48 +++++++++++++++++++++++ src/compiler/sema_stmts.c | 1 + test/test_suite/defer/defer_try_catch.c3t | 10 +++++ 4 files changed, 60 insertions(+) create mode 100644 test/test_suite/defer/defer_try_catch.c3t diff --git a/releasenotes.md b/releasenotes.md index 6f7eb3a48..60a86a24d 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -13,6 +13,7 @@ - A distinct inline pointer type can now participate in pointer arithmetics. - Support &a[0] returning the distinct type when applying it to a distinct of a pointer. - Fix error when calling `HashMap.remove` on uninitialized `HashMap`. +- Fix issue with resolved try-unwrap in defer. ### Stdlib changes - Add `io::MultiReader`, `io::MultiWriter`, and `io::TeeReader` structs. diff --git a/resources/castrules.md b/resources/castrules.md index eca24eb92..879b885f5 100644 --- a/resources/castrules.md +++ b/resources/castrules.md @@ -12,6 +12,54 @@ Some short names: 11. edist - explicit to anything underlying type can convert to, if inline as underlying 12. arve - if array or vec ptr +### Bool + +1. Explicitly convert to float, int, bool vector +2. Implicitly convert to bool vector init +3. Implicitly convert to distinct bool if constant value. + +### Int + +1. Implicitly convert to unsigned counterpart +2. Implicitly convert to float / wider int for simple expressions. +3. Implicitly convert to same int vector init (or any?) +4. Explicitly convert to pointer if pointer sized +5. Explicitly convert to same size int vector, enum with same backing size, bitstruct with same backing size. +6. Explicitly convert to bool, any int or float. +7. Implicitly convert to bool in conditionals. +8. Implicitly convert to any distinct integer. (Same size only?) +9. Implicitly convert to any float/int/distinct float/int if constant value that fits. + +### Float + +1. Implicitly convert to wider float for simple expressions. +2. Implicitly convert to same float vector init (or any?) +3. Explicitly convert to bool, any int or float. +4. Explicitly convert to any distinct float. (Same size only?) +5. Implicitly convert to any float / distinct float constant value that fits. + +### Non void* pointer + +1. Implicitly convert to void* and `any`. +2. Implicitly convert to an interface if the pointee implements the interface. +3. Explicitly convert to pointer sized int. +4. Implicitly convert to slice if it is a pointer to a vector or array. +5. Explicitly convert to any other pointer. +6. Explicitly convert to any distinct pointer. + +### void* pointer + +1. Implicitly convert to a pointer sized int. +2. Implicitly convert to any other pointer. +3. Explicitly convert to any distinct pointer. +4. Implicitly convert to any distinct pointer if constant value. + +### Slice + +1. Implicitly convert to a pointer of the same type. +2. + + | from, to | bool | int | float | pointer | subarr | vec | bits | distc | array | struct | union | any | fault | enum | typeid | |----------|--------|----------|--------|---------|--------|----------|----------|-------|----------|--------|--------|--------|--------|--------|--------| | bool | n/a | expl | expl | no | no | expand | no | edist | no | no | no | no | no | no | no | diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index bc3391a3e..c191c96ef 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -741,6 +741,7 @@ static inline bool sema_analyse_try_unwrap(SemaContext *context, Expr *expr) expr->try_unwrap_expr.optional = ident; expr->try_unwrap_expr.lhs = NULL; expr->try_unwrap_expr.assign_existing = true; + expr->resolve_status = RESOLVE_DONE; expr->type = type_bool; return true; } diff --git a/test/test_suite/defer/defer_try_catch.c3t b/test/test_suite/defer/defer_try_catch.c3t new file mode 100644 index 000000000..c3e1c2f61 --- /dev/null +++ b/test/test_suite/defer/defer_try_catch.c3t @@ -0,0 +1,10 @@ +// Check that this work. +import std::net; + +fn void main() { + Socket s; + defer { + char[1024] buf; + while (try s.read(&buf)); + } +} \ No newline at end of file