diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-06-07 13:30:45 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-06-08 07:01:26 +1000 |
| commit | 1acbe7573dc32f917f51a784a36b7afc690900e3 (patch) | |
| tree | 1b4e8d414de3d349bd3ae038c28e9dc77e54c345 /compiler/rustc_middle/src/mir | |
| parent | 582b9cbc45334a73467d6ccaf0a8b9de559c2011 (diff) | |
| download | rust-1acbe7573dc32f917f51a784a36b7afc690900e3.tar.gz rust-1acbe7573dc32f917f51a784a36b7afc690900e3.zip | |
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`.
Diffstat (limited to 'compiler/rustc_middle/src/mir')
| -rw-r--r-- | compiler/rustc_middle/src/mir/graph_cyclic_cache.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/interpret/mod.rs | 15 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/mod.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/predecessors.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/switch_sources.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/traversal.rs | 4 |
6 files changed, 16 insertions, 23 deletions
diff --git a/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs b/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs index e2f3d6e078f..096bf8cbc15 100644 --- a/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs +++ b/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs @@ -38,8 +38,8 @@ impl GraphIsCyclicCache { impl<S: serialize::Encoder> serialize::Encodable<S> for GraphIsCyclicCache { #[inline] - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - serialize::Encodable::encode(&(), s) + fn encode(&self, s: &mut S) { + serialize::Encodable::encode(&(), s); } } diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 06cd6a66e39..214b919e24d 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -207,27 +207,26 @@ pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>>( encoder: &mut E, tcx: TyCtxt<'tcx>, alloc_id: AllocId, -) -> Result<(), E::Error> { +) { match tcx.global_alloc(alloc_id) { GlobalAlloc::Memory(alloc) => { trace!("encoding {:?} with {:#?}", alloc_id, alloc); - AllocDiscriminant::Alloc.encode(encoder)?; - alloc.encode(encoder)?; + AllocDiscriminant::Alloc.encode(encoder); + alloc.encode(encoder); } GlobalAlloc::Function(fn_instance) => { trace!("encoding {:?} with {:#?}", alloc_id, fn_instance); - AllocDiscriminant::Fn.encode(encoder)?; - fn_instance.encode(encoder)?; + AllocDiscriminant::Fn.encode(encoder); + fn_instance.encode(encoder); } GlobalAlloc::Static(did) => { assert!(!tcx.is_thread_local_static(did)); // References to statics doesn't need to know about their allocations, // just about its `DefId`. - AllocDiscriminant::Static.encode(encoder)?; - did.encode(encoder)?; + AllocDiscriminant::Static.encode(encoder); + did.encode(encoder); } } - Ok(()) } // Used to avoid infinite recursion when decoding cyclic allocations. diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index f3db359ec33..581b3257c73 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -672,16 +672,16 @@ const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1; impl<E: TyEncoder, T: Encodable<E>> Encodable<E> for ClearCrossCrate<T> { #[inline] - fn encode(&self, e: &mut E) -> Result<(), E::Error> { + fn encode(&self, e: &mut E) { if E::CLEAR_CROSS_CRATE { - return Ok(()); + return; } match *self { ClearCrossCrate::Clear => TAG_CLEAR_CROSS_CRATE_CLEAR.encode(e), ClearCrossCrate::Set(ref val) => { - TAG_CLEAR_CROSS_CRATE_SET.encode(e)?; - val.encode(e) + TAG_CLEAR_CROSS_CRATE_SET.encode(e); + val.encode(e); } } } diff --git a/compiler/rustc_middle/src/mir/predecessors.rs b/compiler/rustc_middle/src/mir/predecessors.rs index 0b9ddaf64d4..9bc0cb1138f 100644 --- a/compiler/rustc_middle/src/mir/predecessors.rs +++ b/compiler/rustc_middle/src/mir/predecessors.rs @@ -56,9 +56,7 @@ impl PredecessorCache { impl<S: serialize::Encoder> serialize::Encodable<S> for PredecessorCache { #[inline] - fn encode(&self, _s: &mut S) -> Result<(), S::Error> { - Ok(()) - } + fn encode(&self, _s: &mut S) {} } impl<D: serialize::Decoder> serialize::Decodable<D> for PredecessorCache { diff --git a/compiler/rustc_middle/src/mir/switch_sources.rs b/compiler/rustc_middle/src/mir/switch_sources.rs index fbb26800e29..4872a7835e3 100644 --- a/compiler/rustc_middle/src/mir/switch_sources.rs +++ b/compiler/rustc_middle/src/mir/switch_sources.rs @@ -56,9 +56,7 @@ impl SwitchSourceCache { impl<S: serialize::Encoder> serialize::Encodable<S> for SwitchSourceCache { #[inline] - fn encode(&self, _s: &mut S) -> Result<(), S::Error> { - Ok(()) - } + fn encode(&self, _s: &mut S) {} } impl<D: serialize::Decoder> serialize::Decodable<D> for SwitchSourceCache { diff --git a/compiler/rustc_middle/src/mir/traversal.rs b/compiler/rustc_middle/src/mir/traversal.rs index 7e395902ad3..f745e55307a 100644 --- a/compiler/rustc_middle/src/mir/traversal.rs +++ b/compiler/rustc_middle/src/mir/traversal.rs @@ -367,9 +367,7 @@ impl PostorderCache { impl<S: serialize::Encoder> serialize::Encodable<S> for PostorderCache { #[inline] - fn encode(&self, _s: &mut S) -> Result<(), S::Error> { - Ok(()) - } + fn encode(&self, _s: &mut S) {} } impl<D: serialize::Decoder> serialize::Decodable<D> for PostorderCache { |
