Skip to main content

is_default_method

Function is_default_method 

Source
pub fn is_default_method(fn_decl: &AIRNode) -> bool
Expand description

True when fn_decl is a trait default method — one that carries a body.

A required (signature-only) trait method has no body in source (fn compare(self, other: Self) -> Ordering); the AIR lowerer represents that absence as an empty Block (stmts empty, no tail expression). A default method (fn not_eq(self, other: Self) -> Bool { ... }) lowers to a non-empty block. We therefore detect “has a default body” structurally as “the body block is non-empty”.

HEURISTIC NOTE: this is the empty-block heuristic. It is exact for code produced by the current AIR lowerer (bock-air::lower::lower_fn synthesizes Block { stmts: vec![], tail: None } for a bodyless method and only for a bodyless method). A user default method whose body is literally {} (an empty block) would be misclassified as required — but an empty-bodied method returning a non-Void type does not type-check, and a Void default with an empty body is behaviorally identical to no default, so the misclassification is harmless. A robust, unambiguous fix would be an explicit has_body flag on the AIR FnDecl (carried from the AST’s Option<Block>); that is a possible follow-up (a bock-air change, out of scope here).