Skip to content

Fix ClassCastException, add Try::Tiny, fix substr and caller/set_subname#349

Merged
fglock merged 4 commits into
masterfrom
fix/classcastexception-require-override
Mar 22, 2026
Merged

Fix ClassCastException, add Try::Tiny, fix substr and caller/set_subname#349
fglock merged 4 commits into
masterfrom
fix/classcastexception-require-override

Conversation

@fglock
Copy link
Copy Markdown
Owner

@fglock fglock commented Mar 21, 2026

Summary

This PR includes several fixes:

1. Fix ClassCastException when CORE::GLOBAL::require is overridden

  • Handle the case where CORE::GLOBAL::require returns a non-RuntimeScalar value
  • Properly convert RuntimeList to RuntimeScalar when needed

2. Add bundled Try::Tiny implementation

  • Complete implementation of Try::Tiny in src/main/perl/lib/Try/Tiny.pm
  • All Try::Tiny tests pass: basic.t (25/25), context.t (25/25), erroneous_usage.t (8/8), finally.t (30/30), named.t (3/3)

3. Fix substr negative offset handling

  • Without explicit length: substr("abc", -10) clips to start (no warning)
  • With explicit length: substr("abc", -10, 1) warns and returns undef

4. Fix caller() to honor set_subname() for JVM-compiled code

  • Add callerWithSub() method that takes __SUB__ parameter
  • JVM backend passes __SUB__ to caller() via new handleCallerOperator
  • For caller(0), if __SUB__ has a subName set by set_subname(), use it
  • Zero-overhead solution: __SUB__ only pushed when caller() is actually used

Test Plan

  • make passes (all unit tests)
  • Try::Tiny tests all pass (91/91 total)
  • set_subname works correctly with caller()

Generated with Devin

fglock and others added 3 commits March 21, 2026 20:19
The bug occurred when:
1. CORE::GLOBAL::require was overridden
2. A file was loaded via do/require
3. That file contained a use statement

The parser assumed requireOp.operand was always a ListNode, but it could
be a NumberNode (e.g., for version requirements like 'use 5.008').

Added defensive instanceof checks in:
- ParsePrimary.java (root cause)
- EmitControlFlow.java
- EmitOperator.java
- EmitRegex.java
- CompileOperator.java

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Try::Tiny:
- Pure Perl implementation compatible with Try::Tiny 0.32
- All essential tests pass: basic.t (25/25), finally.t (30/30),
  context.t (25/25), erroneous_usage.t (8/8)
- Supports nested try/catch/finally, multiple finally blocks
- Properly re-throws if catch block dies (after finally runs)
- Warning format matches original for fatal finally blocks

Operator.java (substr):
- Fix negative offset behavior to match Perl 5 semantics
- Without explicit length: clip negative offsets to 0 (no warning)
- With explicit length: warn and return undef if offset is too negative
- Fixes unit/warnings.t test

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
- Add callerWithSub() method that takes __SUB__ parameter
- JVM backend now passes __SUB__ to caller() via handleCallerOperator
- For caller(0), if __SUB__ has a subName set by set_subname(), use it
- Interpreter already worked via InterpreterState tracking code.subName

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@fglock fglock changed the title Fix ClassCastException when CORE::GLOBAL::require is overridden Fix ClassCastException, add Try::Tiny, fix substr and caller/set_subname Mar 21, 2026
Two fixes for run/switches.t regression introduced in PR #347:

1. Backticks now preserve exact output by reading raw bytes instead of
   using BufferedReader.readLine() which added extra trailing newlines

2. $^I is now initialized from compilerOptions.inPlaceExtension so the
   -i switch properly enables in-place editing with backup files

Test results: run/switches.t improved from 8/107 to 38/142 (baseline was 29/142)

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@fglock fglock merged commit 10234af into master Mar 22, 2026
2 checks passed
@fglock fglock deleted the fix/classcastexception-require-override branch March 22, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant