Copilot commented on code in PR #3787:
URL: https://github.com/apache/thrift/pull/3787#discussion_r3986957459


##########
compiler/cpp/src/thrift/generate/t_php_generator.cc:
##########
@@ -2681,13 +2681,39 @@ void 
t_php_generator::generate_serialize_container(ostream& out, t_type* ttype,
   } else if (ttype->is_set()) {
     string iter = tmp("iter");
     string iter_val = tmp("iter");
-    indent(out) << "foreach ($" << prefix << " as $" << iter << " => $" << 
iter_val << ") {" << '\n';
-    indent_up();
-
     t_type* elem_type = ((t_set*)ttype)->get_elem_type();
-    if(php_is_scalar(elem_type)) {
-      generate_serialize_set_element(out, (t_set*)ttype, iter);
+    if (php_is_scalar(elem_type)) {
+      string set_uses_values = tmp("setUsesValues");
+      // Preserve the legacy `element => true` marker form when every value is
+      // `true`. This keeps ambiguous `set<bool>` inputs such as `[true]` on
+      // the backward-compatible path instead of guessing they are value lists.
+      string list_val = tmp("iter");
+      string iter_elem = tmp("iter");
+      indent(out) << "$" << set_uses_values << " = false;" << '\n';
+      indent(out) << "if (array_is_list($" << prefix << ")) {" << '\n';
+      indent_up();
+      indent(out) << "foreach ($" << prefix << " as $" << list_val << ") {" << 
'\n';
+      indent_up();
+      indent(out) << "if ($" << list_val << " !== true) {" << '\n';
+      indent_up();
+      indent(out) << "$" << set_uses_values << " = true;" << '\n';
+      indent(out) << "break;" << '\n';
+      scope_down(out);
+      scope_down(out);
+      scope_down(out);
+      indent(out) << "foreach ($" << prefix << " as $" << iter << " => $" << 
iter_val << ") {" << '\n';
+      indent_up();
+      indent(out) << "$" << iter_elem << " = $" << set_uses_values << " ? $" 
<< iter_val << " : $" << iter << ";" << '\n';
+      if (elem_type->is_bool()) {
+        indent(out) << "if (!$" << set_uses_values << ") {" << '\n';
+        indent_up();
+        indent(out) << "$" << iter_elem << " = (bool) $" << iter_elem << ";" 
<< '\n';

Review Comment:
   The new generator branch for `set<bool>` is not exercised by the PHP tests: 
the generated JSON regression covers a string set, while the bool cases only 
exercise the shared TBase/TException helpers. The bool-specific cast and 
ambiguous legacy-marker path here can therefore regress without detection; add 
a generated `set<bool>` fixture/test covering both `[true, false]` and the 
legacy keyed form.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to