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
evaluator: Implement eager binding for nested template arguments (WerWolv#204)
* [PoC] Implement eager binding for nested template arguments
This commit changes the evaluation of nested template arguments from lazy (pass-by-name) to eager (pass-by-value) to enforce correct lexical scoping.
Motivation:
Previously, in a nested template like `T<K<x>>`, the argument `x` was resolved lazily within the scope of `T` instead of the scope where it was declared. This pass-by-name behavior for nested types violated lexical scoping and caused unpredictable variable resolution.
Implementation:
The core of the fix was to separate a type's declaration from its application:
1. A new `ASTNodeTypeApplication` node was introduced to represent a specific instantiation of a type. It captures the fully resolved template arguments at the declaration site, effectively creating a closure over the surrounding scope.
2. The parser was updated to eagerly evaluate all arguments—including nested types—into this new `ASTNodeTypeApplication` node.
3. `ASTNodeTypeDecl` was simplified to only handle the static declaration of a type, leaving all instantiation-specific logic to `ASTNodeTypeApplication`. As a key benefit of this cleaner separation, forward-declared types can now be used in templates, enabling mutually recursive template definitions.
4. A new test suite (`test_pattern_template_parameters_scope.hpp`) was added to verify the corrected behavior with various nested and recursive template scenarios that previously failed.
* Fix memory leak caused by retain cycle formed by recursive type.
Break the cycle manually when the parser and parser manager get reset.
* fix the issue that typenameof does not return the correct type name for template parameters.
* Retain custom build types as they are global and have lifetimes beyond a single evaluation.
* Fix formatting style
---------
Co-authored-by: Nik <werwolv98@gmail.com>
0 commit comments