Skip to content

Enforce the §9.1c global data-race rule (conservative single-thread proof) #387

Closed

Description

@ehartford

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

  1. Decide and document the module-scope spelling rule before coding: either keep bare top-level let/var as aliases covered by §9.1c, or make them directed parse/sema errors that require global / global var. The existing parser comment at src/Parser.w:2341-2347 and docs/completed/mut.md §12 are the current design tension.
  2. Add whole-program conservative concurrency evidence in Sema: record the first source span for any async construct, std.thread.spawn_os call, actual @[c_export] function, or coercion of a With function to extern "C" fn. The v1 proof succeeds only if none are present.
  3. 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.
  4. Implement a requires_global_data_race_unsafe(global_sym, access_node) helper in SemaCheck.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.
  5. 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.
  6. 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.
  7. 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.
  • unsafe around a proof-covered global access is a warning path, not an unnecessary-unsafe hard error.
  • with build && with build :fixpoint && with build :test pass.

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

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions