|
| 1 | +# Fix Dynamic Eval String Warning Handler Inheritance |
| 2 | + |
| 3 | +## Objective |
| 4 | +Fix 30 test failures in op/infnan.t where dynamically constructed eval strings don't inherit the `$SIG{__WARN__}` handler, preventing numeric warnings from being captured. |
| 5 | + |
| 6 | +## Current Status |
| 7 | +- **Test file:** t/op/infnan.t |
| 8 | +- **Failures:** 30 tests (all "numify warn" tests) |
| 9 | +- **Pass rate:** Currently 90% (would be ~93% after fix) |
| 10 | +- **Pattern:** All failures are for malformed Inf/NaN strings like "infxy", "nanxy", "nan34" |
| 11 | + |
| 12 | +## Root Cause Analysis |
| 13 | + |
| 14 | +### The Problem |
| 15 | +Warnings are not captured when eval strings are **dynamically constructed** (using string concatenation), even though they work correctly for: |
| 16 | +1. Literal eval strings: `eval 'my $x = "infxy" + 1'` ✅ |
| 17 | +2. Eval blocks: `eval { my $x = "infxy" + 1 }` ✅ |
| 18 | +3. Dynamically constructed strings: `eval '$a = "' . $value . '" + 1'` ❌ |
| 19 | + |
| 20 | +### Evidence |
| 21 | +Test case demonstrates the issue clearly: |
| 22 | +```perl |
| 23 | +$SIG{__WARN__} = sub { $w = shift }; |
| 24 | + |
| 25 | +# Works - captures warning |
| 26 | +eval 'my $x = "infxy" + 1'; |
| 27 | + |
| 28 | +# Fails - doesn't capture warning |
| 29 | +my $str = '$a = "infxy" + 1'; |
| 30 | +eval $str; |
| 31 | +``` |
| 32 | + |
| 33 | +### Technical Investigation |
| 34 | +1. Warnings ARE being generated correctly (seen with --disassemble) |
| 35 | +2. The WarnDie.warn() method IS being called |
| 36 | +3. The issue is that dynamically compiled eval code doesn't inherit the current `$SIG{__WARN__}` handler |
| 37 | + |
| 38 | +## Why Simple Fixes Don't Work |
| 39 | + |
| 40 | +### Attempt 1: Check evalStringHelper context |
| 41 | +- The evalStringHelper method creates a new EmitterContext from a snapshot |
| 42 | +- The symbolTable snapshot might not include the current signal handlers |
| 43 | +- Signal handlers are stored in GlobalVariable, not in the symbol table |
| 44 | + |
| 45 | +### Attempt 2: Copy signal handlers |
| 46 | +- Would require passing current %SIG state to evalStringHelper |
| 47 | +- This breaks the caching mechanism (cacheKey would need to include %SIG state) |
| 48 | +- Performance impact would be significant |
| 49 | + |
| 50 | +## Implementation Strategy |
| 51 | + |
| 52 | +### Phase 1: Understand Current Architecture |
| 53 | +1. How are signal handlers stored? (GlobalVariable.getGlobalHash("main::SIG")) |
| 54 | +2. How does eval string compilation access globals? |
| 55 | +3. Why do literal eval strings work but dynamic ones don't? |
| 56 | + |
| 57 | +### Phase 2: Implement Fix |
| 58 | +**Option A: Runtime Context Inheritance** |
| 59 | +- Pass current runtime context to dynamically compiled code |
| 60 | +- Ensure %SIG is accessible from compiled eval code |
| 61 | +- Maintain compatibility with caching |
| 62 | + |
| 63 | +**Option B: Compile-time Binding** |
| 64 | +- Bind signal handlers at eval compilation time |
| 65 | +- Store handler references in compiled code |
| 66 | +- May require changes to EmitterContext |
| 67 | + |
| 68 | +### Phase 3: Testing |
| 69 | +1. Verify all 30 infnan.t tests pass |
| 70 | +2. Check for regressions in other eval-related tests |
| 71 | +3. Test performance impact on eval string compilation |
| 72 | + |
| 73 | +## Testing Strategy |
| 74 | + |
| 75 | +### Minimal Test Case |
| 76 | +```perl |
| 77 | +#!/usr/bin/perl |
| 78 | +use strict; |
| 79 | +use warnings; |
| 80 | + |
| 81 | +my $captured = ''; |
| 82 | +$SIG{__WARN__} = sub { $captured = $_[0] }; |
| 83 | + |
| 84 | +# Test dynamic eval |
| 85 | +my $code = '$x = "infxy" + 1'; |
| 86 | +eval $code; |
| 87 | + |
| 88 | +if ($captured =~ /isn't numeric/) { |
| 89 | + print "PASS\n"; |
| 90 | +} else { |
| 91 | + print "FAIL: Warning not captured\n"; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## Expected Impact |
| 96 | +- **Tests fixed:** 30 tests in op/infnan.t |
| 97 | +- **Pass rate improvement:** 90% → 93% (+3%) |
| 98 | +- **Side benefits:** All dynamic eval string warning capture would be fixed |
| 99 | +- **Potential regressions:** None expected if implemented correctly |
| 100 | + |
| 101 | +## Complexity Assessment |
| 102 | +- **Estimated effort:** 2-4 hours |
| 103 | +- **Risk level:** Medium (affects core eval mechanism) |
| 104 | +- **Files to modify:** |
| 105 | + - RuntimeCode.java (evalStringHelper) |
| 106 | + - EmitterContext.java (context passing) |
| 107 | + - Possibly WarnDie.java (signal handler lookup) |
| 108 | + |
| 109 | +## Recommendation |
| 110 | +**Priority:** HIGH (30 tests, clear pattern) |
| 111 | +**Approach:** Defer for now and move to simpler targets |
| 112 | +**Reason:** Complex architectural issue requiring deep understanding of eval compilation |
| 113 | + |
| 114 | +This is a valuable fix but requires careful implementation to avoid breaking the eval caching mechanism or causing performance regressions. The issue is well-documented here for future implementation. |
| 115 | + |
| 116 | +## Alternative Quick Fix |
| 117 | +If we just want the tests to pass without fixing the underlying issue: |
| 118 | +- Modify the test to use literal eval strings instead of dynamic construction |
| 119 | +- This would be a test-only change, not fixing the actual bug |
| 120 | +- Not recommended as it masks a real compatibility issue |
0 commit comments