<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi Lang,</div><div class=""><br class=""></div><div class="">I’m glad someone tackle this long lived issue :)</div><div class="">I’ve started to think about it recently but didn’t as far as you did!</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Feb 2, 2016, at 5:29 PM, Lang Hames via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><span style="font-size:13px" class="">Hi All,</span><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">I've been thinking lately about how to improve LLVM's error model and error reporting. A lack of good error reporting in Orc and MCJIT has forced me to spend a lot of time investigating hard-to-debug errors that could easily have been identified if we provided richer error information to the client, rather than just aborting. Kevin Enderby has made similar observations about the state of libObject and the difficulty of producing good error messages for damaged object files. I expect to encounter more issues like this as I continue work on the MachO side of LLD. I see tackling the error modeling problem as a first step towards improving error handling in general: if we make it easy to model errors, it may pave the way for better error handling in many parts of our libraries.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">At present in LLVM we model errors with std::error_code (and its helper, ErrorOr) and use diagnostic streams for error reporting. Neither of these seem entirely up to the job of providing a solid error-handling mechanism for library code. Diagnostic streams are great if all you want to do is report failure to the user and then terminate, but they can't be used to distinguish between different kinds of errors</div></div></div></blockquote><div><br class=""></div><div>I’m not sure to understand this claim? You are supposed to be able to extend and subclass the type of diagnostics? (I remember doing it for an out-of-tree LLVM-based project).</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div style="font-size:13px" class="">, and so are unsuited to many use-cases (especially error recovery). On the other hand, std::error_code allows error kinds to be distinguished, but suffers a number of drawbacks:</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">1. It carries no context: It tells you what went wrong, but not where or why, making it difficult to produce good diagnostics.</div><div style="font-size:13px" class="">2. It's extremely easy to ignore or forget: instances can be silently dropped.</div><div style="font-size:13px" class="">3. It's not especially debugger friendly: Most people call the error_code constructors directly for both success and failure values. Breakpoints have to be set carefully to avoid stopping when success values are constructed.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">In fairness to std::error_code, it has some nice properties too:</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">1. It's extremely lightweight.</div><div style="font-size:13px" class="">2. It's explicit in the API (unlike exceptions).</div><div style="font-size:13px" class="">3. It doesn't require C++ RTTI (a requirement for use in LLVM).</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">To address these shortcomings I have prototyped a new error-handling scheme partially inspired by C++ exceptions. The aim was to preserve the performance and API visibility of std::error_code, while allowing users to define custom error classes and inheritance relationships between them. My hope is that library code could use this scheme to model errors in a meaningful way, allowing clients to inspect the error information and recover where possible, or provide a rich diagnostic when aborting.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">The scheme has three major "moving parts":</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">1. A new 'TypedError' class that can be used as a replacement for std::error_code. E.g.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">std::error_code foo();</font></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">becomes</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">TypedError foo();</font></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">The TypedError class serves as a lightweight wrapper for the real error information (see (2)). It also contains a 'Checked' flag, initially set to false, that tracks whether the error has been handled or not. If a TypedError is ever destructed without being checked (or passed on to someone else) it will call std::terminate(). TypedError cannot be silently dropped.</div></div></div></blockquote><div><br class=""></div><div>I really like the fact that not checking the error triggers an error (this is the "hard to misuse” part of API design IMO).</div><div>You don’t mention it, but I’d rather see this “checked” flag compiled out with NDEBUG.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">2. A utility class, TypedErrorInfo, for building error class hierarchies rooted at 'TypedErrorInfoBase' with custom RTTI. E.g.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">// Define a new error type implicitly inheriting from TypedErrorInfoBase.</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">class MyCustomError : public TypedErrorInfo<MyCustomError> {</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">public:</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">  // Custom error info.</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">};</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class=""><br class=""></font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">// Define a subclass of MyCustomError.</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">class MyCustomSubError : public TypedErrorInfo<MyCustomSubError, MyCustomError> {</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">public:</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">  // Extends MyCustomError, adds new members.</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">};</font></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">3.  A set of utility functions that use the custom RTTI system to inspect and handle typed errors. For example 'catchAllTypedErrors' and 'handleTypedError' cooperate to handle error instances in a type-safe way:</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">TypedError foo() {</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">  if (SomeFailureCondition)</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">    return make_typed_error<MyCustomError>();</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">}</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class=""><br class=""></font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">TypedError Err = foo();</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class=""><br class=""></font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">catchAllTypedErrors(std::move(Err),</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">  handleTypedError<MyCustomError>(</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">    [](std::unique_ptr<MyCustomError> E) {</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">      // Handle the error.</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">      return TypedError(); // <- Indicate success from handler.</font></div></div></div></blockquote><div><br class=""></div><div>What does success or failure means for the handler?</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div style="font-size:13px" class=""><font face="monospace, monospace" class="">    }</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">  )</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">);</font></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">If your initial reaction is "Too much boilerplate!" I understand, but take comfort: (1) In the overwhelmingly common case of simply returning errors, the usage is identical to std::error_code:</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">if (TypedError Err = foo())</font></div><div style="font-size:13px" class=""><font face="monospace, monospace" class="">  return Err;</font></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">and (2) the boilerplate for catching errors is usually easily contained in a handful of utility functions, and tends not to crowd the rest of your source code. My initial experiments with this scheme involved updating many source lines, but did not add much code at all beyond the new error classes that were introduced.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">I believe that this scheme addresses many of the shortcomings of std::error_code while maintaining the strengths: </div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">1. Context - Custom error classes enable the user to attach as much contextual information as desired.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">2. Difficult to drop - The 'checked' flag in TypedError ensures that it can't be dropped, it must be explicitly "handled", even if that only involves catching the error and doing nothing.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">3. Debugger friendly - You can set a breakpoint on any custom error class's constructor to catch that error being created. Since the error class hierarchy is rooted you can break on TypedErrorInfoBase::TypedErrorInfoBase to catch any error being raised.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">4. Lightweight - Because TypedError instances are just a pointer and a checked-bit, move-constructing it is very cheap. We may also want to consider ignoring the 'checked' bit in release mode, at which point TypedError should be as cheap as std::error_code.</div></div></div></blockquote><div><br class=""></div><div>Oh here you mention compiling out the “checked” flag :)</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">5. Explicit - TypedError is represented explicitly in the APIs, the same as std::error_code.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">6. Does not require C++ RTTI - The custom RTTI system does not rely on any standard C++ RTTI features.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">This scheme also has one attribute that I haven't seen in previous error handling systems (though my experience in this area is limited): Errors are not copyable, due to ownership semantics of TypedError. I think this actually neatly captures the idea that there is a chain of responsibility for dealing with any given error. Responsibility may be transferred (e.g. by returning it to a caller), but it cannot be duplicated as it doesn't generally make sense for multiple people to report or attempt to recover from the same error.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">I've tested this prototype out by threading it through the object-creation APIs of libObject and using custom error classes to report errors in MachO headers. My initial experience is that this has enabled much richer error messages than are possible with std::error_code.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">To enable interaction with APIs that still use std::error_code I have added a custom ECError class that wraps a std::error_code, and can be converted back to a std::error_code using the typedErrorToErrorCode function. For now, all custom error code classes should (and do, in the prototype) derive from this utility class. In my experiments, this has made it easy to thread TypedError selectively through parts of the API. Eventually my hope is that TypedError could replace std::error_code for user-facing APIs, at which point custom errors would no longer need to derive from ECError, and ECError could be relegated to a utility for interacting with other codebases that still use std::error_code.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">So - I look forward to hearing your thoughts. :)</div></div></div></blockquote><br class=""><div>Is your call to catchAllTypedErrors(…) actually like a switch on the type of the error? What about a syntax that looks like a switch?</div><div><br class=""></div><div>switchErr(std::move(Err))</div><div>    .case< MyCustomError>([] () { /* … */ })</div><div>    .case< MyOtherCustomError>([] () { /* … */ })</div><div>    .default([] () { /* … */ })</div><div><br class=""></div><div><br class=""></div><div>— </div><div>Mehdi       </div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">Cheers,</div><div style="font-size:13px" class="">Lang.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class=""><div class="">Attached files:<br class=""></div><div class=""><br class=""></div><div class="">typed_error.patch - Adds include/llvm/Support/TypedError.h (also adds anchor() method to lib/Support/ErrorHandling.cpp).</div><div class=""><br class=""></div><div class="">error_demo.tgz - Stand-alone program demo'ing basic use of the TypedError API.</div></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">libobject_typed_error_demo.patch - Threads TypedError through the binary-file creation methods (createBinary, createObjectFile, etc). Proof-of-concept for how TypedError can be integrated into an existing system.</div><div style="font-size:13px" class=""><br class=""></div></div>
<span id="cid:805E7710-31FE-4048-AE9F-3A9DA7F36544@apple.com"><typed_error.patch></span><span id="cid:EBDE4E0F-70DD-4853-BF13-2BC6E03ADD3F@apple.com"><error_demo.tgz></span><span id="cid:C9BD519F-6DC1-4C80-A8AE-7E99A3C5A7F0@apple.com"><thread_typederror_through_object_creation.patch></span>_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br class=""></div></blockquote></div><br class=""></body></html>