fix(resolver): stop corrupting single-segment use const imports - #22
Open
AlessioGiacobbe wants to merge 1 commit into
Open
fix(resolver): stop corrupting single-segment use const imports#22AlessioGiacobbe wants to merge 1 commit into
AlessioGiacobbe wants to merge 1 commit into
Conversation
parseUse rebuilt the imported constant name by splitting on the last backslash, but strrpos() returns false for a single-segment name: `use const PHP_EOL;` registered the import as `\HP_EOL` (substr($id, false + 1) drops the first character), and the compiled program failed at runtime with `Undefined constant "HP_EOL"`. $id is already the fully qualified constant name, so store it directly. For multi-segment imports the removed recomposition produced the same value, so their behavior is unchanged.
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.
Problem
parseUserebuilds the imported constant name by splitting on the last backslash, butstrrpos()returnsfalsefor a single-segment name, andsubstr($id, false + 1)drops the first character:The mangled name is visible verbatim in the runtime error of the compiled program.
Fix
$idis already the fully qualified constant name — store it directly and drop the split/re-join. For multi-segment imports the removed recomposition produced the identical value, so nothing else changes.Tests
tests/compiler/namespace/use-const-single-segment.phpt: a namespaced file importing the globalPHP_EOLplus a cross-namespaceuse const App\ANSWER. Expected output verified against PHP 8.4; on master the compiled binary dies withUndefined constant "HP_EOL".tests/compiler/namespace/suite: 34/34 pass. No PHPStan errors on the touched file.