Commit 91e2cc5
Fix IFNDR circular dependency in not_null conversion operator SFINAE
Summary:
The `is_not_null_castable` and `is_not_null_move_castable` traits used `std::integral_constant<bool, A && B>` where B contains
`std::is_convertible<not_null<PtrT>&, ToT>`. Since template arguments to `std::integral_constant` are eagerly instantiated regardless of short-circuit semantics, checking `std::is_convertible<not_null<PtrT>, U>` would instantiate `operator U()`, whose SFINAE constraint instantiates `is_not_null_castable`, which recursively instantiates `std::is_convertible<not_null<PtrT>, U>`, causing unbounded template recursion.
This manifests under libc++ when libraries like `nlohmann::json` perform exhaustive SFINAE probing of `is_convertible` for arbitrary type combinations.
The fix replaces `std::integral_constant<bool, A && B>` with `std::conjunction<A, B>`. Per [meta.logical], `std::conjunction` implements short-circuit instantiation: if any argument is `false_type`, subsequent arguments are not instantiated. This breaks the cycle since when `is_convertible<PtrT&, ToT>` yields false_type, the second trait containing the recursive `is_convertible<not_null<...>, ToT>` check is never instantiated.
Note: D74599633 attempted to fix this. However, the `check_constraint_if_not wrapper` in `operator U()` only prevents instantiation of `is_not_null_castable` when U is the same type as PtrT:
template <
typename U,
typename = std::enable_if_t<detail::check_constraint_if_not<
std::is_same_v<PtrT, folly::remove_cvref_t<U>>, // Only skips when U == PtrT
detail::is_not_null_castable>::template apply<PtrT, U>>>
operator U() const& ...
The problem is the recursion happens for types other than `PtrT`. The `check_constraint_if_not mechanism` only breaks the cycle for the `U == Ptr`T case (which would cause ambiguity with `operator const PtrT&()`). It doesn't help when U is an unrelated type like `string_view`.
Reviewed By: yfeldblum
Differential Revision: D92435995
fbshipit-source-id: f8af523c5f323ef26e2f333ccff2d1025ffa5f2a1 parent 7c5920a commit 91e2cc5
1 file changed
Lines changed: 23 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
91 | 100 | | |
92 | 101 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
101 | 109 | | |
102 | 110 | | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
111 | 118 | | |
112 | 119 | | |
113 | 120 | | |
| |||
0 commit comments