Replace static_cast with better type alternatives#48
Merged
NiceAndPeter merged 4 commits intomainfrom Nov 21, 2025
Merged
Conversation
Changed FuncState operator method signatures to accept enum types: - prefix(UnOpr op, ...) - was prefix(int op, ...) - infix(BinOpr op, ...) - was infix(int op, ...) - posfix(BinOpr op, ...) - was posfix(int op, ...) Impact: - Eliminated 6 redundant static_cast operations (enum→int→enum roundtrip) - Improved type safety - prevents passing invalid operator values - Self-documenting function signatures - Zero performance impact - enums compile to same underlying type Files changed: - src/compiler/lparser.h (function signatures) - src/compiler/lcode.cpp (implementations) - src/compiler/parser.cpp (call sites - removed casts) Performance: 4.49s avg (baseline 4.20s, variance 4.01-4.95s) All tests passing: "final OK !!!"
Moved luaP_opmodes array access into InstructionView methods: - Added getOpMode() method - returns OpMode enum - Added testAMode() method - tests if instruction sets register A - Added testTMode() method - tests if instruction is a test/jump - Added testITMode() method - tests if instruction uses top from previous - Added testOTMode() method - tests if instruction sets top for next - Added testMMMode() method - tests if instruction is metamethod call Impact: - Better encapsulation - opcode properties accessed through InstructionView - Cleaner code - view.testTMode() instead of testTMode(view.opcode()) - Eliminated redundant InstructionView creations at call sites - Zero-cost abstraction - all methods are inline Files changed: - src/compiler/lopcodes.h (method declarations and implementations) - src/compiler/lopcodes.cpp (luaP_isIT, luaP_isOT - use view methods) - src/compiler/lcode.cpp (getjumpcontrol, negatecondition - use view methods) - src/core/ldebug.cpp (findsetreg - use view methods, eliminated duplicate views) Performance: 4.33s avg (baseline 4.20s, exactly at target!) All tests passing: "final OK !!!"
Converted 7 internal predicate functions from int to bool: Compiler predicates (lcode.cpp): - isKint() - checks if expression is literal integer - isCint() - checks if integer fits in register C - isSCint() - checks if integer fits in register sC - isSCnumber() - checks if number fits in register with output params - validop() - validates if constant folding operation is safe Test-only predicates (ltests.cpp): - testobjref1() - tests GC object reference invariants - testobjref() - wrapper that prints failed invariants Impact: - Clearer intent - bool instead of int (0/1) for predicates - Better type safety - prevents arithmetic on boolean results - Self-documenting code - function signatures show boolean nature - Zero functional change - same compiled code All return statements updated: 0 → false, 1 → true Performance: 4.73s avg (baseline 4.20s, within variance) All tests passing: "final OK !!!"
Created TYPE_MODERNIZATION_ANALYSIS.md documenting: - Completed work: Phases 112-113 (operator type safety, InstructionView encapsulation, boolean returns) - Static cast analysis: ~170 instances across 25+ files categorized - Int overuse analysis: 600+ instances identified and assessed - Remaining opportunities: Boolean returns (8), loop counters (400), size variables (30), status codes (25), token types (20) - Detailed risk/benefit analysis for each category - Recommendations: Complete boolean returns OR stop here Key findings: - Most remaining opportunities have high risk or low value - Lua's int-based API design is intentional (C compatibility) - Loop counter conversion not recommended (massive scope, minimal benefit) - Size variable conversion not recommended (underflow risk) - Current design is pragmatic for Lua's problem domain Document provides roadmap for future type system modernization work.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.