mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:50:13 +01:00
When using this with filesystem errors, a few issues came up that I'm fixing here. They're small enough and near enough in code that it didn't seem worth splitting part. - It's nice to forward declare custom error types and an API using them and then define both later. That doesn't work with `requires` but works fine with `static_assert`, so go back to that pattern here. A test is added that checks this pattern compiles. - The `operator*` didn't support moving out of `ErrorOr`, which is especially important when writing code that is happy with just `CARBON_CHECK`-failing on any errors. For example, we have a lot of filesystem code in tests that is made *much* more concise by just using `*` on a function return and letting the built-in checking ensure no errors were present. But when the value is move-only, this requires special overloading. Add that and add a test with a move-only value. - There wasn't an idiomatic way to do something like `operator*` for `ErrorOr<Success, ...>`. This PR factors out the checking for `ok()` into a `Check()` method that can be used to make code more readable that is intentionally just verifying no error. Also makes the result of `operator*` `[[nodiscard]]` to improve error messages and help void accidental bugs. - The `IsError` and `IsSuccess` test helpers required printable values which isn't always realistic. Teach the printing logic to be conditional on some indication of a printable value and gracefully fall back to a generic string otherwise for testing output. - The use of the `listener` in `IsError` and `IsSuccess` assumed a non-null stream. Instead, streaming should go directly to the `listener` as it is configured to only actually do the output when a stream is installed. When a stream isn't installed, the previous code would crash if the `MatchAndExplain` method ended up called without an 'interesting' stream attached to the listener. - When doing a `CARBON_CHECK` that there isn't an error, print the error out as the check failure message. Without this, all the nice error message work doesn't end up helping the debugging of test code that hits these errors, etc.