Commit a1b6c06
refactor(semantic): rewrite template resolver — eliminate SubstType, fix crashes (#388)
## Summary
Rewrites the template resolver to eliminate
`SubstType`/`CodeSynthesisContexts` dependency, fixing widespread
crashes on real-world C++ code.
### What changed
**Architecture**: replaced double-TreeTransform (PseudoInstantiator +
SubstType) with single-layer design:
- **`SubstituteOnly`** — new lightweight TreeTransform for Phase 2
(typedef expansion + parameter substitution). Does NOT override
`TransformDependentNameType`, breaking the typedef ↔ lookup infinite
cycle.
- **`PseudoInstantiator`** — retains heuristic lookup (the unique value
clang doesn't provide), delegates substitution to `SubstituteOnly`.
**Deleted**:
- `DesugarOnly` class
- `instantiate()` method and all `CodeSynthesisContexts` / `SubstType`
usage
- `state()` / `rewind()` stack management
- `std::abort()` on valid NNS kinds
- `#ifndef NDEBUG` debug flag + `std::print` logging
**Added**:
- `SubstituteOnly` class with depth guard
- `InstantiationStack::findArgument()` — depth/index based parameter
lookup
- CTD→TST resolution for `DependentTemplateSpecializationType` (enables
`__alloc_traits::rebind<T>::other` resolution)
- `active_resolutions` (DNT cycle detection) + `active_ctd_lookups` (CTD
cycle detection via RAII guard)
- Stack frame pollution fix: pop lookup frames before further
`TransformType`
- Pack argument support (single-element forwarding)
- Null safety on all Transform return paths
- Structured `LOG_DEBUG` trace logging with indentation
- `--log-level` / `--test-filter` CLI options for unit test runner
- Bounds checks in `hole()`, `ResugarOnly`, `visitTemplateDeclContexts`
**Tests**: 20 → 36 passing test cases (+5 documented TODOs for known
limitations). New coverage: recursive base classes, multiple
inheritance, typedef chains, CRTP, `remove_reference` partial specs,
`std::map`, `std::basic_string`, pack forwarding.
### Stress test result
```
CDB: llvm-project build (4669 C++ files)
Types resolved: 3,690,190
Types unchanged: 75,907,298
Crashes: 0
```
Before this PR, the same test produced ~52% crash rate (413 crashes in
800 files).
### Known limitations (documented as TODOs)
- NTTP partial specialization matching (`enable_if<true, X>`, `A<X, 0>`)
- Template template parameter deduction
- Non-dependent qualifier nested class templates
(`Outer<int>::Inner<X>`)
- Multi-element pack expansion
- `CXXDependentScopeMemberExpr` lookup (unimplemented)
- Operator-name lookup in dependent contexts
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
## Release Notes
* **New Features**
* Enhanced template resolution with improved cycle detection to prevent
infinite loops
* Better type substitution handling for complex dependent types
* **Bug Fixes**
* Fixed edge cases in template specialization resolution
* Improved null-safety in type transformations
* Enhanced handling of standard library template traits
* **Tests**
* Expanded test coverage for recursive and complex template patterns
* Added validation for standard library type resolution
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 1dd94e5 commit a1b6c06
File tree
4 files changed
+1206
-370
lines changed- src/semantic
- tests/unit
- semantic
4 files changed
+1206
-370
lines changed
0 commit comments