You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@thrift.apache.org by "Copilot (via GitHub)" <gi...@apache.org> on 2026/10/01 05:32:52 UTC

Re: [PR] THRIFT-6333: fix(compiler): Resolve every typedef before any generator runs [thrift]

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


##########
compiler/cpp/src/thrift/parse/parse.cc:
##########
@@ -33,3 +37,65 @@ const t_type* t_type::get_true_type() const {
   }
   return type;
 }
+
+namespace {
+
+/**
+ * Follows typedefs and container elements one step at a time. path holds the
+ * types on the way here, so that a typedef reached again through itself,
+ * which would send get_true_type() round forever, is reported instead.
+ * done holds the types already walked in full.
+ */
+void resolve_type(const t_type* type,
+                  std::set<const t_type*>& path,
+                  std::set<const t_type*>& done) {
+  if (done.count(type) != 0) {
+    return;
+  }
+  if (!path.insert(type).second) {
+    throw std::string("Type \"") + type->get_name() + "\" refers to itself";
+  }
+  if (type->is_typedef()) {
+    resolve_type(((const t_typedef*)type)->get_type(), path, done);
+  } else if (type->is_list()) {
+    resolve_type(((const t_list*)type)->get_elem_type(), path, done);
+  } else if (type->is_set()) {
+    resolve_type(((const t_set*)type)->get_elem_type(), path, done);
+  } else if (type->is_map()) {
+    resolve_type(((const t_map*)type)->get_key_type(), path, done);
+    resolve_type(((const t_map*)type)->get_val_type(), path, done);
+  }
+  path.erase(type);
+  done.insert(type);
+}
+
+void resolve_type(const t_struct* tstruct,
+                  std::set<const t_type*>& path,
+                  std::set<const t_type*>& done) {
+  for (const t_field* field : tstruct->get_members()) {
+    resolve_type(field->get_type(), path, done);

Review Comment:
   The anonymous struct attached through `field->get_xsd_attrs()` is not part of `objects_`, so its field types are never visited. For example, an undefined or cyclic type inside `xsd_attrs { ... }` still passes this preflight and lets generators begin writing output. Recursively resolve non-null XSD attributes here and add a regression case for that path.



##########
compiler/cpp/src/thrift/main.cc:
##########
@@ -1002,6 +1002,12 @@ void parse(t_program* program, t_program* parent_program, std::set<std::string>&
     if (yyparse() != 0) {
       failure("Parser error during types pass.");
     }
+
+    // A type used before it is declared is a forward typedef, resolved on
+    // first use. Resolve every type the program refers to here, so that a
+    // target that is never declared, or a typedef that refers to itself, is
+    // reported before any generator has written a file.
+    program->resolve_types();

Review Comment:
   The new tests call `program->resolve_types()` directly, so they still pass if this pre-generation hook is removed or moved after generation. The regression being fixed is specifically that earlier generators leave files behind; add an integration test that runs multiple generators on an invalid typedef and asserts failure with no generated files, as required for submitted changes by the repository contribution guidelines.



-- 
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: notifications-unsubscribe@thrift.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org