[PATCH] Add ErrorOr<>::getOrDie() which uses report_fatal_error instead of assert.

Rafael Espíndola rafael.espindola at gmail.com
Thu Oct 9 13:26:21 PDT 2014

On 9 October 2014 15:44, Justin Bogner <mail at justinbogner.com> wrote:
> Alexander Kornienko <alexfh at google.com> writes:
>> This method allows writing code that consistently fails in case of
>> dereferencing an ErrorOr<> containing no value rather than crashing in
>> debug and using uninitialized data in release build.
> I don't see why this would be better than doing this check in the
> caller, for two reasons:
> 1. Situations where report_fatal_error is the right thing are fairly
>    rare, so I wouldn't expect it to be a lot of boilerplate.
> 2. The caller generally can and should report a more specific error

I don't thing anyone disagrees that a nice error message is a feature,
but it might not be the priority of every code to use a SMDiagnostic.
Using report_fatal_error is a reasonable way to start (or an
improvement over a misplaced assert/unreachable, like we have in parts
of LLVM (pr20485 for example)).

Having said that, calling report_fatal_error in a class that is pretty
independent from llvm looks a bit odd. Could we maybe have something

template<typename T>
const T& extractOrDie(const ErrorOr<T> &V) {
  if (V)
    return *V;

defined in some other header?


More information about the llvm-commits mailing list