Skip to content

A global is never moved out of: transfer out of a non-const global is rejected (#1242, D52) - #1247

Merged
ehartford merged 2 commits into
mainfrom
reject-move-out-of-global
Sep 21, 2026
Merged

ehartford merged 2 commits into
mainfrom
reject-move-out-of-global

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1242.

let out = g; g = Vec.new(); out double-freed (debug-alloc: DOUBLE FREE origin=Vec). --dump-mir: inside a function the global read lowered as _1 = copy _2 — a byte copy of a non-Copy value, no blanking — so the reassignment's drop(_2) and the returned value freed one buffer (§2.3). In main the same spelling blanked the global instead, and the checker's MOVED mark on a global leaked into every later function body ("use of moved value").

Rule (D52; §9.1c blessed and landed as #1245): a global always holds a value — observed, mutated in place or reassigned, never moved out of; .clone() spells the owned copy; a const is a value, not a place. Sema records ident nodes that resolve to a non-const global; the consume arm and the return walker reject a transfer whose type needs drop, with the fix-it in the message.

Compiler sites: ClangBridge (with_str_clone_ref, no-prelude object), CImport reads g_migrate_raw_source in place and clones the bail accessors, CiMigrate/build/compiler.w clone; the embedded stdlib/runtime/clang-resource accessors return .clone() (switching them to const exposed #1246: comptime re-interprets escapes in a raw literal).

Tests: err_move_out_of_global_{binding,return,arg}, behav_global_observed_and_cloned. Sweep of src, lib/std, tools, build, test and examples: only the sites fixed here.

Battery (seed-driven, worktree, ownership change alone): build, :fixpoint, :move-audit, :drop-audit, :test, :test-green, :last-green all rc=0.

Filed on the way: #1244 (let s: &T = x moves x), #1246 (const raw-string escapes).

Eric Hartford added 2 commits September 21, 2026 06:30
… rejected (#1242)

`let out = g; g = Vec.new(); out` double-freed (debug-alloc: DOUBLE FREE
origin=Vec first_drop=__drop_struct_193 second_drop=__drop_struct_193).
`--dump-mir` shows why: inside a function the global read lowered as
`_1 = copy _2` — a byte copy of a non-Copy value with no blanking — so the
reassignment's `drop(_2)` and the returned value freed one buffer (§2.3:
transport never produces a second live value). In `main` the same spelling
lowered as `move` + `_1 = const zst`, a blanked global every other function
still sees as holding a value. The checker's MOVED mark on a global is not
per function either: after `fn f(): let s = g`, every later body reported
"use of moved value" for `g` (str/Vec x ret/swap/use matrix).

Sema now records ident nodes that resolve to a non-`const` module global
(`global_value_ident_nodes`, `const_global_syms`); the consume arm of
mark_moved_if_consumed and the return walker (check_returned_field_move,
which runs after the body scope popped) reject a transfer whose type needs
drop: "cannot move out of global `g`: a global always holds a value; clone
it (`.clone()`) instead". Reads, views, in-place mutation and reassignment
stay legal; a `const` is a comptime value that every use materializes.

Compiler sites that moved a str global: ClangBridge (with_str_clone_ref,
no-prelude object), CImport reads g_migrate_raw_source in place and clones
the two bail accessors, CiMigrate clones the defs prefix, build/compiler.w
clones the sentinel; the embedded stdlib/runtime/clang-resource generators
emit `const`. Tests: err_move_out_of_global_{binding,return,arg},
behav_global_observed_and_cloned. D52 in decisions.md proposes the §9.1c
sentence for blessing.
…lessed

Switching the generators to `const` un-escaped the embedded sources:
comptime re-interprets escapes inside a raw string literal (#1246), so
<embedded-std>/std/build.w lost its backslashes and every `use std.build`
program failed to parse (35 behavior lanes red). Until #1246 is fixed the
generated accessors return `EMBEDDED_X.clone()`; §9.1c landed as #1245.
@ehartford
ehartford merged commit 500a29f into main Sep 21, 2026
5 checks passed
ehartford added a commit that referenced this pull request Sep 21, 2026
#1246) (#1255)

`const C: str = r#"a\"b\n"#` printed `a"b` and a newline. `const` desugars
to a comptime-wrapped let; the evaluator decodes the literal once
(comptime_decode_string_escapes honors the parser's `\x01raw\x01` marker
and returns the bytes as written), then ComptimeTransform re-emitted the
CV_STR value as a fresh NK_STRING_LIT interned WITHOUT the marker, so
codegen's decoder processed the already-decoded `\"` and `\n` a second
time. Same at ct_build_string_lit (derived display/variant-name literals).

Both re-emission sites now go through ct_raw_literal_text: a literal the
transform emits holds decoded bytes and says so. Found when switching the
embedded stdlib generators to `const` broke every `use std.build` program
(#1247); behav_const_raw_string_keeps_escapes pins const/let/plain-const
byte for byte.

Co-authored-by: Eric Hartford <[email protected]>
@ehartford
ehartford deleted the reject-move-out-of-global branch September 24, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Moving a Vec out of a global and reassigning it double-frees: safe code, no diagnostic

1 participant