Skip to main content

Module scope

Module scope 

Source
Expand description

Scope analysis for variable hoisting.

Python variables are function-scoped, so assigned names are hoisted to declarations at the top of the generated scope and assignments lower to plain stores (see Assign). This module decides which of those declarations actually need mut, mirroring rustc’s own definite-initialization rules so the generated code carries neither unused_mut warnings nor missing-mut errors:

  • A store into a variable that may already hold a value (reassignment, assignment on a path where it was maybe-assigned, any assignment inside a loop or try-closure) needs mut.
  • The first store into a definitely-uninitialized variable is initialization and does not.
  • Augmented assignment, storing through the variable (x[i] = v, x.f = v), and calling a known-mutating Python method on it (x.append(...)) need mut.

Structs§

ScopeBindings
The binding facts for one generated scope.

Functions§

analyze_scope
Analyze a statement list (one generated scope). initialized names — the parameters — hold values at entry, so any store into them needs a mutable rebinding.