Commit graph

5 commits

Author SHA1 Message Date
Idan Horowitz 8a879e205b AK: Mark the error branch of the TRY() macro as unlikely
This results in a measurable (and free!) 2% improvement in test-js
run time.
2022-01-16 02:01:23 +02:00
Andreas Kling cd49f30bea AK+LibJS: Simplify MUST() and move it from LibJS to AK/Try.h
This is generally useful so let's move it to AK. Also it seems that we
don't need the temporary variable hack anymore, so let's lose that.
2021-11-10 21:58:58 +01:00
Andreas Kling 79fa9765ca Kernel: Replace KResult and KResultOr<T> with Error and ErrorOr<T>
We now use AK::Error and AK::ErrorOr<T> in both kernel and userspace!
This was a slightly tedious refactoring that took a long time, so it's
not unlikely that some bugs crept in.

Nevertheless, it does pass basic functionality testing, and it's just
real nice to finally see the same pattern in all contexts. :^)
2021-11-08 01:10:53 +01:00
Linus Groh cbdd069279 AK: Rename the local variable in the TRY() macro to avoid name clashes
"result" is a tad bit too generic to provide a clash-free experience -
we found instances in LibJS where this breaks already. Essentially this
doesn't work:

    auto foo = TRY(bar(result));

Because it expands to the following within the TRY() scope:

    {
        auto result = bar(result);
        ...
    }

And that of course fails:

    error: use of ‘result’ before deduction of ‘auto’

The simple solution here is to use a name that is much less likely to
clash with anything used in the expression ("_temporary_result"). :^)
2021-09-15 23:46:53 +01:00
Andreas Kling b4d8e166d8 AK: Add a TRY(expression) macro to simplify the unwrap-or-return pattern
The way we use classes like Kernel::KResultOr<T> and AK::Result<T, E>
makes checking for errors (and short-circuiting returns) quite verbose.

This patch adds a new TRY(expression) macro that either evaluates to
the released result of the expression if successful, or returns the
error if not.

Before:

    auto foo_or_error = get_foo();
    if (foo_or_error.is_error())
        return foo_or_error.release_error();
    auto foo = foo_or_error.release_value();

After:

    auto foo = TRY(get_foo());

The macro uses a GNU C++ extension which is supported by GCC, Clang,
Intel C++, and possibly others. It's not *ideal*, but since it makes our
codebase considerably nicer, let's try(!) it out. :^)

Co-authored-by: Ali Mohammad Pur <mpfard@serenityos.org>
2021-09-05 14:08:12 +02:00