[base] Let ASSIGN_OR_RETURN/RETURN_IF_ERROR return a plain value
The trailing arguments of these macros had to be an invocable, so
returning a fixed value on failure needed a lambda that ignores the
error:
ASSIGN_OR_RETURN(auto v, Foo(), [](auto) { return nullptr; });
Accept a single non-invocable argument as the value to return:
ASSIGN_OR_RETURN(auto v, Foo(), nullptr);
This matches the Google-internal macros these were modeled on, which
are open sourced as ABSL_ASSIGN_OR_RETURN in
absl/status/status_macros.h. There the third argument is an expression
that is evaluated to produce the return value. The value is returned
as `return value;` would, so it converts to the return type the same
way, and a function that returns base::expected<T, E> passes
base::unexpected(...) to return an error. The value is only evaluated
on failure and is not copied, so a function that returns a reference
or a view can return an lvalue. Invocable arguments behave as before.
Function pointers, member pointers and objects with a single,
non-template call operator (such as lambdas that are not generic) are
never treated as a value. Additional arguments that are neither
invocable nor a single value are now a compile error. In a void
function a handler that can't be invoked used to compile and was never
run.
ErrorReturn is overloaded on the result type and returned directly
from the macro: void handlers return void, plain values are forwarded
as-is, and errors and non-void handler calls are wrapped in
UnexpectedDeducer. This drops the macro's inner lambda,
UnexpectedDeducer::Ret(), and the workaround for discarded
constexpr-if arms in generic lambdas.
The WebNN fuzzer relied on a handler that can't be invoked: 34 call
sites passed a std::optional to ASSIGN_OR_RETURN_VOID, whose handler
takes a std::string. That is now a compile error, so they use the
fuzzer's ASSIGN_OPTIONAL_OR_RETURN_VOID instead.
Change-Id: I60630bb68081e9ffd8bee442862f32ca6a6a6964
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8814793
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1753136}
NOKEYCHECK=True
GitOrigin-RevId: ea2b0684789c12bdbde4b891903608452cbdecc9
Contains a written down set of principles and other information on //base. Please add to it!
Chromium is a very mature project. Most things that are generally useful are already here and things not here aren't generally useful.
The bar for adding stuff to base is that it must have demonstrated wide applicability. Prefer to add things closer to where they're used (i.e. “not base”), and pull into base only when needed. In a project our size, sometimes even duplication is OK and inevitable.
Adding a new logging macro DPVELOG_NE is not more clear than just writing the stuff you want to log in a regular logging statement, even if it makes your calling code longer. Just add it to your own code.
If the code in question does not need to be used inside base, but will have multiple consumers across the codebase, consider placing it in a new directory under components/ instead.
base is written for the Chromium project and is not intended to be used outside it. Using base outside of src.git is explicitly not supported, and base makes no guarantees about API (or even ABI) stability (like all other code in Chromium). New code that depends on base/ must be in src.git. Code that's not in src.git but pulled in through DEPS (for example, v8) cannot use base.
Owners are added when a contributor has shown the above qualifications and when they express interest. There isn't an upper bound on the number of OWNERS.
Since the primitives provided by //base are used very widely, it is important to ensure they scale to the necessary workloads and perform well under all supported platforms. The base_perftests target is a suite of synthetic microbenchmarks that measure performance in various scenarios:
thread_local, the implementation in //base, the POSIX/WinAPI directly)Regressions in these benchmarks can generally by caused by 1) operating system changes, 2) compiler version or flag changes or 3) changes in //base code itself.
Rust code in base should be organized into very small crates, split up by function. Merging crates is sometimes unavoidable (due to dependency cycles or the orphaning rule).
Rust files should live near the equivalent C++ files (if any), and use the same naming scheme (for example, run_loop.rs, not run_loop_rust.rs).
When adding FFI shims, prefer separate _shim.h files rather than adding code to existing C++ files. This helps avoid circular dependencies with the //base target. It also avoids adding code to commonly-used headers, which can increase compile size by a lot.
Crates which you expect to be widely used should be added to the public_deps of the //base:base_rust target, so that developers can simply depend on //base:base_rust the same way they do with //base.