1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:27:35 +00:00

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
4 changed files with 21 additions and 15 deletions

View file

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

View file

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

View file

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

View file

@ -139,9 +139,9 @@ private:
ErrorOr<void, ParseError> expect(StringView); ErrorOr<void, ParseError> expect(StringView);
template<typename Pred> 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> 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; static size_t s_debug_indent_level;
[[nodiscard]] auto rollback_point(SourceLocation location = SourceLocation::current()) [[nodiscard]] auto rollback_point(SourceLocation location = SourceLocation::current())