Repository navigation
Description
Summary
Implement the v7.1 global data-race rule so ordinary single-threaded globals remain ceremony-free, while shared mutable global state becomes explicit once the program can run concurrently. This preserves With's ergonomics-first model without making global mutation silently race-prone.
Spec
docs/with-specification.md §9.1c defines module-level runtime state with global / global var, initialization before main, and a layered safety rule. Never-mutated globals are always safe; synchronized globals such as Atomic[T], Mutex[T], and RwLock[T] are always safe; all other global mutation, and reads of globals mutated anywhere, are safe only when the compiler proves the program single-threaded (docs/with-specification.md:3176-3201). Failed proof emits E0921 and names the concurrency-introducing construct with remedies (docs/with-specification.md:3203-3219). The proof may be conservative (docs/with-specification.md:3221-3225), and §19.4's unnecessary-unsafe rule must treat proof-dependent global access specially: an unsafe block that would be required after a conservative proof failure should warn, not hard-error, when a later proof improvement covers it.
Current state
Parsing supports both explicit and bare top-level globals: Parser.parse_top_level_let records LET_FLAG_GLOBAL / LET_FLAG_GLOBAL_VAR only for the global keyword, while plain top-level let/var leave those bits clear (src/Parser.w:2341-2373). Mutability of stable globals is enforced through stable_global_syms and assignment diagnostics (src/Sema.w:372-377, src/SemaCheck.w:5131-5136), but no pass classifies global reads/writes against §9.1c's data-race rule. Existing behavior tests cover global syntax and stable-vs-rebindable behavior (test/behavior/behav_mut_md_p1_syntax.w:11-13, test/behavior/issue142_global_struct_init.w:8-10) without any concurrency proof.
The root cause is that globals are currently just scope entries plus stable/rebindable metadata. There is no whole-program concurrency-summary data, no per-global mutation/read classification, and no unsafe-operation hook for proof-dependent global access.
Implementation plan
- Decide and document the module-scope spelling rule before coding: either keep bare top-level
let/varas aliases covered by §9.1c, or make them directed parse/sema errors that requireglobal/global var. The existing parser comment atsrc/Parser.w:2341-2347anddocs/completed/mut.md§12 are the current design tension. - Add whole-program conservative concurrency evidence in Sema: record the first source span for any
asyncconstruct,std.thread.spawn_oscall, actual@[c_export]function, or coercion of a With function toextern "C" fn. The v1 proof succeeds only if none are present. - Add a global usage scan after declaration/type collection and before ordinary expression checking completes. Classify each global as never-mutated, synchronized, or bare-mutated. Mutation includes rebinding, field/index writes, mutating receiver calls on the global place, and mutable/raw-mutable borrows of the global.
- Implement a
requires_global_data_race_unsafe(global_sym, access_node)helper inSemaCheck.w. It should allow never-mutated and Sync globals, allow bare mutable/read-of-mutated globals when the conservative proof succeeds, and otherwise require an unsafe context with E0921 at the access site plus a secondary note at the first concurrency evidence span. - Define the initial Sync allowlist narrowly around implemented std types and intrinsics.
Atomic[T]exists in the MIR intrinsic set (src/Mir.w:193-204); Mutex/RwLock semantics are tracked separately by Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471, so do not claim ordinary facades are truly synchronized until that issue lands. - Extend unsafe tracking so the same global access can count as an unsafe operation only when the proof currently fails. When the proof succeeds, emit the §19.4 proof-dependent-unsafe warning amendment rather than the normal unnecessary-unsafe error.
- Add E0921 to the diagnostic/error-code inventory once Emit spec'd diagnostic codes (E0901, E0951-3, E1101-2, E1201) (§29.12) #429's structured error-code work exists, or emit the code text in the message for this slice.
Test plan
Add test/compile_errors/err_global_mutation_async_requires_unsafe.w: a bare mutable global plus an async use should fail with //! expect-error: E0921 and stderr naming both the global access and the async site.
Add test/compile_errors/err_global_read_of_mutated_global_concurrent.w: mutate a global in one function, read it in another, introduce spawn_os, and expect E0921 on the read.
Add test/behavior/behav_global_single_thread_mutation.w: single-threaded global var counter mutation and read should compile and run without unsafe.
Add test/behavior/behav_global_never_mutated_concurrent_read.w: a never-mutated global read from async/fiber code should compile cleanly.
Add test/behavior/behav_global_atomic_concurrent.w or defer it behind #471 if only atomics are ready; it should prove synchronized globals are exempt.
Add a warning test for unsafe { counter += 1 } in a proof-success program once warning harness coverage is available.
Acceptance criteria
- Global accesses are classified according to §9.1c's three-layer rule.
- Failed conservative proof emits E0921 and names the first concurrency-introducing construct.
- Single-threaded bare global mutation remains ceremony-free.
- Never-mutated globals are safe in concurrent programs.
unsafearound a proof-covered global access is a warning path, not an unnecessary-unsafe hard error.with build && with build :fixpoint && with build :testpass.
Dependencies
#429 for structured diagnostic codes. #471 for real Mutex/RwLock synchronization if those types are included in the Sync allowlist. Overlaps #473's Send/Sync enforcement.
Activity
ehartford commentedon Jun 13, 2026
Implemented in a39e82e. Sema now records conservative concurrency evidence, classifies global reads/writes after body checking, enforces E0921 for bare mutable global access when the single-thread proof fails, exempts implemented atomic globals, and treats unsafe global access as proof-dependent with user-facing warning coverage. Verification passed: build, fixpoint, full test suite, and test-green.