Skip to content

Error on @trace if assignments to untraced mutable struct fields - #3271

Merged
Pangoraw merged 1 commit into
EnzymeAD:mainfrom
ChrisRackauckas-Claude:untraced-field-diagnostic
Sep 14, 2026
Merged

Pangoraw merged 1 commit into
EnzymeAD:mainfrom
ChrisRackauckas-Claude:untraced-field-diagnostic

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Contributor

Summary

Split out of #3232 (comment), where this change was flagged as independent of the enum tracing work.

When a @trace if branch assigns a new value to a field of a captured mutable struct whose slot is untraced (e.g. a plain Int or Float64 field), the if result has no location to be written back into. Previously the assignment was silently discarded; if_condition now checks such :resarg paths and raises an error explaining that the field must be traced before the if, e.g. via ReactantCore.promote_to_traced.

Caveat: the check only inspects collected result paths, so it is not a general detector of unsupported mutations — a leaf the tracing machinery never tracks produces no path and remains undetectable (this is noted in a comment on check_untraced_branch_state).

The tutorial's conditional-control-flow section now documents the requirement on mutable struct fields.

Test plan

  • core/control_flow passes locally (Linux CPU, PJRT): 150/150, including the new "if: assignment to an untraced struct field" testset which asserts the error fires for plain Int and Float64 fields and that an untouched untraced field still works.
  • Verified locally that the numeric case throws "untraced location" with this change and silently dropped the assignment without it.

🤖 Generated with Devin (harness: Devin CLI, model: SWE-2 High) — local CLI session, no shareable URL.

An untraced location in a captured mutable argument has nothing an if result
can be written back into, so a branch assigning to it was silently dropped.
Detect the discarded assignment during if_condition lowering and error,
pointing at promote_to_traced. Applies to plain numbers and enums alike.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI
Agent-Model: SWE-2 High
Agent-Session: local CLI session (no shareable URL)

@jumerckx jumerckx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct to me, thanks!

@Pangoraw
Pangoraw merged commit 7b3715f into EnzymeAD:main Sep 14, 2026
61 of 82 checks passed
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.

4 participants