Unify CSSCompoundDataType with its variant (#55738)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/55738

Previously, CSS compound data types used two separate types:
`CSSCompoundDataType<T1, T2, ...>` as an empty marker struct for template
parameter deduction, and `CSSVariantWithTypes<CSSCompoundDataType<T1, T2, ...>>`
which resolved to `std::variant<T1, T2, ...>` for actual storage. This resulted
in redundant type aliases like `CSSTransformFunctionVariant`.

This change makes `CSSCompoundDataType<T1, T2, ...>` a `using` alias for
`std::variant<T1, T2, ...>` directly, so one type serves both roles: template
parameter deduction for parsing AND storage.

- Change `CSSCompoundDataType` from a marker struct to a `using` alias for
  `std::variant`
- Replace `CSSValidCompoundDataType` concept with `is_variant_of_data_types`
  trait that pattern-matches `std::variant` where all types satisfy `CSSDataType`
- Update `merge_data_types` and `merge_variant` specializations to use
  `std::variant` directly
- Store `AllowedTypesT` directly in `CSSList` compound type specialization
  (since it IS now the variant)
- Remove redundant `CSSTransformFunctionVariant`, `CSSFilterFunctionVariant`,
  and `CSSBackgroundImageVariant` aliases
- Update all downstream references in conversions

Changelog: [Internal]

Reviewed By: jorge-cab

Differential Revision: D94347088

fbshipit-source-id: 99cfe13400325c36682297263aa87d00ac13de13
This commit is contained in:
Nick Gerleman authored and meta-codesync[bot] committed 2026-02-25 12:39:16 -08:00
1 parent 481134aafe
commit 36212c248b
9 files changed
+29 -38

No files matched your search

@@ -477,7 +477,7 @@ void fromCSSColorStop(
}
std::optional<BackgroundImage> fromCSSBackgroundImage(
const CSSBackgroundImageVariant& cssBackgroundImage) {
const CSSBackgroundImage& cssBackgroundImage) {
if (std::holds_alternative<CSSLinearGradientFunction>(cssBackgroundImage)) {
const auto& gradient =
std::get<CSSLinearGradientFunction>(cssBackgroundImage);
@@ -108,7 +108,7 @@ parseProcessedFilter(const PropsParserContext &context, const RawValue &value, s
result = filter;
}
inline FilterType filterTypeFromVariant(const CSSFilterFunctionVariant &filter)
inline FilterType filterTypeFromVariant(const CSSFilterFunction &filter)
{
return std::visit(
[](auto &&filter) -> FilterType {
@@ -148,7 +148,7 @@ inline FilterType filterTypeFromVariant(const CSSFilterFunctionVariant &filter)
filter);
}
inline std::optional<FilterFunction> fromCSSFilter(const CSSFilterFunctionVariant &cssFilter)
inline std::optional<FilterFunction> fromCSSFilter(const CSSFilterFunction &cssFilter)
{
return std::visit(
[&](auto &&filter) -> std::optional<FilterFunction> {
@@ -550,7 +550,7 @@ inline ValueUnit cssLengthPercentageToValueUnit(const std::variant<CSSLength, CS
}
}
inline std::optional<TransformOperation> fromCSSTransformFunction(const CSSTransformFunctionVariant &cssTransform)
inline std::optional<TransformOperation> fromCSSTransformFunction(const CSSTransformFunction &cssTransform)
{
constexpr auto Zero = ValueUnit(0, UnitType::Point);
constexpr auto One = ValueUnit(1, UnitType::Point);
@@ -845,11 +845,6 @@ static_assert(CSSDataType<CSSLinearGradientFunction>);
*/
using CSSBackgroundImage = CSSCompoundDataType<CSSLinearGradientFunction, CSSRadialGradientFunction>;
/**
* Variant of possible CSS background image types
*/
using CSSBackgroundImageVariant = CSSVariantWithTypes<CSSBackgroundImage>;
/**
* Representation of <background-image-list>
*/
@@ -11,18 +11,24 @@
namespace facebook::react {
namespace detail {
struct CSSCompoundDataTypeMarker {};
} // namespace detail
/**
* Allows grouping together multiple possible CSSDataType to parse.
* CSSCompoundDataType is a std::variant of the allowed types, serving both
* as storage and for template parameter deduction during parsing.
*/
template <CSSDataType... AllowedTypesT>
struct CSSCompoundDataType : public detail::CSSCompoundDataTypeMarker {};
using CSSCompoundDataType = std::variant<AllowedTypesT...>;
namespace detail {
template <typename T>
struct is_variant_of_data_types : std::false_type {};
template <CSSDataType... Ts>
struct is_variant_of_data_types<std::variant<Ts...>> : std::true_type {};
} // namespace detail
template <typename T>
concept CSSValidCompoundDataType = std::is_base_of_v<detail::CSSCompoundDataTypeMarker, T>;
concept CSSValidCompoundDataType = detail::is_variant_of_data_types<T>::value;
/**
* A concrete data type, or a compound data type which represents multiple other
@@ -38,40 +44,40 @@ template <CSSMaybeCompoundDataType... AllowedTypesT>
struct merge_data_types;
template <CSSDataType... AlllowedTypes1T, CSSDataType... AlllowedTypes2T, CSSMaybeCompoundDataType... RestT>
struct merge_data_types<CSSCompoundDataType<AlllowedTypes1T...>, CSSCompoundDataType<AlllowedTypes2T...>, RestT...> {
using type = typename merge_data_types<CSSCompoundDataType<AlllowedTypes1T..., AlllowedTypes2T...>, RestT...>::type;
struct merge_data_types<std::variant<AlllowedTypes1T...>, std::variant<AlllowedTypes2T...>, RestT...> {
using type = typename merge_data_types<std::variant<AlllowedTypes1T..., AlllowedTypes2T...>, RestT...>::type;
};
template <CSSDataType AlllowedType1T, CSSDataType... AlllowedTypes2T, CSSMaybeCompoundDataType... RestT>
struct merge_data_types<AlllowedType1T, CSSCompoundDataType<AlllowedTypes2T...>, RestT...> {
using type = typename merge_data_types<CSSCompoundDataType<AlllowedType1T, AlllowedTypes2T...>, RestT...>::type;
struct merge_data_types<AlllowedType1T, std::variant<AlllowedTypes2T...>, RestT...> {
using type = typename merge_data_types<std::variant<AlllowedType1T, AlllowedTypes2T...>, RestT...>::type;
};
template <CSSDataType AlllowedType2T, CSSDataType... AlllowedTypes1T, CSSMaybeCompoundDataType... RestT>
struct merge_data_types<CSSCompoundDataType<AlllowedTypes1T...>, AlllowedType2T, RestT...> {
using type = typename merge_data_types<CSSCompoundDataType<AlllowedTypes1T..., AlllowedType2T>, RestT...>::type;
struct merge_data_types<std::variant<AlllowedTypes1T...>, AlllowedType2T, RestT...> {
using type = typename merge_data_types<std::variant<AlllowedTypes1T..., AlllowedType2T>, RestT...>::type;
};
template <CSSDataType AlllowedType1T, CSSDataType AlllowedType2T, CSSMaybeCompoundDataType... RestT>
struct merge_data_types<AlllowedType1T, AlllowedType2T, RestT...> {
using type = typename merge_data_types<CSSCompoundDataType<AlllowedType1T, AlllowedType2T>, RestT...>::type;
using type = typename merge_data_types<std::variant<AlllowedType1T, AlllowedType2T>, RestT...>::type;
};
template <CSSDataType... AllowedTypesT>
struct merge_data_types<CSSCompoundDataType<AllowedTypesT...>> {
using type = CSSCompoundDataType<AllowedTypesT...>;
struct merge_data_types<std::variant<AllowedTypesT...>> {
using type = std::variant<AllowedTypesT...>;
};
template <CSSDataType AllowedTypeT>
struct merge_data_types<AllowedTypeT> {
using type = CSSCompoundDataType<AllowedTypeT>;
using type = std::variant<AllowedTypeT>;
};
template <typename... T>
struct merge_variant;
template <CSSDataType... AllowedTypesT, typename... RestT>
struct merge_variant<CSSCompoundDataType<AllowedTypesT...>, RestT...> {
struct merge_variant<std::variant<AllowedTypesT...>, RestT...> {
using type = std::variant<RestT..., AllowedTypesT...>;
};
} // namespace detail
@@ -307,11 +307,6 @@ using CSSFilterFunction = CSSCompoundDataType<
CSSSaturateFilter,
CSSSepiaFilter>;
/**
* Variant of possible CSS filter function types
*/
using CSSFilterFunctionVariant = CSSVariantWithTypes<CSSFilterFunction>;
/**
* Representation of <filter-value-list>
* https://www.w3.org/TR/filter-effects-1/#typedef-filter-value-list
@@ -24,7 +24,7 @@ template <CSSDataType AllowedTypeT, CSSDelimiter Delim>
struct CSSList<AllowedTypeT, Delim> : public std::vector<AllowedTypeT> {};
template <CSSValidCompoundDataType AllowedTypesT, CSSDelimiter Delim>
struct CSSList<AllowedTypesT, Delim> : public std::vector<CSSVariantWithTypes<AllowedTypesT>> {};
struct CSSList<AllowedTypesT, Delim> : public std::vector<AllowedTypesT> {};
template <CSSMaybeCompoundDataType AllowedTypeT, CSSDelimiter Delim>
struct CSSDataTypeParser<CSSList<AllowedTypeT, Delim>> {
@@ -452,11 +452,6 @@ using CSSTransformFunction = CSSCompoundDataType<
CSSSkewY,
CSSPerspective>;
/**
* Variant of possible CSS transform function types
*/
using CSSTransformFunctionVariant = CSSVariantWithTypes<CSSTransformFunction>;
/**
* Represents the <transform-list> type.
* https://drafts.csswg.org/css-transforms-1/#typedef-transform-list
@@ -33,7 +33,7 @@ class CSSValueParser {
template <CSSDataType... AllowedTypesT>
constexpr std::variant<std::monostate, AllowedTypesT...> consumeValue(
CSSDelimiter delimeter,
CSSCompoundDataType<AllowedTypesT...> /*unused*/)
std::variant<AllowedTypesT...> /*unused*/)
{
using ReturnT = std::variant<std::monostate, AllowedTypesT...>;