You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: struct with str fields can be used after by-value move #562
A temporary #555 probe showed a non-Copy struct containing str fields being passed by value, lowered as move _1, and then used again afterward without a Sema error.
Minimal shape
type Acc { buf: str, name: str }
fn touch(a: Acc) -> i32:
if a.buf.len() > 100:
return 1
0
fn main:
var a = Acc { buf: "", name: "n" }
let _ = touch(a)
a.buf = a.buf ++ "x"
The MIR dump from the probe included the call as call ... (move _1) and then later _1.f4 = ..., followed by drop(_1). That suggests a use-after-move gap for aggregate values with string fields.
Expected
If Acc is non-Copy, the later use of a after the by-value call should be rejected. If Acc is intended to be Copy, the MIR should not lower the argument as an owning move/drop path.
Context
Found while implementing #555. The #555 implementation avoids depending on this behavior by using a shared-reference call in the invalidation test, but this ownership gap should be fixed independently.
Investigation results — confirmed, and it's worse than the str-field shape suggests: the missing sema error is the bug (MIR is spec-correct), and for types with a real drop impl this is an observable double-drop today, not a latent one.
Reproduction facts:
Sema accepts the probe in all forms (let _ =, plain statement, let r =). MIR shows call (move _1) then _1.f4 = ... then drop(_1) in the caller, andtouch drops the moved-in param on every path — same value dropped in both callee and caller.
The move checker itself works: let b = a then a.buf = ... IS rejected ("use of moved value"), so Acc is correctly non-Copy. Only the call-argument path fails to invalidate.
str/Vec field shapes run clean (no leaks, clean under MallocScribble/MallocGuardEdges) only because struct drop glue (Codegen.mir_emit_drop_ptr) invokes user-defined drop methods only, and str/Vec have none — drop(_1) is a no-op and OK_MOVE codegens as memcpy.
Control with a real Drop impl: sema still accepts, and at runtime the value drops twice (once in callee, once at caller scope exit). That becomes double-free the moment drop glue releases real resources.
Root cause (5 Whys): check_call marks an argument moved only when the callee param's inferred effect carries EFF_CONSUME|EFF_ESCAPE_VALUE (src/SemaCheck.w:8761-8765); effects are inferred from the callee body, so a read-only body records no consume. That's the old share-place call model. Spec §3.8 (v7.0/v7.1, b18ffc43) replaced it: a plain T parameter consumes unconditionally — the caller's binding is invalidated regardless of what the body does. MirLower already implements the new model; sema was never updated. #372's removal of the call-site move x gate (19f7cb9a) kept effect inference, leaving the sema/MIR disagreement in place.
Fix direction:
Sema (this issue): in check_call (and the method-call path), mark any non-Copy argument passed to a plain by-value T parameter as moved unconditionally, not gated on inferred EFF_CONSUME. Keep effect inference for transitive propagation/EFF_ESCAPE_VIEW/the Directed diagnostic: by-value parameter that is only read (or whose view escapes) should suggest &T #373 suggestion. Expect this to surface genuine §3.8 violations in the compiler's own sources and existing tests — those are real moves to fix, not check-weakening candidates.
Closed by 89ad31a — scope note so this closure reads precisely:
What landed (slice 1):check_call and the method-argument path now mark a by-value argument moved unconditionally — not gated on body-inferred effects — for argument types with a user drop impl. That is exactly the class where the sema/MIR disagreement was an observable double-drop at runtime (demonstrated during investigation: a Resource with a printing drop ran it twice). Receivers are untouched (mut self is a place receiver; move self was already handled), and extern/c_import callees are exempt (bit-copy, cannot drop). New tests: err_use_after_byvalue_call.w, err_use_after_byvalue_method.w, behav_byvalue_copy_arg.w.
What deliberately did not land: this issue's literal probe (Acc { buf: str }, no drop impl) still compiles. Widening the gate to all non-Copy types per spec §3.8 surfaces 9,194 violations of the legacy by-value share idiom across the compiler, the embedded stdlib, and generated code — that is a tracked migration, #564, with the inventory and direction (impl Copy for shared handle types per the existing AstPool precedent, &T for read-only params, gate flip last). For types without drop glue the accepted behavior is benign today: drops are no-ops and bitwise copies share state by design.
The caller-side scope-exit drop that isn't unscheduled on move handoff remains #430.
Activity
ehartford commentedon Jun 12, 2026
Investigation results — confirmed, and it's worse than the str-field shape suggests: the missing sema error is the bug (MIR is spec-correct), and for types with a real
dropimpl this is an observable double-drop today, not a latent one.Reproduction facts:
let _ =, plain statement,let r =). MIR showscall (move _1)then_1.f4 = ...thendrop(_1)in the caller, andtouchdrops the moved-in param on every path — same value dropped in both callee and caller.let b = athena.buf = ...IS rejected ("use of moved value"), soAccis correctly non-Copy. Only the call-argument path fails to invalidate.MallocScribble/MallocGuardEdges) only because struct drop glue (Codegen.mir_emit_drop_ptr) invokes user-defineddropmethods only, and str/Vec have none —drop(_1)is a no-op andOK_MOVEcodegens as memcpy.Dropimpl: sema still accepts, and at runtime the value drops twice (once in callee, once at caller scope exit). That becomes double-free the moment drop glue releases real resources.Root cause (5 Whys):
check_callmarks an argument moved only when the callee param's inferred effect carriesEFF_CONSUME|EFF_ESCAPE_VALUE(src/SemaCheck.w:8761-8765); effects are inferred from the callee body, so a read-only body records no consume. That's the old share-place call model. Spec §3.8 (v7.0/v7.1,b18ffc43) replaced it: a plainTparameter consumes unconditionally — the caller's binding is invalidated regardless of what the body does. MirLower already implements the new model; sema was never updated. #372's removal of the call-sitemove xgate (19f7cb9a) kept effect inference, leaving the sema/MIR disagreement in place.Fix direction:
check_call(and the method-call path), mark any non-Copy argument passed to a plain by-valueTparameter as moved unconditionally, not gated on inferredEFF_CONSUME. Keep effect inference for transitive propagation/EFF_ESCAPE_VIEW/the Directed diagnostic: by-value parameter that is only read (or whose view escapes) should suggest &T #373 suggestion. Expect this to surface genuine §3.8 violations in the compiler's own sources and existing tests — those are real moves to fix, not check-weakening candidates.drop(_1)after move: already tracked as Bug: Drop expression temporaries are never dropped (§2.4) #430 (moving a named Drop local doesn't unschedule its scope-exit drop; its table covers exactly thisconsume(a)case). This issue should fix the sema gap and cross-reference Bug: Drop expression temporaries are never dropped (§2.4) #430 for the drop handoff.&Tinstead" suggestion makes the new rejection humane rather than ceremonial.ehartford commentedon Jun 12, 2026
Closed by 89ad31a — scope note so this closure reads precisely:
What landed (slice 1):
check_calland the method-argument path now mark a by-value argument moved unconditionally — not gated on body-inferred effects — for argument types with a user drop impl. That is exactly the class where the sema/MIR disagreement was an observable double-drop at runtime (demonstrated during investigation: aResourcewith a printingdropran it twice). Receivers are untouched (mut selfis a place receiver;move selfwas already handled), and extern/c_import callees are exempt (bit-copy, cannot drop). New tests:err_use_after_byvalue_call.w,err_use_after_byvalue_method.w,behav_byvalue_copy_arg.w.What deliberately did not land: this issue's literal probe (
Acc { buf: str }, no drop impl) still compiles. Widening the gate to all non-Copy types per spec §3.8 surfaces 9,194 violations of the legacy by-value share idiom across the compiler, the embedded stdlib, and generated code — that is a tracked migration, #564, with the inventory and direction (impl Copy for shared handle types per the existingAstPoolprecedent,&Tfor read-only params, gate flip last). For types without drop glue the accepted behavior is benign today: drops are no-ops and bitwise copies share state by design.The caller-side scope-exit drop that isn't unscheduled on move handoff remains #430.