Skip to content

Commit 0083ac4

Browse files
committed
Use modern C++20 concepts in static_assert across WTF and JSC
https://bugs.webkit.org/show_bug.cgi?id=304025 rdar://166327045 Reviewed by Cole Carley and Keith Miller. Replace std::is_base_of_v with std::derived_from, std::is_same with std::same_as and std::is_final<T>::value with std::is_final_v<T> in static_asserts and the WTF cast helpers. std::derived_from is false for private, protected and ambiguous bases, where std::is_base_of_v is true. Every type these asserts see uses public inheritance, so none of them fires. The negated asserts in DFG and FTL get weaker for private bases, and no such classes exist. * Source/JavaScriptCore/dfg/DFGJITCompiler.h: * Source/JavaScriptCore/dfg/DFGOpInfo.h: (JSC::DFG::OpInfo::OpInfo): * Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h: (JSC::DFG::SpeculativeJIT::TrustedImmPtr::TrustedImmPtr): * Source/JavaScriptCore/ftl/FTLOutput.h: (JSC::FTL::Output::callWithoutSideEffects): * Source/JavaScriptCore/runtime/JSCast.h: (JSC::JSCastingHelpers::FinalTypeDispatcher::inheritsGeneric): (JSC::JSCastingHelpers::InheritsTraits::inherits): * Source/WTF/wtf/TypeCasts.h: (WTF::is): (WTF::uncheckedDowncast): (WTF::downcast): (WTF::dynamicDowncast): Canonical link: https://commits.webkit.org/324203@main
1 parent 7288502 commit 0083ac4

6 files changed

Lines changed: 21 additions & 20 deletions

File tree

‎Source/JavaScriptCore/dfg/DFGJITCompiler.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ class JITCompiler : public CCallHelpers {
305305
template<typename T>
306306
static LinkableConstant nonCellPointer(JITCompiler& jit, T* pointer)
307307
{
308-
static_assert(!std::is_base_of_v<JSCell, T>);
308+
static_assert(!std::derived_from<T, JSCell>);
309309
return LinkableConstant(jit, pointer, NonCell);
310310
}
311311

‎Source/JavaScriptCore/dfg/DFGOpInfo.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ struct OpInfo {
6666
template <typename T>
6767
explicit OpInfo(T* ptr)
6868
{
69-
static_assert(!std::is_base_of<HeapCell, T>::value, "To make an OpInfo with a cell in it, the cell must be registered or frozen.");
70-
static_assert(!std::is_base_of<StructureSet, T>::value, "To make an OpInfo with a structure set in, make sure to use RegisteredStructureSet.");
69+
static_assert(!std::derived_from<T, HeapCell>, "To make an OpInfo with a cell in it, the cell must be registered or frozen.");
70+
static_assert(!std::derived_from<T, StructureSet>, "To make an OpInfo with a structure set in, make sure to use RegisteredStructureSet.");
7171
m_value = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
7272
}
7373

‎Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class SpeculativeJIT : public JITCompiler {
107107
explicit TrustedImmPtr(T* value)
108108
: m_value(value)
109109
{
110-
static_assert(!std::is_base_of<JSCell, T>::value, "To use a GC pointer, the graph must be aware of it. Use SpeculativeJIT::JITCompiler::LinkableConstant instead.");
110+
static_assert(!std::derived_from<T, JSCell>, "To use a GC pointer, the graph must be aware of it. Use SpeculativeJIT::JITCompiler::LinkableConstant instead.");
111111
}
112112

113113
explicit TrustedImmPtr(RegisteredStructure structure)

‎Source/JavaScriptCore/ftl/FTLOutput.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class Output : public CommonValues {
406406
requires (std::is_convertible_v<Args, LValue> && ...)
407407
LValue callWithoutSideEffects(B3::Type type, Function function, LValue arg1, Args... args)
408408
{
409-
static_assert(!std::is_same<Function, LValue>::value);
409+
static_assert(!std::same_as<Function, LValue>);
410410
return m_block->appendNew<B3::CCallValue>(m_proc, type, origin(), B3::Effects::none(),
411411
constIntPtr(tagCFunctionPtr<void*, OperationPtrTag>(function)), arg1, args...);
412412
}

‎Source/JavaScriptCore/runtime/JSCast.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ struct FinalTypeDispatcher</* isFinal */ true> {
176176
{
177177
static_assert(!std::same_as<JSObject*, To*>, "This ensures our overloads work");
178178
static_assert(std::derived_from<To, JSCell> && std::derived_from<std::remove_pointer_t<From>, JSCell>, "JS casting expects that the types you are casting to/from are subclasses of JSCell");
179-
static_assert(std::is_final<To>::value, "To is a final type");
179+
static_assert(std::is_final_v<To>, "To is a final type");
180180
bool canCast = from->JSCell::classInfo() == To::info();
181181
// Do not use inherits<To>() since inherits<T> depends on this function.
182182
ASSERT(canCast == from->JSCell::inheritsSlow(To::info()));
@@ -200,7 +200,7 @@ template<typename To>
200200
struct InheritsTraits {
201201
static constexpr std::optional<JSTypeRange> typeRange { std::nullopt };
202202
template<typename From>
203-
static inline bool inherits(From* from) { return FinalTypeDispatcher<std::is_final<To>::value>::template inheritsGeneric<To>(from); }
203+
static inline bool inherits(From* from) { return FinalTypeDispatcher<std::is_final_v<To>>::template inheritsGeneric<To>(from); }
204204
};
205205

206206
#define DEFINE_TRAITS_FOR_JS_TYPE_OVERLOAD(className, firstJSType, lastJSType) \

‎Source/WTF/wtf/TypeCasts.h‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#pragma once
2727

28+
#include <concepts>
2829
#include <cstddef>
2930
#include <memory>
3031
#include <type_traits>
@@ -59,7 +60,7 @@ struct TypeCastTraits<ExpectedType, ArgType, true /* isBaseType */> {
5960
template<typename ExpectedType, typename ArgType>
6061
inline bool is(const ArgType& source)
6162
{
62-
static_assert(std::is_base_of_v<ArgType, ExpectedType>, "Unnecessary type check");
63+
static_assert(std::derived_from<ExpectedType, ArgType>, "Unnecessary type check");
6364
return TypeCastTraits<const ExpectedType, const ArgType>::isOfType(source);
6465
}
6566

@@ -89,52 +90,52 @@ using match_constness_t =
8990
template<typename Target, typename Source>
9091
inline match_constness_t<Source, Target>& uncheckedDowncast(Source& source LIFETIME_BOUND)
9192
{
92-
static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type");
93-
static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast");
93+
static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type");
94+
static_assert(std::derived_from<Target, Source>, "Should be a downcast");
9495
ASSERT_WITH_SECURITY_IMPLICATION(is<Target>(source));
9596
SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<match_constness_t<Source, Target>&>(source);
9697
}
9798

9899
template<typename Target, typename Source>
99100
inline match_constness_t<Source, Target>* uncheckedDowncast(Source* source LIFETIME_BOUND)
100101
{
101-
static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type");
102-
static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast");
102+
static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type");
103+
static_assert(std::derived_from<Target, Source>, "Should be a downcast");
103104
ASSERT_WITH_SECURITY_IMPLICATION(!source || is<Target>(*source));
104105
SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<match_constness_t<Source, Target>*>(source);
105106
}
106107

107108
template<typename Target, typename Source>
108109
inline match_constness_t<Source, Target>& downcast(Source& source LIFETIME_BOUND)
109110
{
110-
static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type");
111-
static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast");
111+
static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type");
112+
static_assert(std::derived_from<Target, Source>, "Should be a downcast");
112113
RELEASE_ASSERT_WITH_UNQUALIFIED_FUNCTION_NAME(is<Target>(source));
113114
SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<match_constness_t<Source, Target>&>(source);
114115
}
115116

116117
template<typename Target, typename Source>
117118
inline match_constness_t<Source, Target>* downcast(Source* source LIFETIME_BOUND)
118119
{
119-
static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type");
120-
static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast");
120+
static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type");
121+
static_assert(std::derived_from<Target, Source>, "Should be a downcast");
121122
RELEASE_ASSERT_WITH_UNQUALIFIED_FUNCTION_NAME(!source || is<Target>(*source));
122123
SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<match_constness_t<Source, Target>*>(source);
123124
}
124125

125126
template<typename Target, typename Source>
126127
inline match_constness_t<Source, Target>* dynamicDowncast(Source& source LIFETIME_BOUND)
127128
{
128-
static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type");
129-
static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast");
129+
static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type");
130+
static_assert(std::derived_from<Target, Source>, "Should be a downcast");
130131
SUPPRESS_MEMORY_UNSAFE_CAST return is<Target>(source) ? &static_cast<match_constness_t<Source, Target>&>(source) : nullptr;
131132
}
132133

133134
template<typename Target, typename Source>
134135
inline match_constness_t<Source, Target>* dynamicDowncast(Source* source LIFETIME_BOUND)
135136
{
136-
static_assert(!std::is_same_v<Source, Target>, "Unnecessary cast to same type");
137-
static_assert(std::is_base_of_v<Source, Target>, "Should be a downcast");
137+
static_assert(!std::same_as<Source, Target>, "Unnecessary cast to same type");
138+
static_assert(std::derived_from<Target, Source>, "Should be a downcast");
138139
SUPPRESS_MEMORY_UNSAFE_CAST return is<Target>(source) ? static_cast<match_constness_t<Source, Target>*>(source) : nullptr;
139140
}
140141

0 commit comments

Comments
 (0)