p4lang / p4c

P4_16 reference compiler
https://p4.org/
Apache License 2.0
670 stars 441 forks source link

[P4Testgen] Better error handling in the test backends. #4403

Open fruffy opened 8 months ago

fruffy commented 8 months ago

Improve the error handling in the test back ends. Instead of throwing exceptions everywhere, try to gracefully handle errors and bubble them upwards. Considering using a utility macro for that.

_Originally posted by @vlstill in https://github.com/p4lang/p4c/pull/4372#discussion_r1483091124_

vlstill commented 8 months ago

Considering using a utility macro for that.

I would be quite weary of macros, especially like the one you suggested that contains return.

Something like https://github.com/TartanLlama/optional could be better, but I would strongly prefer something that matches the names C++23 gives these functions (sadly C++23 is far away, we currently support GCC 9 and would need GCC 12+ to have these functions). I am little afraid it would lead to lambda mess, but in many cases it could probably work. When we would introduce a lib for such things, something akin to std::expected would be probably better than better optional.

fruffy commented 8 months ago

I would be quite weary of macros, especially like the one you suggested that contains return.

It would effectively be the same as the Protobuf status macros, which are used extensively for Abseil code: https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/stubs/status_macros.h

I also would prefer the TartanLlama optional, especially because it also includes optional reference support, which is not coming to C++ any time soon. For expected expected-lite can be used because it has the same interface. But it does not come with the same kind of convenience functions the TartanLlama expected provides.

I could try incubating any of these on the P4Testgen side at least and then eventually contribute to the wider compiler infrastructure if it works out.