Repository navigation
fix(compiler): infer static return type for uniform match expressions - #169
Open
prateekbhujel wants to merge 2 commits into
Open
prateekbhujel wants to merge 2 commits into
prateekbhujel wants to merge 2 commits into
Conversation
Infer the static return type of match expressions when all non-throwing arms evaluate to the same uniform type (e.g., int, float, bool, string). Previously, Expr\Match_ was absent from CompilerBase::detectTypeOfExpr(), causing all match expressions to default to Type::VAR (php::Var). This forced redundant runtime return type checks (php::throwReturnTypeError), unnecessary unboxing (php::toInt, etc.), and loose php::Var variable allocation when returning or assigning match results. This change: 1. Adds Expr_Match handling in detectTypeOfExpr() by collecting and unifying the types of all non-throwing arms. 2. In parseMatch(), sets the lambda return type to the inferred assign type and tags the expression with ATTR_LOWERED_TYPE. 3. In formatMatchReturn(), handles Expr\Throw_ arms cleanly and allocates temporaries matching the inferred return type. 4. Adds regression tests in MatchExpressionCodegenTest.
…ExpressionCodegenTest
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
This PR adds static type inference for PHP
matchexpressions when all non-throwing arms evaluate to the same uniform type (such asint,float,bool,string, etc.).Problem
Previously,
Expr\Match_was absent fromCompilerBase::detectTypeOfExpr(), defaulting all match expressions toType::VAR(php::Var).Even when every arm returned a known primitive (e.g.
match ($x) { 1 => 10, 2 => 20, default => 30 }in a function returning: int):php::Var, boxing every return value into a ZendVariant.php::throwReturnTypeError()was emitted at the function exit.php::toInt(), etc.) was required.$val = match (...)) forced the variable storage tophp::Var.Changes
CompilerBase::detectTypeOfExpr():case 'Expr_Match': aggregates the types of all non-throwing arm bodies.Type::VARif types are mixed or dynamic.SelectionExpressionTrait::parseMatch():$returnTypeusingdetectTypeOfExpr()/getNormalAssignType().ATTR_LOWERED_TYPE.php::throwException("UnhandledMatchError", ...); return {};for typed lambdas.SelectionExpressionTrait::formatMatchReturn():Expr\Throw_arms without attempting to returnphp::throwException()as an unconvertible primitive value.$returnTypewhen after-statements are present.MatchExpressionCodegenTestand fixturematch-expression-codegen.phpverifying typed lambda signatures, avoidance of redundant runtime return type checks, throw arm handling, and local variable type inference.