Skip to content

mpgen: Add .retval annotation - #404

Open
ViniciusCestarii wants to merge 2 commits into
bitcoin-core:masterfrom
ViniciusCestarii:retval-annotation
Open

ViniciusCestarii wants to merge 2 commits into
bitcoin-core:masterfrom
ViniciusCestarii:retval-annotation

Conversation

@ViniciusCestarii

@ViniciusCestarii ViniciusCestarii commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Implement .reval, a new annotation for marking a result field as the C++ method return value instead of naming that field as "result" (previously mentioned in #282 (comment)).

This allows better naming for non libmultiprocess clients. For example: result.result. With this annotation the field could be named block, so it would be read result.block

@DrahtBot

DrahtBot commented Nov 30, 2026 •

Copy link
Copy Markdown

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK nullfail, enochaz

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #342 (Allow request cancellation for wrapped C++ methods by nullfail)
  • #337 (proxy-types: Remove requirement for return types to be default-constructible by ryanofsky)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@ViniciusCestarii

Copy link
Copy Markdown
Contributor Author

CI failure seems unrelated

Comment thread src/mp/gen.cpp
if (is_retval) {
field.retval = true;
// Can't have 2 results returned from a single method
if (has_result) throw std::runtime_error("Multiple return value fields");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This PR could add a test checking the "Multiple return value fields" error. But I think mpgen has a lot to test on its own, so we can do this in a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it can be a follow up that exercises all the mpgen possible exceptions and not only this one

@nullfail

nullfail commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Concept ACK. I think this should be the de facto approach to indicate to mpgen the C++ return value for ProxyClient methods rather than relying on field names. But I'm not sure if we want to break compatibility now, so logging a warning when "result" is used should be enough for now.

Later, a downstream PR should migrate from relying on "result" to using $Proxy.retval in the schemas.

Comment thread src/mp/gen.cpp Outdated

if (field.retval) {
client_invoke << field_name;
client_invoke << "result";

@nullfail nullfail Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested declaring a method where one parameter is named "result" and the return value relies on $Proxy.retval, and it fails to compile because of a redefinition of 'result' error:

diff --git a/test/mp/test/foo.capnp b/test/mp/test/foo.capnp
index 859c8e7..ca53e50 100644
--- a/test/mp/test/foo.capnp
+++ b/test/mp/test/foo.capnp
@@ -41,6 +41,7 @@ interface FooInterface $Proxy.wrap("mp::test::FooImplementation") {
     callMessageAsync @26 (context :Proxy.Context) -> (result :FooMessage);
     callbackFlat @27 (context :Proxy.Context, callback :FlatCallback, arg: Int32) -> (result :Int32);
     addNamedOut @28 (a :Int32, b :Int32) -> (sum :Int32 $Proxy.retval);
+    addResultParam @29 (result :Int32, b :Int32) -> (sum :Int32 $Proxy.retval);
 }
 
 interface FooInit $Proxy.wrap("mp::test::FooInit") {
diff --git a/test/mp/test/foo.h b/test/mp/test/foo.h
index 67208d8..ad1429b 100644
--- a/test/mp/test/foo.h
+++ b/test/mp/test/foo.h
@@ -117,6 +117,7 @@ class FooImplementation
 public:
     int add(int a, int b) { return a + b; }
     int addNamedOut(int a, int b) { return a + b; }
+    int addResultParam(int result, int b) { return result + b; }
     void addOut(int a, int b, int& out) { out = a + b; }
     void addInOut(int x, int& sum) { sum += x; }
     int mapSize(const std::map<std::string, std::string>& map) { return map.size(); }
diff --git a/test/mp/test/test.cpp b/test/mp/test/test.cpp
index ee6425b..990010f 100644
--- a/test/mp/test/test.cpp
+++ b/test/mp/test/test.cpp
@@ -147,6 +147,7 @@ KJ_TEST("Call FooInterface methods")
 
     KJ_EXPECT(foo->add(1, 2) == 3);
     KJ_EXPECT(foo->addNamedOut(1, 2) == 3);
+    KJ_EXPECT(foo->addResultParam(1, 2) == 3);
     int ret;
     foo->addOut(3, 4, ret);
     KJ_EXPECT(ret == 7);

This happens because the generated ProxyClient method declares a local variable result that stores the return value from clientInvoke. A fix would be to rename it to _result and check that no parameter is equal to it.

diff --git a/src/mp/gen.cpp b/src/mp/gen.cpp
index cf86967..3dbdf3f 100644
--- a/src/mp/gen.cpp
+++ b/src/mp/gen.cpp
@@ -561,6 +561,12 @@ static void Generate(kj::StringPtr src_prefix,
                     methods << "};\n\n";
                 }

+                const kj::StringPtr result_var = "_result";
+                const auto result_field = fields.field_idx.find(result_var);
+                if (fields.has_result && result_field != fields.field_idx.end() && !fields.fields[result_field->second].retval) {
+                    throw std::runtime_error(std::string("Method ") + method_name.cStr() + " has a parameter named \"" + result_var.cStr() + "\", which conflicts with its return value");
+                }
+
                 std::ostringstream client_args;
                 std::ostringstream client_invoke;
                 std::ostringstream server_invoke_start;
@@ -600,7 +606,7 @@ static void Generate(kj::StringPtr src_prefix,
                     client_invoke << AccessorType(base_name, field) << ">(";

                     if (field.retval) {
-                        client_invoke << "result";
+                        client_invoke << result_var;
                     } else {
                         client_invoke << fwd_args.str();
                     }
@@ -630,11 +636,11 @@ static void Generate(kj::StringPtr src_prefix,
                            << "::Result ProxyClient<" << message_namespace << "::" << node_name << ">::" << method_name
                            << "(" << super_str << client_args.str() << ") {\n";
                 if (fields.has_result) {
-                    def_client << "    typename M" << method_ordinal << "::Result result;\n";
+                    def_client << "    typename M" << method_ordinal << "::Result " << result_var << ";\n";
                 }
                 def_client << "    clientInvoke(" << self_str << ", &" << message_namespace << "::" << node_name
                            << "::Client::" << method_name << "Request" << client_invoke.str() << ");\n";
-                if (fields.has_result) def_client << "    return result;\n";
+                if (fields.has_result) def_client << "    return " << result_var << ";\n";
                 def_client << "}\n";

                 server << "    kj::Promise<void> " << method_name << "(" << Cap(method_name)

EDIT: I was wrong about Cap'n Proto not allowing underscores. Updated the fix diff as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I updated the comment because I was wrong about Cap'n Proto not allowing underscores

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spoted. This type of collision was already possible before this PR (e.g. (result :Int32) -> (result :Int32)), but since .retval makes it easier to hit, I pushed commit 7d41288 with your patch

I used your first version, because _result is so unconventional in a Capnp schema that I think the compiler's error is enough.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, thanks!

@ViniciusCestarii

Copy link
Copy Markdown
Contributor Author

Thanks @nullfail for the review! Force-pushed addressing #404 (comment).

I think this should be the de facto approach to indicate to mpgen the C++ return value for ProxyClient methods rather than relying on field names.

I agree. It makes it more explicit than the field name being "result".

But I'm not sure if we want to break compatibility now, so logging a warning when "result" is used should be enough for now.

IMO adding a warning wouldn't help much, since it's easy to miss. If we want this to be the only way of marking the return value, I think we should drop support for "result" directly. This would be a breaking change, but only at the schema level, so it would just require a mechanical update of the downstream schemas. For now, I'd prefer to keep this PR backwards compatible.

@enochaz enochaz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants