Repository navigation
A global is never moved out of: transfer out of a non-const global is rejected (#1242, D52) - #1247
Merged
Merged
Conversation
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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1242.
let out = g; g = Vec.new(); outdouble-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'sdrop(_2)and the returned value freed one buffer (§2.3). Inmainthe 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; aconstis a value, not a place. Sema records ident nodes that resolve to a non-constglobal; 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 readsg_migrate_raw_sourcein place and clones the bail accessors, CiMigrate/build/compiler.w clone; the embedded stdlib/runtime/clang-resource accessors return.clone()(switching them toconstexposed #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 = xmovesx), #1246 (const raw-string escapes).