|
| 1 | +# Code Generation Bugs Found Through Comprehensive Testing |
| 2 | + |
| 3 | +Generated: 2025-10-14 |
| 4 | +Test: Compilation of all 199 WIT test stubs |
| 5 | +Result: 148 successful, 51 failed |
| 6 | +Success Rate: 74% |
| 7 | + |
| 8 | +## Summary |
| 9 | + |
| 10 | +The comprehensive compilation test of all 199 WIT stubs successfully identified multiple categories of code generation bugs in wit-codegen. This document categorizes and prioritizes these issues. |
| 11 | + |
| 12 | +## Bug Categories |
| 13 | + |
| 14 | +### 1. Resource Method Naming Mismatch (HIGH PRIORITY) |
| 15 | +**Affected**: 12 stubs (resource-*, import-and-export-resource*) |
| 16 | + |
| 17 | +**Issue**: Resource methods are generated with prefixed names in headers but unprefixed names in WAMR bindings. |
| 18 | + |
| 19 | +**Example** (`import-and-export-resource-alias`): |
| 20 | +- WIT defines: `resource x { get: func() -> string; }` |
| 21 | +- Header generates: `cmcpp::string_t x_get();` |
| 22 | +- WAMR binding tries to call: `host::foo::get` ❌ (should be `host::foo::x_get`) |
| 23 | + |
| 24 | +**Affected stubs**: |
| 25 | +- import-and-export-resource |
| 26 | +- import-and-export-resource-alias |
| 27 | +- resource-alias |
| 28 | +- resource-local-alias |
| 29 | +- resource-local-alias-borrow |
| 30 | +- resource-local-alias-borrow-import |
| 31 | +- resource-own-in-other-interface |
| 32 | +- resources |
| 33 | +- resources-in-aggregates |
| 34 | +- resources-with-futures |
| 35 | +- resources-with-lists |
| 36 | +- resources-with-streams |
| 37 | +- return-resource-from-export |
| 38 | + |
| 39 | +**Fix needed**: wit-codegen must consistently use resource-prefixed method names (`x_get`, `x_set`, etc.) in both headers and WAMR bindings. |
| 40 | + |
| 41 | +### 2. Duplicate Monostate in Variants (HIGH PRIORITY) |
| 42 | +**Affected**: variants.wit, variants-unioning-types.wit |
| 43 | + |
| 44 | +**Issue**: Variants with multiple "empty" cases generate duplicate `std::monostate` alternatives, which is invalid C++. |
| 45 | + |
| 46 | +**Example** (`variants`): |
| 47 | +```cpp |
| 48 | +// Generated (INVALID): |
| 49 | +using v1 = cmcpp::variant_t< |
| 50 | + cmcpp::monostate, // First empty case |
| 51 | + cmcpp::enum_t<e1>, |
| 52 | + cmcpp::string_t, |
| 53 | + empty, |
| 54 | + cmcpp::monostate, // ❌ DUPLICATE - Second empty case |
| 55 | + uint32_t, |
| 56 | + cmcpp::float32_t |
| 57 | +>; |
| 58 | + |
| 59 | +// Also affects: |
| 60 | +using no_data = cmcpp::variant_t<cmcpp::monostate, cmcpp::monostate>; // ❌ Both empty |
| 61 | +``` |
| 62 | + |
| 63 | +**Fix needed**: wit-codegen should generate unique wrapper types for each empty variant case, or use a numbering scheme like `monostate_0`, `monostate_1`, etc. |
| 64 | + |
| 65 | +### 3. WASI Interface Function Name Mismatches (MEDIUM PRIORITY) |
| 66 | +**Affected**: 5 stubs (wasi-cli, wasi-io, wasi-http, wasi-filesystem, wasi-clocks) |
| 67 | + |
| 68 | +**Issue**: Interface methods use kebab-case names in WIT but are generated with different names. |
| 69 | + |
| 70 | +**Examples**: |
| 71 | +- WIT: `to-debug-string` → Generated: `error_to_debug_string` but called as `to_debug_string` ❌ |
| 72 | +- WIT: `ready`, `block` (poll methods) → Generated with prefixes or missing entirely |
| 73 | + |
| 74 | +**Fix needed**: wit-codegen needs consistent snake_case conversion and proper name prefixing for interface methods. |
| 75 | + |
| 76 | +### 4. Missing Guest Function Type Definitions (MEDIUM PRIORITY) |
| 77 | +**Affected**: resources-in-aggregates, resource-alias, return-resource-from-export |
| 78 | + |
| 79 | +**Issue**: Guest export function types are not generated (`f_t`, `transmogriphy_t`, `return_resource_t` missing). |
| 80 | + |
| 81 | +**Example** (`resources-in-aggregates`): |
| 82 | +```cpp |
| 83 | +// Header comment says: |
| 84 | +// TODO: transmogriphy - Type definitions for local types (variant/enum/record) not yet parsed |
| 85 | + |
| 86 | +// But WAMR binding tries to use: |
| 87 | +guest_function<guest::aggregates::f_t>() // ❌ f_t doesn't exist |
| 88 | +``` |
| 89 | + |
| 90 | +**Fix needed**: wit-codegen must generate all guest function type aliases, even for resource-related functions. |
| 91 | + |
| 92 | +### 5. Other Naming Issues (LOW PRIORITY) |
| 93 | +**Affected**: guest-name, issue929-only-methods, lift-lower-foreign |
| 94 | + |
| 95 | +**Issue**: Edge cases in name generation for specific WIT patterns. |
| 96 | + |
| 97 | +## Testing Value |
| 98 | + |
| 99 | +This comprehensive test successfully identified: |
| 100 | +- **4 major bug categories** affecting 21 stubs |
| 101 | +- **Specific line numbers and examples** for each issue |
| 102 | +- **Clear reproduction cases** from the wit-bindgen test suite |
| 103 | + |
| 104 | +## Recommended Fix Priority |
| 105 | + |
| 106 | +1. **Resource method naming** - Affects 12 stubs, common pattern |
| 107 | +2. **Duplicate monostate** - Breaks C++ semantics, affects variant testing |
| 108 | +3. **WASI function names** - Affects important real-world interfaces |
| 109 | +4. **Missing type definitions** - Required for complete codegen |
| 110 | +5. **Edge case naming** - Lower frequency issues |
| 111 | + |
| 112 | +## Next Steps |
| 113 | + |
| 114 | +1. File issues in wit-codegen project with specific examples |
| 115 | +2. Add workaround detection in cmcpp for common patterns |
| 116 | +3. Continue using this test suite to validate fixes |
| 117 | +4. Expand test coverage as more WIT features are added |
| 118 | + |
| 119 | +## Test Command |
| 120 | + |
| 121 | +```bash |
| 122 | +# Run comprehensive compilation test: |
| 123 | +cmake --build build --target validate-root-cmake-build |
| 124 | + |
| 125 | +# Check results: |
| 126 | +cat build/root_cmake_build.log | grep "error:" |
| 127 | + |
| 128 | +# Count successes: |
| 129 | +grep "Built target" build/root_cmake_build.log | wc -l # 148/199 |
| 130 | +``` |
0 commit comments