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`.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -69,11 +69,7 @@ pub trait RefDecodable<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> {
|
||||
}
|
||||
|
||||
/// Encode the given value or a previously cached shorthand.
|
||||
pub fn encode_with_shorthand<'tcx, E, T, M>(
|
||||
encoder: &mut E,
|
||||
value: &T,
|
||||
cache: M,
|
||||
) -> Result<(), E::Error>
|
||||
pub fn encode_with_shorthand<'tcx, E, T, M>(encoder: &mut E, value: &T, cache: M)
|
||||
where
|
||||
E: TyEncoder<I = TyCtxt<'tcx>>,
|
||||
M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
|
||||
@@ -83,13 +79,14 @@ where
|
||||
{
|
||||
let existing_shorthand = cache(encoder).get(value).copied();
|
||||
if let Some(shorthand) = existing_shorthand {
|
||||
return encoder.emit_usize(shorthand);
|
||||
encoder.emit_usize(shorthand);
|
||||
return;
|
||||
}
|
||||
|
||||
let variant = value.variant();
|
||||
|
||||
let start = encoder.position();
|
||||
variant.encode(encoder)?;
|
||||
variant.encode(encoder);
|
||||
let len = encoder.position() - start;
|
||||
|
||||
// The shorthand encoding uses the same usize as the
|
||||
@@ -108,57 +105,55 @@ where
|
||||
if leb128_bits >= 64 || (shorthand as u64) < (1 << leb128_bits) {
|
||||
cache(encoder).insert(*value, shorthand);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for Ty<'tcx> {
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
encode_with_shorthand(e, self, TyEncoder::type_shorthands)
|
||||
fn encode(&self, e: &mut E) {
|
||||
encode_with_shorthand(e, self, TyEncoder::type_shorthands);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E>
|
||||
for ty::Binder<'tcx, ty::PredicateKind<'tcx>>
|
||||
{
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
self.bound_vars().encode(e)?;
|
||||
encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands)
|
||||
fn encode(&self, e: &mut E) {
|
||||
self.bound_vars().encode(e);
|
||||
encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Predicate<'tcx> {
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
self.kind().encode(e)
|
||||
fn encode(&self, e: &mut E) {
|
||||
self.kind().encode(e);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Region<'tcx> {
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
self.kind().encode(e)
|
||||
fn encode(&self, e: &mut E) {
|
||||
self.kind().encode(e);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Const<'tcx> {
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
self.0.0.encode(e)
|
||||
fn encode(&self, e: &mut E) {
|
||||
self.0.0.encode(e);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ConstAllocation<'tcx> {
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
fn encode(&self, e: &mut E) {
|
||||
self.inner().encode(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for AdtDef<'tcx> {
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
fn encode(&self, e: &mut E) {
|
||||
self.0.0.encode(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for AllocId {
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
fn encode(&self, e: &mut E) {
|
||||
e.encode_alloc_id(self)
|
||||
}
|
||||
}
|
||||
@@ -508,9 +503,9 @@ macro_rules! impl_binder_encode_decode {
|
||||
($($t:ty),+ $(,)?) => {
|
||||
$(
|
||||
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::Binder<'tcx, $t> {
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
self.bound_vars().encode(e)?;
|
||||
self.as_ref().skip_binder().encode(e)
|
||||
fn encode(&self, e: &mut E) {
|
||||
self.bound_vars().encode(e);
|
||||
self.as_ref().skip_binder().encode(e);
|
||||
}
|
||||
}
|
||||
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Binder<'tcx, $t> {
|
||||
|
||||
@@ -140,9 +140,9 @@ impl<CTX> crate::ty::HashStable<CTX> for ScalarInt {
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for ScalarInt {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_u128(self.data)?;
|
||||
s.emit_u8(self.size)
|
||||
fn encode(&self, s: &mut S) {
|
||||
s.emit_u128(self.data);
|
||||
s.emit_u8(self.size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
|
||||
|
||||
fn drop_serialized_data(&self, tcx: TyCtxt<'tcx>);
|
||||
|
||||
fn serialize(&self, tcx: TyCtxt<'tcx>, encoder: &mut FileEncoder) -> FileEncodeResult;
|
||||
fn serialize(&self, tcx: TyCtxt<'tcx>, encoder: FileEncoder) -> FileEncodeResult;
|
||||
}
|
||||
|
||||
#[allow(rustc::usage_of_ty_tykind)]
|
||||
@@ -1466,8 +1466,8 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn serialize_query_result_cache(self, encoder: &mut FileEncoder) -> FileEncodeResult {
|
||||
self.on_disk_cache.as_ref().map_or(Ok(()), |c| c.serialize(self, encoder))
|
||||
pub fn serialize_query_result_cache(self, encoder: FileEncoder) -> FileEncodeResult {
|
||||
self.on_disk_cache.as_ref().map_or(Ok(0), |c| c.serialize(self, encoder))
|
||||
}
|
||||
|
||||
/// If `true`, we should use lazy normalization for constants, otherwise
|
||||
|
||||
@@ -117,8 +117,8 @@ impl<T: fmt::Debug> fmt::Debug for List<T> {
|
||||
|
||||
impl<S: Encoder, T: Encodable<S>> Encodable<S> for List<T> {
|
||||
#[inline]
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
(**self).encode(s)
|
||||
fn encode(&self, s: &mut S) {
|
||||
(**self).encode(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -217,7 +217,7 @@ impl<'tcx> TypeFoldable<'tcx> for GenericArg<'tcx> {
|
||||
}
|
||||
|
||||
impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for GenericArg<'tcx> {
|
||||
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
|
||||
fn encode(&self, e: &mut E) {
|
||||
self.unpack().encode(e)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user