1
Fork 0
rust/compiler/rustc_codegen_ssa/src
Nicholas Nethercote 1acbe7573d Use delayed error handling for Encodable and Encoder infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.

Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).

This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.

This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.

Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
  `into_inner` method is changed into `finish`, which returns
  `Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
  strategy. Its `Ok` type is a `usize`, returning the number of bytes
  written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
  passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-08 07:01:26 +10:00
..
back Rollup merge of #97058 - bjorn3:multi_artifact_work_products, r=nagisa 2022-06-07 11:41:06 +02:00
coverageinfo Remove in_band_lifetimes from rustc_codegen_ssa 2021-12-15 00:41:41 -05:00
debuginfo Remove NodeIdHashingMode. 2022-04-12 19:59:32 +02:00
mir comment 2022-06-02 11:12:12 -04:00
traits Merge new_metadata into codegen_allocator 2022-04-30 21:20:08 +02:00
base.rs Rollup merge of #97738 - Kixiron:zst-panic, r=eddyb 2022-06-07 11:41:08 +02:00
common.rs rustc_codegen_ssa: derive copy and clone for various enums 2022-05-25 10:34:35 +02:00
glue.rs Improve AdtDef interning. 2022-03-11 13:31:24 +11:00
lib.rs Use delayed error handling for Encodable and Encoder infallible. 2022-06-08 07:01:26 +10:00
meth.rs debuginfo: Refactor debuginfo generation for types -- Rename DebugInfoMethods::create_vtable_metadata() to DebugInfoMethods::create_vtable_debuginfo() 2022-03-14 17:25:24 +01:00
mono_item.rs Add codegen for global_asm! sym operands 2022-04-15 14:36:30 +01:00
target_features.rs Add some unstable target features for the wasm target codegen 2022-06-06 15:01:17 -07:00