Semantic Analysis
Class field initialization verification and the GetMutability analysis system.
The semantic checker (semantics.rs) performs class field initialization analysis. When a class has a constructor, it verifies that every declared field is mutated (directly or indirectly via method calls) within the constructor body.
Field Initialization
The primary semantic check ensures all class fields are initialized in the constructor:
class Player
{
i32 health;
str& name;
pub constructor(str& name)
{
self.name = name;
// Error: field 'health' is uninitialized
}
}How It Works
The GetMutability trait tracks which identifiers are mutated:
- Collects all identifiers that get
&mutreferences - Tracks
self.field = valuepatterns - Follows method calls that might initialize fields (transitively)
- Ensures all conditional branches initialize the same fields (intersection semantics)
superassignments count for_superfield initialization
If any field is uninitialized after the analysis, a SemanticError is reported with the field's source position.