1
Fork 0
rust/compiler/rustc_middle/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
..
dep_graph Remove crate visibility usage in compiler 2022-05-20 20:04:54 -04:00
hir Compute lifetimes in scope at diagnostic time. 2022-06-04 15:28:15 +02:00
infer fix most compiler/ doctests 2022-05-02 17:40:30 -07:00
middle Rollup merge of #97301 - semicoleon:unstable-reexport, r=petrochenkov 2022-06-07 11:41:07 +02:00
mir Use delayed error handling for Encodable and Encoder infallible. 2022-06-08 07:01:26 +10:00
query Rollup merge of #97312 - cjgillot:no-path-in-scope, r=compiler-errors 2022-06-06 14:34:55 +02:00
thir Reimplement lowering of sym operands for asm! so that it also works with global_asm! 2022-04-14 15:32:03 +01:00
traits Lifetime variance fixes for rustc 2022-05-22 14:29:32 -07:00
ty Use delayed error handling for Encodable and Encoder infallible. 2022-06-08 07:01:26 +10:00
util span: move MultiSpan 2022-04-05 07:01:00 +01:00
arena.rs try to cache region_scope_tree as a query 2022-05-25 13:52:32 +08:00
lib.rs Fully stabilize NLL 2022-06-03 17:16:41 -04:00
lint.rs Move lint expectation checking into a separate query (RFC 2383) 2022-05-08 14:37:14 +02:00
macros.rs Rename TypeFolderFallible to FallibleTypeFolder 2021-12-02 16:14:18 +00:00
metadata.rs resolve/metadata: Stop encoding macros as reexports 2022-02-24 22:55:40 +03:00
tests.rs
thir.rs correctly deal with user type ascriptions in pat 2022-05-21 08:13:17 +02:00