AK: Check the return type in IsCallableWithArguments

Template argument are checked to ensure that the `Out` type is equal or
convertible to the type returned by the invokee.

Compilation now fails on:
`Function<void()> f = []() -> int { return 0; };`

But this is allowed:
`Function<ErrorOr<int>()> f = []() -> int { return 0; };`
This commit is contained in:
Lucas CHOLLET 2023-01-18 18:52:14 -05:00 committed by Andrew Kaster
parent d9f632fee7
commit 79006c03b4
Notes: sideshowbarker 2024-07-17 00:46:17 +09:00
4 changed files with 21 additions and 15 deletions

View file

@ -38,10 +38,16 @@ namespace AK {
namespace Detail {
template<typename T, typename... Args>
template<typename T, typename Out, typename... Args>
inline constexpr bool IsCallableWithArguments = requires(T t) {
t(declval<Args>()...);
};
{
t(declval<Args>()...)
} -> ConvertibleTo<Out>;
} || requires(T t) {
{
t(declval<Args>()...)
} -> SameAs<Out>;
};
}
using Detail::IsCallableWithArguments;
@ -75,14 +81,14 @@ public:
template<typename CallableType>
Function(CallableType&& callable)
requires((IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, In...> && !IsSame<RemoveCVReference<CallableType>, Function>))
requires((IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, Out, In...> && !IsSame<RemoveCVReference<CallableType>, Function>))
{
init_with_callable(forward<CallableType>(callable));
}
template<typename FunctionType>
Function(FunctionType f)
requires((IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, In...> && !IsSame<RemoveCVReference<FunctionType>, Function>))
requires((IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, Out, In...> && !IsSame<RemoveCVReference<FunctionType>, Function>))
{
init_with_callable(move(f));
}
@ -109,7 +115,7 @@ public:
template<typename CallableType>
Function& operator=(CallableType&& callable)
requires((IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, In...>))
requires((IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, Out, In...>))
{
clear();
init_with_callable(forward<CallableType>(callable));
@ -118,7 +124,7 @@ public:
template<typename FunctionType>
Function& operator=(FunctionType f)
requires((IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, In...>))
requires((IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, Out, In...>))
{
clear();
if (f)

View file

@ -51,14 +51,14 @@ public:
template<typename CallableType>
SafeFunction(CallableType&& callable)
requires((AK::IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, In...> && !IsSame<RemoveCVReference<CallableType>, SafeFunction>))
requires((AK::IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, Out, In...> && !IsSame<RemoveCVReference<CallableType>, SafeFunction>))
{
init_with_callable(forward<CallableType>(callable), CallableKind::FunctionObject);
}
template<typename FunctionType>
SafeFunction(FunctionType f)
requires((AK::IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, In...> && !IsSame<RemoveCVReference<FunctionType>, SafeFunction>))
requires((AK::IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, Out, In...> && !IsSame<RemoveCVReference<FunctionType>, SafeFunction>))
{
init_with_callable(move(f), CallableKind::FunctionPointer);
}
@ -85,7 +85,7 @@ public:
template<typename CallableType>
SafeFunction& operator=(CallableType&& callable)
requires((AK::IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, In...>))
requires((AK::IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, Out, In...>))
{
clear();
init_with_callable(forward<CallableType>(callable));
@ -94,7 +94,7 @@ public:
template<typename FunctionType>
SafeFunction& operator=(FunctionType f)
requires((AK::IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, In...>))
requires((AK::IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, Out, In...>))
{
clear();
if (f)

View file

@ -242,7 +242,7 @@ ErrorOr<void, ParseError> Parser::expect(StringView expected)
}
template<typename Pred>
requires(IsCallableWithArguments<Pred, char>) ErrorOr<StringView, ParseError> Parser::expect(Pred predicate, StringView description)
requires(IsCallableWithArguments<Pred, bool, char>) ErrorOr<StringView, ParseError> Parser::expect(Pred predicate, StringView description)
{
auto rollback = rollback_point();
auto start = m_lexer.tell();
@ -257,7 +257,7 @@ requires(IsCallableWithArguments<Pred, char>) ErrorOr<StringView, ParseError> Pa
}
template<typename Pred>
requires(IsCallableWithArguments<Pred, char>) ErrorOr<StringView, ParseError> Parser::expect_many(Pred predicate, StringView description)
requires(IsCallableWithArguments<Pred, bool, char>) ErrorOr<StringView, ParseError> Parser::expect_many(Pred predicate, StringView description)
{
auto rollback = rollback_point();
auto start = m_lexer.tell();

View file

@ -139,9 +139,9 @@ private:
ErrorOr<void, ParseError> expect(StringView);
template<typename Pred>
requires(IsCallableWithArguments<Pred, char>) ErrorOr<StringView, ParseError> expect(Pred, StringView description);
requires(IsCallableWithArguments<Pred, bool, char>) ErrorOr<StringView, ParseError> expect(Pred, StringView description);
template<typename Pred>
requires(IsCallableWithArguments<Pred, char>) ErrorOr<StringView, ParseError> expect_many(Pred, StringView description);
requires(IsCallableWithArguments<Pred, bool, char>) ErrorOr<StringView, ParseError> expect_many(Pred, StringView description);
static size_t s_debug_indent_level;
[[nodiscard]] auto rollback_point(SourceLocation location = SourceLocation::current())