Skip to content

Commit 7c89e6b

Browse files
janofacebook-github-bot
authored andcommitted
Split TypeConstraint::verifyFail() into verifyParamFail() and verifyReturnFail()
Summary: There's literally just a single line of code shared between handling of these two situations. Split the function into two and kill the useless wrappers. Reviewed By: ricklavoie Differential Revision: D52854912 fbshipit-source-id: 3c78749c22971cdf36bd95f79d7b4864831171a3
1 parent fd6224a commit 7c89e6b

File tree

2 files changed

+55
-62
lines changed

2 files changed

+55
-62
lines changed

hphp/runtime/vm/type-constraint.cpp

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ bool TypeConstraint::checkNamedTypeNonObj(tv_rval val) const {
11211121
case AnnotAction::ConvertClass:
11221122
case AnnotAction::WarnLazyClass:
11231123
case AnnotAction::ConvertLazyClass:
1124-
// verifyFail will deal with the conversion/warning
1124+
// verify*Fail will deal with the conversion/warning
11251125
return false;
11261126
case AnnotAction::WarnClassname:
11271127
if (folly::Random::oneIn(RO::EvalClassnameNoticesSampleRate)) {
@@ -1344,7 +1344,7 @@ bool TypeConstraint::checkImpl(tv_rval val,
13441344
case AnnotAction::ConvertClass:
13451345
case AnnotAction::WarnLazyClass:
13461346
case AnnotAction::ConvertLazyClass:
1347-
// verifyFail will deal with the conversion/warning
1347+
// verify*Fail will deal with the conversion/warning
13481348
return false;
13491349
case AnnotAction::WarnClassname:
13501350
if (folly::Random::oneIn(RO::EvalClassnameNoticesSampleRate)) {
@@ -1669,19 +1669,6 @@ MaybeDataType TypeConstraint::asSystemlibType() const {
16691669
return underlyingDataType();
16701670
}
16711671

1672-
void TypeConstraint::verifyParamFail(tv_lval val,
1673-
const Class* ctx,
1674-
const Func* func,
1675-
int paramNums) const {
1676-
verifyFail(val, ctx, func, paramNums);
1677-
assertx(
1678-
isSoft() ||
1679-
isThis() ||
1680-
(RO::EvalEnforceGenericsUB < 2 && isUpperBound()) ||
1681-
check(val, ctx)
1682-
);
1683-
}
1684-
16851672
void TypeConstraint::verifyOutParamFail(TypedValue* c,
16861673
const Class* ctx,
16871674
const Func* func,
@@ -1746,51 +1733,19 @@ void TypeConstraint::validForPropFail(const Class* declCls,
17461733
);
17471734
}
17481735

1749-
void TypeConstraint::verifyFail(tv_lval c, const Class* ctx, const Func* func,
1750-
int id) const {
1736+
void TypeConstraint::verifyParamFail(tv_lval c,
1737+
const Class* ctx,
1738+
const Func* func,
1739+
int id) const {
1740+
assertx(id != ReturnId);
17511741
auto const tcInfo = [&] {
1752-
if (id == ReturnId) {
1753-
return folly::sformat("return of {}()", func->fullName());
1754-
} else {
1755-
return folly::sformat("argument {} passed to {}()", id+1, func->fullName());
1756-
}
1742+
return folly::sformat("argument {} passed to {}()", id+1, func->fullName());
17571743
};
17581744
if (tryCommonCoercions(c, ctx, nullptr, tcInfo)) return;
17591745

17601746
std::string name = displayName(func->cls());
17611747
auto const givenType = describe_actual_type(c);
17621748

1763-
// Handle return type constraint failures
1764-
if (id == ReturnId) {
1765-
std::string msg;
1766-
if (func->isClosureBody()) {
1767-
msg =
1768-
folly::format(
1769-
"Value returned from {}closure must be of type {}, {} given",
1770-
func->isAsync() ? "async " : "",
1771-
name,
1772-
givenType
1773-
).str();
1774-
} else {
1775-
msg =
1776-
folly::format(
1777-
"Value returned from {}{} {}() must be {} {}, {} given",
1778-
func->isAsync() ? "async " : "",
1779-
func->preClass() ? "method" : "function",
1780-
func->fullName(),
1781-
isUpperBound() ? "upper-bounded by" : "of type",
1782-
name,
1783-
givenType
1784-
).str();
1785-
}
1786-
if (!isSoft() && (!isUpperBound() || RuntimeOption::EvalEnforceGenericsUB >= 2)) {
1787-
raise_return_typehint_error(msg);
1788-
} else {
1789-
raise_warning_unsampled(msg);
1790-
}
1791-
return;
1792-
}
1793-
17941749
// Handle parameter type constraint failures
17951750
if (isExtended() &&
17961751
(isSoft() ||
@@ -1844,6 +1799,51 @@ void TypeConstraint::verifyFail(tv_lval c, const Class* ctx, const Func* func,
18441799
}
18451800
}
18461801
}
1802+
1803+
assertx(
1804+
isSoft() ||
1805+
isThis() ||
1806+
(RO::EvalEnforceGenericsUB < 2 && isUpperBound()) ||
1807+
check(c, ctx)
1808+
);
1809+
}
1810+
1811+
void TypeConstraint::verifyReturnFail(tv_lval c, const Class* ctx,
1812+
const Func* func) const {
1813+
auto const tcInfo = [&] {
1814+
return folly::sformat("return of {}()", func->fullName());
1815+
};
1816+
if (tryCommonCoercions(c, ctx, nullptr, tcInfo)) return;
1817+
1818+
std::string name = displayName(func->cls());
1819+
auto const givenType = describe_actual_type(c);
1820+
1821+
std::string msg;
1822+
if (func->isClosureBody()) {
1823+
msg =
1824+
folly::format(
1825+
"Value returned from {}closure must be of type {}, {} given",
1826+
func->isAsync() ? "async " : "",
1827+
name,
1828+
givenType
1829+
).str();
1830+
} else {
1831+
msg =
1832+
folly::format(
1833+
"Value returned from {}{} {}() must be {} {}, {} given",
1834+
func->isAsync() ? "async " : "",
1835+
func->preClass() ? "method" : "function",
1836+
func->fullName(),
1837+
isUpperBound() ? "upper-bounded by" : "of type",
1838+
name,
1839+
givenType
1840+
).str();
1841+
}
1842+
if (!isSoft() && (!isUpperBound() || RuntimeOption::EvalEnforceGenericsUB >= 2)) {
1843+
raise_return_typehint_error(msg);
1844+
} else {
1845+
raise_warning_unsampled(msg);
1846+
}
18471847
}
18481848

18491849
//////////////////////////////////////////////////////////////////////

hphp/runtime/vm/type-constraint.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,9 @@ struct TypeConstraint {
500500
const Class* ctx,
501501
const Func* func,
502502
int paramNum) const;
503-
void verifyReturnFail(TypedValue* tv,
503+
void verifyReturnFail(tv_lval val,
504504
const Class* ctx,
505-
const Func* func) const {
506-
verifyFail(tv, ctx, func, ReturnId);
507-
}
505+
const Func* func) const;
508506
// TODO(T61738946): We can take a tv_rval here once we remove support for
509507
// coercing class_meth types.
510508
void verifyPropFail(const Class* thisCls, const Class* declCls,
@@ -562,11 +560,6 @@ struct TypeConstraint {
562560

563561
template <bool> static bool checkTypeAliasImpl(const ClassConstraint& oc, const Class* type);
564562

565-
void verifyFail(tv_lval val,
566-
const Class* ctx,
567-
const Func* func,
568-
int id) const;
569-
570563
bool checkStringCompatible() const;
571564

572565
void validForPropFail(const Class*, const StringData*) const;

0 commit comments

Comments
 (0)