Skip to content

Commit 2d2c00c

Browse files
ondrejmirtesclaude
andcommitted
Port reverted Error-throw handling of expression statements to turbo-ext
Mirrors the reverted PHP twins: InternalThrowPoint and ThrowPoint lose the fromThrowExpr flag, and ExpressionHandler counts every explicit throw point as an effect of the statement again. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LQS31c8jwYW7xZwaA37tVR
1 parent ecf6048 commit 2d2c00c

20 files changed

Lines changed: 134 additions & 235 deletions

‎turbo-ext/src/AnalyserValues.h‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,6 @@ inline bool pt_internal_throw_point_can_contain_any_throwable(zval *throwPoint,
176176
return ptav::readBool(throwPoint, pt_ce_internal_throw_point, ptdecl::InternalThrowPoint::slot::canContainAnyThrowable, PT_LC("cancontainanythrowable"), out);
177177
}
178178

179-
inline bool pt_internal_throw_point_is_from_throw_expr(zval *throwPoint, bool &out)
180-
{
181-
return ptav::readBool(throwPoint, pt_ce_internal_throw_point, ptdecl::InternalThrowPoint::slot::fromThrowExpr, PT_LC("isfromthrowexpr"), out);
182-
}
183-
184179
/* }}} */
185180

186181
/* {{{ ArgsResult: $argsResult->findArgResult($argValue) (the stored result
@@ -531,11 +526,6 @@ inline bool pt_throw_point_can_contain_any_throwable(zval *throwPoint, bool &out
531526
return ptav::readBool(throwPoint, pt_ce_throw_point, ptdecl::ThrowPoint::slot::canContainAnyThrowable, PT_LC("cancontainanythrowable"), out);
532527
}
533528

534-
inline bool pt_throw_point_is_from_throw_expr(zval *throwPoint, bool &out)
535-
{
536-
return ptav::readBool(throwPoint, pt_ce_throw_point, ptdecl::ThrowPoint::slot::fromThrowExpr, PT_LC("isfromthrowexpr"), out);
537-
}
538-
539529
inline zval *pt_impure_point_identifier(zval *impurePoint, zv::Val &hold)
540530
{
541531
return ptav::read(impurePoint, pt_ce_impure_point, ptdecl::ImpurePoint::slot::identifier, PT_LC("getidentifier"), hold);

‎turbo-ext/src/ArgumentsHandler.cpp‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,6 @@ zv::Val calleeGetNamedArgumentsVariants(zval *reflection)
678678
pt_method_site pt_ah_simple_throw_point_is_explicit_site;
679679
pt_method_site pt_ah_simple_throw_point_get_type_site;
680680
pt_method_site pt_ah_simple_throw_point_can_contain_any_throwable_site;
681-
pt_method_site pt_ah_simple_throw_point_is_from_throw_expr_site;
682681
pt_property_site pt_ah_invalidate_expr_node_expr_site;
683682

684683
/* $impurePoint->getIdentifier() / ->getDescription() / ->isCertain(): the
@@ -1685,9 +1684,7 @@ class ArgumentsHandler
16851684
if (UNEXPECTED(type == NULL)) return false;
16861685
bool canContainAnyThrowable = false;
16871686
if (UNEXPECTED(!pt_internal_throw_point_can_contain_any_throwable(throwPoint, canContainAnyThrowable))) return false;
1688-
bool fromThrowExpr = false;
1689-
if (UNEXPECTED(!pt_internal_throw_point_is_from_throw_expr(throwPoint, fromThrowExpr))) return false;
1690-
mappedPoint = pt_internal_throw_point_create_explicit(scope, type, value, canContainAnyThrowable, fromThrowExpr);
1687+
mappedPoint = pt_internal_throw_point_create_explicit(scope, type, value, canContainAnyThrowable);
16911688
} else {
16921689
mappedPoint = pt_internal_throw_point_create_implicit(scope, value);
16931690
}
@@ -2057,9 +2054,7 @@ class ArgumentsHandler
20572054
if (UNEXPECTED(type.isUndef())) return false;
20582055
zv::Val canContainAnyThrowable = callOn(pt_ah_simple_throw_point_can_contain_any_throwable_site, throwPoint, PT_LC("cancontainanythrowable"), "canContainAnyThrowable", 0, NULL);
20592056
if (UNEXPECTED(canContainAnyThrowable.isUndef())) return false;
2060-
zv::Val fromThrowExpr = callOn(pt_ah_simple_throw_point_is_from_throw_expr_site, throwPoint, PT_LC("isfromthrowexpr"), "isFromThrowExpr", 0, NULL);
2061-
if (UNEXPECTED(fromThrowExpr.isUndef())) return false;
2062-
mapped = pt_internal_throw_point_create_explicit(scope, type.raw(), value, zend_is_true(canContainAnyThrowable.raw()), zend_is_true(fromThrowExpr.raw()));
2057+
mapped = pt_internal_throw_point_create_explicit(scope, type.raw(), value, zend_is_true(canContainAnyThrowable.raw()));
20632058
} else {
20642059
mapped = pt_internal_throw_point_create_implicit(scope, value);
20652060
}

‎turbo-ext/src/AssignHandler.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ zv::Val itpCreateImplicit(zval *scope, zval *node)
667667
/* InternalThrowPoint::createExplicit($scope, $type, $node, $canContainAnyThrowable) */
668668
zv::Val itpCreateExplicit(zval *scope, zval *type, zval *node, bool canContainAnyThrowable)
669669
{
670-
return pt_internal_throw_point_create_explicit(scope, type, node, canContainAnyThrowable, false);
670+
return pt_internal_throw_point_create_explicit(scope, type, node, canContainAnyThrowable);
671671
}
672672

673673
/* AssignTargetWalkMode::assign() / virtualAssign() */

‎turbo-ext/src/AssignOpHandler.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ zv::Val walkModeCoalesceReadModifyWrite()
272272
/* InternalThrowPoint::createExplicit($scope, $type, $node, $canContainAnyThrowable) */
273273
zv::Val itpCreateExplicit(zval *scope, zval *type, zval *node, bool canContainAnyThrowable)
274274
{
275-
return pt_internal_throw_point_create_explicit(scope, type, node, canContainAnyThrowable, false);
275+
return pt_internal_throw_point_create_explicit(scope, type, node, canContainAnyThrowable);
276276
}
277277

278278
/* new CoalesceExpressionNode($originalExpr, $subjectResult, $rightResult, $operatorDescription) */

‎turbo-ext/src/BinaryOpHandler.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class BinaryOpHandler
527527
if (UNEXPECTED(errorType.isUndef())) return zv::Val();
528528
leftScope = pt_expression_result_scope(leftResult.raw(), leftScopeHold);
529529
if (UNEXPECTED(leftScope == NULL)) return zv::Val();
530-
zv::Val throwPoint = pt_internal_throw_point_create_explicit(leftScope, errorType.raw(), expr, false, false);
530+
zv::Val throwPoint = pt_internal_throw_point_create_explicit(leftScope, errorType.raw(), expr, false);
531531
if (UNEXPECTED(throwPoint.isUndef())) return zv::Val();
532532
zv::Arr points = zv::Arr::adoptVal(std::move(throwPoints));
533533
points.push(std::move(throwPoint));

‎turbo-ext/src/ClosureTypeResolver.cpp‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ zv::Val initializerExprType(zval *initializerExprTypeResolver, zval *expr, zval
7373
return pt_initializer_expr_type_resolver_get_type(initializerExprTypeResolver, expr, context.raw());
7474
}
7575

76-
/* SimpleThrowPoint::createExplicit($type, $canContainAnyThrowable, $fromThrowExpr) / ::createImplicit() */
77-
zv::Val simpleThrowPointExplicit(zval *type, bool canContainAnyThrowable, bool fromThrowExpr)
76+
/* SimpleThrowPoint::createExplicit($type, $canContainAnyThrowable) / ::createImplicit() */
77+
zv::Val simpleThrowPointExplicit(zval *type, bool canContainAnyThrowable)
7878
{
79-
zv::Args argv{type, canContainAnyThrowable, fromThrowExpr};
80-
return pt_call_static_cached(pt_ctr_simple_throw_point_explicit_site, PT_CLASS_SIMPLE_THROW_POINT, PT_LC("createexplicit"), 3, argv);
79+
zv::Args argv{type, canContainAnyThrowable};
80+
return pt_call_static_cached(pt_ctr_simple_throw_point_explicit_site, PT_CLASS_SIMPLE_THROW_POINT, PT_LC("createexplicit"), 2, argv);
8181
}
8282

8383
zv::Val simpleThrowPointImplicit()
@@ -1145,8 +1145,7 @@ class ClosureTypeResolver
11451145

11461146
/* array_map(static fn (ThrowPoint $throwPoint) => $throwPoint->isExplicit()
11471147
* ? SimpleThrowPoint::createExplicit($throwPoint->getType(),
1148-
* $throwPoint->canContainAnyThrowable(), $throwPoint->isFromThrowExpr())
1149-
* : SimpleThrowPoint::createImplicit(),
1148+
* $throwPoint->canContainAnyThrowable()) : SimpleThrowPoint::createImplicit(),
11501149
* $throwPoints) */
11511150
static zv::Val simpleThrowPointsOf(zval *throwPoints)
11521151
{
@@ -1169,9 +1168,7 @@ class ClosureTypeResolver
11691168
if (UNEXPECTED(type == NULL)) return zv::Val();
11701169
bool canContainAnyThrowable = false;
11711170
if (UNEXPECTED(!pt_throw_point_can_contain_any_throwable(throwPoint, canContainAnyThrowable))) return zv::Val();
1172-
bool fromThrowExpr = false;
1173-
if (UNEXPECTED(!pt_throw_point_is_from_throw_expr(throwPoint, fromThrowExpr))) return zv::Val();
1174-
simple = simpleThrowPointExplicit(type, canContainAnyThrowable, fromThrowExpr);
1171+
simple = simpleThrowPointExplicit(type, canContainAnyThrowable);
11751172
} else {
11761173
simple = simpleThrowPointImplicit();
11771174
}

‎turbo-ext/src/ExpressionHandler.cpp‎

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ namespace {
3333
pt_property_site pt_exh_expression_expr_site;
3434
pt_property_site pt_exh_throw_expr_site;
3535

36-
/* the `Error` class name of the twin's `new ObjectType(Error::class)`, a
37-
* permanent interned string (module startup) */
38-
zend_string *pt_exh_error_class_name = nullptr;
39-
4036
/* $stmt->expr of the Expression statement (NULL = pending exception) */
4137
zval *statementExpr(zval *stmt)
4238
{
@@ -124,15 +120,12 @@ class ExpressionHandler
124120

125121
if (UNEXPECTED(!pt_node_scope_resolver_call_node_callback(nodeScopeResolver, nodeCallback, stmt, stmtScope, storage))) return zv::Val();
126122

127-
// Errors signal programmer mistakes (ValueError, TypeError, DivisionByZeroError...),
128-
// nobody calls an otherwise pure expression just to have them thrown, so they
129-
// do not make the expression statement meaningful.
130123
zval *resultValue = result.raw();
131124
zv::Val throwPointsHold;
132125
zval *throwPoints = pt_expression_result_throw_points(resultValue, throwPointsHold);
133126
if (UNEXPECTED(throwPoints == NULL)) return zv::Val();
134-
uint32_t meaningfulThrowPoints = 0;
135-
if (UNEXPECTED(!countMeaningfulThrowPoints(throwPoints, meaningfulThrowPoints))) return zv::Val();
127+
uint32_t explicitThrowPoints = 0;
128+
if (UNEXPECTED(!countExplicitThrowPoints(throwPoints, explicitThrowPoints))) return zv::Val();
136129

137130
zv::Val impurePointsHold;
138131
zval *impurePoints = pt_expression_result_impure_points(resultValue, impurePointsHold);
@@ -141,7 +134,7 @@ class ExpressionHandler
141134
zend_type_error("count(): Argument #1 ($value) must be of type Countable|array, %s given", zend_zval_value_name(impurePoints));
142135
return zv::Val();
143136
}
144-
if (zend_hash_num_elements(Z_ARRVAL_P(impurePoints)) == 0 && meaningfulThrowPoints == 0) {
137+
if (zend_hash_num_elements(Z_ARRVAL_P(impurePoints)) == 0 && explicitThrowPoints == 0) {
145138
expr = statementExpr(stmt);
146139
if (UNEXPECTED(expr == NULL)) return zv::Val();
147140
bool incDec = ptsh::isInstanceOf(expr, PT_CLASS_POST_INC, error)
@@ -263,17 +256,14 @@ class ExpressionHandler
263256
return pt_expression_result_with_scope(result.raw(), constrainedScope.raw());
264257
}
265258

266-
/* count(array_filter($throwPoints, static fn ($throwPoint) =>
267-
* $throwPoint->isExplicit() && !$errorType->isSuperTypeOf($throwPoint->getType())->yes())),
268-
* with the twin's `new ObjectType(Error::class)` created on the first
269-
* explicit throw point; false = pending exception */
270-
[[nodiscard]] static bool countMeaningfulThrowPoints(zval *throwPoints, uint32_t &count)
259+
/* count(array_filter($throwPoints, static fn ($throwPoint) => $throwPoint->isExplicit()));
260+
* false = pending exception */
261+
[[nodiscard]] static bool countExplicitThrowPoints(zval *throwPoints, uint32_t &count)
271262
{
272263
if (UNEXPECTED(Z_TYPE_P(throwPoints) != IS_ARRAY)) {
273264
zend_type_error("array_filter(): Argument #1 ($array) must be of type array, %s given", zend_zval_value_name(throwPoints));
274265
return false;
275266
}
276-
zv::Val errorType;
277267
for (auto entry : zv::TableRef(Z_ARRVAL_P(throwPoints))) {
278268
zval *throwPoint = entry.value().deref().raw();
279269
if (UNEXPECTED(Z_TYPE_P(throwPoint) != IS_OBJECT)) {
@@ -282,30 +272,7 @@ class ExpressionHandler
282272
}
283273
bool isExplicit;
284274
if (UNEXPECTED(!pt_internal_throw_point_is_explicit(throwPoint, isExplicit))) return false;
285-
if (!isExplicit) continue;
286-
bool fromThrowExpr;
287-
if (UNEXPECTED(!pt_internal_throw_point_is_from_throw_expr(throwPoint, fromThrowExpr))) return false;
288-
if (fromThrowExpr) {
289-
count++;
290-
continue;
291-
}
292-
if (errorType.isUndef()) {
293-
zval errorTypeValue;
294-
if (UNEXPECTED(!pt_object_type_new(&errorTypeValue, pt_exh_error_class_name))) return false;
295-
errorType = zv::Val::adopt(errorTypeValue);
296-
}
297-
zv::Val typeHold;
298-
zval *type = pt_internal_throw_point_type(throwPoint, typeHold);
299-
if (UNEXPECTED(type == NULL)) return false;
300-
zv::Val isSuperType = pt_type_op(Z_OBJ_P(errorType.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, type);
301-
if (UNEXPECTED(isSuperType.isUndef())) return false;
302-
if (UNEXPECTED(Z_TYPE_P(isSuperType.raw()) != IS_OBJECT)) {
303-
memberCallOnNonObject("yes", isSuperType.raw());
304-
return false;
305-
}
306-
zend_long value = pt_result_value(Z_OBJ_P(isSuperType.raw()));
307-
if (UNEXPECTED(value < 0)) return false;
308-
if (value != PT_TRI_YES) {
275+
if (isExplicit) {
309276
count++;
310277
}
311278
}
@@ -357,8 +324,6 @@ using phpstanturbo::ExpressionHandler;
357324

358325
void pt_register_expression_handler()
359326
{
360-
pt_exh_error_class_name = zend_string_init_interned(PT_LC("Error"), 1);
361-
362327
reg::Class cls("PHPStan\\Analyser\\StmtHandler\\ExpressionHandler");
363328
ptdecl::ExpressionHandler::declareClass(cls);
364329
ptdecl::ExpressionHandler::declareProperties(cls);

‎turbo-ext/src/ForeachHandler.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ class ForeachHandler
17871787
zend_long isVoid = typeTrinary(throwType.raw(), PT_OP_IS_VOID, "isVoid");
17881788
if (UNEXPECTED(isVoid < 0)) return zv::Val();
17891789
if (isVoid == PT_TRI_YES) return zv::Val::null();
1790-
return pt_internal_throw_point_create_explicit(scope, throwType.raw(), iteratee, true, false);
1790+
return pt_internal_throw_point_create_explicit(scope, throwType.raw(), iteratee, true);
17911791
}
17921792

17931793
if (!ptlh::boolSlot(self, slots::implicitThrows)) return zv::Val::null();

‎turbo-ext/src/FuncCallHandler.cpp‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ constexpr uint32_t PT_FCH_POLY_SITE_SLOTS_LIMIT = 1u << PT_FCH_POLY_SITE_SLOT_BI
6767
pt_method_site pt_fch_throw_point_explicit_site;
6868
pt_method_site pt_fch_throw_point_type_site;
6969
pt_method_site pt_fch_throw_point_any_throwable_site;
70-
pt_method_site pt_fch_throw_point_from_throw_expr_site;
7170
pt_method_site pt_fch_dynamic_function_return_type_extensions_site;
7271
pt_method_site pt_fch_find_specified_type_site;
7372
pt_method_site pt_fch_closure_expr_site;
@@ -996,9 +995,7 @@ class FuncCallHandler
996995
if (UNEXPECTED(type.isUndef())) return false;
997996
zv::Val anyThrowable = callNoArgs(pt_fch_throw_point_any_throwable_site, simpleThrowPoint, PT_LC("cancontainanythrowable"), "canContainAnyThrowable");
998997
if (UNEXPECTED(anyThrowable.isUndef())) return false;
999-
zv::Val fromThrowExpr = callNoArgs(pt_fch_throw_point_from_throw_expr_site, simpleThrowPoint, PT_LC("isfromthrowexpr"), "isFromThrowExpr");
1000-
if (UNEXPECTED(fromThrowExpr.isUndef())) return false;
1001-
throwPoint = pt_internal_throw_point_create_explicit(scope.raw(), type.raw(), expr, zend_is_true(anyThrowable.raw()), zend_is_true(fromThrowExpr.raw()));
998+
throwPoint = pt_internal_throw_point_create_explicit(scope.raw(), type.raw(), expr, zend_is_true(anyThrowable.raw()));
1002999
} else {
10031000
throwPoint = pt_internal_throw_point_create_implicit(scope.raw(), expr);
10041001
}
@@ -1377,7 +1374,7 @@ class FuncCallHandler
13771374
if (UNEXPECTED(throwTypeIsVoid < 0)) return zv::Val();
13781375
if (throwTypeIsVoid == PT_TRI_YES) return zv::Val::null();
13791376

1380-
return pt_internal_throw_point_create_explicit(scope, throwType.raw(), normalizedFuncCall, false, false);
1377+
return pt_internal_throw_point_create_explicit(scope, throwType.raw(), normalizedFuncCall, false);
13811378
}
13821379
}
13831380

@@ -1405,7 +1402,7 @@ class FuncCallHandler
14051402
if (!throwType.isNull()) {
14061403
zend_long isVoid = typeOpTrinary(throwType.raw(), PT_OP_IS_VOID, "isVoid");
14071404
if (UNEXPECTED(isVoid < 0)) return zv::Val();
1408-
if (isVoid != PT_TRI_YES) return pt_internal_throw_point_create_explicit(scope, throwType.raw(), normalizedFuncCall, true, false);
1405+
if (isVoid != PT_TRI_YES) return pt_internal_throw_point_create_explicit(scope, throwType.raw(), normalizedFuncCall, true);
14091406
} else if (flag(slots::implicitThrows)) {
14101407
bool hasRequiredParameters = false;
14111408
zend_long requiredParameters = 0;

0 commit comments

Comments
 (0)