mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +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:
parent
d9f632fee7
commit
79006c03b4
Notes:
sideshowbarker
2024-07-17 00:46:17 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/79006c03b4 Pull-request: https://github.com/SerenityOS/serenity/pull/17090 Reviewed-by: https://github.com/ADKaster ✅
4 changed files with 21 additions and 15 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Add table
Reference in a new issue