fix(parser): don't greedily consume new as indirect-object class#577
Merged
fix(parser): don't greedily consume new as indirect-object class#577
new as indirect-object class#577Conversation
When parsing `myfunc new Foo (4,5)`, the parser was treating `new` as
the indirect-object class for `myfunc`, producing
`new->myfunc(Foo(4,5))` and then failing with
"Undefined subroutine &main::Foo".
Real Perl parses this as `myfunc(Foo->new(4,5))`. The disambiguation
rule: if the candidate "class" identifier is itself an unknown package
and is followed by another bareword identifier, prefer the inner
indirect-object interpretation (the candidate is actually a method
name).
This fixes XML::Generator's t/DOM.t which uses
`croak new XML::DOM::DOMException (WRONG_DOCUMENT_ERR, ...)`
inside XML::DOM.pm.
Before:
$ ./jperl -e 'sub f{}; package Foo; sub new{}; package main; f new Foo (1)'
Undefined subroutine &main::Foo called
After:
(parses correctly as f(Foo->new(1)))
Generated with [Devin](https://app.devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.
Summary
Fixes a parser bug where PerlOnJava greedily consumed a bareword after a function name as an indirect-object class, breaking common idioms like:
croak new XML::DOM::DOMException (WRONG_DOCUMENT_ERR, "...")Found while running
jcpan -t XML::Generator— t/DOM.t failed with:Root cause
In
SubroutineParser.java, when seeingmyfunc new Foo (4,5):myfunc, peek =new(IDENTIFIER) → enter indirect-method blocknew, peek =Foo(IDENTIFIER)newas the indirect-object class formyfunc, producingnew->myfunc(Foo(4,5))— which then calledFooas a function and failed.Real Perl parses this as
myfunc(Foo->new(4,5)).Fix
When the candidate "class" identifier is not a known package and not a known sub, AND the next token is itself a bareword identifier, reject the outer indirect form. The candidate is most likely a method name (e.g.
new), and the innerpackageName IDENTpair is the real indirect-object call.Minimal repro
Before:
Undefined subroutine &main::Foo calledAfter:
FooTest plan
make(full unit test suite passes)jcpan -t XML::Generator: was 154/158 with 4 failures in t/DOM.t → 158/158 PASSnew Pkg (args)still works (was already working)myfunc(new Pkg (args))with explicit parens still worksmyfunc Foo barwhereFoois declared) still worksGenerated with Devin