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
Diagnose derivatives that use a variable before its declaration
clad assembles derivative bodies by hand, so nothing enforces that a
declaration precedes its uses the way parsing does for user code, and the
generated function is never handed to Sema::ActOnFinishFunctionBody,
which would re-check it. Such a body can therefore name a variable that
is not in scope at that point and still compile.
Add findUseBeforeDecl alongside the existing structural checks. It walks
the body in execution order carrying the block-scope locals declared so
far, which also validates lambda bodies against what is visible at the
lambda's definition point -- notably catching references built outside a
closure that never became captures, and which LambdaExpr::captures()
therefore cannot report. It is gated on a clean derivation like StrayRef:
an unsupported construct is diagnosed and its body is not expected to be
well-formed.
The check found three violations, fixed here. Cloning a lambda body
created fresh VarDecls but left later statements referring to the
original lambda's variables, so register the clones for remapping. A
range-for declared its adjoint iterator and loop variable inside the
forward loop while the reverse sweep, a sibling block, restored them --
for a nested loop no block encloses both short of the function body, so
declare them there and initialize in place.
0 commit comments