Sanitize identifiers starting with underscores#374
Conversation
- Modified `_sanitize_name` in `base.py` to prefix identifiers starting with `_` with `py`, as V doesn't allow leading underscores in variable and field names. - Added `_get_attribute_name` helper in `base.py` to handle consistent attribute name translation, including mangling and sanitization. - Updated `classes.py` to generate V struct fields within a `mut:` block, ensuring they are mutable as expected by Python semantics. - Lowercased class names in Python private name mangling (`_mangle_name`) to comply with V's snake_case requirements for fields. - Updated `functions.py` to preserve operator overloading names and specific dunder-mapped methods (`str`, `repr`, `iter`, etc.) during sanitization. - Refactored `assignments.py`, `calls.py`, and `match.py` to use `_get_attribute_name` for all attribute and field accesses. - Restored `[1]T` mapping for PEP 695 `tuple[*Ts]` in `v_types.py` to maintain test compatibility. - Verified fix with reproduction script and full test suite. Co-authored-by: yaskhan <3676373+yaskhan@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
- Modified `_sanitize_name` in `base.py` to prefix identifiers starting with `_` with `py`, as V prohibits leading underscores in variable and field names. - Added `_get_attribute_name` helper in `base.py` to centralize attribute translation, including Python private name mangling and V sanitization. - Updated `_mangle_name` in `base.py` to lowercase class names, ensuring mangled fields remain in snake_case as required by V. - Modified `classes.py` to emit V struct fields within a `mut:` block, reflecting Python's mutable attribute semantics. - Refactored `functions.py` to properly map operator overloads and common dunder methods (`str`, `repr`, `iter`, `next`, `enter`, `exit`) to their V equivalents before applying sanitization. - Updated `assignments.py`, `calls.py`, `attributes.py`, and `match.py` to use `_get_attribute_name` for consistent field and attribute access. - Adjusted `v_types.py` to ensure `tuple[*Ts]` correctly maps to `[1]T` for single-element generic results. - Verified the solution with a multi-case reproduction script and ensured no regressions in the existing test suite. Co-authored-by: yaskhan <3676373+yaskhan@users.noreply.github.com>
This commit addresses V compiler errors caused by identifiers and struct fields starting with underscores, which are prohibited in V. Key changes: - Modified `_sanitize_name` to prefix identifiers starting with '_' with 'py'. - Introduced `_get_attribute_name` to unify field renaming and mangling. - Ensured struct fields are generated within a 'mut:' block for Python compatibility. - Fixed 'mut self' detection in `FunctionsMixin` using analyzer data. - Sanitized blank identifier '_' to 'py_' to satisfy V's write-only constraint. - Updated existing tests and added new coverage for underscore identifiers. Co-authored-by: yaskhan <3676373+yaskhan@users.noreply.github.com>
The transpiler now correctly handles Python identifiers starting with underscores by prefixing them with 'py' in the generated V code. This resolves V compiler errors such as "field name cannot start with _". Additionally, struct fields are now marked as mutable by default, and name mangling for private attributes has been updated to produce V-compatible snake_case names. Common dunder methods and operator overloads are correctly excluded from sanitization to preserve V's special method functionality.
Fixes #282
PR created automatically by Jules for task 11360647767689653876 started by @yaskhan