From 0b916e813437ec9be34b423bbc4050eb5d9326ec Mon Sep 17 00:00:00 2001 From: femto Date: Fri, 20 Nov 2026 14:54:07 +0800 Subject: [PATCH 1/7] Shared(T) and Atomic: Erlang-style shared memory between threads threads.share(v) moves v into a Shared(T) that no thread can change: s.get() lends the T in place for one expression (moving it is E139), s.copy(a) is a deep copy of one's own, s.clone() another holder and s.close() ends one, the last freeing the value. T may not hold an owned struct, a channel's end, a socket or a pointer (E155). threads.atomic(n) is an Atomic i64: load, store, add (the new value; a hardware fetch-add, then an overflow check that panics as + does), swap and compare_swap (the old value, so a retry loop needs no reload), all sequentially consistent; clone and close count holders. The C uses __atomic builtins, or the _Interlocked intrinsics on MSVC; each operation is a helper emitted only when used. Co-Authored-By: Claude Opus 5.5 --- README.md | 9 +- docs/design.md | 2 +- docs/design.zh.md | 2 +- lib/egg/codegen_c.rb | 198 +++++++++++++++++++++++++++++++- lib/egg/own.rb | 6 + lib/egg/typecheck.rb | 121 ++++++++++++++++++- spec/core.md | 121 ++++++++++++++++++- spec/diagnostics.md | 3 +- test/errors/shared_resource.egg | 23 ++++ 9 files changed, 467 insertions(+), 18 deletions(-) create mode 100644 test/errors/shared_resource.egg diff --git a/README.md b/README.md index 0cb2e40..a5b3b24 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Egg compiles to portable C99. It is designed to be written by AI and read by peo - **Ownership without a GC.** A buffer has one owner. Use after free, double free and leaks are compile errors, with no lifetime annotations. - **No undefined behaviour** outside `allow unsafe` code. Integer overflow, integer division by zero and out-of-bounds indexing panic. - **Fast enough to be a systems language.** Near C on most programs, with every check on, and 1.3 to 5.6 times the Rust port's throughput on a real web app's benchmark routes, both with caches on ([numbers](#performance)). -- **Threads without shared data.** Owned values move between threads over channels. +- **Threads without data races.** Owned values move between threads over channels; what they share is immutable (`Shared(T)`) or atomic, as in Erlang, so there are no locks. ## Performance @@ -430,7 +430,12 @@ v3 (memory without GC, and what realistic programs needed): - Threads: `uses thread` gives a `Threads` value; `threads.spawn(f, x, ...)` returns an owned `Thread` to `join` or `detach`. Owned arguments move to the thread and plain values are copied, so outside `allow unsafe` raw pointers - nothing is shared and no locks are needed. `threads.channel(T, n)` returns + nothing is shared but what is made to be, and no locks are needed: + `threads.share(v)` makes a `Shared(T)` no thread can change, read in place + with `s.get()` (lent, not copied; `s.copy(a)` gives a copy to change), + held by more threads with `s.clone()` and freed by the last `s.close()`; + `threads.atomic(n)` an `Atomic` i64 (`load`, `store`, `add` giving the new + value, `swap`, `compare_swap` giving the old one). `threads.channel(T, n)` returns a `Receiver(T)` (with n of 0, `send` waits for the receiver), and `rx.sender()` a `Sender(T)`; `tx.send(v)` moves v, and `rx.recv()` gives null once every sender has closed and it is empty; `rx.recv_timeout(ms)` diff --git a/docs/design.md b/docs/design.md index eecd468..d6b7f1f 100644 --- a/docs/design.md +++ b/docs/design.md @@ -102,7 +102,7 @@ First capabilities: | `args` | command-line arguments | done (`len` and `get`) | | `fs` | files, one implementation per OS | done: whole-file `read` and `write`, open files as owned `Reader` and `Writer` values, `seek` on a `Reader`, directories (`make_dir`, `open_dir` listing as an owned `DirReader`), `remove_file`, `remove_dir`, `rename`, `stat` and `real_path`; a file open for reading and writing at once not yet | | `net` | TCP, UDP and name resolution, one implementation per OS | done: blocking sockets; Unix sockets and TLS not yet | -| `thread` | system threads; owned values move between them, so nothing is shared outside `allow unsafe` | done: `spawn`, `join`, `detach`, channels | +| `thread` | system threads; owned values move between them; shared data is immutable (`Shared(T)`) or atomic (`Atomic`), Erlang-style, with no locks | done: `spawn`, `join`, `detach`, channels, `select`, `Shared(T)`, `Atomic` | | `time` | the wall clock and a monotonic clock | done: `now` and `monotonic`, in nanoseconds; `std/time` has the calendar, RFC 3339 and HTTP dates, and `std/time/zone` time zones from the system's TZif files or POSIX TZ rules (none from Windows' registry yet) | | `rand` | the system's cryptographic random source | done: `fill` | | `raise` | raising and catching exceptions, implemented in core code | after v3 | diff --git a/docs/design.zh.md b/docs/design.zh.md index e6ca265..1cd02e0 100644 --- a/docs/design.zh.md +++ b/docs/design.zh.md @@ -91,7 +91,7 @@ fn read_config(files: Fs, a: Allocator, path: str) !Config { | `args` | 命令行参数 | 已完成(`len` 和 `get`) | | `fs` | 文件,按操作系统各有实现 | 已完成:整文件 `read` 和 `write`,打开的文件是 owned 的 `Reader` 和 `Writer`,`Reader` 可以 `seek`;目录(`make_dir`,`open_dir` 得到 owned 的 `DirReader` 逐个列出)、`remove_file`、`remove_dir`、`rename`、`stat` 和 `real_path`;同时读写打开一个文件还没有 | | `net` | TCP、UDP 和域名解析,按操作系统各有实现 | 已完成:阻塞式 socket;Unix socket 和 TLS 还没有 | -| `thread` | 系统线程;owned 值在线程间移动,`allow unsafe` 之外不共享内存 | 已完成:`spawn`、`join`、`detach`、channel | +| `thread` | 系统线程;owned 值在线程间移动;共享的数据要么不可变(`Shared(T)`),要么是原子量(`Atomic`),和 Erlang 一样不用锁 | 已完成:`spawn`、`join`、`detach`、channel、`select`、`Shared(T)`、`Atomic` | | `time` | 墙上时钟和单调时钟 | 已完成:`now` 和 `monotonic`,单位纳秒;`std/time` 有日历、RFC 3339 和 HTTP 日期,以及 `std/time/zone` 的时区(读系统的 TZif 文件或 POSIX TZ 规则;Windows 注册表里的时区还没有) | | `rand` | 系统的密码学随机源 | 已完成:`fill` | | `raise` | 异常抛出与捕获,用核心代码实现 | v3 之后 | diff --git a/lib/egg/codegen_c.rb b/lib/egg/codegen_c.rb index 0f753e3..fa69527 100644 --- a/lib/egg/codegen_c.rb +++ b/lib/egg/codegen_c.rb @@ -129,7 +129,8 @@ class CodegenC "Threads" => "typedef struct { char unused; } egg_threads;\n", "Clock" => "typedef struct { char unused; } egg_clock;\n", "Random" => "typedef struct { char unused; } egg_random;\n", - "Thread" => "typedef struct { void *h; } egg_thread;\n" + "Thread" => "typedef struct { void *h; } egg_thread;\n", + "Atomic" => "typedef struct { void *c; } egg_atomic;\n" }.freeze # Runtime helpers, emitted only when the program uses them @@ -145,6 +146,7 @@ class CodegenC egg_net_base egg_thread_base egg_thread_start egg_thread_join egg_thread_detach egg_chan egg_chan_queue egg_chan_line egg_chan_get egg_chan_put egg_chan_line_close egg_chan_wait egg_chan_new egg_chan_free egg_chan_sender egg_chan_receiver egg_chan_send egg_chan_try_send egg_chan_recv egg_chan_recv_for egg_chan_tx_close egg_chan_rx_close egg_select + egg_atomic_base egg_atomic_fetch_add egg_atomic_load egg_atomic_swap egg_atomic_cas egg_ref_retain egg_ref_release egg_atomic_new egg_atomic_add egg_atomic_close egg_shared_new egg_net_last egg_net_close egg_net_prep egg_net_start egg_net_addrinfo egg_net_recv egg_net_timeout egg_sockaddr_port egg_net_port egg_addr_port egg_print_addr egg_net_resolve egg_conn egg_conn_new egg_tcp_connect egg_net_bind egg_tcp_accept egg_tcp_read_line egg_tcp_read @@ -1293,6 +1295,96 @@ class CodegenC #endif } C + # Sequentially consistent operations on an int64_t: an Atomic's value, + # and the count of holders of an Atomic or a Shared(T); each emitted + # only when used. + "egg_atomic_base" => <<~C, + #if defined(_MSC_VER) && !defined(__clang__) + #include + #define EGG_ATOMIC_MSVC 1 + #endif + C + "egg_atomic_fetch_add" => <<~C, + static int64_t egg_atomic_fetch_add(int64_t *p, int64_t d) { + #ifdef EGG_ATOMIC_MSVC + return _InterlockedExchangeAdd64((volatile __int64 *)p, d); + #else + return __atomic_fetch_add(p, d, __ATOMIC_SEQ_CST); + #endif + } + C + "egg_atomic_load" => <<~C, + static int64_t egg_atomic_load(int64_t *p) { + #ifdef EGG_ATOMIC_MSVC + return _InterlockedOr64((volatile __int64 *)p, 0); + #else + return __atomic_load_n(p, __ATOMIC_SEQ_CST); + #endif + } + C + "egg_atomic_swap" => <<~C, + static int64_t egg_atomic_swap(int64_t *p, int64_t v) { + #ifdef EGG_ATOMIC_MSVC + return _InterlockedExchange64((volatile __int64 *)p, v); + #else + return __atomic_exchange_n(p, v, __ATOMIC_SEQ_CST); + #endif + } + C + # The old value either way: on failure, expected becomes what was there. + "egg_atomic_cas" => <<~C, + static int64_t egg_atomic_cas(int64_t *p, int64_t expected, int64_t v) { + #ifdef EGG_ATOMIC_MSVC + return _InterlockedCompareExchange64((volatile __int64 *)p, v, expected); + #else + __atomic_compare_exchange_n(p, &expected, v, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); + return expected; + #endif + } + C + "egg_ref_retain" => <<~C, + static void *egg_ref_retain(void *p, int64_t *refs) { + egg_atomic_fetch_add(refs, 1); + return p; + } + C + # Drops a holder: true for the last, which frees what is held. + "egg_ref_release" => <<~C, + static bool egg_ref_release(int64_t *refs) { + return egg_atomic_fetch_add(refs, -1) == 1; + } + C + # An Atomic: its value, then its count of holders. + "egg_atomic_new" => <<~C, + static void *egg_atomic_new(int64_t v) { + int64_t *c = (int64_t *)egg_buf_resize(NULL, 2, sizeof(int64_t)); + c[0] = v; + c[1] = 1; + return c; + } + C + # One instruction (no compare-and-swap loop to retry under + # contention); an overflow is caught after the fact, and panics. + "egg_atomic_add" => <<~C, + static int64_t egg_atomic_add(void *c, int64_t d) { + int64_t old = egg_atomic_fetch_add((int64_t *)c, d); + if ((d > 0 && old > INT64_MAX - d) || (d < 0 && old < INT64_MIN - d)) egg_panic("integer overflow"); + return old + d; + } + C + "egg_atomic_close" => <<~C, + static void egg_atomic_close(void *c) { + if (egg_ref_release((int64_t *)c + 1)) free(c); + } + C + # A Shared(T): its count of holders, then the T, 16 bytes in. + "egg_shared_new" => <<~C, + static void *egg_shared_new(size_t size) { + int64_t *p = (int64_t *)egg_buf_resize(NULL, 1, 16 + size); + p[0] = 1; + return p; + } + C "egg_thread_base" => <<~C, #ifdef _WIN32 #ifndef WIN32_LEAN_AND_MEAN @@ -3086,6 +3178,11 @@ class CodegenC "egg_net_addrinfo" => %w[egg_net_base egg_net_start egg_fs_path], "egg_thread_start" => %w[egg_thread_base egg_buf_resize], "egg_thread_join" => ["egg_thread_base"], "egg_thread_detach" => ["egg_thread_base"], "egg_chan" => ["egg_thread_base"], + "egg_atomic_fetch_add" => ["egg_atomic_base"], "egg_atomic_load" => ["egg_atomic_base"], + "egg_atomic_swap" => ["egg_atomic_base"], "egg_atomic_cas" => ["egg_atomic_base"], + "egg_ref_retain" => ["egg_atomic_fetch_add"], "egg_ref_release" => ["egg_atomic_fetch_add"], + "egg_atomic_new" => ["egg_buf_resize"], "egg_atomic_add" => ["egg_atomic_fetch_add"], + "egg_atomic_close" => ["egg_ref_release"], "egg_shared_new" => ["egg_buf_resize"], "egg_chan_new" => %w[egg_chan egg_buf_resize], "egg_chan_free" => ["egg_chan"], "egg_chan_sender" => ["egg_chan"], "egg_chan_receiver" => ["egg_chan"], "egg_chan_queue" => ["egg_chan"], "egg_chan_line" => %w[egg_chan egg_chan_queue], "egg_chan_get" => ["egg_chan_line"], "egg_chan_put" => ["egg_chan_line"], "egg_chan_line_close" => ["egg_chan"], "egg_chan_wait" => %w[egg_chan egg_chan_queue], "egg_chan_send" => %w[egg_chan_wait egg_chan_put], "egg_chan_try_send" => ["egg_chan_put"], "egg_chan_recv" => %w[egg_chan_get egg_chan_wait], "egg_chan_recv_for" => %w[egg_chan_get egg_chan_wait], "egg_chan_tx_close" => %w[egg_chan_line_close egg_chan_free], "egg_chan_rx_close" => %w[egg_chan_line_close egg_chan_free], @@ -3120,7 +3217,7 @@ class CodegenC # Helpers that never panic, so need no egg_panic. NO_PANIC = %w[egg_secure_zero egg_mul_wide egg_str_eq egg_str_cmp egg_fmt_float egg_args_state egg_args_len egg_stdin_prepare egg_clock_now egg_clock_monotonic egg_net_base egg_net_last egg_net_close egg_net_prep egg_net_recv egg_sockaddr_port egg_net_port - egg_thread_base egg_thread_join egg_thread_detach egg_chan egg_chan_free egg_chan_sender egg_chan_receiver egg_chan_queue egg_chan_line egg_chan_get egg_chan_line_close + egg_atomic_base egg_atomic_fetch_add egg_atomic_load egg_atomic_swap egg_atomic_cas egg_ref_retain egg_ref_release egg_thread_base egg_thread_join egg_thread_detach egg_chan egg_chan_free egg_chan_sender egg_chan_receiver egg_chan_queue egg_chan_line egg_chan_get egg_chan_line_close egg_chan_tx_close egg_addr_port egg_tcp_close egg_udp_send].freeze FLOAT_C = { "f32" => "float", "f64" => "double" }.freeze @@ -3153,6 +3250,7 @@ def initialize(program, checker, debug_path = nil) @need_cap = {} @int_helpers = {} # name -> C text, generated in use order @drops = {} # owned type -> its egg_drop_ function's name + @dups = {} # owned type -> its egg_dup_ function's name @out = +"" @indent = 0 @locals = [] @@ -3246,6 +3344,7 @@ def generate(roots = [@checker.main_sig]) @spawns.each { |text| out << text << "\n" } # After the prototypes: an owned struct's drop calls its deinit. emit_drops(out) + emit_dups(out) out << body out end @@ -3273,6 +3372,50 @@ def drop(t) @drops[t] end + # Deep copy of an owned value without owned structs (a Shared(T)'s T): + # each buffer anew, with its elements copied. Returns the function. + def dup(t) + return @dups[t] if @dups.key?(t) + @dups[t] = "egg_dup_#{@types[t].c_name}" + info = @types[t] + helper("egg_buf_resize") if info.kind == :buf + case info.kind + when :buf, :array then dup(info.elem) if @checker.owned?(info.elem) + when :union then info.fields.each { |_, ft, _| dup(ft) if ft != "void" && @checker.owned?(ft) } + when :struct then info.fields.each { |_, ft, _| dup(ft) if @checker.owned?(ft) } + end + @dups[t] + end + + def emit_dups(out) + return if @dups.empty? + @dups.each { |t, name| out << "static #{c_type(t)} #{name}(#{c_type(t)} v);\n" } + @dups.each do |t, name| + info = @types[t] + body = case info.kind + when :buf + copy = if @checker.owned?(info.elem) + " for (int64_t i = 0; i < v.len; i++) r.ptr[i] = #{dup(info.elem)}(v.ptr[i]);\n" + else + " memcpy(r.ptr, v.ptr, (size_t)v.len * sizeof(*v.ptr));\n" + end + " #{c_type(t)} r = { .ptr = NULL, .len = v.len, .cap = v.len };\n if (v.len == 0) return r;\n" \ + " r.ptr = egg_buf_resize(NULL, v.len, sizeof(*v.ptr));\n#{copy} return r;\n" + when :array + " #{c_type(t)} r = v;\n for (int64_t i = 0; i < #{info.len}; i++) r.a[i] = #{dup(info.elem)}(v.a[i]);\n return r;\n" + when :union + members = info.fields.each_with_index.select { |(_, ft, _), _| ft != "void" && @checker.owned?(ft) } + .map { |(m, ft, _), i| " if (v.tag == #{i}) r.u.m_#{m} = #{dup(ft)}(v.u.m_#{m});\n" }.join + " #{c_type(t)} r = v;\n#{members} return r;\n" + when :struct + fields = info.fields.select { |_, ft, _| @checker.owned?(ft) }.map { |f, ft, _| " r.f_#{f} = #{dup(ft)}(v.f_#{f});\n" }.join + " #{c_type(t)} r = v;\n#{fields} return r;\n" + end + out << "static #{c_type(t)} #{name}(#{c_type(t)} v) {\n#{body}}\n" + end + out << "\n" + end + def emit_drops(out) return if @drops.empty? @drops.each { |t, name| out << "static void #{name}(#{c_type(t)} v);\n" } @@ -3494,7 +3637,7 @@ def c_type(t) when "Args" @need_cap[t] = true "egg_args" - when "Reader", "Writer", "DirReader", "Net", "Addr", "Threads", "Thread", "Clock", "Random" + when "Reader", "Writer", "DirReader", "Net", "Addr", "Threads", "Thread", "Clock", "Random", "Atomic" @need_cap[t] = true "egg_#{t.downcase}" when "TcpConn", "TcpListener", "UdpSocket", "Poller", "Waker" @@ -4644,6 +4787,54 @@ def chan_call(m, obj, recv, args, ret) end end + # threads.share and threads.atomic, and the methods of what they make. + def shared_call(m, obj, recv, args, ret) + case m + when "Threads.share" + elem = @types[ret].elem + line("(void)#{recv};") + v = hoist(coerce(expr(args[0]), args[0].ty, elem), elem) + p = tmp + line("void *#{p} = #{helper('egg_shared_new')}(sizeof(#{c_type(elem)}));") + line("*(#{c_type(elem)} *)((char *)#{p} + 16) = #{v};") + hoist("(#{c_type(ret)}){ #{p} }", ret) + when "Threads.atomic" + line("(void)#{recv};") + hoist("(egg_atomic){ #{helper('egg_atomic_new')}(#{expr(args[0])}) }", ret) + else + c = "#{hoist(recv, obj.ty)}.c" + if m.start_with?("Shared.") + elem = @types[obj.ty].elem + value = "(*(#{c_type(elem)} *)((char *)#{c} + 16))" + case m + when "Shared.get" then value + when "Shared.copy" + line("(void)#{expr(args[0])};") + @checker.owned?(elem) ? "#{dup(elem)}(#{value})" : value + when "Shared.clone" + hoist("(#{c_type(ret)}){ #{helper('egg_ref_retain')}(#{c}, (int64_t *)#{c}) }", ret) + else + drop = @checker.owned?(elem) ? " #{drop(elem)}(#{value});" : "" + line("if (#{helper('egg_ref_release')}((int64_t *)#{c})) {#{drop} free(#{c}); }") + "(void)0" + end + else + cell = "(int64_t *)#{c}" + case m + when "Atomic.load" then "#{helper('egg_atomic_load')}(#{cell})" + when "Atomic.store" then "(void)#{helper('egg_atomic_swap')}(#{cell}, #{expr(args[0])})" + when "Atomic.add" then "#{helper('egg_atomic_add')}(#{c}, #{expr(args[0])})" + when "Atomic.swap" then "#{helper('egg_atomic_swap')}(#{cell}, #{expr(args[0])})" + when "Atomic.compare_swap" + expected = hoist(expr(args[0]), "i64") + "#{helper('egg_atomic_cas')}(#{cell}, #{expected}, #{expr(args[1])})" + when "Atomic.clone" then "(egg_atomic){ #{helper('egg_ref_retain')}(#{c}, (int64_t *)#{c} + 1) }" + else "#{helper('egg_atomic_close')}(#{c})" + end + end + end + end + # The FILE of a Reader or a Writer, computed once. def file_of_value(recv, obj) "#{hoist(recv, obj.ty)}.f" @@ -4658,6 +4849,7 @@ def io_failed def cap_call(m, obj, args, ret) recv = expr(obj) return chan_call(m, obj, recv, args, ret) if m.start_with?("Sender.", "Receiver.") || m == "Threads.channel" + return shared_call(m, obj, recv, args, ret) if m.start_with?("Shared.", "Atomic.") || %w[Threads.share Threads.atomic].include?(m) codes = args.each_with_index.map do |a, i| a.is_a?(AddrOf) && a.expr.is_a?(SliceExpr) ? slice_expr(a.expr) : in_order(expr(a), a, args[(i + 1)..]) end diff --git a/lib/egg/own.rb b/lib/egg/own.rb index 18c2727..6b7fbc1 100644 --- a/lib/egg/own.rb +++ b/lib/egg/own.rb @@ -914,6 +914,12 @@ def walk(e, borrow = false) end # info[:args]: the runtime arguments, without a generic call's types. in Call[args:, info:] then walk_args(info&.[](:args) || args, info) + in MethodCall[obj:, line:, col:] if @checker.shared_get?(e) + # s.get() lends the shared value: it may be read, not moved. + if !borrow && owned?(e.ty) + err("E139", "'#{place_text(obj)}.get()' only lends the shared #{e.ty}; it cannot be moved. Copy it with #{place_text(obj)}.copy(a)", line, col) + end + walk(obj, true) in MethodCall[obj:, args:, info:] unless obj.ty == "module" || obj.ty == "type" if info&.[](:self_inout) diff --git a/lib/egg/typecheck.rb b/lib/egg/typecheck.rb index 271d64e..41bbeac 100644 --- a/lib/egg/typecheck.rb +++ b/lib/egg/typecheck.rb @@ -91,12 +91,12 @@ class TypeChecker # literal rounds to zero), for checking literals. F32_MAX = 3.4028234663852886e38 F32_TINY = 7.006492321624085e-46 - PRIMS = (INT_TYPES + FLOAT_TYPES + %w[bool str void Io Allocator Fs Net Args Reader Writer DirReader TcpConn TcpListener UdpSocket Poller Waker Addr Threads Thread Clock Random]).freeze + PRIMS = (INT_TYPES + FLOAT_TYPES + %w[bool str void Io Allocator Fs Net Args Reader Writer DirReader TcpConn TcpListener UdpSocket Poller Waker Addr Threads Thread Atomic Clock Random]).freeze CAP_TYPES = { "Io" => "io", "Allocator" => "alloc", "Fs" => "fs", "Net" => "net", "Threads" => "thread", "Args" => "args", "Clock" => "time", "Random" => "rand" }.freeze CAPABILITIES = %w[alloc args fs io net rand thread time].freeze # Open files and sockets: owned values, made by an Fs or a Net and only # by one. They work like capability values, but main cannot receive them. - FILE_TYPES = { "Reader" => "fs", "Writer" => "fs", "DirReader" => "fs", "TcpConn" => "net", "TcpListener" => "net", "UdpSocket" => "net", "Poller" => "net", "Waker" => "net", "Thread" => "thread" }.freeze + FILE_TYPES = { "Reader" => "fs", "Writer" => "fs", "DirReader" => "fs", "TcpConn" => "net", "TcpListener" => "net", "UdpSocket" => "net", "Poller" => "net", "Waker" => "net", "Thread" => "thread", "Atomic" => "thread" }.freeze NET_ERRORS = %w[HostNotFound ConnectionRefused ConnectionReset AddressInUse TimedOut PermissionDenied IoFailed LineTooLong Closed].freeze # The errors of Fs methods, in the order the runtime numbers them from 1. FS_ERRORS = %w[NotFound PermissionDenied IsDirectory InvalidPath IoFailed AlreadyExists NotEmpty NotDirectory].freeze @@ -133,7 +133,12 @@ class TypeChecker "UdpSocket" => { "recv_from" => [%w[Allocator inout_buffer i64], "!Addr"], "send_to" => [%w[Addr text], "!void"], "set_timeout" => [["i64"], "!void"], "port" => [[], "i64"], "close" => [[], "void"] }, "Addr" => { "port" => [[], "i64"] }, - "Threads" => { "spawn" => [[], "Thread"], "channel" => [[], "Receiver"] }, + "Threads" => { "spawn" => [[], "Thread"], "channel" => [[], "Receiver"], "share" => [[], "Shared"], "atomic" => [["i64"], "Atomic"] }, + # An i64 every thread holding a clone reaches; add returns the new + # value and panics on overflow, as + does; swap and compare_swap + # return the old one (compare_swap stored new if it was expected). + "Atomic" => { "load" => [[], "i64"], "store" => [["i64"], "void"], "add" => [["i64"], "i64"], "swap" => [["i64"], "i64"], + "compare_swap" => [%w[i64 i64], "i64"], "clone" => [[], "Atomic"], "close" => [[], "void"] }, "Clock" => { "now" => [[], "i64"], "monotonic" => [[], "i64"] }, "Random" => { "fill" => [["inout_buffer"], "void"] }, "Thread" => { "join" => [[], "void"], "detach" => [[], "void"] }, @@ -158,7 +163,7 @@ class TypeChecker "push" => [%w[inout_buffer element], "void"], "pop" => [["inout_buffer"], "?T"] } }.freeze # Type names the language defines; no declaration may take them. - RESERVED_TYPES = (PRIMS + FS_VALUE_TYPES + NET_VALUE_TYPES + %w[Buf Sender Receiver TrySend]).freeze + RESERVED_TYPES = (PRIMS + FS_VALUE_TYPES + NET_VALUE_TYPES + %w[Buf Sender Receiver TrySend Shared]).freeze ARITH_OPS = %w[+ - * / %].freeze ORDER_OPS = %w[< <= > >=].freeze # Integers only, both sides of one type. @@ -1079,6 +1084,7 @@ def resolve_type(t, line, col) return buf_type(args, line, col) if t.name == "Buf" return chan_type(t.name, args, line, col) if %w[Sender Receiver].include?(t.name) return try_send_type(args, line, col) if t.name == "TrySend" + return shared_type(args, line, col) if t.name == "Shared" instantiate_type_fn(qualify(t.name), args, line, col, t.name) when PtrType inner = resolve_type(t.inner, line, col) @@ -1165,6 +1171,61 @@ def chan_end?(t) end public :chan_end? + # Shared(T): a T no thread may change, which every thread holding a + # clone may copy out. Owned, like a channel's end, and laid out like + # one (a pointer to the T and its count of holders). + def shared_type(args, line, col) + if args.length != 1 + err("E104", "Shared takes one type argument, but #{args.length} were given", line, col) + return "error" + end + t = args[0] + return "error" if t == "error" + return value_type_error(t, "a shared value", line, col) unless storable?(t) + return owned_place_error(t, "a shared value", line, col) if owned_err?(t) + r = any_resource(t) + unless r.nil? + held = r == t ? "" : " (it holds a #{r})" + err("E155", "a shared value cannot be a #{t}#{held}: #{r} is an owned struct, which copying could not duplicate; share the data it holds", line, col) + return "error" + end + p = ptr_in(t) + unless p.nil? + err("E155", "a shared value cannot hold a pointer (#{p}): what it points at could change under other threads", line, col) + return "error" + end + canon = "Shared(#{t})" + info = register(canon, :struct, line, col) + info.resource = true + info.fields = [] + info.elem = t + info.mod = "" + canon + end + + def shared?(t) + t.start_with?("Shared(") && chan_end?(t) + end + public :shared? + + # The pointer type t is or holds, or nil. + def ptr_in(t, seen = {}) + return nil if seen[t] + seen[t] = true + info = @types[t] + case info&.kind + when :ptr then t + when :array, :buf, :opt then ptr_in(info.elem || info.inner, seen) + when :struct, :union + (info.fields || []).each do |_, ft, _| + next if ft == "void" + r = ptr_in(ft, seen) + return r unless r.nil? + end + nil + end + end + # ?T: a T or null. def opt_type(inner) canon = "?#{inner}" @@ -1348,10 +1409,18 @@ def owned_place?(e) in Var then true in Field[obj:] then owned_place?(obj) in Index[obj:] then owned_place?(obj) + in MethodCall then shared_get?(e) else false end end + # s.get(): a Shared(T)'s value, lent for as long as the expression it + # is in; moving it is E139 (own.rb). + def shared_get?(e) + e.is_a?(MethodCall) && e.info.is_a?(Hash) && e.info[:cap_method] == "Shared.get" + end + public :shared_get? + # An owned value made without taking anything: Buf(T).empty, and # literals and @splat built only of those. a.alloc, a.resize and @splat # copy their fill value, which is safe only for these. @@ -2776,6 +2845,8 @@ def check_method_call(e, obj, name, args, line, col, expected = nil) return check_all_error(args) if t == "error" return check_spawn(e, args, line, col) if t == "Threads" && name == "spawn" return check_channel(e, args, line, col) if t == "Threads" && name == "channel" + return check_share(e, args, line, col) if t == "Threads" && name == "share" + return check_shared_method(e, t, name, args, line, col) if shared?(t) return check_chan_method(e, t, name, args, line, col) if chan_end?(t) if CAP_METHODS.key?(t) r = check_cap_method(e, obj, t, name, args, line, col, expected) @@ -2910,6 +2981,46 @@ def check_channel(e, args, line, col) chan_type("Receiver", [t], line, col) end + # threads.share(v): v, moved into a Shared(T). + def check_share(e, args, line, col) + @current.direct["thread"] ||= [line, col, "Threads.share"] + if args.length != 1 + err("E104", "'share' takes the value to share, but #{args.length} were given", line, col) + return check_all_error(args) + end + t = check_expr(args[0]) + return "error" if t == "error" + st = shared_type([t], args[0].line, args[0].col) + return "error" if st == "error" + check_args(Sig.new("share", [[t, "sink"]], "void"), args, "share", line, col) + e.info[:cap_method] = "Threads.share" + e.info[:args] = args + e.info[:params] = [[t, "sink"]] + st + end + + SHARED_METHODS = %w[get copy clone close].freeze + + def check_shared_method(e, t, name, args, line, col) + elem = @types[t].elem + @current.direct["thread"] ||= [line, col, "Shared.#{name}"] + unless SHARED_METHODS.include?(name) + err("E122", "Shared has no method '#{name}' (methods: #{SHARED_METHODS.join(', ')})", line, col) + return check_all_error(args) + end + params = name == "copy" ? [["Allocator", nil]] : [] + check_args(Sig.new(name, params, "void"), args, name, line, col) + @current.direct["alloc"] ||= [line, col, "Shared.copy"] if name == "copy" + e.info[:cap_method] = "Shared.#{name}" + e.info[:args] = args + e.info[:params] = params + case name + when "get", "copy" then elem + when "clone" then t + else "void" + end + end + CHAN_METHODS = { "Sender" => %w[send try_send clone close], "Receiver" => %w[recv recv_timeout sender clone close] }.freeze def check_chan_method(e, t, name, args, line, col) @@ -2965,7 +3076,7 @@ def check_cap_method(e, obj, t, name, args, line, col, expected = nil) @current.direct["alloc"] ||= [line, col, "Fs.#{name}"] if %w[read real_path].include?(name) end # Reading standard input can fail, and fills a buffer. - if (t == "Io" && name.start_with?("read_")) || (FILE_TYPES.key?(t) && name != "close") + if (t == "Io" && name.start_with?("read_")) || (FILE_TYPES.key?(t) && name != "close" && t != "Atomic") error_id("IoFailed") @current.direct["alloc"] ||= [line, col, "#{t}.#{name}"] if name.start_with?("read") || %w[recv_from next peer_ip].include?(name) end diff --git a/spec/core.md b/spec/core.md index 0d74f5f..f58dfaa 100644 --- a/spec/core.md +++ b/spec/core.md @@ -2132,11 +2132,13 @@ system thread, for `f` a function returning `void`. It returns an owned `main` returns, the program ends, detached threads with it. A panic on any thread ends the program, and so does failing to start a thread. -Nothing is shared. The arguments must be things a thread can own: `f` -takes owned values by `sink`, so they move to the new thread, and plain -values by copy. A parameter that would lend something to it, `inout`, a slice -or a plain owned value, is E148, as is a function that returns a value. So -two threads never reach the same memory, without locks. Capability values +Nothing is shared but what is made to be. The arguments must be things a +thread can own: `f` takes owned values by `sink`, so they move to the new +thread, and plain values by copy. A parameter that would lend something to +it, `inout`, a slice or a plain owned value, is E148, as is a function that +returns a value. So two threads never reach the same memory, without +locks, except through a `Shared(T)`, which no thread can change, or an +`Atomic` (below). Capability values are plain values, and `str`s never change, so both may be passed. Raw pointers in an `allow unsafe` file are the exception: what they point at is not checked. @@ -2339,6 +2341,115 @@ fn main(io: Io, a: Allocator, threads: Threads) void { } ``` +**Shared values.** As in Erlang, memory threads share is either never +changed or reached only through operations that cannot race; there are no +locks. `threads.share(v)` moves v into a `Shared(T)`, which every thread +holding one reads at once, without copying: `s.get()` lends its `T` for +the expression it is in, as a plain parameter is lent (index it, slice it, +read its fields, pass it to a plain parameter; moving it, writing to it, +passing it as `inout` or keeping it in a local borrow is E139, E002, E105 +or E121). `s.copy(a)` is a `T` of the caller's own, every buffer in it +copied, to change or keep. `s.clone()` is another holder of the same +value, to send to another thread or move into one; `s.close()` ends a +holder, and the last one frees the value. `Shared(T)` is owned (E135 if +not closed), and T is any value type without an owned struct, a channel's +end, a file or socket (copying could not duplicate them) or a pointer +(E155). Use `get`; `copy` is for the value that will be changed. + +**Atomics.** `threads.atomic(n)` is an `Atomic`: an `i64` every thread +holding one reads and changes in single steps. `x.load()` reads it, +`x.store(v)` writes it, `x.add(d)` adds d and returns the new value (an +overflow panics `integer overflow`, as `+` does: the value has wrapped by +then, but the program is ending), `x.swap(v)` writes v and returns the old +value, and `x.compare_swap(expected, v)` writes v if the value is +expected, and returns the old value either way, so a loop that retries +has the value to retry with. All are sequentially consistent. `x.clone()` +and `x.close()` hold and end it, as for a `Shared(T)`. + +```egg +// expect: 4 readers saw 11 bytes +// expect: hello world 2 +// expect: copy hello world! +// expect: 10 +uses alloc, io, thread + +const Post = struct { + id: i64 + body: Buf(u8) +} + +fn read(sink post: Shared(Post), sink seen: Atomic, sink done: Sender(i64)) void { + defer done.close() + done.send(@len(post.get().body)) + post.close() + _ = seen.add(1) + seen.close() +} + +fn show(io: Io, p: Post) void { + io.print(p.body, " ", p.id) +} + +fn main(io: Io, a: Allocator, threads: Threads) void { + const post = threads.share(Post{ .id = 2, .body = a.concat("hello", " world") }) + const seen = threads.atomic(0) + const results = threads.channel(i64, 4) + var i = 0 + while (i < 4) { + threads.spawn(read, post.clone(), seen.clone(), results.sender()).detach() + i = i + 1 + } + var bytes = 0 + while (results.recv()) |n| { + bytes = n + } + results.close() + io.print(seen.load(), " readers saw ", bytes, " bytes") + show(io, post.get()) // lent, not copied + var mine = post.copy(a) // a Post of its own + defer a.free(mine) + a.append_str(&mine.body, "!") + io.print("copy ", mine.body) + post.close() + // Doubled in a loop that retries: compare_swap gives the value to retry with. + var cur = seen.load() + while (true) { + const old = seen.compare_swap(cur, cur * 2 + 2) + if (old == cur) { + break + } + cur = old + } + io.print(seen.load()) + seen.close() +} +``` + +```egg +// error: E139 +uses alloc, io, thread + +const Post = struct { id: i64, body: Buf(u8) } + +fn main(io: Io, a: Allocator, threads: Threads) void { + const post = threads.share(Post{ .id = 1, .body = a.concat("a", "b") }) + const p = post.get() // would take the body from every holder + io.print(p.id) + post.close() +} +``` + +```egg +// panic: integer overflow +uses thread + +fn main(threads: Threads) void { + const x = threads.atomic(9223372036854775807) + _ = x.add(1) + x.close() +} +``` + Capability values cannot be created or compared. `main` receives them from the runtime, and other code gets them as arguments; they may be stored in locals and struct fields like any value. diff --git a/spec/diagnostics.md b/spec/diagnostics.md index a3d4f7c..0e61a7e 100644 --- a/spec/diagnostics.md +++ b/spec/diagnostics.md @@ -52,7 +52,7 @@ program imports: | E136 | ownership | a variable used after its buffer or owned struct was moved (passed, assigned, returned, freed or forgotten), also by a `defer` that runs after the move; a struct used whole while one of its buffer fields is moved; a field read or assigned after the whole struct was moved | use it before moving it, or give it a new buffer first (`v.data = ...`, or `v = ...` after moving all of `v`) | | E137 | ownership | a buffer moved, reassigned, or passed as `inout` to a function that may move, reassign or resize it (directly or through the functions it calls; an `Allocator` method, such as `a.push(&b, x)`, always counts as one that does) inside a `for` loop over it (or over a field of it, or over a struct holding it); an element swapped (`@replace`, `@take`, `&xs[i]`) inside a `for` loop over `xs` when its elements own buffers | if the elements own no buffers, change only them (`b[i] = v`); otherwise collect changes and apply them after the loop, or loop over an index with `while` | | E138 | checker | a `link` whose library name is not letters, digits and `_ . + -` (or not valid UTF-8, from a `\xNN` byte), or starts with `.`, `+` or `-` | write the name the C compiler takes after `-l`: `link "m"` for `-lm` | -| E139 | ownership | moving an owned value out of an element of an array or buffer (`xs[i]`, `xs[i].data`), assigning over one, or moving a `for` loop variable that borrows an owned element, or moving or `@forget`ting a local borrow (`const r = &p` or `inout r = &p`) or a part of it, or a switch capture that borrows the payload of a union that cannot be moved here (a plain parameter, an element) | for a capture: declare the parameter `sink`, or take the element out with `@replace(&xs[i], v)` and switch on that; `a.free(@replace(&xs[i], v))` to put a new value in, `@take(&xs[i])` to take a buffer out (leaving `.empty`) | +| E139 | ownership | moving an owned value out of an element of an array or buffer (`xs[i]`, `xs[i].data`), assigning over one, or moving a `for` loop variable that borrows an owned element, or moving or `@forget`ting a local borrow (`const r = &p` or `inout r = &p`) or a part of it, or a switch capture that borrows the payload of a union that cannot be moved here (a plain parameter, an element), or moving an owned value out of a `Shared(T)` with `s.get()` | `s.copy(a)` for a value of one's own; for a capture: declare the parameter `sink`, or take the element out with `@replace(&xs[i], v)` and switch on that; `a.free(@replace(&xs[i], v))` to put a new value in, `@take(&xs[i])` to take a buffer out (leaving `.empty`) | | E140 | checker | the fill value of `a.alloc`, `a.resize` or `@splat` owns buffers or is (or holds) an owned struct, so it would be copied into every element | fill with `Buf(T).empty` (or a literal made of `.empty` buffers) and put values in with `@replace`; for owned structs use a `Buf`: start from `.empty` and `a.push` each one (an array literal cannot hold owned values, so there is no way yet to fill an array of them) | | E141 | checker | an `owned struct` without a `deinit`, or a value holding one, freed with `a.free` or used as an element of an array or a `Buf` | give it a `deinit`; or end it with its own API (such as `close`) and keep it in a local or a struct field | | E142 | checker | building an `owned struct` (`Db{ ... }`) or ending one with `@forget` outside the file that declares it | get one from, and end it with, the functions of its file | @@ -67,6 +67,7 @@ program imports: | E152 | checker | `from` on a function whose result is not a slice, or naming something that is not a plain parameter able to hold the result's elements (not a parameter, an `inout`, `sink` or `comptime` one, or one of a type such as `i64`) | name the plain parameters the result views, or remove `from` | | E153 | checker | a `select` arm that is not a `Receiver(T)` or a `tx.send(v)`; a receiving arm without `\|v\|` whose values are owned; an owned value sent from something other than a variable or a field; `try` (or `orelse`/`catch` that leaves) in an arm's head; more than one `timeout` or `closed` arm; no arm that receives or sends; only receiving arms but no `closed` arm, or a `closed` arm beside a sending one, where it could never run | receive from a `Receiver` or send with `tx.send(v)` on a `Sender`; give each owned value a name, send owned values from a variable, compute what may fail before the `select`; keep one `timeout` and one `closed` arm; add `closed => { ... }` to a select that only receives, and remove it beside a sending arm | | E154 | checker | a field of an `extern struct` that is not a C type: an integer, float, `bool`, pointer to a C type, function type whose parameters and result are C types, array of a C type, or extern struct (not `str`, a slice, `Buf`, `?T`, `!T`, an enum, a union or a plain struct) | hold C types only: declare a nested struct as `extern struct` too, and pass text as a `*u8` | +| E155 | checker | a `Shared(T)` whose T is or holds an owned struct, a channel's end, a file or socket, or a pointer | share the plain data instead (buffers, structs and unions of them) | Runtime panics (the program aborts with `egg panic: `): diff --git a/test/errors/shared_resource.egg b/test/errors/shared_resource.egg new file mode 100644 index 0000000..019381e --- /dev/null +++ b/test/errors/shared_resource.egg @@ -0,0 +1,23 @@ +// error: E155 @16 +// error: E155 @17 +// error: E155 @18 +uses alloc, net, thread + +const Wrap = struct { tx: Sender(i64) } + +const Db = owned struct { + n: i64 + + fn deinit(sink self: Db) void { + @forget(self) + } +} + +fn keep(s: Shared(Db)) void {} +fn conn(s: Shared(TcpConn)) void {} +fn wrapped(s: Shared(Wrap)) void {} + +fn main(threads: Threads) void { + const s = threads.share(1) + s.close() +} From 49fdf5675edd2e36d6dc5f3da4eb36642560eb0d Mon Sep 17 00:00:00 2001 From: femto Date: Fri, 20 Nov 2026 15:15:54 +0800 Subject: [PATCH 2/7] Shared/Atomic review fixes: results computed in order, get() borrows tracked Atomic results and Shared.copy are hoisted, so a later operand cannot run first. A field of s.get() cannot be moved out (E139); for over s.get() holds s (E137); an operand changing s conflicts with s.get() (E117); a temporary Shared cannot be read (E133). E155 waits for types still being registered (no crash on Node { next: Shared(Node) }) and sees pointers in error unions. Co-Authored-By: Claude Opus 5.5 --- examples/thread_atomic_order.egg | 22 ++++++++++++++++ lib/egg/codegen_c.rb | 14 +++++----- lib/egg/own.rb | 16 ++++++++++++ lib/egg/typecheck.rb | 43 +++++++++++++++++++++---------- test/errors/shared_get_borrow.egg | 15 +++++++++++ test/errors/shared_get_moves.egg | 28 ++++++++++++++++++++ test/errors/shared_resource.egg | 15 ++++++++--- 7 files changed, 131 insertions(+), 22 deletions(-) create mode 100644 examples/thread_atomic_order.egg create mode 100644 test/errors/shared_get_borrow.egg create mode 100644 test/errors/shared_get_moves.egg diff --git a/examples/thread_atomic_order.egg b/examples/thread_atomic_order.egg new file mode 100644 index 0000000..aa4014f --- /dev/null +++ b/examples/thread_atomic_order.egg @@ -0,0 +1,22 @@ +// An Atomic's results are taken where they stand in an expression, before +// the operands after them run: here bump changes x after each is read. +// expect: 0 1 +// expect: 1 6 +// expect: 6 10 +uses io, thread + +fn bump(x: Atomic) i64 { + return x.add(1) +} + +fn observe(io: Io, before: i64, after: i64) void { + io.print(before, " ", after) +} + +fn main(io: Io, threads: Threads) void { + const x = threads.atomic(0) + observe(io, x.load(), bump(x)) + observe(io, x.swap(5), bump(x)) + observe(io, x.compare_swap(6, 9), bump(x)) + x.close() +} diff --git a/lib/egg/codegen_c.rb b/lib/egg/codegen_c.rb index fa69527..974f2f6 100644 --- a/lib/egg/codegen_c.rb +++ b/lib/egg/codegen_c.rb @@ -4810,7 +4810,7 @@ def shared_call(m, obj, recv, args, ret) when "Shared.get" then value when "Shared.copy" line("(void)#{expr(args[0])};") - @checker.owned?(elem) ? "#{dup(elem)}(#{value})" : value + hoist(@checker.owned?(elem) ? "#{dup(elem)}(#{value})" : value, elem) when "Shared.clone" hoist("(#{c_type(ret)}){ #{helper('egg_ref_retain')}(#{c}, (int64_t *)#{c}) }", ret) else @@ -4820,15 +4820,17 @@ def shared_call(m, obj, recv, args, ret) end else cell = "(int64_t *)#{c}" + # Each result is computed here, before any later operand of the + # expression it is in runs: observe(x.load(), bump(x)). case m - when "Atomic.load" then "#{helper('egg_atomic_load')}(#{cell})" + when "Atomic.load" then hoist("#{helper('egg_atomic_load')}(#{cell})", "i64") when "Atomic.store" then "(void)#{helper('egg_atomic_swap')}(#{cell}, #{expr(args[0])})" - when "Atomic.add" then "#{helper('egg_atomic_add')}(#{c}, #{expr(args[0])})" - when "Atomic.swap" then "#{helper('egg_atomic_swap')}(#{cell}, #{expr(args[0])})" + when "Atomic.add" then hoist("#{helper('egg_atomic_add')}(#{c}, #{expr(args[0])})", "i64") + when "Atomic.swap" then hoist("#{helper('egg_atomic_swap')}(#{cell}, #{expr(args[0])})", "i64") when "Atomic.compare_swap" expected = hoist(expr(args[0]), "i64") - "#{helper('egg_atomic_cas')}(#{cell}, #{expected}, #{expr(args[1])})" - when "Atomic.clone" then "(egg_atomic){ #{helper('egg_ref_retain')}(#{c}, (int64_t *)#{c} + 1) }" + hoist("#{helper('egg_atomic_cas')}(#{cell}, #{expected}, #{expr(args[1])})", "i64") + when "Atomic.clone" then hoist("(egg_atomic){ #{helper('egg_ref_retain')}(#{c}, (int64_t *)#{c} + 1) }", ret) else "#{helper('egg_atomic_close')}(#{c})" end end diff --git a/lib/egg/own.rb b/lib/egg/own.rb index 6b7fbc1..31ad91b 100644 --- a/lib/egg/own.rb +++ b/lib/egg/own.rb @@ -726,6 +726,9 @@ def assign_path(path, line, col) # one), or nil. def iter_root(iter) v = iter.is_a?(SliceExpr) ? iter.obj : iter + # for (s.get().items): the loop holds s. + holder = shared_holder(v) + return path_of(holder) unless holder.nil? p = path_of(v) !p.nil? && owned?(v.ty) ? p : nil end @@ -1176,7 +1179,20 @@ def moved_error(path, moved, line, col) # Moving a value out of an element (xs[i], xs[i].f): not followed, so # not allowed. + # The Shared(T) whose s.get() e is, or a field or element of: what + # lends it. nil for anything else. + def shared_holder(e) + e = e.obj while e.is_a?(Field) || e.is_a?(Index) || e.is_a?(SliceExpr) + @checker.shared_get?(e) ? e.obj : nil + end + def moved_out(e, line, col) + holder = shared_holder(e) + unless holder.nil? + h = place_text(holder) + err("E139", "cannot move a #{e.ty} out of '#{h}.get()': it only lends the shared value; copy it with #{h}.copy(a)", line, col) + return + end p = e p = p.obj while p.is_a?(Field) return unless p.is_a?(Index) diff --git a/lib/egg/typecheck.rb b/lib/egg/typecheck.rb index 41bbeac..7a24736 100644 --- a/lib/egg/typecheck.rb +++ b/lib/egg/typecheck.rb @@ -891,7 +891,7 @@ def flush_elem_checks saved = @mod @mod = mod begin - resource_elem_error(elem, what, line, col) + what == :shared ? shared_value_error(elem, line, col) : resource_elem_error(elem, what, line, col) ensure @mod = saved end @@ -1183,15 +1183,11 @@ def shared_type(args, line, col) return "error" if t == "error" return value_type_error(t, "a shared value", line, col) unless storable?(t) return owned_place_error(t, "a shared value", line, col) if owned_err?(t) - r = any_resource(t) - unless r.nil? - held = r == t ? "" : " (it holds a #{r})" - err("E155", "a shared value cannot be a #{t}#{held}: #{r} is an owned struct, which copying could not duplicate; share the data it holds", line, col) - return "error" - end - p = ptr_in(t) - unless p.nil? - err("E155", "a shared value cannot hold a pointer (#{p}): what it points at could change under other threads", line, col) + # A struct still being registered (Node { next: Shared(Node) }) has + # no fields yet: checked once every type is. + if @registering.positive? + @elem_checks << [t, :shared, line, col, @mod] + elsif shared_value_error(t, line, col) return "error" end canon = "Shared(#{t})" @@ -1203,6 +1199,20 @@ def shared_type(args, line, col) canon end + # E155 for a T no Shared(T) can hold. + def shared_value_error(t, line, col) + r = any_resource(t) + unless r.nil? + held = r == t ? "" : " (it holds a #{r})" + err("E155", "a shared value cannot be a #{t}#{held}: #{r} is an owned struct, which copying could not duplicate; share the data it holds", line, col) + return true + end + p = ptr_in(t) + return false if p.nil? + err("E155", "a shared value cannot hold a pointer (#{p}): what it points at could change under other threads", line, col) + true + end + def shared?(t) t.start_with?("Shared(") && chan_end?(t) end @@ -1215,7 +1225,9 @@ def ptr_in(t, seen = {}) info = @types[t] case info&.kind when :ptr then t - when :array, :buf, :opt then ptr_in(info.elem || info.inner, seen) + when :array, :buf, :opt, :err + inner = info.elem || info.inner + inner.nil? || inner == "void" ? nil : ptr_in(inner, seen) when :struct, :union (info.fields || []).each do |_, ft, _| next if ft == "void" @@ -2846,7 +2858,7 @@ def check_method_call(e, obj, name, args, line, col, expected = nil) return check_spawn(e, args, line, col) if t == "Threads" && name == "spawn" return check_channel(e, args, line, col) if t == "Threads" && name == "channel" return check_share(e, args, line, col) if t == "Threads" && name == "share" - return check_shared_method(e, t, name, args, line, col) if shared?(t) + return check_shared_method(e, obj, t, name, args, line, col) if shared?(t) return check_chan_method(e, t, name, args, line, col) if chan_end?(t) if CAP_METHODS.key?(t) r = check_cap_method(e, obj, t, name, args, line, col, expected) @@ -3001,13 +3013,15 @@ def check_share(e, args, line, col) SHARED_METHODS = %w[get copy clone close].freeze - def check_shared_method(e, t, name, args, line, col) + def check_shared_method(e, obj, t, name, args, line, col) elem = @types[t].elem @current.direct["thread"] ||= [line, col, "Shared.#{name}"] unless SHARED_METHODS.include?(name) err("E122", "Shared has no method '#{name}' (methods: #{SHARED_METHODS.join(', ')})", line, col) return check_all_error(args) end + # A holder that is a temporary would never be closed. + check_borrowed(obj, t) unless name == "close" params = name == "copy" ? [["Allocator", nil]] : [] check_args(Sig.new(name, params, "void"), args, name, line, col) @current.direct["alloc"] ||= [line, col, "Shared.copy"] if name == "copy" @@ -3636,6 +3650,9 @@ def holds?(e, path) in Binary[lhs:, rhs:] then holds?(lhs, path) || holds?(rhs, path) in Call[args:, info:] then sunk(info&.[](:args) || args, info).any? { |x| holds?(x, path) } in Builtin[args:] then args.any? { |x| holds?(x, path) } + in MethodCall[obj:] if shared_get?(e) + # s.get() lends what s holds. + holds?(obj, path) in MethodCall[obj:, args:, info:] # A borrowed argument is given back when the call returns. own_obj = info.nil? || info[:params].nil? || info[:self_sink] diff --git a/test/errors/shared_get_borrow.egg b/test/errors/shared_get_borrow.egg new file mode 100644 index 0000000..109894c --- /dev/null +++ b/test/errors/shared_get_borrow.egg @@ -0,0 +1,15 @@ +// error: E133 @12 +// error: E117 @14 +uses alloc, thread + +fn consume(b: Buf(u8), n: i64) void {} +fn close_and_zero(sink s: Shared(Buf(u8))) i64 { + s.close() + return 0 +} + +fn main(a: Allocator, threads: Threads) void { + const c = threads.share(a.concat("a", "b")).copy(a) + const s = threads.share(c) + consume(s.get(), close_and_zero(s)) +} diff --git a/test/errors/shared_get_moves.egg b/test/errors/shared_get_moves.egg new file mode 100644 index 0000000..bb6a222 --- /dev/null +++ b/test/errors/shared_get_moves.egg @@ -0,0 +1,28 @@ +// error: E139 @16 +// error: E137 @24 +uses alloc, io, thread + +const Post = struct { id: i64, body: Buf(u8) } + +fn reset(threads: Threads, a: Allocator, inout s: Shared(Buf(Buf(u8)))) void { + var fresh: Buf(Buf(u8)) = .empty + a.push(&fresh, a.concat("x", "")) + @replace(&s, threads.share(fresh)).close() +} + +fn main(io: Io, a: Allocator, threads: Threads) void { + const p = threads.share(Post{ .id = 1, .body = a.concat("a", "b") }) + // A field of what get() lends: still not the caller's to free. + const b = p.get().body + a.free(b) + p.close() + var items: Buf(Buf(u8)) = .empty + a.push(&items, a.concat("a", "")) + a.push(&items, a.concat("b", "")) + var s = threads.share(items) + for (s.get()) |item| { + reset(threads, a, &s) + io.print(item) + } + s.close() +} diff --git a/test/errors/shared_resource.egg b/test/errors/shared_resource.egg index 019381e..80f3174 100644 --- a/test/errors/shared_resource.egg +++ b/test/errors/shared_resource.egg @@ -1,6 +1,9 @@ -// error: E155 @16 -// error: E155 @17 -// error: E155 @18 +// error: E155 @19 +// error: E155 @20 +// error: E155 @21 +// error: E155 @22 +// error: E155 @26 +allow unsafe uses alloc, net, thread const Wrap = struct { tx: Sender(i64) } @@ -16,6 +19,12 @@ const Db = owned struct { fn keep(s: Shared(Db)) void {} fn conn(s: Shared(TcpConn)) void {} fn wrapped(s: Shared(Wrap)) void {} +fn pointer(s: Shared(!*i64)) void {} + +// A struct still being registered when Shared(Node) is met. +const Node = struct { + next: Shared(Node) +} fn main(threads: Threads) void { const s = threads.share(1) From 5ecfc16a84d94553c4c15b61d14788486bb77406 Mon Sep 17 00:00:00 2001 From: femto Date: Fri, 20 Nov 2026 16:24:18 +0800 Subject: [PATCH 3/7] Shared/Atomic: receivers, indices and switch subjects lend too A receiver is an operand: x.add(closer(x)) is E117, as is an index or slice bound that closes the holder of s.get() (s.get()[close(s)]). A switch over s.get() borrows the payload its arms capture (E139 to move it), instead of treating it as a fresh result. Co-Authored-By: Claude Opus 5.5 --- lib/egg/own.rb | 10 +++++++++- lib/egg/typecheck.rb | 29 ++++++++++++++++++++++------- test/errors/shared_get_moves.egg | 17 +++++++++++++++-- test/errors/shared_get_operands.egg | 24 ++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 test/errors/shared_get_operands.egg diff --git a/lib/egg/own.rb b/lib/egg/own.rb index 31ad91b..98ec11f 100644 --- a/lib/egg/own.rb +++ b/lib/egg/own.rb @@ -646,7 +646,8 @@ def walk_union_switch(subject, arms, line, col) t = arm.names&.length == 1 ? member.(arm.names[0]) : nil if !arm.capture.nil? && arm.capture != "_" && !t.nil? && owned?(t) cap = arm.capture - if subject.is_a?(Call) || subject.is_a?(MethodCall) || subject.is_a?(Builtin) + holder = shared_holder(subject) + if (subject.is_a?(Call) || subject.is_a?(MethodCall) || subject.is_a?(Builtin)) && holder.nil? # A call's result, which nothing else owns: the arm owns what it # captures. (An element has no path either, but its buffer still # owns it: its capture only borrows, below.) @@ -657,7 +658,14 @@ def walk_union_switch(subject, arms, line, col) declare(cap, :local, arm.line, arm.col, t) end else + h = holder.nil? ? nil : place_text(holder) why = case kind + when nil + if h.nil? + "'#{text}', which cannot be moved; swap it out with @replace(&#{text}, v) and switch on that" + else + "'#{h}.get()', which only lends the shared value; copy it with #{h}.copy(a) and switch on that" + end when :bparam then "'#{text}', which is only borrowed here; to take its payload, declare parameter '#{root(path)}' as sink" when :borrowed c = @borrowed[root(path)] diff --git a/lib/egg/typecheck.rb b/lib/egg/typecheck.rb index 7a24736..44feef8 100644 --- a/lib/egg/typecheck.rb +++ b/lib/egg/typecheck.rb @@ -1795,8 +1795,9 @@ def temp_union_switch?(subject, t) owned?(t) && @types[t]&.kind == :union && fresh_value?(subject) end + # A call's result, which nothing else owns (s.get() lends its value). def fresh_value?(e) - e.is_a?(Call) || e.is_a?(MethodCall) || e.is_a?(Builtin) + (e.is_a?(Call) || e.is_a?(MethodCall) || e.is_a?(Builtin)) && !shared_get?(e) end # Each member that holds something owned needs an arm of its own that @@ -2307,6 +2308,8 @@ def infer(e, expected) t = check_expr(obj) check_borrowed(obj, t) expect_type("i64", check_expr(index), index) + # s.get()[i]: i must not close or replace s first. + check_bounds_writes(obj, index, nil, "an index into") unless shared_root(obj).nil? e.info[:len] = @types[t]&.len index_type(t, line, col) in SliceExpr[obj:, lo:, hi:, line:, col:] @@ -2858,11 +2861,16 @@ def check_method_call(e, obj, name, args, line, col, expected = nil) return check_spawn(e, args, line, col) if t == "Threads" && name == "spawn" return check_channel(e, args, line, col) if t == "Threads" && name == "channel" return check_share(e, args, line, col) if t == "Threads" && name == "share" - return check_shared_method(e, obj, t, name, args, line, col) if shared?(t) - return check_chan_method(e, t, name, args, line, col) if chan_end?(t) + # The receiver is an operand too: an argument must not take it + # (x.add(close(x))) before it is used. + if shared?(t) || chan_end?(t) + r = shared?(t) ? check_shared_method(e, obj, t, name, args, line, col) : check_chan_method(e, t, name, args, line, col) + check_nested_writes([obj] + args) + return r + end if CAP_METHODS.key?(t) r = check_cap_method(e, obj, t, name, args, line, col, expected) - check_nested_writes(args) + check_nested_writes([obj] + args) return r end if t == "type" @@ -3318,12 +3326,12 @@ def check_slice_arg(a, _expected) # x[lo..hi]: the bounds may not change x (E117). x may be an element of # a buffer (b[i][lo..hi]), which a bound that grew b would move while # it is being sliced. - def check_bounds_writes(obj, lo, hi) - root = root_name(obj) + def check_bounds_writes(obj, lo, hi, what = "a bound of a slice of") + root = root_name(obj) || shared_root(obj) return if root.nil? [lo, hi].compact.each do |b| if inout_writes(b).any? { |p| p.is_a?(String) && p.split(".").first == root } - err("E117", "'#{root}' is changed by this bound of a slice of it; compute the bound into a local first", b.line, b.col) + err("E117", "'#{root}' is changed by #{what} it; compute it into a local first", b.line, b.col) end end end @@ -3599,6 +3607,13 @@ def viewable?(e) end end + # The variable holding the Shared(T) whose s.get() e is, or a field, + # element or slice of; nil for anything else. + def shared_root(e) + e = e.obj while e.is_a?(Field) || e.is_a?(Index) || e.is_a?(SliceExpr) + shared_get?(e) ? root_name(e.obj) : nil + end + def root_name(e) case e in Var[name:] then name diff --git a/test/errors/shared_get_moves.egg b/test/errors/shared_get_moves.egg index bb6a222..0252c8f 100644 --- a/test/errors/shared_get_moves.egg +++ b/test/errors/shared_get_moves.egg @@ -1,5 +1,6 @@ -// error: E139 @16 -// error: E137 @24 +// error: E139 @17 +// error: E137 @25 +// error: E139 @37 uses alloc, io, thread const Post = struct { id: i64, body: Buf(u8) } @@ -26,3 +27,15 @@ fn main(io: Io, a: Allocator, threads: Threads) void { } s.close() } + +const Msg = union(enum) { text: Buf(u8), none } + +// The payload of what get() lends is borrowed too. +fn take_payload(a: Allocator, s: Shared(Msg)) void { + switch (s.get()) { + .text => |t| { + a.free(t) + } + .none => {} + } +} diff --git a/test/errors/shared_get_operands.egg b/test/errors/shared_get_operands.egg new file mode 100644 index 0000000..cd30301 --- /dev/null +++ b/test/errors/shared_get_operands.egg @@ -0,0 +1,24 @@ +// error: E117 @17 +// error: E117 @18 +// error: E117 @19 +uses io, alloc, thread + +fn close_and_zero(sink s: Shared(Buf(u8))) i64 { + s.close() + return 0 +} + +fn closer(sink x: Atomic) i64 { + x.close() + return 1 +} + +fn use(io: Io, sink s: Shared(Buf(u8)), sink x: Atomic) void { + io.print(s.get()[close_and_zero(s)]) + io.print(s.get()[close_and_zero(s)..]) + _ = x.add(closer(x)) +} + +fn main(io: Io, a: Allocator, threads: Threads) void { + use(io, threads.share(a.concat("a", "b")), threads.atomic(0)) +} From 1538617ed267145e2153084dd7fd01278af021a1 Mon Sep 17 00:00:00 2001 From: femto Date: Fri, 20 Nov 2026 17:00:47 +0800 Subject: [PATCH 4/7] A switch capture holds what lends it; resources seen through ?/! wrappers A capture that borrows a union's payload keeps the subject's variable (or the holder of s.get()) from being moved or closed while the capture is used later (E149): s.close() or a.free(m) before a later read of the capture was a use after free. any_resource follows error unions and optionals, so Shared(!?Atomic) is E155. Co-Authored-By: Claude Opus 5.5 --- lib/egg/own.rb | 7 +++++ lib/egg/typecheck.rb | 1 + test/errors/shared_resource.egg | 7 +++-- test/errors/switch_capture_holder.egg | 37 +++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 test/errors/switch_capture_holder.egg diff --git a/lib/egg/own.rb b/lib/egg/own.rb index 98ec11f..7f495c5 100644 --- a/lib/egg/own.rb +++ b/lib/egg/own.rb @@ -672,9 +672,16 @@ def walk_union_switch(subject, arms, line, col) c.is_a?(Array) ? "'#{text}', which #{c[1].delete_prefix("it ")}" : "'#{text}', which only borrows an element of '#{c}'; loop over an index and take the element out with @replace(&#{c}[i], v) or @take(&#{c}[i])" else "'#{text}', which cannot be moved; swap it out with @replace(&#{text}, v) and switch on that" end + # What the capture views stays put while it is used: the + # subject's variable, or the holder of s.get(). + lent = holder.nil? ? path : path_of(holder) pre = lambda do @borrowed[cap] = [:payload, "it borrows the payload of #{why}"] declare(cap, :borrowed, arm.line, arm.col, t) + unless lent.nil? + @slices[cap] = [[lent], @rest.length, false, nil, :borrow] + @frames.last.slices << cap + end end end elsif !path.nil? && !arm.names.nil? && arm.names.all? { |n| (m = member.(n)) == "void" || !owned?(m) } diff --git a/lib/egg/typecheck.rb b/lib/egg/typecheck.rb index 44feef8..afadeae 100644 --- a/lib/egg/typecheck.rb +++ b/lib/egg/typecheck.rb @@ -1336,6 +1336,7 @@ def any_resource(t, seen = {}) info = @types[t] case info&.kind when :array, :buf then any_resource(info.elem, seen) + when :err, :opt then info.inner == "void" ? nil : any_resource(info.inner, seen) when :struct, :union return t if info.resource info.fields.each do |_, ft, _| diff --git a/test/errors/shared_resource.egg b/test/errors/shared_resource.egg index 80f3174..5ce5f11 100644 --- a/test/errors/shared_resource.egg +++ b/test/errors/shared_resource.egg @@ -1,8 +1,9 @@ -// error: E155 @19 // error: E155 @20 // error: E155 @21 // error: E155 @22 -// error: E155 @26 +// error: E155 @23 +// error: E155 @27 +// error: E155 @30 allow unsafe uses alloc, net, thread @@ -26,6 +27,8 @@ const Node = struct { next: Shared(Node) } +fn nested(s: Shared(!?Atomic)) void {} + fn main(threads: Threads) void { const s = threads.share(1) s.close() diff --git a/test/errors/switch_capture_holder.egg b/test/errors/switch_capture_holder.egg new file mode 100644 index 0000000..78e5cef --- /dev/null +++ b/test/errors/switch_capture_holder.egg @@ -0,0 +1,37 @@ +// error: E149 @13 +// error: E149 @25 +uses io, alloc, thread + +const Msg = union(enum) { text: Buf(u8), none } + +// A capture borrows the payload while it is used: what lends it cannot be +// closed or freed before then. +fn shared(io: Io, sink s: Shared(Msg)) void { + switch (s.get()) { + .text => |t| { + io.print(t) + s.close() + io.print(t) // t is still used: s cannot close above + } + .none => { + s.close() + } + } +} + +fn local(io: Io, a: Allocator, sink m: Msg) void { + switch (m) { + .text => |t| { + a.free(m) + io.print(t) + } + .none => { + a.free(m) + } + } +} + +fn main(io: Io, a: Allocator, threads: Threads) void { + shared(io, threads.share(Msg{ .text = a.concat("a", "b") })) + local(io, a, Msg{ .text = a.concat("a", "b") }) +} From 2b345efe33b60693f29769dc06d54f2f3f51b479 Mon Sep 17 00:00:00 2001 From: femto Date: Fri, 20 Nov 2026 17:26:26 +0800 Subject: [PATCH 5/7] owned_err? looks through nested wrappers: !?Buf(u8) must be unwrapped A Shared(!?Buf(u8)) let try s.get() give an owned buffer that the caller could free under every other holder; the same type as a struct field or Buf element kept a buffer nothing tracked. Now each is E133, as !Buf(u8) and ?Buf(u8) are; results of that type are unchanged. Co-Authored-By: Claude Opus 5.5 --- lib/egg/typecheck.rb | 5 +++-- test/errors/shared_resource.egg | 12 ++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/egg/typecheck.rb b/lib/egg/typecheck.rb index afadeae..38be533 100644 --- a/lib/egg/typecheck.rb +++ b/lib/egg/typecheck.rb @@ -1384,10 +1384,11 @@ def resource_elem_error(elem, what, line, col) true end - # !T or ?T with an owned T: it must be unwrapped right away. + # !T or ?T with an owned T, or a wrapper of one (!?Buf(u8)): it must be + # unwrapped right away. def owned_err?(t) info = @types[t] - %i[err opt].include?(info&.kind) && owned?(info.inner) + %i[err opt].include?(info&.kind) && (owned?(info.inner) || owned_err?(info.inner)) end def unwrap_how(t) diff --git a/test/errors/shared_resource.egg b/test/errors/shared_resource.egg index 5ce5f11..b1279c3 100644 --- a/test/errors/shared_resource.egg +++ b/test/errors/shared_resource.egg @@ -1,9 +1,11 @@ -// error: E155 @20 -// error: E155 @21 // error: E155 @22 // error: E155 @23 -// error: E155 @27 -// error: E155 @30 +// error: E155 @24 +// error: E155 @25 +// error: E155 @29 +// error: E133 @32 +// error: E133 @33 +// error: E133 @34 allow unsafe uses alloc, net, thread @@ -28,6 +30,8 @@ const Node = struct { } fn nested(s: Shared(!?Atomic)) void {} +fn wrapped_buf(s: Shared(!?Buf(u8))) void {} +const Holder = struct { f: !?Buf(u8) } fn main(threads: Threads) void { const s = threads.share(1) From 8ea600316fc92315d4e12a4b0cf7cb4ca85e0228 Mon Sep 17 00:00:00 2001 From: femto Date: Fri, 20 Nov 2026 17:45:48 +0800 Subject: [PATCH 6/7] owned? ends on a struct containing itself; Buf(?Node) judged after Node owned? keeps the structs and unions on its way, so ?Node or !?Node of a Node that contains itself reaches E119 instead of overflowing the stack. A Buf element that wraps a type still being registered is checked once registration is done, so Buf(?Node) inside Node is E133 whatever the declaration order. Co-Authored-By: Claude Opus 5.5 --- lib/egg/typecheck.rb | 31 ++++++++++++++++++++++------- test/errors/owned_wrapped_order.egg | 15 ++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 test/errors/owned_wrapped_order.egg diff --git a/lib/egg/typecheck.rb b/lib/egg/typecheck.rb index 38be533..675e791 100644 --- a/lib/egg/typecheck.rb +++ b/lib/egg/typecheck.rb @@ -891,7 +891,11 @@ def flush_elem_checks saved = @mod @mod = mod begin - what == :shared ? shared_value_error(elem, line, col) : resource_elem_error(elem, what, line, col) + case what + when :shared then shared_value_error(elem, line, col) + when :owned_err then owned_place_error(elem, "a Buf element", line, col) if owned_err?(elem) + else resource_elem_error(elem, what, line, col) + end ensure @mod = saved end @@ -1273,7 +1277,13 @@ def buf_type(args, line, col) end elem = args[0] return value_type_error(elem, "a Buf element", line, col) unless storable?(elem) - return owned_place_error(elem, "a Buf element", line, col) if owned_err?(elem) + # ?Node while Node is being registered: whether Node owns anything is + # known once it is (as for E141 below). + if %i[err opt].include?(@types[elem]&.kind) && @registering.positive? + @elem_checks << [elem, :owned_err, line, col, @mod] + elsif owned_err?(elem) + return owned_place_error(elem, "a Buf element", line, col) + end return "error" if resource_elem_error(elem, "a Buf element", line, col) canon = "Buf(#{elem})" register(canon, :buf, line, col).elem = elem @@ -1281,15 +1291,22 @@ def buf_type(args, line, col) end # An owned value: a buffer, or a struct or array holding one. It is - # moved, not copied (own.rb). - def owned?(t) + # moved, not copied (own.rb). seen: the structs and unions on the way + # here, so that one containing itself (E119, reported elsewhere) ends. + def owned?(t, seen = nil) info = @types[t] return false if info.nil? case info.kind when :buf then true - when :array then owned?(info.elem) - when :struct then info.resource || (info.fields || []).any? { |_, ft, _| owned?(ft) } - when :union then (info.fields || []).any? { |_, ft, _| ft != "void" && owned?(ft) } + when :array then owned?(info.elem, seen) + when :struct, :union + return true if info.resource + fields = info.fields || [] + return false if fields.empty? + seen ||= {} + return false if seen[t] + seen[t] = true + fields.any? { |_, ft, _| ft != "void" && owned?(ft, seen) } else false end end diff --git a/test/errors/owned_wrapped_order.egg b/test/errors/owned_wrapped_order.egg new file mode 100644 index 0000000..17de31f --- /dev/null +++ b/test/errors/owned_wrapped_order.egg @@ -0,0 +1,15 @@ +// error: E133 @6 +// error: E119 @11 +// A Buf element ?Node is judged once Node is known to own a buffer, +// whatever the order the declarations are met in. +const Tree = struct { + kids: Buf(?Tree) + name: Buf(u8) +} + +// A struct containing itself ends the ownership check too (no crash). +const Loop = struct { next: Loop } + +fn f(n: !?Loop) void {} + +fn main() void {} From f745be1673b061e0b2178c04178b3d7082ca29b9 Mon Sep 17 00:00:00 2001 From: femto Date: Fri, 20 Nov 2026 18:12:59 +0800 Subject: [PATCH 7/7] holds?: a call's slice result holds what it views; an owned element its container show(view(b), free(b)) and show(xs[0], clear(&xs)) passed the nested write check: a slice result's borrowed arguments and an owned element's container were not counted as held. Both are E117 now, with s.get() as with any buffer. Co-Authored-By: Claude Opus 5.5 --- lib/egg/typecheck.rb | 14 +++++++++--- test/errors/held_by_views.egg | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 test/errors/held_by_views.egg diff --git a/lib/egg/typecheck.rb b/lib/egg/typecheck.rb index 675e791..4987c8e 100644 --- a/lib/egg/typecheck.rb +++ b/lib/egg/typecheck.rb @@ -3677,12 +3677,19 @@ def holds?(e, path) return owned?(e.ty) && overlap?(p, path) unless p.nil? case e in SliceExpr[obj:, lo:, hi:] then mentions_path?(obj, path) || holds?(lo, path) || (!hi.nil? && holds?(hi, path)) - in Index[obj:, index:] then (place_path(obj).nil? && holds?(obj, path)) || holds?(index, path) + in Index[obj:, index:] + # An owned element is lent by its container: xs[0] holds xs. + op = place_path(obj) + held = op.nil? ? holds?(obj, path) : owned?(e.ty) && overlap?(op, path) + held || holds?(index, path) in Field[obj:] then holds?(obj, path) in Builtin[name: "len"] then false in Unary[operand:] then holds?(operand, path) in Binary[lhs:, rhs:] then holds?(lhs, path) || holds?(rhs, path) - in Call[args:, info:] then sunk(info&.[](:args) || args, info).any? { |x| holds?(x, path) } + in Call[args:, info:] + # A slice result views the arguments it borrows. + borrowed = info&.[](:borrow_args) || [] + (borrowed + sunk(info&.[](:args) || args, info)).any? { |x| holds?(x, path) } in Builtin[args:] then args.any? { |x| holds?(x, path) } in MethodCall[obj:] if shared_get?(e) # s.get() lends what s holds. @@ -3690,7 +3697,8 @@ def holds?(e, path) in MethodCall[obj:, args:, info:] # A borrowed argument is given back when the call returns. own_obj = info.nil? || info[:params].nil? || info[:self_sink] - (own_obj && holds?(obj, path)) || sunk(info&.[](:args) || args, info).any? { |x| holds?(x, path) } + borrowed = info&.[](:borrow_args) || [] + (own_obj && holds?(obj, path)) || (borrowed + sunk(info&.[](:args) || args, info)).any? { |x| holds?(x, path) } in StructLit[fields:] then fields.any? { |f| holds?(f[1], path) } in ArrayLit[items:] then items.any? { |x| holds?(x, path) } in AddrOf[expr:] then mentions_path?(expr, path) diff --git a/test/errors/held_by_views.egg b/test/errors/held_by_views.egg new file mode 100644 index 0000000..963a749 --- /dev/null +++ b/test/errors/held_by_views.egg @@ -0,0 +1,41 @@ +// error: E117 @26 +// error: E117 @29 +// error: E117 @32 +uses io, alloc, thread + +// A slice a call returns holds what it views; an owned element holds its +// container. Another operand of the call may not take or change them. +fn view(b: Buf(u8)) []u8 { + return b[0..] +} +fn show(io: Io, v: []u8, n: i64) void {} +fn show_buf(io: Io, v: Buf(u8), n: i64) void {} +fn close_and_zero(sink s: Shared(Buf(u8))) i64 { + s.close() + return 0 +} +fn free_zero(a: Allocator, sink b: Buf(u8)) i64 { + a.free(b) + return 0 +} +fn clear(a: Allocator, inout xs: Buf(Buf(u8))) i64 { + a.free(@replace(&xs, .empty)) + return 0 +} +fn shared(io: Io, sink s: Shared(Buf(u8))) void { + show(io, view(s.get()), close_and_zero(s)) +} +fn plain(io: Io, a: Allocator, sink b: Buf(u8)) void { + show(io, view(b), free_zero(a, b)) +} +fn element(io: Io, a: Allocator, inout xs: Buf(Buf(u8))) void { + show_buf(io, xs[0], clear(a, &xs)) +} + +fn main(io: Io, a: Allocator, threads: Threads) void { + shared(io, threads.share(a.concat("a", "b"))) + plain(io, a, a.concat("a", "b")) + var xs: Buf(Buf(u8)) = .empty + element(io, a, &xs) + a.free(xs) +}