From 92b1ab8d57c73468dd648b013ec8b7952b6e3c53 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 7 Jun 2022 11:08:10 +1000 Subject: [PATCH 1/5] Remove an unnecessary encoder operation. --- compiler/rustc_codegen_ssa/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 453c57b46d74f..005825931d797 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -209,8 +209,6 @@ impl CodegenResults { // Encoder's inner representation of `u32`. encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes()).unwrap(); encoder.emit_str(RUSTC_VERSION.unwrap()).unwrap(); - - let mut encoder = rustc_serialize::opaque::Encoder::new(encoder.into_inner()); rustc_serialize::Encodable::encode(codegen_results, &mut encoder).unwrap(); encoder.into_inner() } From 582b9cbc45334a73467d6ccaf0a8b9de559c2011 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 7 Jun 2022 13:30:08 +1000 Subject: [PATCH 2/5] Don't pass in a vector to `Encoder::new`. It's not necessary. --- compiler/rustc_codegen_ssa/src/lib.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- compiler/rustc_serialize/src/opaque.rs | 4 ++-- compiler/rustc_serialize/tests/opaque.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 005825931d797..cc7efaac8930d 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -203,7 +203,7 @@ const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION"); impl CodegenResults { pub fn serialize_rlink(codegen_results: &CodegenResults) -> Vec { - let mut encoder = opaque::Encoder::new(vec![]); + let mut encoder = opaque::Encoder::new(); encoder.emit_raw_bytes(RLINK_MAGIC).unwrap(); // `emit_raw_bytes` is used to make sure that the version representation does not depend on // Encoder's inner representation of `u32`. diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 1425c5467af87..2b606018f86f5 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -2194,7 +2194,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata { } fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata { - let mut encoder = opaque::Encoder::new(vec![]); + let mut encoder = opaque::Encoder::new(); encoder.emit_raw_bytes(METADATA_HEADER).unwrap(); // Will be filled with the root position after encoding everything. diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 3bcb7cc365054..40b79ba89ef4e 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -18,8 +18,8 @@ pub struct Encoder { } impl Encoder { - pub fn new(data: Vec) -> Encoder { - Encoder { data } + pub fn new() -> Encoder { + Encoder { data: vec![] } } pub fn into_inner(self) -> Vec { diff --git a/compiler/rustc_serialize/tests/opaque.rs b/compiler/rustc_serialize/tests/opaque.rs index 298eb1151118a..ec3c34f8bdb5d 100644 --- a/compiler/rustc_serialize/tests/opaque.rs +++ b/compiler/rustc_serialize/tests/opaque.rs @@ -31,7 +31,7 @@ struct Struct { fn check_round_trip + for<'a> Decodable> + PartialEq + Debug>( values: Vec, ) { - let mut encoder = Encoder::new(Vec::new()); + let mut encoder = Encoder::new(); for value in &values { Encodable::encode(value, &mut encoder).unwrap(); From 1acbe7573dc32f917f51a784a36b7afc690900e3 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 7 Jun 2022 13:30:45 +1000 Subject: [PATCH 3/5] 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, !>`. - 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`. --- compiler/rustc_ast/src/ast.rs | 4 +- compiler/rustc_ast/src/ptr.rs | 8 +- compiler/rustc_ast/src/tokenstream.rs | 4 +- compiler/rustc_codegen_ssa/src/lib.rs | 12 +- .../rustc_data_structures/src/fingerprint.rs | 9 +- compiler/rustc_data_structures/src/svh.rs | 4 +- .../src/persist/file_format.rs | 49 ++-- .../rustc_incremental/src/persist/save.rs | 29 +-- compiler/rustc_index/src/vec.rs | 4 +- compiler/rustc_macros/src/newtype.rs | 4 +- compiler/rustc_macros/src/serialize.rs | 22 +- .../src/rmeta/def_path_hash_map.rs | 8 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 98 ++++--- compiler/rustc_metadata/src/rmeta/table.rs | 2 +- .../src/mir/graph_cyclic_cache.rs | 4 +- .../rustc_middle/src/mir/interpret/mod.rs | 15 +- compiler/rustc_middle/src/mir/mod.rs | 8 +- compiler/rustc_middle/src/mir/predecessors.rs | 4 +- .../rustc_middle/src/mir/switch_sources.rs | 4 +- compiler/rustc_middle/src/mir/traversal.rs | 4 +- compiler/rustc_middle/src/ty/codec.rs | 47 ++-- compiler/rustc_middle/src/ty/consts/int.rs | 6 +- compiler/rustc_middle/src/ty/context.rs | 6 +- compiler/rustc_middle/src/ty/list.rs | 4 +- compiler/rustc_middle/src/ty/subst.rs | 2 +- compiler/rustc_query_impl/src/lib.rs | 1 + .../rustc_query_impl/src/on_disk_cache.rs | 151 +++++------ compiler/rustc_query_impl/src/plumbing.rs | 6 +- .../rustc_query_system/src/dep_graph/graph.rs | 2 +- .../src/dep_graph/serialized.rs | 25 +- .../rustc_serialize/src/collection_impls.rs | 74 +++--- compiler/rustc_serialize/src/opaque.rs | 240 ++++++++++-------- compiler/rustc_serialize/src/serialize.rs | 156 ++++++------ compiler/rustc_serialize/tests/opaque.rs | 7 +- compiler/rustc_span/src/def_id.rs | 16 +- compiler/rustc_span/src/hygiene.rs | 47 ++-- compiler/rustc_span/src/lib.rs | 52 ++-- compiler/rustc_span/src/symbol.rs | 4 +- compiler/rustc_type_ir/src/codec.rs | 5 +- compiler/rustc_type_ir/src/sty.rs | 101 +++----- src/librustdoc/scrape_examples.rs | 6 +- .../deriving-encodable-decodable-box.rs | 12 +- ...riving-encodable-decodable-cell-refcell.rs | 12 +- src/test/ui-fulldeps/issue-11881.rs | 1 + src/test/ui-fulldeps/issue-14021.rs | 12 +- 45 files changed, 610 insertions(+), 681 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index b64f7b8ad1b11..9e6a186219b64 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2473,9 +2473,7 @@ rustc_index::newtype_index! { } impl rustc_serialize::Encodable for AttrId { - fn encode(&self, _s: &mut S) -> Result<(), S::Error> { - Ok(()) - } + fn encode(&self, _s: &mut S) {} } impl rustc_serialize::Decodable for AttrId { diff --git a/compiler/rustc_ast/src/ptr.rs b/compiler/rustc_ast/src/ptr.rs index bab85a3019d61..30481eddf9160 100644 --- a/compiler/rustc_ast/src/ptr.rs +++ b/compiler/rustc_ast/src/ptr.rs @@ -121,8 +121,8 @@ impl> Decodable for P { } impl> Encodable for P { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - (**self).encode(s) + fn encode(&self, s: &mut S) { + (**self).encode(s); } } @@ -191,8 +191,8 @@ impl<'a, T> IntoIterator for &'a P<[T]> { } impl> Encodable for P<[T]> { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - Encodable::encode(&**self, s) + fn encode(&self, s: &mut S) { + Encodable::encode(&**self, s); } } diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs index c58fe7287bf79..84d8829c398e6 100644 --- a/compiler/rustc_ast/src/tokenstream.rs +++ b/compiler/rustc_ast/src/tokenstream.rs @@ -142,9 +142,9 @@ impl fmt::Debug for LazyTokenStream { } impl Encodable for LazyTokenStream { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) { // Used by AST json printing. - Encodable::encode(&self.create_token_stream(), s) + Encodable::encode(&self.create_token_stream(), s); } } diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index cc7efaac8930d..771157dcad954 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -29,7 +29,7 @@ use rustc_middle::dep_graph::WorkProduct; use rustc_middle::middle::dependency_format::Dependencies; use rustc_middle::middle::exported_symbols::SymbolExportKind; use rustc_middle::ty::query::{ExternProviders, Providers}; -use rustc_serialize::{opaque, Decodable, Decoder, Encoder}; +use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder}; use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT}; use rustc_session::cstore::{self, CrateSource}; use rustc_session::utils::NativeLibKind; @@ -204,13 +204,13 @@ const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION"); impl CodegenResults { pub fn serialize_rlink(codegen_results: &CodegenResults) -> Vec { let mut encoder = opaque::Encoder::new(); - encoder.emit_raw_bytes(RLINK_MAGIC).unwrap(); + encoder.emit_raw_bytes(RLINK_MAGIC); // `emit_raw_bytes` is used to make sure that the version representation does not depend on // Encoder's inner representation of `u32`. - encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes()).unwrap(); - encoder.emit_str(RUSTC_VERSION.unwrap()).unwrap(); - rustc_serialize::Encodable::encode(codegen_results, &mut encoder).unwrap(); - encoder.into_inner() + encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes()); + encoder.emit_str(RUSTC_VERSION.unwrap()); + Encodable::encode(codegen_results, &mut encoder); + encoder.finish().unwrap() } pub fn deserialize_rlink(data: Vec) -> Result { diff --git a/compiler/rustc_data_structures/src/fingerprint.rs b/compiler/rustc_data_structures/src/fingerprint.rs index c88f3e73cff37..a032b039f34ef 100644 --- a/compiler/rustc_data_structures/src/fingerprint.rs +++ b/compiler/rustc_data_structures/src/fingerprint.rs @@ -144,9 +144,8 @@ impl_stable_hash_via_hash!(Fingerprint); impl Encodable for Fingerprint { #[inline] - fn encode(&self, s: &mut E) -> Result<(), E::Error> { - s.emit_raw_bytes(&self.to_le_bytes())?; - Ok(()) + fn encode(&self, s: &mut E) { + s.emit_raw_bytes(&self.to_le_bytes()); } } @@ -187,10 +186,10 @@ impl std::fmt::Display for PackedFingerprint { impl Encodable for PackedFingerprint { #[inline] - fn encode(&self, s: &mut E) -> Result<(), E::Error> { + fn encode(&self, s: &mut E) { // Copy to avoid taking reference to packed field. let copy = self.0; - copy.encode(s) + copy.encode(s); } } diff --git a/compiler/rustc_data_structures/src/svh.rs b/compiler/rustc_data_structures/src/svh.rs index 12ef286091c5d..61654b9e8f5f5 100644 --- a/compiler/rustc_data_structures/src/svh.rs +++ b/compiler/rustc_data_structures/src/svh.rs @@ -49,8 +49,8 @@ impl fmt::Display for Svh { } impl Encodable for Svh { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_u64(self.as_u64().to_le()) + fn encode(&self, s: &mut S) { + s.emit_u64(self.as_u64().to_le()); } } diff --git a/compiler/rustc_incremental/src/persist/file_format.rs b/compiler/rustc_incremental/src/persist/file_format.rs index 68180a2214a50..2dbd4b6bce85a 100644 --- a/compiler/rustc_incremental/src/persist/file_format.rs +++ b/compiler/rustc_incremental/src/persist/file_format.rs @@ -30,22 +30,20 @@ const HEADER_FORMAT_VERSION: u16 = 0; /// the Git commit hash. const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION"); -pub(crate) fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) -> FileEncodeResult { - stream.emit_raw_bytes(FILE_MAGIC)?; - stream.emit_raw_bytes(&[ - (HEADER_FORMAT_VERSION >> 0) as u8, - (HEADER_FORMAT_VERSION >> 8) as u8, - ])?; +pub(crate) fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) { + stream.emit_raw_bytes(FILE_MAGIC); + stream + .emit_raw_bytes(&[(HEADER_FORMAT_VERSION >> 0) as u8, (HEADER_FORMAT_VERSION >> 8) as u8]); let rustc_version = rustc_version(nightly_build); assert_eq!(rustc_version.len(), (rustc_version.len() as u8) as usize); - stream.emit_raw_bytes(&[rustc_version.len() as u8])?; - stream.emit_raw_bytes(rustc_version.as_bytes()) + stream.emit_raw_bytes(&[rustc_version.len() as u8]); + stream.emit_raw_bytes(rustc_version.as_bytes()); } pub(crate) fn save_in(sess: &Session, path_buf: PathBuf, name: &str, encode: F) where - F: FnOnce(&mut FileEncoder) -> FileEncodeResult, + F: FnOnce(FileEncoder) -> FileEncodeResult, { debug!("save: storing data in {}", path_buf.display()); @@ -80,28 +78,21 @@ where } }; - if let Err(err) = write_file_header(&mut encoder, sess.is_nightly_build()) { - sess.err(&format!("failed to write {} header to `{}`: {}", name, path_buf.display(), err)); - return; - } - - if let Err(err) = encode(&mut encoder) { - sess.err(&format!("failed to write {} to `{}`: {}", name, path_buf.display(), err)); - return; - } + write_file_header(&mut encoder, sess.is_nightly_build()); - if let Err(err) = encoder.flush() { - sess.err(&format!("failed to flush {} to `{}`: {}", name, path_buf.display(), err)); - return; + match encode(encoder) { + Ok(position) => { + sess.prof.artifact_size( + &name.replace(' ', "_"), + path_buf.file_name().unwrap().to_string_lossy(), + position as u64, + ); + debug!("save: data written to disk successfully"); + } + Err(err) => { + sess.err(&format!("failed to write {} to `{}`: {}", name, path_buf.display(), err)); + } } - - sess.prof.artifact_size( - &name.replace(' ', "_"), - path_buf.file_name().unwrap().to_string_lossy(), - encoder.position() as u64, - ); - - debug!("save: data written to disk successfully"); } /// Reads the contents of a file with a file header as defined in this module. diff --git a/compiler/rustc_incremental/src/persist/save.rs b/compiler/rustc_incremental/src/persist/save.rs index 0223976b08a5b..9341a742925d9 100644 --- a/compiler/rustc_incremental/src/persist/save.rs +++ b/compiler/rustc_incremental/src/persist/save.rs @@ -3,7 +3,7 @@ use rustc_data_structures::sync::join; use rustc_middle::dep_graph::{DepGraph, SerializedDepGraph, WorkProduct, WorkProductId}; use rustc_middle::ty::TyCtxt; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; -use rustc_serialize::Encodable as RustcEncodable; +use rustc_serialize::{Encodable as RustcEncodable, Encoder}; use rustc_session::Session; use std::fs; @@ -96,8 +96,9 @@ pub fn save_work_product_index( debug!("save_work_product_index()"); dep_graph.assert_ignored(); let path = work_products_path(sess); - file_format::save_in(sess, path, "work product index", |e| { - encode_work_product_index(&new_work_products, e) + file_format::save_in(sess, path, "work product index", |mut e| { + encode_work_product_index(&new_work_products, &mut e); + e.finish() }); // We also need to clean out old work-products, as not all of them are @@ -123,7 +124,7 @@ pub fn save_work_product_index( fn encode_work_product_index( work_products: &FxHashMap, encoder: &mut FileEncoder, -) -> FileEncodeResult { +) { let serialized_products: Vec<_> = work_products .iter() .map(|(id, work_product)| SerializedWorkProduct { @@ -135,7 +136,7 @@ fn encode_work_product_index( serialized_products.encode(encoder) } -fn encode_query_cache(tcx: TyCtxt<'_>, encoder: &mut FileEncoder) -> FileEncodeResult { +fn encode_query_cache(tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult { tcx.sess.time("incr_comp_serialize_result_cache", || tcx.serialize_query_result_cache(encoder)) } @@ -170,24 +171,10 @@ pub fn build_dep_graph( } }; - if let Err(err) = file_format::write_file_header(&mut encoder, sess.is_nightly_build()) { - sess.err(&format!( - "failed to write dependency graph header to `{}`: {}", - path_buf.display(), - err - )); - return None; - } + file_format::write_file_header(&mut encoder, sess.is_nightly_build()); // First encode the commandline arguments hash - if let Err(err) = sess.opts.dep_tracking_hash(false).encode(&mut encoder) { - sess.err(&format!( - "failed to write dependency graph hash `{}`: {}", - path_buf.display(), - err - )); - return None; - } + sess.opts.dep_tracking_hash(false).encode(&mut encoder); Some(DepGraph::new( &sess.prof, diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index a8c611e18ff39..1a55519d7b120 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -60,8 +60,8 @@ pub struct IndexVec { unsafe impl Send for IndexVec where T: Send {} impl> Encodable for IndexVec { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - Encodable::encode(&self.raw, s) + fn encode(&self, s: &mut S) { + Encodable::encode(&self.raw, s); } } diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs index c8b31cd0c4d78..0a77b734c7641 100644 --- a/compiler/rustc_macros/src/newtype.rs +++ b/compiler/rustc_macros/src/newtype.rs @@ -137,8 +137,8 @@ impl Parse for Newtype { } } impl ::rustc_serialize::Encodable for #name { - fn encode(&self, e: &mut E) -> Result<(), E::Error> { - e.emit_u32(self.private) + fn encode(&self, e: &mut E) { + e.emit_u32(self.private); } } } diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index e99fa6c113b0d..82e6972d0270c 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -146,21 +146,17 @@ fn encodable_body( .map(|binding| { let bind_ident = &binding.binding; let result = quote! { - match ::rustc_serialize::Encodable::<#encoder_ty>::encode( + ::rustc_serialize::Encodable::<#encoder_ty>::encode( #bind_ident, __encoder, - ) { - ::std::result::Result::Ok(()) => (), - ::std::result::Result::Err(__err) - => return ::std::result::Result::Err(__err), - } + ); }; result }) .collect::() }); quote! { - ::std::result::Result::Ok(match *self { #encode_inner }) + match *self { #encode_inner } } } _ => { @@ -172,14 +168,10 @@ fn encodable_body( .map(|binding| { let bind_ident = &binding.binding; let result = quote! { - match ::rustc_serialize::Encodable::<#encoder_ty>::encode( + ::rustc_serialize::Encodable::<#encoder_ty>::encode( #bind_ident, __encoder, - ) { - ::std::result::Result::Ok(()) => (), - ::std::result::Result::Err(__err) - => return ::std::result::Result::Err(__err), - } + ); }; result }) @@ -190,7 +182,7 @@ fn encodable_body( ::rustc_serialize::Encoder::emit_enum_variant( __encoder, #variant_idx, - |__encoder| { ::std::result::Result::Ok({ #encode_fields }) } + |__encoder| { #encode_fields } ) } } else { @@ -223,7 +215,7 @@ fn encodable_body( fn encode( &self, __encoder: &mut #encoder_ty, - ) -> ::std::result::Result<(), <#encoder_ty as ::rustc_serialize::Encoder>::Error> { + ) { #lints #encode_body } diff --git a/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs b/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs index 15fd190b0492a..40c94b372bbf3 100644 --- a/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs +++ b/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs @@ -4,7 +4,7 @@ use crate::rmeta::MetadataBlob; use rustc_data_structures::owning_ref::OwningRef; use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap}; use rustc_middle::parameterized_over_tcx; -use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder}; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::def_id::{DefIndex, DefPathHash}; pub(crate) enum DefPathHashMapRef<'tcx> { @@ -29,12 +29,12 @@ impl DefPathHashMapRef<'_> { } impl<'a, 'tcx> Encodable> for DefPathHashMapRef<'tcx> { - fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { + fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) { match *self { DefPathHashMapRef::BorrowedFromTcx(def_path_hash_map) => { let bytes = def_path_hash_map.raw_bytes(); - e.emit_usize(bytes.len())?; - e.emit_raw_bytes(bytes) + e.emit_usize(bytes.len()); + e.emit_raw_bytes(bytes); } DefPathHashMapRef::OwnedFromMetadata(_) => { panic!("DefPathHashMap::OwnedFromMetadata variant only exists for deserialization") diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 2b606018f86f5..3285273ba909e 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -86,14 +86,15 @@ macro_rules! empty_proc_macro { macro_rules! encoder_methods { ($($name:ident($ty:ty);)*) => { - $(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> { + $(fn $name(&mut self, value: $ty) { self.opaque.$name(value) })* } } impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> { - type Error = ::Error; + type Ok = ::Ok; + type Err = ::Err; encoder_methods! { emit_usize(usize); @@ -117,60 +118,63 @@ impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> { emit_str(&str); emit_raw_bytes(&[u8]); } + + fn finish(self) -> Result { + self.opaque.finish() + } } impl<'a, 'tcx, T> Encodable> for LazyValue { - fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { - e.emit_lazy_distance(self.position) + fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) { + e.emit_lazy_distance(self.position); } } impl<'a, 'tcx, T> Encodable> for LazyArray { - fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { - e.emit_usize(self.num_elems)?; - if self.num_elems == 0 { - return Ok(()); + fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) { + e.emit_usize(self.num_elems); + if self.num_elems > 0 { + e.emit_lazy_distance(self.position) } - e.emit_lazy_distance(self.position) } } impl<'a, 'tcx, I, T> Encodable> for LazyTable { - fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { - e.emit_usize(self.encoded_size)?; - e.emit_lazy_distance(self.position) + fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) { + e.emit_usize(self.encoded_size); + e.emit_lazy_distance(self.position); } } impl<'a, 'tcx> Encodable> for CrateNum { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { + fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { if *self != LOCAL_CRATE && s.is_proc_macro { panic!("Attempted to encode non-local CrateNum {:?} for proc-macro crate", self); } - s.emit_u32(self.as_u32()) + s.emit_u32(self.as_u32()); } } impl<'a, 'tcx> Encodable> for DefIndex { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { - s.emit_u32(self.as_u32()) + fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { + s.emit_u32(self.as_u32()); } } impl<'a, 'tcx> Encodable> for ExpnIndex { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { - s.emit_u32(self.as_u32()) + fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { + s.emit_u32(self.as_u32()); } } impl<'a, 'tcx> Encodable> for SyntaxContext { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { - rustc_span::hygiene::raw_encode_syntax_context(*self, &s.hygiene_ctxt, s) + fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { + rustc_span::hygiene::raw_encode_syntax_context(*self, &s.hygiene_ctxt, s); } } impl<'a, 'tcx> Encodable> for ExpnId { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { + fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { if self.krate == LOCAL_CRATE { // We will only write details for local expansions. Non-local expansions will fetch // data from the corresponding crate's metadata. @@ -178,13 +182,13 @@ impl<'a, 'tcx> Encodable> for ExpnId { // metadata from proc-macro crates. s.hygiene_ctxt.schedule_expn_data_for_encoding(*self); } - self.krate.encode(s)?; - self.local_id.encode(s) + self.krate.encode(s); + self.local_id.encode(s); } } impl<'a, 'tcx> Encodable> for Span { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { + fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { let span = self.data(); // Don't serialize any `SyntaxContext`s from a proc-macro crate, @@ -219,9 +223,9 @@ impl<'a, 'tcx> Encodable> for Span { // `rustc_span::hygiene::raw_encode_expn_id` to handle // encoding `ExpnData` for proc-macro crates. if s.is_proc_macro { - SyntaxContext::root().encode(s)?; + SyntaxContext::root().encode(s); } else { - span.ctxt.encode(s)?; + span.ctxt.encode(s); } if self.is_dummy() { @@ -289,22 +293,20 @@ impl<'a, 'tcx> Encodable> for Span { (TAG_VALID_SPAN_LOCAL, span.lo, span.hi) }; - tag.encode(s)?; - lo.encode(s)?; + tag.encode(s); + lo.encode(s); // Encode length which is usually less than span.hi and profits more // from the variable-length integer encoding that we use. let len = hi - lo; - len.encode(s)?; + len.encode(s); if tag == TAG_VALID_SPAN_FOREIGN { // This needs to be two lines to avoid holding the `s.source_file_cache` // while calling `cnum.encode(s)` let cnum = s.source_file_cache.0.cnum; - cnum.encode(s)?; + cnum.encode(s); } - - Ok(()) } } @@ -325,13 +327,10 @@ impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> { &mut self.predicate_shorthands } - fn encode_alloc_id( - &mut self, - alloc_id: &rustc_middle::mir::interpret::AllocId, - ) -> Result<(), Self::Error> { + fn encode_alloc_id(&mut self, alloc_id: &rustc_middle::mir::interpret::AllocId) { let (index, _) = self.interpret_allocs.insert_full(*alloc_id); - index.encode(self) + index.encode(self); } } @@ -360,10 +359,7 @@ macro_rules! record_array { } impl<'a, 'tcx> EncodeContext<'a, 'tcx> { - fn emit_lazy_distance( - &mut self, - position: NonZeroUsize, - ) -> Result<(), ::Error> { + fn emit_lazy_distance(&mut self, position: NonZeroUsize) { let pos = position.get(); let distance = match self.lazy_state { LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"), @@ -382,7 +378,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } }; self.lazy_state = LazyState::Previous(NonZeroUsize::new(pos).unwrap()); - self.emit_usize(distance) + self.emit_usize(distance); } fn lazy>>(&mut self, value: B) -> LazyValue @@ -393,7 +389,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { assert_eq!(self.lazy_state, LazyState::NoNode); self.lazy_state = LazyState::NodeStart(pos); - value.borrow().encode(self).unwrap(); + value.borrow().encode(self); self.lazy_state = LazyState::NoNode; assert!(pos.get() <= self.position()); @@ -412,7 +408,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { assert_eq!(self.lazy_state, LazyState::NoNode); self.lazy_state = LazyState::NodeStart(pos); - let len = values.into_iter().map(|value| value.borrow().encode(self).unwrap()).count(); + let len = values.into_iter().map(|value| value.borrow().encode(self)).count(); self.lazy_state = LazyState::NoNode; assert!(pos.get() <= self.position()); @@ -615,7 +611,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let id = self.interpret_allocs[idx]; let pos = self.position() as u32; interpret_alloc_index.push(pos); - interpret::specialized_encode_alloc_id(self, tcx, id).unwrap(); + interpret::specialized_encode_alloc_id(self, tcx, id); } n = new_n; } @@ -1640,18 +1636,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let mut expn_data_table: TableBuilder<_, _> = Default::default(); let mut expn_hash_table: TableBuilder<_, _> = Default::default(); - let _: Result<(), !> = self.hygiene_ctxt.encode( + self.hygiene_ctxt.encode( &mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table), |(this, syntax_contexts, _, _), index, ctxt_data| { syntax_contexts.set(index, this.lazy(ctxt_data)); - Ok(()) }, |(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| { if let Some(index) = index.as_local() { expn_data_table.set(index.as_raw(), this.lazy(expn_data)); expn_hash_table.set(index.as_raw(), this.lazy(hash)); } - Ok(()) }, ); @@ -2195,10 +2189,10 @@ pub fn encode_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata { fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata { let mut encoder = opaque::Encoder::new(); - encoder.emit_raw_bytes(METADATA_HEADER).unwrap(); + encoder.emit_raw_bytes(METADATA_HEADER); // Will be filled with the root position after encoding everything. - encoder.emit_raw_bytes(&[0, 0, 0, 0]).unwrap(); + encoder.emit_raw_bytes(&[0, 0, 0, 0]); let source_map_files = tcx.sess.source_map().files(); let source_file_cache = (source_map_files[0].clone(), 0); @@ -2223,13 +2217,13 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata { }; // Encode the rustc version string in a predictable location. - rustc_version().encode(&mut ecx).unwrap(); + rustc_version().encode(&mut ecx); // Encode all the entries and extra information in the crate, // culminating in the `CrateRoot` which points to all of it. let root = ecx.encode_crate_root(); - let mut result = ecx.opaque.into_inner(); + let mut result = ecx.opaque.finish().unwrap(); // Encode the root position. let header = METADATA_HEADER.len(); diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index 100bac15b80a4..8baa67a8f9fcf 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -287,7 +287,7 @@ where { let pos = buf.position(); for block in &self.blocks { - buf.emit_raw_bytes(block).unwrap(); + buf.emit_raw_bytes(block); } let num_bytes = self.blocks.len() * N; LazyTable::from_position_and_encoded_size( diff --git a/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs b/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs index e2f3d6e078f97..096bf8cbc158a 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 serialize::Encodable 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 06cd6a66e39f5..214b919e24d03 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>>( 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 f3db359ec3348..581b3257c736f 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> Encodable for ClearCrossCrate { #[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 0b9ddaf64d4e2..9bc0cb1138ff1 100644 --- a/compiler/rustc_middle/src/mir/predecessors.rs +++ b/compiler/rustc_middle/src/mir/predecessors.rs @@ -56,9 +56,7 @@ impl PredecessorCache { impl serialize::Encodable for PredecessorCache { #[inline] - fn encode(&self, _s: &mut S) -> Result<(), S::Error> { - Ok(()) - } + fn encode(&self, _s: &mut S) {} } impl serialize::Decodable for PredecessorCache { diff --git a/compiler/rustc_middle/src/mir/switch_sources.rs b/compiler/rustc_middle/src/mir/switch_sources.rs index fbb26800e29c9..4872a7835e3fa 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 serialize::Encodable for SwitchSourceCache { #[inline] - fn encode(&self, _s: &mut S) -> Result<(), S::Error> { - Ok(()) - } + fn encode(&self, _s: &mut S) {} } impl serialize::Decodable for SwitchSourceCache { diff --git a/compiler/rustc_middle/src/mir/traversal.rs b/compiler/rustc_middle/src/mir/traversal.rs index 7e395902ad324..f745e55307ae2 100644 --- a/compiler/rustc_middle/src/mir/traversal.rs +++ b/compiler/rustc_middle/src/mir/traversal.rs @@ -367,9 +367,7 @@ impl PostorderCache { impl serialize::Encodable for PostorderCache { #[inline] - fn encode(&self, _s: &mut S) -> Result<(), S::Error> { - Ok(()) - } + fn encode(&self, _s: &mut S) {} } impl serialize::Decodable for PostorderCache { diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 1e2d1fbeb4bf6..9a363914dc332 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -69,11 +69,7 @@ pub trait RefDecodable<'tcx, D: TyDecoder>> { } /// 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>, M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap, @@ -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>> Encodable 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>> Encodable 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>> Encodable 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>> Encodable 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>> Encodable 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>> Encodable 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>> Encodable 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>> Encodable 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>> Encodable 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>> Decodable for ty::Binder<'tcx, $t> { diff --git a/compiler/rustc_middle/src/ty/consts/int.rs b/compiler/rustc_middle/src/ty/consts/int.rs index a3ce674c11524..51e51a63fd043 100644 --- a/compiler/rustc_middle/src/ty/consts/int.rs +++ b/compiler/rustc_middle/src/ty/consts/int.rs @@ -140,9 +140,9 @@ impl crate::ty::HashStable for ScalarInt { } impl Encodable 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); } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index e668edad7c43e..610234d45ce67 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -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 diff --git a/compiler/rustc_middle/src/ty/list.rs b/compiler/rustc_middle/src/ty/list.rs index 3c9e96df59aa4..db3b5cfd18056 100644 --- a/compiler/rustc_middle/src/ty/list.rs +++ b/compiler/rustc_middle/src/ty/list.rs @@ -117,8 +117,8 @@ impl fmt::Debug for List { impl> Encodable for List { #[inline] - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - (**self).encode(s) + fn encode(&self, s: &mut S) { + (**self).encode(s); } } diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index 290485ab5fe0f..de30387ef4c5b 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -217,7 +217,7 @@ impl<'tcx> TypeFoldable<'tcx> for GenericArg<'tcx> { } impl<'tcx, E: TyEncoder>> Encodable for GenericArg<'tcx> { - fn encode(&self, e: &mut E) -> Result<(), E::Error> { + fn encode(&self, e: &mut E) { self.unpack().encode(e) } } diff --git a/compiler/rustc_query_impl/src/lib.rs b/compiler/rustc_query_impl/src/lib.rs index 5e28c229aa5ce..d06eb97798e73 100644 --- a/compiler/rustc_query_impl/src/lib.rs +++ b/compiler/rustc_query_impl/src/lib.rs @@ -2,6 +2,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![feature(min_specialization)] +#![feature(never_type)] #![feature(once_cell)] #![feature(rustc_attrs)] #![recursion_limit = "256"] diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index e93bf1a475225..c2c876f7f1a12 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -221,7 +221,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> { *self.serialized_data.write() = None; } - fn serialize<'tcx>(&self, tcx: TyCtxt<'tcx>, encoder: &mut FileEncoder) -> FileEncodeResult { + fn serialize<'tcx>(&self, tcx: TyCtxt<'tcx>, encoder: FileEncoder) -> FileEncodeResult { // Serializing the `DepGraph` should not modify it. tcx.dep_graph.with_ignore(|| { // Allocate `SourceFileIndex`es. @@ -259,27 +259,25 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> { // Encode query results. let mut query_result_index = EncodedDepNodeIndex::new(); - tcx.sess.time("encode_query_results", || -> FileEncodeResult { + tcx.sess.time("encode_query_results", || { let enc = &mut encoder; let qri = &mut query_result_index; - QueryCtxt::from_tcx(tcx).encode_query_results(enc, qri) - })?; + QueryCtxt::from_tcx(tcx).encode_query_results(enc, qri); + }); // Encode side effects. let side_effects_index: EncodedDepNodeIndex = self .current_side_effects .borrow() .iter() - .map( - |(dep_node_index, side_effects)| -> Result<_, ::Error> { - let pos = AbsoluteBytePos::new(encoder.position()); - let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index()); - encoder.encode_tagged(dep_node_index, side_effects)?; + .map(|(dep_node_index, side_effects)| { + let pos = AbsoluteBytePos::new(encoder.position()); + let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index()); + encoder.encode_tagged(dep_node_index, side_effects); - Ok((dep_node_index, pos)) - }, - ) - .collect::>()?; + (dep_node_index, pos) + }) + .collect(); let interpret_alloc_index = { let mut interpret_alloc_index = Vec::new(); @@ -296,7 +294,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> { let id = encoder.interpret_allocs[idx]; let pos = encoder.position() as u32; interpret_alloc_index.push(pos); - interpret::specialized_encode_alloc_id(&mut encoder, tcx, id)?; + interpret::specialized_encode_alloc_id(&mut encoder, tcx, id); } n = new_n; } @@ -312,23 +310,21 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> { hygiene_encode_context.encode( &mut encoder, - |encoder, index, ctxt_data| -> FileEncodeResult { + |encoder, index, ctxt_data| { let pos = AbsoluteBytePos::new(encoder.position()); - encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data)?; + encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data); syntax_contexts.insert(index, pos); - Ok(()) }, - |encoder, expn_id, data, hash| -> FileEncodeResult { + |encoder, expn_id, data, hash| { if expn_id.krate == LOCAL_CRATE { let pos = AbsoluteBytePos::new(encoder.position()); - encoder.encode_tagged(TAG_EXPN_DATA, data)?; + encoder.encode_tagged(TAG_EXPN_DATA, data); expn_data.insert(hash, pos); } else { foreign_expn_data.insert(hash, expn_id.local_id.as_u32()); } - Ok(()) }, - )?; + ); // `Encode the file footer. let footer_pos = encoder.position() as u64; @@ -343,16 +339,16 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> { expn_data, foreign_expn_data, }, - )?; + ); // Encode the position of the footer as the last 8 bytes of the // file so we know where to look for it. - IntEncodedWithFixedSize(footer_pos).encode(encoder.encoder)?; + IntEncodedWithFixedSize(footer_pos).encode(&mut encoder.encoder); // DO NOT WRITE ANYTHING TO THE ENCODER AFTER THIS POINT! The address // of the footer must be the last thing in the data stream. - Ok(()) + encoder.finish() }) } } @@ -825,7 +821,7 @@ impl OpaqueEncoder for FileEncoder { /// An encoder that can write to the incremental compilation cache. pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> { tcx: TyCtxt<'tcx>, - encoder: &'a mut E, + encoder: E, type_shorthands: FxHashMap, usize>, predicate_shorthands: FxHashMap, usize>, interpret_allocs: FxIndexSet, @@ -836,7 +832,7 @@ pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> { impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E> where - E: 'a + OpaqueEncoder, + E: OpaqueEncoder, { fn source_file_index(&mut self, source_file: Lrc) -> SourceFileIndex { self.file_to_file_index[&(&*source_file as *const SourceFile)] @@ -847,48 +843,44 @@ where /// encode the specified tag, then the given value, then the number of /// bytes taken up by tag and value. On decoding, we can then verify that /// we get the expected tag and read the expected number of bytes. - fn encode_tagged, V: Encodable>( - &mut self, - tag: T, - value: &V, - ) -> Result<(), E::Error> { + fn encode_tagged, V: Encodable>(&mut self, tag: T, value: &V) { let start_pos = self.position(); - tag.encode(self)?; - value.encode(self)?; + tag.encode(self); + value.encode(self); let end_pos = self.position(); - ((end_pos - start_pos) as u64).encode(self) + ((end_pos - start_pos) as u64).encode(self); } } impl<'a, 'tcx, E> Encodable> for SyntaxContext where - E: 'a + OpaqueEncoder, + E: OpaqueEncoder, { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> { - rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s) + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { + rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s); } } impl<'a, 'tcx, E> Encodable> for ExpnId where - E: 'a + OpaqueEncoder, + E: OpaqueEncoder, { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { s.hygiene_context.schedule_expn_data_for_encoding(*self); - self.expn_hash().encode(s) + self.expn_hash().encode(s); } } impl<'a, 'tcx, E> Encodable> for Span where - E: 'a + OpaqueEncoder, + E: OpaqueEncoder, { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { let span_data = self.data_untracked(); - span_data.ctxt.encode(s)?; - span_data.parent.encode(s)?; + span_data.ctxt.encode(s); + span_data.parent.encode(s); if span_data.is_dummy() { return TAG_PARTIAL_SPAN.encode(s); @@ -897,10 +889,10 @@ where if let Some(parent) = span_data.parent { let enclosing = s.tcx.definitions_untracked().def_span(parent).data_untracked(); if enclosing.contains(span_data) { - TAG_RELATIVE_SPAN.encode(s)?; - (span_data.lo - enclosing.lo).to_u32().encode(s)?; - (span_data.hi - enclosing.lo).to_u32().encode(s)?; - return Ok(()); + TAG_RELATIVE_SPAN.encode(s); + (span_data.lo - enclosing.lo).to_u32().encode(s); + (span_data.hi - enclosing.lo).to_u32().encode(s); + return; } } @@ -920,17 +912,17 @@ where let source_file_index = s.source_file_index(file_lo); - TAG_FULL_SPAN.encode(s)?; - source_file_index.encode(s)?; - line_lo.encode(s)?; - col_lo.encode(s)?; - len.encode(s) + TAG_FULL_SPAN.encode(s); + source_file_index.encode(s); + line_lo.encode(s); + col_lo.encode(s); + len.encode(s); } } impl<'a, 'tcx, E> TyEncoder for CacheEncoder<'a, 'tcx, E> where - E: 'a + OpaqueEncoder, + E: OpaqueEncoder, { type I = TyCtxt<'tcx>; const CLEAR_CROSS_CRATE: bool = false; @@ -944,36 +936,36 @@ where fn predicate_shorthands(&mut self) -> &mut FxHashMap, usize> { &mut self.predicate_shorthands } - fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> { + fn encode_alloc_id(&mut self, alloc_id: &interpret::AllocId) { let (index, _) = self.interpret_allocs.insert_full(*alloc_id); - index.encode(self) + index.encode(self); } } impl<'a, 'tcx, E> Encodable> for CrateNum where - E: 'a + OpaqueEncoder, + E: OpaqueEncoder, { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> { - s.tcx.stable_crate_id(*self).encode(s) + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { + s.tcx.stable_crate_id(*self).encode(s); } } impl<'a, 'tcx, E> Encodable> for DefId where - E: 'a + OpaqueEncoder, + E: OpaqueEncoder, { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> { - s.tcx.def_path_hash(*self).encode(s) + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { + s.tcx.def_path_hash(*self).encode(s); } } impl<'a, 'tcx, E> Encodable> for DefIndex where - E: 'a + OpaqueEncoder, + E: OpaqueEncoder, { - fn encode(&self, _: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> { + fn encode(&self, _: &mut CacheEncoder<'a, 'tcx, E>) { bug!("encoding `DefIndex` without context"); } } @@ -981,7 +973,7 @@ where macro_rules! encoder_methods { ($($name:ident($ty:ty);)*) => { #[inline] - $(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> { + $(fn $name(&mut self, value: $ty) { self.encoder.$name(value) })* } @@ -989,9 +981,10 @@ macro_rules! encoder_methods { impl<'a, 'tcx, E> Encoder for CacheEncoder<'a, 'tcx, E> where - E: 'a + OpaqueEncoder, + E: OpaqueEncoder, { - type Error = E::Error; + type Ok = E::Ok; + type Err = E::Err; encoder_methods! { emit_usize(usize); @@ -1015,6 +1008,10 @@ where emit_str(&str); emit_raw_bytes(&[u8]); } + + fn finish(self) -> Result { + self.encoder.finish() + } } // This ensures that the `Encodable::encode` specialization for byte slices @@ -1022,8 +1019,8 @@ where // Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder` // and the encoding traits currently work. impl<'a, 'tcx> Encodable> for [u8] { - fn encode(&self, e: &mut CacheEncoder<'a, 'tcx, FileEncoder>) -> FileEncodeResult { - self.encode(e.encoder) + fn encode(&self, e: &mut CacheEncoder<'a, 'tcx, FileEncoder>) { + self.encode(&mut e.encoder); } } @@ -1031,8 +1028,7 @@ pub fn encode_query_results<'a, 'tcx, CTX, Q>( tcx: CTX, encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>, query_result_index: &mut EncodedDepNodeIndex, -) -> FileEncodeResult -where +) where CTX: QueryContext + 'tcx, Q: super::QueryDescription, Q::Value: Encodable>, @@ -1044,11 +1040,7 @@ where assert!(Q::query_state(tcx).all_inactive()); let cache = Q::query_cache(tcx); - let mut res = Ok(()); cache.iter(&mut |key, value, dep_node| { - if res.is_err() { - return; - } if Q::cache_on_disk(*tcx.dep_context(), &key) { let dep_node = SerializedDepNodeIndex::new(dep_node.index()); @@ -1057,14 +1049,7 @@ where // Encode the type check tables with the `SerializedDepNodeIndex` // as tag. - match encoder.encode_tagged(dep_node, value) { - Ok(()) => {} - Err(e) => { - res = Err(e); - } - } + encoder.encode_tagged(dep_node, value); } }); - - res } diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 634236c0dac75..87aedc6542d8d 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -142,7 +142,7 @@ impl<'tcx> QueryCtxt<'tcx> { self, encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx, opaque::FileEncoder>, query_result_index: &mut on_disk_cache::EncodedDepNodeIndex, - ) -> opaque::FileEncodeResult { + ) { macro_rules! encode_queries { ($($query:ident,)*) => { $( @@ -150,14 +150,12 @@ impl<'tcx> QueryCtxt<'tcx> { self, encoder, query_result_index - )?; + ); )* } } rustc_cached_queries!(encode_queries!); - - Ok(()) } pub fn try_print_query_stack( diff --git a/compiler/rustc_query_system/src/dep_graph/graph.rs b/compiler/rustc_query_system/src/dep_graph/graph.rs index f6d06e4362c53..5a32d7075db06 100644 --- a/compiler/rustc_query_system/src/dep_graph/graph.rs +++ b/compiler/rustc_query_system/src/dep_graph/graph.rs @@ -842,7 +842,7 @@ impl DepGraph { if let Some(data) = &self.data { data.current.encoder.steal().finish(profiler) } else { - Ok(()) + Ok(0) } } diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index 1f8d87a7e913f..7fde9c0119b85 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -20,7 +20,7 @@ use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sync::Lock; use rustc_index::vec::{Idx, IndexVec}; use rustc_serialize::opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize}; -use rustc_serialize::{Decodable, Decoder, Encodable}; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use smallvec::SmallVec; use std::convert::TryInto; @@ -166,7 +166,6 @@ struct EncoderState { encoder: FileEncoder, total_node_count: usize, total_edge_count: usize, - result: FileEncodeResult, stats: Option>>, } @@ -176,7 +175,6 @@ impl EncoderState { encoder, total_edge_count: 0, total_node_count: 0, - result: Ok(()), stats: record_stats.then(FxHashMap::default), } } @@ -208,29 +206,28 @@ impl EncoderState { } let encoder = &mut self.encoder; - if self.result.is_ok() { - self.result = node.encode(encoder); - } + node.encode(encoder); index } fn finish(self, profiler: &SelfProfilerRef) -> FileEncodeResult { - let Self { mut encoder, total_node_count, total_edge_count, result, stats: _ } = self; - let () = result?; + let Self { mut encoder, total_node_count, total_edge_count, stats: _ } = self; let node_count = total_node_count.try_into().unwrap(); let edge_count = total_edge_count.try_into().unwrap(); debug!(?node_count, ?edge_count); debug!("position: {:?}", encoder.position()); - IntEncodedWithFixedSize(node_count).encode(&mut encoder)?; - IntEncodedWithFixedSize(edge_count).encode(&mut encoder)?; + IntEncodedWithFixedSize(node_count).encode(&mut encoder); + IntEncodedWithFixedSize(edge_count).encode(&mut encoder); debug!("position: {:?}", encoder.position()); // Drop the encoder so that nothing is written after the counts. - let result = encoder.flush(); - // FIXME(rylev): we hardcode the dep graph file name so we don't need a dependency on - // rustc_incremental just for that. - profiler.artifact_size("dep_graph", "dep-graph.bin", encoder.position() as u64); + let result = encoder.finish(); + if let Ok(position) = result { + // FIXME(rylev): we hardcode the dep graph file name so we + // don't need a dependency on rustc_incremental just for that. + profiler.artifact_size("dep_graph", "dep-graph.bin", position as u64); + } result } } diff --git a/compiler/rustc_serialize/src/collection_impls.rs b/compiler/rustc_serialize/src/collection_impls.rs index c4541bbcac921..5e53f0b104dfc 100644 --- a/compiler/rustc_serialize/src/collection_impls.rs +++ b/compiler/rustc_serialize/src/collection_impls.rs @@ -10,9 +10,9 @@ use std::sync::Arc; use smallvec::{Array, SmallVec}; impl>> Encodable for SmallVec { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) { let slice: &[A::Item] = self; - slice.encode(s) + slice.encode(s); } } @@ -24,12 +24,11 @@ impl>> Decodable for SmallVec { } impl> Encodable for LinkedList { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_usize(self.len())?; + fn encode(&self, s: &mut S) { + s.emit_usize(self.len()); for e in self.iter() { - e.encode(s)?; + e.encode(s); } - Ok(()) } } @@ -41,12 +40,11 @@ impl> Decodable for LinkedList { } impl> Encodable for VecDeque { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_usize(self.len())?; + fn encode(&self, s: &mut S) { + s.emit_usize(self.len()); for e in self.iter() { - e.encode(s)?; + e.encode(s); } - Ok(()) } } @@ -62,13 +60,12 @@ where K: Encodable + PartialEq + Ord, V: Encodable, { - fn encode(&self, e: &mut S) -> Result<(), S::Error> { - e.emit_usize(self.len())?; + fn encode(&self, e: &mut S) { + e.emit_usize(self.len()); for (key, val) in self.iter() { - key.encode(e)?; - val.encode(e)?; + key.encode(e); + val.encode(e); } - Ok(()) } } @@ -93,12 +90,11 @@ impl Encodable for BTreeSet where T: Encodable + PartialEq + Ord, { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_usize(self.len())?; + fn encode(&self, s: &mut S) { + s.emit_usize(self.len()); for e in self.iter() { - e.encode(s)?; + e.encode(s); } - Ok(()) } } @@ -122,13 +118,12 @@ where V: Encodable, S: BuildHasher, { - fn encode(&self, e: &mut E) -> Result<(), E::Error> { - e.emit_usize(self.len())?; + fn encode(&self, e: &mut E) { + e.emit_usize(self.len()); for (key, val) in self.iter() { - key.encode(e)?; - val.encode(e)?; + key.encode(e); + val.encode(e); } - Ok(()) } } @@ -156,12 +151,11 @@ where T: Encodable + Eq, S: BuildHasher, { - fn encode(&self, s: &mut E) -> Result<(), E::Error> { - s.emit_usize(self.len())?; + fn encode(&self, s: &mut E) { + s.emit_usize(self.len()); for e in self.iter() { - e.encode(s)?; + e.encode(s); } - Ok(()) } } @@ -187,13 +181,12 @@ where V: Encodable, S: BuildHasher, { - fn encode(&self, e: &mut E) -> Result<(), E::Error> { - e.emit_usize(self.len())?; + fn encode(&self, e: &mut E) { + e.emit_usize(self.len()); for (key, val) in self.iter() { - key.encode(e)?; - val.encode(e)?; + key.encode(e); + val.encode(e); } - Ok(()) } } @@ -221,12 +214,11 @@ where T: Encodable + Hash + Eq, S: BuildHasher, { - fn encode(&self, s: &mut E) -> Result<(), E::Error> { - s.emit_usize(self.len())?; + fn encode(&self, s: &mut E) { + s.emit_usize(self.len()); for e in self.iter() { - e.encode(s)?; + e.encode(s); } - Ok(()) } } @@ -247,9 +239,9 @@ where } impl> Encodable for Rc<[T]> { - fn encode(&self, s: &mut E) -> Result<(), E::Error> { + fn encode(&self, s: &mut E) { let slice: &[T] = self; - slice.encode(s) + slice.encode(s); } } @@ -261,9 +253,9 @@ impl> Decodable for Rc<[T]> { } impl> Encodable for Arc<[T]> { - fn encode(&self, s: &mut E) -> Result<(), E::Error> { + fn encode(&self, s: &mut E) { let slice: &[T] = self; - slice.encode(s) + slice.encode(s); } } diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 40b79ba89ef4e..b2dbf937eb75a 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -11,8 +11,6 @@ use std::ptr; // Encoder // ----------------------------------------------------------------------------- -pub type EncodeResult = Result<(), !>; - pub struct Encoder { pub data: Vec, } @@ -22,10 +20,6 @@ impl Encoder { Encoder { data: vec![] } } - pub fn into_inner(self) -> Vec { - self.data - } - #[inline] pub fn position(&self) -> usize { self.data.len() @@ -49,8 +43,6 @@ macro_rules! write_leb128 { let encoded = leb128::$fun(buf, $value); $enc.data.set_len(old_len + encoded.len()); } - - Ok(()) }}; } @@ -62,108 +54,109 @@ macro_rules! write_leb128 { const STR_SENTINEL: u8 = 0xC1; impl serialize::Encoder for Encoder { - type Error = !; + type Ok = Vec; + type Err = !; #[inline] - fn emit_usize(&mut self, v: usize) -> EncodeResult { + fn emit_usize(&mut self, v: usize) { write_leb128!(self, v, usize, write_usize_leb128) } #[inline] - fn emit_u128(&mut self, v: u128) -> EncodeResult { - write_leb128!(self, v, u128, write_u128_leb128) + fn emit_u128(&mut self, v: u128) { + write_leb128!(self, v, u128, write_u128_leb128); } #[inline] - fn emit_u64(&mut self, v: u64) -> EncodeResult { - write_leb128!(self, v, u64, write_u64_leb128) + fn emit_u64(&mut self, v: u64) { + write_leb128!(self, v, u64, write_u64_leb128); } #[inline] - fn emit_u32(&mut self, v: u32) -> EncodeResult { - write_leb128!(self, v, u32, write_u32_leb128) + fn emit_u32(&mut self, v: u32) { + write_leb128!(self, v, u32, write_u32_leb128); } #[inline] - fn emit_u16(&mut self, v: u16) -> EncodeResult { + fn emit_u16(&mut self, v: u16) { self.data.extend_from_slice(&v.to_le_bytes()); - Ok(()) } #[inline] - fn emit_u8(&mut self, v: u8) -> EncodeResult { + fn emit_u8(&mut self, v: u8) { self.data.push(v); - Ok(()) } #[inline] - fn emit_isize(&mut self, v: isize) -> EncodeResult { + fn emit_isize(&mut self, v: isize) { write_leb128!(self, v, isize, write_isize_leb128) } #[inline] - fn emit_i128(&mut self, v: i128) -> EncodeResult { + fn emit_i128(&mut self, v: i128) { write_leb128!(self, v, i128, write_i128_leb128) } #[inline] - fn emit_i64(&mut self, v: i64) -> EncodeResult { + fn emit_i64(&mut self, v: i64) { write_leb128!(self, v, i64, write_i64_leb128) } #[inline] - fn emit_i32(&mut self, v: i32) -> EncodeResult { + fn emit_i32(&mut self, v: i32) { write_leb128!(self, v, i32, write_i32_leb128) } #[inline] - fn emit_i16(&mut self, v: i16) -> EncodeResult { + fn emit_i16(&mut self, v: i16) { self.data.extend_from_slice(&v.to_le_bytes()); - Ok(()) } #[inline] - fn emit_i8(&mut self, v: i8) -> EncodeResult { - self.emit_u8(v as u8) + fn emit_i8(&mut self, v: i8) { + self.emit_u8(v as u8); } #[inline] - fn emit_bool(&mut self, v: bool) -> EncodeResult { - self.emit_u8(if v { 1 } else { 0 }) + fn emit_bool(&mut self, v: bool) { + self.emit_u8(if v { 1 } else { 0 }); } #[inline] - fn emit_f64(&mut self, v: f64) -> EncodeResult { + fn emit_f64(&mut self, v: f64) { let as_u64: u64 = v.to_bits(); - self.emit_u64(as_u64) + self.emit_u64(as_u64); } #[inline] - fn emit_f32(&mut self, v: f32) -> EncodeResult { + fn emit_f32(&mut self, v: f32) { let as_u32: u32 = v.to_bits(); - self.emit_u32(as_u32) + self.emit_u32(as_u32); } #[inline] - fn emit_char(&mut self, v: char) -> EncodeResult { - self.emit_u32(v as u32) + fn emit_char(&mut self, v: char) { + self.emit_u32(v as u32); } #[inline] - fn emit_str(&mut self, v: &str) -> EncodeResult { - self.emit_usize(v.len())?; - self.emit_raw_bytes(v.as_bytes())?; - self.emit_u8(STR_SENTINEL) + fn emit_str(&mut self, v: &str) { + self.emit_usize(v.len()); + self.emit_raw_bytes(v.as_bytes()); + self.emit_u8(STR_SENTINEL); } #[inline] - fn emit_raw_bytes(&mut self, s: &[u8]) -> EncodeResult { + fn emit_raw_bytes(&mut self, s: &[u8]) { self.data.extend_from_slice(s); - Ok(()) + } + + fn finish(self) -> Result { + Ok(self.data) } } -pub type FileEncodeResult = Result<(), io::Error>; +pub type FileEncodeResult = Result; // `FileEncoder` encodes data to file via fixed-size buffer. // @@ -182,6 +175,9 @@ pub struct FileEncoder { buffered: usize, flushed: usize, file: File, + // This is used to implement delayed error handling, as described in the + // comment on `trait Encoder`. + res: Result<(), io::Error>, } impl FileEncoder { @@ -202,7 +198,13 @@ impl FileEncoder { let file = File::create(path)?; - Ok(FileEncoder { buf: Box::new_uninit_slice(capacity), buffered: 0, flushed: 0, file }) + Ok(FileEncoder { + buf: Box::new_uninit_slice(capacity), + buffered: 0, + flushed: 0, + file, + res: Ok(()), + }) } #[inline] @@ -212,7 +214,7 @@ impl FileEncoder { self.flushed + self.buffered } - pub fn flush(&mut self) -> FileEncodeResult { + pub fn flush(&mut self) { // This is basically a copy of `BufWriter::flush`. If `BufWriter` ever // offers a raw buffer access API, we can use it, and remove this. @@ -267,6 +269,12 @@ impl FileEncoder { } } + // If we've already had an error, do nothing. It'll get reported after + // `finish` is called. + if self.res.is_err() { + return; + } + let mut guard = BufGuard::new( unsafe { MaybeUninit::slice_assume_init_mut(&mut self.buf[..self.buffered]) }, &mut self.buffered, @@ -276,18 +284,20 @@ impl FileEncoder { while !guard.done() { match self.file.write(guard.remaining()) { Ok(0) => { - return Err(io::Error::new( + self.res = Err(io::Error::new( io::ErrorKind::WriteZero, "failed to write the buffered data", )); + return; } Ok(n) => guard.consume(n), Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} - Err(e) => return Err(e), + Err(e) => { + self.res = Err(e); + return; + } } } - - Ok(()) } #[inline] @@ -296,14 +306,14 @@ impl FileEncoder { } #[inline] - fn write_one(&mut self, value: u8) -> FileEncodeResult { + fn write_one(&mut self, value: u8) { // We ensure this during `FileEncoder` construction. debug_assert!(self.capacity() >= 1); let mut buffered = self.buffered; if std::intrinsics::unlikely(buffered >= self.capacity()) { - self.flush()?; + self.flush(); buffered = 0; } @@ -314,12 +324,10 @@ impl FileEncoder { } self.buffered = buffered + 1; - - Ok(()) } #[inline] - fn write_all(&mut self, buf: &[u8]) -> FileEncodeResult { + fn write_all(&mut self, buf: &[u8]) { let capacity = self.capacity(); let buf_len = buf.len(); @@ -327,7 +335,7 @@ impl FileEncoder { let mut buffered = self.buffered; if std::intrinsics::unlikely(buf_len > capacity - buffered) { - self.flush()?; + self.flush(); buffered = 0; } @@ -340,16 +348,20 @@ impl FileEncoder { } self.buffered = buffered + buf_len; - - Ok(()) } else { - self.write_all_unbuffered(buf) + self.write_all_unbuffered(buf); } } - fn write_all_unbuffered(&mut self, mut buf: &[u8]) -> FileEncodeResult { + fn write_all_unbuffered(&mut self, mut buf: &[u8]) { + // If we've already had an error, do nothing. It'll get reported after + // `finish` is called. + if self.res.is_err() { + return; + } + if self.buffered > 0 { - self.flush()?; + self.flush(); } // This is basically a copy of `Write::write_all` but also updates our @@ -359,26 +371,30 @@ impl FileEncoder { while !buf.is_empty() { match self.file.write(buf) { Ok(0) => { - return Err(io::Error::new( + self.res = Err(io::Error::new( io::ErrorKind::WriteZero, "failed to write whole buffer", )); + return; } Ok(n) => { buf = &buf[n..]; self.flushed += n; } Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} - Err(e) => return Err(e), + Err(e) => { + self.res = Err(e); + return; + } } } - - Ok(()) } } impl Drop for FileEncoder { fn drop(&mut self) { + // Likely to be a no-op, because `finish` should have been called and + // it also flushes. But do it just in case. let _result = self.flush(); } } @@ -394,7 +410,7 @@ macro_rules! file_encoder_write_leb128 { // This can't overflow. See assertion in `FileEncoder::with_capacity`. if std::intrinsics::unlikely(buffered + MAX_ENCODED_LEN > $enc.capacity()) { - $enc.flush()?; + $enc.flush(); buffered = 0; } @@ -406,106 +422,112 @@ macro_rules! file_encoder_write_leb128 { let encoded = leb128::$fun(buf, $value); $enc.buffered = buffered + encoded.len(); - - Ok(()) }}; } impl serialize::Encoder for FileEncoder { - type Error = io::Error; + type Ok = usize; + type Err = io::Error; #[inline] - fn emit_usize(&mut self, v: usize) -> FileEncodeResult { + fn emit_usize(&mut self, v: usize) { file_encoder_write_leb128!(self, v, usize, write_usize_leb128) } #[inline] - fn emit_u128(&mut self, v: u128) -> FileEncodeResult { + fn emit_u128(&mut self, v: u128) { file_encoder_write_leb128!(self, v, u128, write_u128_leb128) } #[inline] - fn emit_u64(&mut self, v: u64) -> FileEncodeResult { + fn emit_u64(&mut self, v: u64) { file_encoder_write_leb128!(self, v, u64, write_u64_leb128) } #[inline] - fn emit_u32(&mut self, v: u32) -> FileEncodeResult { + fn emit_u32(&mut self, v: u32) { file_encoder_write_leb128!(self, v, u32, write_u32_leb128) } #[inline] - fn emit_u16(&mut self, v: u16) -> FileEncodeResult { - self.write_all(&v.to_le_bytes()) + fn emit_u16(&mut self, v: u16) { + self.write_all(&v.to_le_bytes()); } #[inline] - fn emit_u8(&mut self, v: u8) -> FileEncodeResult { - self.write_one(v) + fn emit_u8(&mut self, v: u8) { + self.write_one(v); } #[inline] - fn emit_isize(&mut self, v: isize) -> FileEncodeResult { + fn emit_isize(&mut self, v: isize) { file_encoder_write_leb128!(self, v, isize, write_isize_leb128) } #[inline] - fn emit_i128(&mut self, v: i128) -> FileEncodeResult { + fn emit_i128(&mut self, v: i128) { file_encoder_write_leb128!(self, v, i128, write_i128_leb128) } #[inline] - fn emit_i64(&mut self, v: i64) -> FileEncodeResult { + fn emit_i64(&mut self, v: i64) { file_encoder_write_leb128!(self, v, i64, write_i64_leb128) } #[inline] - fn emit_i32(&mut self, v: i32) -> FileEncodeResult { + fn emit_i32(&mut self, v: i32) { file_encoder_write_leb128!(self, v, i32, write_i32_leb128) } #[inline] - fn emit_i16(&mut self, v: i16) -> FileEncodeResult { - self.write_all(&v.to_le_bytes()) + fn emit_i16(&mut self, v: i16) { + self.write_all(&v.to_le_bytes()); } #[inline] - fn emit_i8(&mut self, v: i8) -> FileEncodeResult { - self.emit_u8(v as u8) + fn emit_i8(&mut self, v: i8) { + self.emit_u8(v as u8); } #[inline] - fn emit_bool(&mut self, v: bool) -> FileEncodeResult { - self.emit_u8(if v { 1 } else { 0 }) + fn emit_bool(&mut self, v: bool) { + self.emit_u8(if v { 1 } else { 0 }); } #[inline] - fn emit_f64(&mut self, v: f64) -> FileEncodeResult { + fn emit_f64(&mut self, v: f64) { let as_u64: u64 = v.to_bits(); - self.emit_u64(as_u64) + self.emit_u64(as_u64); } #[inline] - fn emit_f32(&mut self, v: f32) -> FileEncodeResult { + fn emit_f32(&mut self, v: f32) { let as_u32: u32 = v.to_bits(); - self.emit_u32(as_u32) + self.emit_u32(as_u32); } #[inline] - fn emit_char(&mut self, v: char) -> FileEncodeResult { - self.emit_u32(v as u32) + fn emit_char(&mut self, v: char) { + self.emit_u32(v as u32); } #[inline] - fn emit_str(&mut self, v: &str) -> FileEncodeResult { - self.emit_usize(v.len())?; - self.emit_raw_bytes(v.as_bytes())?; - self.emit_u8(STR_SENTINEL) + fn emit_str(&mut self, v: &str) { + self.emit_usize(v.len()); + self.emit_raw_bytes(v.as_bytes()); + self.emit_u8(STR_SENTINEL); } #[inline] - fn emit_raw_bytes(&mut self, s: &[u8]) -> FileEncodeResult { - self.write_all(s) + fn emit_raw_bytes(&mut self, s: &[u8]) { + self.write_all(s); + } + + fn finish(mut self) -> Result { + self.flush(); + + let res = std::mem::replace(&mut self.res, Ok(())); + res.map(|()| self.position()) } } @@ -667,16 +689,16 @@ impl<'a> serialize::Decoder for Decoder<'a> { // Specialize encoding byte slices. This specialization also applies to encoding `Vec`s, etc., // since the default implementations call `encode` on their slices internally. impl serialize::Encodable for [u8] { - fn encode(&self, e: &mut Encoder) -> EncodeResult { - serialize::Encoder::emit_usize(e, self.len())?; - e.emit_raw_bytes(self) + fn encode(&self, e: &mut Encoder) { + serialize::Encoder::emit_usize(e, self.len()); + e.emit_raw_bytes(self); } } impl serialize::Encodable for [u8] { - fn encode(&self, e: &mut FileEncoder) -> FileEncodeResult { - serialize::Encoder::emit_usize(e, self.len())?; - e.emit_raw_bytes(self) + fn encode(&self, e: &mut FileEncoder) { + serialize::Encoder::emit_usize(e, self.len()); + e.emit_raw_bytes(self); } } @@ -698,23 +720,21 @@ impl IntEncodedWithFixedSize { impl serialize::Encodable for IntEncodedWithFixedSize { #[inline] - fn encode(&self, e: &mut Encoder) -> EncodeResult { + fn encode(&self, e: &mut Encoder) { let _start_pos = e.position(); - e.emit_raw_bytes(&self.0.to_le_bytes())?; + e.emit_raw_bytes(&self.0.to_le_bytes()); let _end_pos = e.position(); debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE); - Ok(()) } } impl serialize::Encodable for IntEncodedWithFixedSize { #[inline] - fn encode(&self, e: &mut FileEncoder) -> FileEncodeResult { + fn encode(&self, e: &mut FileEncoder) { let _start_pos = e.position(); - e.emit_raw_bytes(&self.0.to_le_bytes())?; + e.emit_raw_bytes(&self.0.to_le_bytes()); let _end_pos = e.position(); debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE); - Ok(()) } } diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index 817a0c9dcb17b..98bb18581f517 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -11,36 +11,47 @@ use std::path; use std::rc::Rc; use std::sync::Arc; +/// A note about error handling. +/// +/// Encoders may be fallible, but in practice failure is rare and there are so +/// many nested calls that typical Rust error handling (via `Result` and `?`) +/// is pervasive and has non-trivial cost. Instead, impls of this trait must +/// implement a delayed error handling strategy. If a failure occurs, they +/// should record this internally, and all subsequent encoding operations can +/// be processed or ignored, whichever is appropriate. Then when `finish()` is +/// called, an error result should be returned to indicate the failure. If no +/// failures occurred, then `finish()` should return a success result. pub trait Encoder { - type Error; + type Ok; + type Err; // Primitive types: - fn emit_usize(&mut self, v: usize) -> Result<(), Self::Error>; - fn emit_u128(&mut self, v: u128) -> Result<(), Self::Error>; - fn emit_u64(&mut self, v: u64) -> Result<(), Self::Error>; - fn emit_u32(&mut self, v: u32) -> Result<(), Self::Error>; - fn emit_u16(&mut self, v: u16) -> Result<(), Self::Error>; - fn emit_u8(&mut self, v: u8) -> Result<(), Self::Error>; - fn emit_isize(&mut self, v: isize) -> Result<(), Self::Error>; - fn emit_i128(&mut self, v: i128) -> Result<(), Self::Error>; - fn emit_i64(&mut self, v: i64) -> Result<(), Self::Error>; - fn emit_i32(&mut self, v: i32) -> Result<(), Self::Error>; - fn emit_i16(&mut self, v: i16) -> Result<(), Self::Error>; - fn emit_i8(&mut self, v: i8) -> Result<(), Self::Error>; - fn emit_bool(&mut self, v: bool) -> Result<(), Self::Error>; - fn emit_f64(&mut self, v: f64) -> Result<(), Self::Error>; - fn emit_f32(&mut self, v: f32) -> Result<(), Self::Error>; - fn emit_char(&mut self, v: char) -> Result<(), Self::Error>; - fn emit_str(&mut self, v: &str) -> Result<(), Self::Error>; - fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error>; + fn emit_usize(&mut self, v: usize); + fn emit_u128(&mut self, v: u128); + fn emit_u64(&mut self, v: u64); + fn emit_u32(&mut self, v: u32); + fn emit_u16(&mut self, v: u16); + fn emit_u8(&mut self, v: u8); + fn emit_isize(&mut self, v: isize); + fn emit_i128(&mut self, v: i128); + fn emit_i64(&mut self, v: i64); + fn emit_i32(&mut self, v: i32); + fn emit_i16(&mut self, v: i16); + fn emit_i8(&mut self, v: i8); + fn emit_bool(&mut self, v: bool); + fn emit_f64(&mut self, v: f64); + fn emit_f32(&mut self, v: f32); + fn emit_char(&mut self, v: char); + fn emit_str(&mut self, v: &str); + fn emit_raw_bytes(&mut self, s: &[u8]); // Convenience for the derive macro: - fn emit_enum_variant(&mut self, v_id: usize, f: F) -> Result<(), Self::Error> + fn emit_enum_variant(&mut self, v_id: usize, f: F) where - F: FnOnce(&mut Self) -> Result<(), Self::Error>, + F: FnOnce(&mut Self), { - self.emit_usize(v_id)?; - f(self) + self.emit_usize(v_id); + f(self); } // We put the field index in a const generic to allow the emit_usize to be @@ -50,9 +61,12 @@ pub trait Encoder { // optimization that would otherwise be necessary here, likely due to the // multiple levels of inlining and const-prop that are needed. #[inline] - fn emit_fieldless_enum_variant(&mut self) -> Result<(), Self::Error> { + fn emit_fieldless_enum_variant(&mut self) { self.emit_usize(ID) } + + // Consume the encoder, getting the result. + fn finish(self) -> Result; } // Note: all the methods in this trait are infallible, which may be surprising. @@ -95,7 +109,7 @@ pub trait Decoder { /// * `TyEncodable` should be used for types that are only serialized in crate /// metadata or the incremental cache. This is most types in `rustc_middle`. pub trait Encodable { - fn encode(&self, s: &mut S) -> Result<(), S::Error>; + fn encode(&self, s: &mut S); } /// Trait for types that can be deserialized @@ -117,8 +131,8 @@ macro_rules! direct_serialize_impls { ($($ty:ident $emit_method:ident $read_method:ident),*) => { $( impl Encodable for $ty { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.$emit_method(*self) + fn encode(&self, s: &mut S) { + s.$emit_method(*self); } } @@ -138,12 +152,14 @@ direct_serialize_impls! { u32 emit_u32 read_u32, u64 emit_u64 read_u64, u128 emit_u128 read_u128, + isize emit_isize read_isize, i8 emit_i8 read_i8, i16 emit_i16 read_i16, i32 emit_i32 read_i32, i64 emit_i64 read_i64, i128 emit_i128 read_i128, + f32 emit_f32 read_f32, f64 emit_f64 read_f64, bool emit_bool read_bool, @@ -154,14 +170,14 @@ impl Encodable for &T where T: Encodable, { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) { (**self).encode(s) } } impl Encodable for ! { - fn encode(&self, _s: &mut S) -> Result<(), S::Error> { - unreachable!() + fn encode(&self, _s: &mut S) { + unreachable!(); } } @@ -172,8 +188,8 @@ impl Decodable for ! { } impl Encodable for ::std::num::NonZeroU32 { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_u32(self.get()) + fn encode(&self, s: &mut S) { + s.emit_u32(self.get()); } } @@ -184,14 +200,14 @@ impl Decodable for ::std::num::NonZeroU32 { } impl Encodable for str { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_str(self) + fn encode(&self, s: &mut S) { + s.emit_str(self); } } impl Encodable for String { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_str(&self[..]) + fn encode(&self, s: &mut S) { + s.emit_str(&self[..]); } } @@ -202,9 +218,7 @@ impl Decodable for String { } impl Encodable for () { - fn encode(&self, _s: &mut S) -> Result<(), S::Error> { - Ok(()) - } + fn encode(&self, _s: &mut S) {} } impl Decodable for () { @@ -212,9 +226,7 @@ impl Decodable for () { } impl Encodable for PhantomData { - fn encode(&self, _s: &mut S) -> Result<(), S::Error> { - Ok(()) - } + fn encode(&self, _s: &mut S) {} } impl Decodable for PhantomData { @@ -231,8 +243,8 @@ impl> Decodable for Box<[T]> { } impl> Encodable for Rc { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - (**self).encode(s) + fn encode(&self, s: &mut S) { + (**self).encode(s); } } @@ -243,19 +255,18 @@ impl> Decodable for Rc { } impl> Encodable for [T] { - default fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_usize(self.len())?; + default fn encode(&self, s: &mut S) { + s.emit_usize(self.len()); for e in self.iter() { - e.encode(s)? + e.encode(s); } - Ok(()) } } impl> Encodable for Vec { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) { let slice: &[T] = self; - slice.encode(s) + slice.encode(s); } } @@ -277,9 +288,9 @@ impl> Decodable for Vec { } impl, const N: usize> Encodable for [T; N] { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) { let slice: &[T] = self; - slice.encode(s) + slice.encode(s); } } @@ -299,9 +310,9 @@ impl<'a, S: Encoder, T: Encodable> Encodable for Cow<'a, [T]> where [T]: ToOwned>, { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) { let slice: &[T] = self; - slice.encode(s) + slice.encode(s); } } @@ -316,7 +327,7 @@ where } impl<'a, S: Encoder> Encodable for Cow<'a, str> { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) { let val: &str = self; val.encode(s) } @@ -330,9 +341,9 @@ impl<'a, D: Decoder> Decodable for Cow<'a, str> { } impl> Encodable for Option { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) { match *self { - None => s.emit_enum_variant(0, |_| Ok(())), + None => s.emit_enum_variant(0, |_| {}), Some(ref v) => s.emit_enum_variant(1, |s| v.encode(s)), } } @@ -349,7 +360,7 @@ impl> Decodable for Option { } impl, T2: Encodable> Encodable for Result { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) { match *self { Ok(ref v) => s.emit_enum_variant(0, |s| v.encode(s)), Err(ref v) => s.emit_enum_variant(1, |s| v.encode(s)), @@ -381,10 +392,9 @@ macro_rules! tuple { } impl),+> Encodable for ($($name,)+) { #[allow(non_snake_case)] - fn encode(&self, s: &mut S) -> Result<(), S::Error> { + fn encode(&self, s: &mut S) { let ($(ref $name,)+) = *self; - $($name.encode(s)?;)+ - Ok(()) + $($name.encode(s);)+ } } peel! { $($name,)+ } @@ -394,14 +404,14 @@ macro_rules! tuple { tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, } impl Encodable for path::Path { - fn encode(&self, e: &mut S) -> Result<(), S::Error> { - self.to_str().unwrap().encode(e) + fn encode(&self, e: &mut S) { + self.to_str().unwrap().encode(e); } } impl Encodable for path::PathBuf { - fn encode(&self, e: &mut S) -> Result<(), S::Error> { - path::Path::encode(self, e) + fn encode(&self, e: &mut S) { + path::Path::encode(self, e); } } @@ -413,8 +423,8 @@ impl Decodable for path::PathBuf { } impl + Copy> Encodable for Cell { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - self.get().encode(s) + fn encode(&self, s: &mut S) { + self.get().encode(s); } } @@ -430,8 +440,8 @@ impl + Copy> Decodable for Cell { // from `encode` when `try_borrow` returns `None`. impl> Encodable for RefCell { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - self.borrow().encode(s) + fn encode(&self, s: &mut S) { + self.borrow().encode(s); } } @@ -442,8 +452,8 @@ impl> Decodable for RefCell { } impl> Encodable for Arc { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - (**self).encode(s) + fn encode(&self, s: &mut S) { + (**self).encode(s); } } @@ -454,8 +464,8 @@ impl> Decodable for Arc { } impl> Encodable for Box { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - (**self).encode(s) + fn encode(&self, s: &mut S) { + (**self).encode(s); } } impl> Decodable for Box { diff --git a/compiler/rustc_serialize/tests/opaque.rs b/compiler/rustc_serialize/tests/opaque.rs index ec3c34f8bdb5d..703b7f5e7a5f4 100644 --- a/compiler/rustc_serialize/tests/opaque.rs +++ b/compiler/rustc_serialize/tests/opaque.rs @@ -2,7 +2,7 @@ use rustc_macros::{Decodable, Encodable}; use rustc_serialize::opaque::{Decoder, Encoder}; -use rustc_serialize::{Decodable, Encodable}; +use rustc_serialize::{Decodable, Encodable, Encoder as EncoderTrait}; use std::fmt::Debug; #[derive(PartialEq, Clone, Debug, Encodable, Decodable)] @@ -32,12 +32,11 @@ fn check_round_trip + for<'a> Decodable> + Par values: Vec, ) { let mut encoder = Encoder::new(); - for value in &values { - Encodable::encode(value, &mut encoder).unwrap(); + Encodable::encode(value, &mut encoder); } - let data = encoder.into_inner(); + let data = encoder.finish().unwrap(); let mut decoder = Decoder::new(&data[..], 0); for value in values { diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index 2bd0880a7c425..a1533fe46b3ef 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -41,8 +41,8 @@ impl fmt::Display for CrateNum { /// As a local identifier, a `CrateNum` is only meaningful within its context, e.g. within a tcx. /// Therefore, make sure to include the context when encode a `CrateNum`. impl Encodable for CrateNum { - default fn encode(&self, s: &mut E) -> Result<(), E::Error> { - s.emit_u32(self.as_u32()) + default fn encode(&self, s: &mut E) { + s.emit_u32(self.as_u32()); } } @@ -203,7 +203,7 @@ rustc_index::newtype_index! { } impl Encodable for DefIndex { - default fn encode(&self, _: &mut E) -> Result<(), E::Error> { + default fn encode(&self, _: &mut E) { panic!("cannot encode `DefIndex` with `{}`", std::any::type_name::()); } } @@ -306,9 +306,9 @@ impl DefId { } impl Encodable for DefId { - default fn encode(&self, s: &mut E) -> Result<(), E::Error> { - self.krate.encode(s)?; - self.index.encode(s) + default fn encode(&self, s: &mut E) { + self.krate.encode(s); + self.index.encode(s); } } @@ -382,8 +382,8 @@ impl fmt::Debug for LocalDefId { } impl Encodable for LocalDefId { - fn encode(&self, s: &mut E) -> Result<(), E::Error> { - self.to_def_id().encode(s) + fn encode(&self, s: &mut E) { + self.to_def_id().encode(s); } } diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 59f2badbabbf6..955db72157c6a 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -1189,12 +1189,12 @@ impl HygieneEncodeContext { } } - pub fn encode( + pub fn encode( &self, encoder: &mut T, - mut encode_ctxt: impl FnMut(&mut T, u32, &SyntaxContextData) -> Result<(), R>, - mut encode_expn: impl FnMut(&mut T, ExpnId, &ExpnData, ExpnHash) -> Result<(), R>, - ) -> Result<(), R> { + mut encode_ctxt: impl FnMut(&mut T, u32, &SyntaxContextData), + mut encode_expn: impl FnMut(&mut T, ExpnId, &ExpnData, ExpnHash), + ) { // When we serialize a `SyntaxContextData`, we may end up serializing // a `SyntaxContext` that we haven't seen before while !self.latest_ctxts.lock().is_empty() || !self.latest_expns.lock().is_empty() { @@ -1213,22 +1213,19 @@ impl HygieneEncodeContext { // order for_all_ctxts_in(latest_ctxts.into_iter(), |index, ctxt, data| { if self.serialized_ctxts.lock().insert(ctxt) { - encode_ctxt(encoder, index, data)?; + encode_ctxt(encoder, index, data); } - Ok(()) - })?; + }); let latest_expns = { std::mem::take(&mut *self.latest_expns.lock()) }; for_all_expns_in(latest_expns.into_iter(), |expn, data, hash| { if self.serialized_expns.lock().insert(expn) { - encode_expn(encoder, expn, data, hash)?; + encode_expn(encoder, expn, data, hash); } - Ok(()) - })?; + }); } debug!("encode_hygiene: Done serializing SyntaxContextData"); - Ok(()) } } @@ -1378,40 +1375,38 @@ pub fn decode_syntax_context SyntaxContext new_ctxt } -fn for_all_ctxts_in Result<(), E>>( +fn for_all_ctxts_in( ctxts: impl Iterator, mut f: F, -) -> Result<(), E> { +) { let all_data: Vec<_> = HygieneData::with(|data| { ctxts.map(|ctxt| (ctxt, data.syntax_context_data[ctxt.0 as usize].clone())).collect() }); for (ctxt, data) in all_data.into_iter() { - f(ctxt.0, ctxt, &data)?; + f(ctxt.0, ctxt, &data); } - Ok(()) } -fn for_all_expns_in( +fn for_all_expns_in( expns: impl Iterator, - mut f: impl FnMut(ExpnId, &ExpnData, ExpnHash) -> Result<(), E>, -) -> Result<(), E> { + mut f: impl FnMut(ExpnId, &ExpnData, ExpnHash), +) { let all_data: Vec<_> = HygieneData::with(|data| { expns.map(|expn| (expn, data.expn_data(expn).clone(), data.expn_hash(expn))).collect() }); for (expn, data, hash) in all_data.into_iter() { - f(expn, &data, hash)?; + f(expn, &data, hash); } - Ok(()) } impl Encodable for LocalExpnId { - fn encode(&self, e: &mut E) -> Result<(), E::Error> { - self.to_expn_id().encode(e) + fn encode(&self, e: &mut E) { + self.to_expn_id().encode(e); } } impl Encodable for ExpnId { - default fn encode(&self, _: &mut E) -> Result<(), E::Error> { + default fn encode(&self, _: &mut E) { panic!("cannot encode `ExpnId` with `{}`", std::any::type_name::()); } } @@ -1432,15 +1427,15 @@ pub fn raw_encode_syntax_context( ctxt: SyntaxContext, context: &HygieneEncodeContext, e: &mut E, -) -> Result<(), E::Error> { +) { if !context.serialized_ctxts.lock().contains(&ctxt) { context.latest_ctxts.lock().insert(ctxt); } - ctxt.0.encode(e) + ctxt.0.encode(e); } impl Encodable for SyntaxContext { - default fn encode(&self, _: &mut E) -> Result<(), E::Error> { + default fn encode(&self, _: &mut E) { panic!("cannot encode `SyntaxContext` with `{}`", std::any::type_name::()); } } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index ae0228d6ea0cd..7f227217e3c2f 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -194,12 +194,10 @@ impl Hash for RealFileName { // This is functionally identical to #[derive(Encodable)], with the exception of // an added assert statement impl Encodable for RealFileName { - fn encode(&self, encoder: &mut S) -> Result<(), S::Error> { + fn encode(&self, encoder: &mut S) { match *self { RealFileName::LocalPath(ref local_path) => encoder.emit_enum_variant(0, |encoder| { - Ok({ - local_path.encode(encoder)?; - }) + local_path.encode(encoder); }), RealFileName::Remapped { ref local_path, ref virtual_name } => encoder @@ -207,9 +205,8 @@ impl Encodable for RealFileName { // For privacy and build reproducibility, we must not embed host-dependant path in artifacts // if they have been remapped by --remap-path-prefix assert!(local_path.is_none()); - local_path.encode(encoder)?; - virtual_name.encode(encoder)?; - Ok(()) + local_path.encode(encoder); + virtual_name.encode(encoder); }), } } @@ -946,10 +943,10 @@ impl Default for Span { } impl Encodable for Span { - default fn encode(&self, s: &mut E) -> Result<(), E::Error> { + default fn encode(&self, s: &mut E) { let span = self.data(); - span.lo.encode(s)?; - span.hi.encode(s) + span.lo.encode(s); + span.hi.encode(s); } } impl Decodable for Span { @@ -1297,17 +1294,17 @@ pub struct SourceFile { } impl Encodable for SourceFile { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - self.name.encode(s)?; - self.src_hash.encode(s)?; - self.start_pos.encode(s)?; - self.end_pos.encode(s)?; + fn encode(&self, s: &mut S) { + self.name.encode(s); + self.src_hash.encode(s); + self.start_pos.encode(s); + self.end_pos.encode(s); // We are always in `Lines` form by the time we reach here. assert!(self.lines.borrow().is_lines()); self.lines(|lines| { // Store the length. - s.emit_u32(lines.len() as u32)?; + s.emit_u32(lines.len() as u32); // Compute and store the difference list. if lines.len() != 0 { @@ -1329,10 +1326,10 @@ impl Encodable for SourceFile { }; // Encode the number of bytes used per diff. - s.emit_u8(bytes_per_diff as u8)?; + s.emit_u8(bytes_per_diff as u8); // Encode the first element. - lines[0].encode(s)?; + lines[0].encode(s); // Encode the difference list. let diff_iter = lines.array_windows().map(|&[fst, snd]| snd - fst); @@ -1359,16 +1356,15 @@ impl Encodable for SourceFile { } _ => unreachable!(), } - s.emit_raw_bytes(&raw_diffs)?; + s.emit_raw_bytes(&raw_diffs); } - Ok(()) - })?; + }); - self.multibyte_chars.encode(s)?; - self.non_narrow_chars.encode(s)?; - self.name_hash.encode(s)?; - self.normalized_pos.encode(s)?; - self.cnum.encode(s) + self.multibyte_chars.encode(s); + self.non_narrow_chars.encode(s); + self.name_hash.encode(s); + self.normalized_pos.encode(s); + self.cnum.encode(s); } } @@ -1916,8 +1912,8 @@ impl_pos! { } impl Encodable for BytePos { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_u32(self.0) + fn encode(&self, s: &mut S) { + s.emit_u32(self.0); } } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index bcaf53639cc63..7b0fa65e8086b 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1802,8 +1802,8 @@ impl fmt::Display for Symbol { } impl Encodable for Symbol { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_str(self.as_str()) + fn encode(&self, s: &mut S) { + s.emit_str(self.as_str()); } } diff --git a/compiler/rustc_type_ir/src/codec.rs b/compiler/rustc_type_ir/src/codec.rs index 09f781fae75bd..6a9ea790a3020 100644 --- a/compiler/rustc_type_ir/src/codec.rs +++ b/compiler/rustc_type_ir/src/codec.rs @@ -31,10 +31,7 @@ pub trait TyEncoder: Encoder { fn predicate_shorthands( &mut self, ) -> &mut FxHashMap<::PredicateKind, usize>; - fn encode_alloc_id( - &mut self, - alloc_id: &::AllocId, - ) -> Result<(), Self::Error>; + fn encode_alloc_id(&mut self, alloc_id: &::AllocId); } pub trait TyDecoder: Decoder { diff --git a/compiler/rustc_type_ir/src/sty.rs b/compiler/rustc_type_ir/src/sty.rs index 22b5b475b2cac..a6a0d02c8ba99 100644 --- a/compiler/rustc_type_ir/src/sty.rs +++ b/compiler/rustc_type_ir/src/sty.rs @@ -715,115 +715,92 @@ where I::PredicateKind: Encodable, I::AllocId: Encodable, { - fn encode(&self, e: &mut E) -> Result<(), ::Error> { + fn encode(&self, e: &mut E) { let disc = discriminant(self); match self { - Bool => e.emit_enum_variant(disc, |_| Ok(())), - Char => e.emit_enum_variant(disc, |_| Ok(())), + Bool => e.emit_enum_variant(disc, |_| {}), + Char => e.emit_enum_variant(disc, |_| {}), Int(i) => e.emit_enum_variant(disc, |e| { - i.encode(e)?; - Ok(()) + i.encode(e); }), Uint(u) => e.emit_enum_variant(disc, |e| { - u.encode(e)?; - Ok(()) + u.encode(e); }), Float(f) => e.emit_enum_variant(disc, |e| { - f.encode(e)?; - Ok(()) + f.encode(e); }), Adt(adt, substs) => e.emit_enum_variant(disc, |e| { - adt.encode(e)?; - substs.encode(e)?; - Ok(()) + adt.encode(e); + substs.encode(e); }), Foreign(def_id) => e.emit_enum_variant(disc, |e| { - def_id.encode(e)?; - Ok(()) + def_id.encode(e); }), - Str => e.emit_enum_variant(disc, |_| Ok(())), + Str => e.emit_enum_variant(disc, |_| {}), Array(t, c) => e.emit_enum_variant(disc, |e| { - t.encode(e)?; - c.encode(e)?; - Ok(()) + t.encode(e); + c.encode(e); }), Slice(t) => e.emit_enum_variant(disc, |e| { - t.encode(e)?; - Ok(()) + t.encode(e); }), RawPtr(tam) => e.emit_enum_variant(disc, |e| { - tam.encode(e)?; - Ok(()) + tam.encode(e); }), Ref(r, t, m) => e.emit_enum_variant(disc, |e| { - r.encode(e)?; - t.encode(e)?; - m.encode(e)?; - Ok(()) + r.encode(e); + t.encode(e); + m.encode(e); }), FnDef(def_id, substs) => e.emit_enum_variant(disc, |e| { - def_id.encode(e)?; - substs.encode(e)?; - Ok(()) + def_id.encode(e); + substs.encode(e); }), FnPtr(polyfnsig) => e.emit_enum_variant(disc, |e| { - polyfnsig.encode(e)?; - Ok(()) + polyfnsig.encode(e); }), Dynamic(l, r) => e.emit_enum_variant(disc, |e| { - l.encode(e)?; - r.encode(e)?; - Ok(()) + l.encode(e); + r.encode(e); }), Closure(def_id, substs) => e.emit_enum_variant(disc, |e| { - def_id.encode(e)?; - substs.encode(e)?; - Ok(()) + def_id.encode(e); + substs.encode(e); }), Generator(def_id, substs, m) => e.emit_enum_variant(disc, |e| { - def_id.encode(e)?; - substs.encode(e)?; - m.encode(e)?; - Ok(()) + def_id.encode(e); + substs.encode(e); + m.encode(e); }), GeneratorWitness(b) => e.emit_enum_variant(disc, |e| { - b.encode(e)?; - Ok(()) + b.encode(e); }), - Never => e.emit_enum_variant(disc, |_| Ok(())), + Never => e.emit_enum_variant(disc, |_| {}), Tuple(substs) => e.emit_enum_variant(disc, |e| { - substs.encode(e)?; - Ok(()) + substs.encode(e); }), Projection(p) => e.emit_enum_variant(disc, |e| { - p.encode(e)?; - Ok(()) + p.encode(e); }), Opaque(def_id, substs) => e.emit_enum_variant(disc, |e| { - def_id.encode(e)?; - substs.encode(e)?; - Ok(()) + def_id.encode(e); + substs.encode(e); }), Param(p) => e.emit_enum_variant(disc, |e| { - p.encode(e)?; - Ok(()) + p.encode(e); }), Bound(d, b) => e.emit_enum_variant(disc, |e| { - d.encode(e)?; - b.encode(e)?; - Ok(()) + d.encode(e); + b.encode(e); }), Placeholder(p) => e.emit_enum_variant(disc, |e| { - p.encode(e)?; - Ok(()) + p.encode(e); }), Infer(i) => e.emit_enum_variant(disc, |e| { - i.encode(e)?; - Ok(()) + i.encode(e); }), Error(d) => e.emit_enum_variant(disc, |e| { - d.encode(e)?; - Ok(()) + d.encode(e); }), } } diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 0fa492af1ad0a..242f926967c93 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -18,7 +18,7 @@ use rustc_middle::hir::nested_filter; use rustc_middle::ty::{self, TyCtxt}; use rustc_serialize::{ opaque::{Decoder, FileEncoder}, - Decodable, Encodable, + Decodable, Encodable, Encoder, }; use rustc_session::getopts; use rustc_span::{ @@ -314,8 +314,8 @@ pub(crate) fn run( // Save output to provided path let mut encoder = FileEncoder::new(options.output_path).map_err(|e| e.to_string())?; - calls.encode(&mut encoder).map_err(|e| e.to_string())?; - encoder.flush().map_err(|e| e.to_string())?; + calls.encode(&mut encoder); + encoder.finish().map_err(|e| e.to_string())?; Ok(()) }; diff --git a/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs b/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs index 678ba18bf045d..a09deeec4f182 100644 --- a/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs +++ b/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs @@ -8,7 +8,7 @@ extern crate rustc_serialize; use rustc_macros::{Decodable, Encodable}; use rustc_serialize::opaque; -use rustc_serialize::{Decodable, Encodable}; +use rustc_serialize::{Decodable, Encodable, Encoder}; #[derive(Encodable, Decodable)] struct A { @@ -17,9 +17,13 @@ struct A { fn main() { let obj = A { foo: Box::new([true, false]) }; - let mut encoder = opaque::Encoder::new(vec![]); - obj.encode(&mut encoder).unwrap(); - let mut decoder = opaque::Decoder::new(&encoder.data, 0); + + let mut encoder = opaque::Encoder::new(); + obj.encode(&mut encoder); + let data = encoder.finish().unwrap(); + + let mut decoder = opaque::Decoder::new(&data, 0); let obj2 = A::decode(&mut decoder); + assert_eq!(obj.foo, obj2.foo); } diff --git a/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs b/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs index 5cc5c41364a66..9b6fb0e580621 100644 --- a/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs +++ b/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs @@ -10,7 +10,7 @@ extern crate rustc_serialize; use rustc_macros::{Decodable, Encodable}; use rustc_serialize::opaque; -use rustc_serialize::{Decodable, Encodable}; +use rustc_serialize::{Decodable, Encodable, Encoder}; use std::cell::{Cell, RefCell}; #[derive(Encodable, Decodable)] @@ -26,10 +26,14 @@ struct B { fn main() { let obj = B { foo: Cell::new(true), bar: RefCell::new(A { baz: 2 }) }; - let mut encoder = opaque::Encoder::new(vec![]); - obj.encode(&mut encoder).unwrap(); - let mut decoder = opaque::Decoder::new(&encoder.data, 0); + + let mut encoder = opaque::Encoder::new(); + obj.encode(&mut encoder); + let data = encoder.finish().unwrap(); + + let mut decoder = opaque::Decoder::new(&data, 0); let obj2 = B::decode(&mut decoder); + assert_eq!(obj.foo.get(), obj2.foo.get()); assert_eq!(obj.bar.borrow().baz, obj2.bar.borrow().baz); } diff --git a/src/test/ui-fulldeps/issue-11881.rs b/src/test/ui-fulldeps/issue-11881.rs index 9641470a68ba2..f6360db9b5f44 100644 --- a/src/test/ui-fulldeps/issue-11881.rs +++ b/src/test/ui-fulldeps/issue-11881.rs @@ -75,6 +75,7 @@ enum WireProtocol { fn encode_json Encodable>>(val: &T, wr: &mut Cursor>) { write!(wr, "{}", as_json(val)); } + fn encode_opaque>(val: &T, wr: Vec) { let mut encoder = OpaqueEncoder(wr); val.encode(&mut encoder); diff --git a/src/test/ui-fulldeps/issue-14021.rs b/src/test/ui-fulldeps/issue-14021.rs index f7e0043f52168..4241456367e46 100644 --- a/src/test/ui-fulldeps/issue-14021.rs +++ b/src/test/ui-fulldeps/issue-14021.rs @@ -9,16 +9,20 @@ extern crate rustc_serialize; use rustc_macros::{Decodable, Encodable}; use rustc_serialize::opaque; -use rustc_serialize::{Decodable, Encodable}; +use rustc_serialize::{Decodable, Encodable, Encoder}; #[derive(Encodable, Decodable, PartialEq, Debug)] struct UnitLikeStruct; pub fn main() { let obj = UnitLikeStruct; - let mut encoder = opaque::Encoder::new(vec![]); - obj.encode(&mut encoder).unwrap(); - let mut decoder = opaque::Decoder::new(&encoder.data, 0); + + let mut encoder = opaque::Encoder::new(); + obj.encode(&mut encoder); + let data = encoder.finish().unwrap(); + + let mut decoder = opaque::Decoder::new(&data, 0); let obj2 = UnitLikeStruct::decode(&mut decoder); + assert_eq!(obj, obj2); } From dc08bc51f2c58a0f5f815a07f9bb3d671153b5a1 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 8 Jun 2022 07:26:35 +1000 Subject: [PATCH 4/5] Move `finish` out of the `Encoder` trait. This simplifies things, but requires making `CacheEncoder` non-generic. --- compiler/rustc_codegen_ssa/src/lib.rs | 2 +- .../rustc_incremental/src/persist/save.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 9 +- .../rustc_query_impl/src/on_disk_cache.rs | 92 ++++++------------- compiler/rustc_query_impl/src/plumbing.rs | 3 +- .../src/dep_graph/serialized.rs | 2 +- compiler/rustc_serialize/src/opaque.rs | 28 +++--- compiler/rustc_serialize/src/serialize.rs | 12 +-- compiler/rustc_serialize/tests/opaque.rs | 4 +- src/librustdoc/scrape_examples.rs | 2 +- .../deriving-encodable-decodable-box.rs | 2 +- ...riving-encodable-decodable-cell-refcell.rs | 2 +- src/test/ui-fulldeps/issue-14021.rs | 2 +- 13 files changed, 51 insertions(+), 111 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 771157dcad954..919df193d60b1 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -210,7 +210,7 @@ impl CodegenResults { encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes()); encoder.emit_str(RUSTC_VERSION.unwrap()); Encodable::encode(codegen_results, &mut encoder); - encoder.finish().unwrap() + encoder.finish() } pub fn deserialize_rlink(data: Vec) -> Result { diff --git a/compiler/rustc_incremental/src/persist/save.rs b/compiler/rustc_incremental/src/persist/save.rs index 9341a742925d9..79836d66011a2 100644 --- a/compiler/rustc_incremental/src/persist/save.rs +++ b/compiler/rustc_incremental/src/persist/save.rs @@ -3,7 +3,7 @@ use rustc_data_structures::sync::join; use rustc_middle::dep_graph::{DepGraph, SerializedDepGraph, WorkProduct, WorkProductId}; use rustc_middle::ty::TyCtxt; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; -use rustc_serialize::{Encodable as RustcEncodable, Encoder}; +use rustc_serialize::Encodable as RustcEncodable; use rustc_session::Session; use std::fs; diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 3285273ba909e..e090b4c37e532 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -93,9 +93,6 @@ macro_rules! encoder_methods { } impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> { - type Ok = ::Ok; - type Err = ::Err; - encoder_methods! { emit_usize(usize); emit_u128(u128); @@ -118,10 +115,6 @@ impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> { emit_str(&str); emit_raw_bytes(&[u8]); } - - fn finish(self) -> Result { - self.opaque.finish() - } } impl<'a, 'tcx, T> Encodable> for LazyValue { @@ -2223,7 +2216,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata { // culminating in the `CrateRoot` which points to all of it. let root = ecx.encode_crate_root(); - let mut result = ecx.opaque.finish().unwrap(); + let mut result = ecx.opaque.finish(); // Encode the root position. let header = METADATA_HEADER.len(); diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index c2c876f7f1a12..0e6435fdf7fde 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -25,6 +25,7 @@ use rustc_span::hygiene::{ use rustc_span::source_map::{SourceMap, StableSourceFileId}; use rustc_span::CachingSourceMapView; use rustc_span::{BytePos, ExpnData, ExpnHash, Pos, SourceFile, Span}; +use std::io; use std::mem; const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE; @@ -807,21 +808,10 @@ impl_ref_decoder! {<'tcx> //- ENCODING ------------------------------------------------------------------- -pub trait OpaqueEncoder: Encoder { - fn position(&self) -> usize; -} - -impl OpaqueEncoder for FileEncoder { - #[inline] - fn position(&self) -> usize { - FileEncoder::position(self) - } -} - /// An encoder that can write to the incremental compilation cache. -pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> { +pub struct CacheEncoder<'a, 'tcx> { tcx: TyCtxt<'tcx>, - encoder: E, + encoder: FileEncoder, type_shorthands: FxHashMap, usize>, predicate_shorthands: FxHashMap, usize>, interpret_allocs: FxIndexSet, @@ -830,10 +820,7 @@ pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> { hygiene_context: &'a HygieneEncodeContext, } -impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E> -where - E: OpaqueEncoder, -{ +impl<'a, 'tcx> CacheEncoder<'a, 'tcx> { fn source_file_index(&mut self, source_file: Lrc) -> SourceFileIndex { self.file_to_file_index[&(&*source_file as *const SourceFile)] } @@ -852,32 +839,27 @@ where let end_pos = self.position(); ((end_pos - start_pos) as u64).encode(self); } + + fn finish(self) -> Result { + self.encoder.finish() + } } -impl<'a, 'tcx, E> Encodable> for SyntaxContext -where - E: OpaqueEncoder, -{ - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for SyntaxContext { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s); } } -impl<'a, 'tcx, E> Encodable> for ExpnId -where - E: OpaqueEncoder, -{ - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for ExpnId { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { s.hygiene_context.schedule_expn_data_for_encoding(*self); self.expn_hash().encode(s); } } -impl<'a, 'tcx, E> Encodable> for Span -where - E: OpaqueEncoder, -{ - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for Span { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { let span_data = self.data_untracked(); span_data.ctxt.encode(s); span_data.parent.encode(s); @@ -920,10 +902,7 @@ where } } -impl<'a, 'tcx, E> TyEncoder for CacheEncoder<'a, 'tcx, E> -where - E: OpaqueEncoder, -{ +impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> { type I = TyCtxt<'tcx>; const CLEAR_CROSS_CRATE: bool = false; @@ -943,29 +922,20 @@ where } } -impl<'a, 'tcx, E> Encodable> for CrateNum -where - E: OpaqueEncoder, -{ - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for CrateNum { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { s.tcx.stable_crate_id(*self).encode(s); } } -impl<'a, 'tcx, E> Encodable> for DefId -where - E: OpaqueEncoder, -{ - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for DefId { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { s.tcx.def_path_hash(*self).encode(s); } } -impl<'a, 'tcx, E> Encodable> for DefIndex -where - E: OpaqueEncoder, -{ - fn encode(&self, _: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for DefIndex { + fn encode(&self, _: &mut CacheEncoder<'a, 'tcx>) { bug!("encoding `DefIndex` without context"); } } @@ -979,13 +949,7 @@ macro_rules! encoder_methods { } } -impl<'a, 'tcx, E> Encoder for CacheEncoder<'a, 'tcx, E> -where - E: OpaqueEncoder, -{ - type Ok = E::Ok; - type Err = E::Err; - +impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> { encoder_methods! { emit_usize(usize); emit_u128(u128); @@ -1008,30 +972,26 @@ where emit_str(&str); emit_raw_bytes(&[u8]); } - - fn finish(self) -> Result { - self.encoder.finish() - } } // This ensures that the `Encodable::encode` specialization for byte slices // is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`. // Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder` // and the encoding traits currently work. -impl<'a, 'tcx> Encodable> for [u8] { - fn encode(&self, e: &mut CacheEncoder<'a, 'tcx, FileEncoder>) { +impl<'a, 'tcx> Encodable> for [u8] { + fn encode(&self, e: &mut CacheEncoder<'a, 'tcx>) { self.encode(&mut e.encoder); } } pub fn encode_query_results<'a, 'tcx, CTX, Q>( tcx: CTX, - encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>, + encoder: &mut CacheEncoder<'a, 'tcx>, query_result_index: &mut EncodedDepNodeIndex, ) where CTX: QueryContext + 'tcx, Q: super::QueryDescription, - Q::Value: Encodable>, + Q::Value: Encodable>, { let _timer = tcx .dep_context() diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 87aedc6542d8d..66f4508f6b497 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -12,7 +12,6 @@ use rustc_query_system::query::{QueryContext, QueryJobId, QueryMap, QuerySideEff use rustc_data_structures::sync::Lock; use rustc_data_structures::thin_vec::ThinVec; use rustc_errors::{Diagnostic, Handler}; -use rustc_serialize::opaque; use std::any::Any; use std::num::NonZeroU64; @@ -140,7 +139,7 @@ impl<'tcx> QueryCtxt<'tcx> { pub(super) fn encode_query_results( self, - encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx, opaque::FileEncoder>, + encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>, query_result_index: &mut on_disk_cache::EncodedDepNodeIndex, ) { macro_rules! encode_queries { diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index 7fde9c0119b85..2c44054e4c847 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -20,7 +20,7 @@ use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sync::Lock; use rustc_index::vec::{Idx, IndexVec}; use rustc_serialize::opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize}; -use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; +use rustc_serialize::{Decodable, Decoder, Encodable}; use smallvec::SmallVec; use std::convert::TryInto; diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index b2dbf937eb75a..88e5239729708 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -24,6 +24,10 @@ impl Encoder { pub fn position(&self) -> usize { self.data.len() } + + pub fn finish(self) -> Vec { + self.data + } } macro_rules! write_leb128 { @@ -54,9 +58,6 @@ macro_rules! write_leb128 { const STR_SENTINEL: u8 = 0xC1; impl serialize::Encoder for Encoder { - type Ok = Vec; - type Err = !; - #[inline] fn emit_usize(&mut self, v: usize) { write_leb128!(self, v, usize, write_usize_leb128) @@ -150,10 +151,6 @@ impl serialize::Encoder for Encoder { fn emit_raw_bytes(&mut self, s: &[u8]) { self.data.extend_from_slice(s); } - - fn finish(self) -> Result { - Ok(self.data) - } } pub type FileEncodeResult = Result; @@ -389,6 +386,13 @@ impl FileEncoder { } } } + + pub fn finish(mut self) -> Result { + self.flush(); + + let res = std::mem::replace(&mut self.res, Ok(())); + res.map(|()| self.position()) + } } impl Drop for FileEncoder { @@ -426,9 +430,6 @@ macro_rules! file_encoder_write_leb128 { } impl serialize::Encoder for FileEncoder { - type Ok = usize; - type Err = io::Error; - #[inline] fn emit_usize(&mut self, v: usize) { file_encoder_write_leb128!(self, v, usize, write_usize_leb128) @@ -522,13 +523,6 @@ impl serialize::Encoder for FileEncoder { fn emit_raw_bytes(&mut self, s: &[u8]) { self.write_all(s); } - - fn finish(mut self) -> Result { - self.flush(); - - let res = std::mem::replace(&mut self.res, Ok(())); - res.map(|()| self.position()) - } } // ----------------------------------------------------------------------------- diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index 98bb18581f517..36585b8d77e0a 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -18,13 +18,10 @@ use std::sync::Arc; /// is pervasive and has non-trivial cost. Instead, impls of this trait must /// implement a delayed error handling strategy. If a failure occurs, they /// should record this internally, and all subsequent encoding operations can -/// be processed or ignored, whichever is appropriate. Then when `finish()` is -/// called, an error result should be returned to indicate the failure. If no -/// failures occurred, then `finish()` should return a success result. +/// be processed or ignored, whichever is appropriate. Then they should provide +/// a `finish` method that finishes up encoding. If the encoder is fallible, +/// `finish` should return a `Result` that indicates success or failure. pub trait Encoder { - type Ok; - type Err; - // Primitive types: fn emit_usize(&mut self, v: usize); fn emit_u128(&mut self, v: u128); @@ -64,9 +61,6 @@ pub trait Encoder { fn emit_fieldless_enum_variant(&mut self) { self.emit_usize(ID) } - - // Consume the encoder, getting the result. - fn finish(self) -> Result; } // Note: all the methods in this trait are infallible, which may be surprising. diff --git a/compiler/rustc_serialize/tests/opaque.rs b/compiler/rustc_serialize/tests/opaque.rs index 703b7f5e7a5f4..5ed6fc769cc29 100644 --- a/compiler/rustc_serialize/tests/opaque.rs +++ b/compiler/rustc_serialize/tests/opaque.rs @@ -2,7 +2,7 @@ use rustc_macros::{Decodable, Encodable}; use rustc_serialize::opaque::{Decoder, Encoder}; -use rustc_serialize::{Decodable, Encodable, Encoder as EncoderTrait}; +use rustc_serialize::{Decodable, Encodable}; use std::fmt::Debug; #[derive(PartialEq, Clone, Debug, Encodable, Decodable)] @@ -36,7 +36,7 @@ fn check_round_trip + for<'a> Decodable> + Par Encodable::encode(value, &mut encoder); } - let data = encoder.finish().unwrap(); + let data = encoder.finish(); let mut decoder = Decoder::new(&data[..], 0); for value in values { diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 242f926967c93..81ce56b3342ed 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -18,7 +18,7 @@ use rustc_middle::hir::nested_filter; use rustc_middle::ty::{self, TyCtxt}; use rustc_serialize::{ opaque::{Decoder, FileEncoder}, - Decodable, Encodable, Encoder, + Decodable, Encodable, }; use rustc_session::getopts; use rustc_span::{ diff --git a/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs b/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs index a09deeec4f182..382fae4a08eb9 100644 --- a/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs +++ b/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs @@ -20,7 +20,7 @@ fn main() { let mut encoder = opaque::Encoder::new(); obj.encode(&mut encoder); - let data = encoder.finish().unwrap(); + let data = encoder.finish(); let mut decoder = opaque::Decoder::new(&data, 0); let obj2 = A::decode(&mut decoder); diff --git a/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs b/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs index 9b6fb0e580621..6097340a6e0c9 100644 --- a/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs +++ b/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs @@ -29,7 +29,7 @@ fn main() { let mut encoder = opaque::Encoder::new(); obj.encode(&mut encoder); - let data = encoder.finish().unwrap(); + let data = encoder.finish(); let mut decoder = opaque::Decoder::new(&data, 0); let obj2 = B::decode(&mut decoder); diff --git a/src/test/ui-fulldeps/issue-14021.rs b/src/test/ui-fulldeps/issue-14021.rs index 4241456367e46..1a19ee0da595f 100644 --- a/src/test/ui-fulldeps/issue-14021.rs +++ b/src/test/ui-fulldeps/issue-14021.rs @@ -19,7 +19,7 @@ pub fn main() { let mut encoder = opaque::Encoder::new(); obj.encode(&mut encoder); - let data = encoder.finish().unwrap(); + let data = encoder.finish(); let mut decoder = opaque::Decoder::new(&data, 0); let obj2 = UnitLikeStruct::decode(&mut decoder); From b983e42936feab29f6333e9835913afc6b4a394e Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 8 Jun 2022 09:17:49 +1000 Subject: [PATCH 5/5] Rename `rustc_serialize::opaque::Encoder` as `MemEncoder`. This avoids the name clash with `rustc_serialize::Encoder` (a trait), and allows lots qualifiers to be removed and imports to be simplified (e.g. fewer `as` imports). --- compiler/rustc_ast/src/ast.rs | 6 +- compiler/rustc_codegen_ssa/src/lib.rs | 7 +- .../rustc_data_structures/src/fingerprint.rs | 10 +-- .../rustc_incremental/src/persist/load.rs | 6 +- .../rustc_incremental/src/persist/save.rs | 2 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 9 +-- compiler/rustc_metadata/src/rmeta/encoder.rs | 7 +- compiler/rustc_metadata/src/rmeta/mod.rs | 4 +- compiler/rustc_metadata/src/rmeta/table.rs | 6 +- .../src/mir/graph_cyclic_cache.rs | 10 +-- compiler/rustc_middle/src/mir/predecessors.rs | 6 +- .../rustc_middle/src/mir/switch_sources.rs | 6 +- compiler/rustc_middle/src/mir/traversal.rs | 6 +- .../rustc_query_impl/src/on_disk_cache.rs | 12 ++-- .../src/dep_graph/serialized.rs | 6 +- compiler/rustc_serialize/src/opaque.rs | 52 +++++++------- compiler/rustc_serialize/tests/opaque.rs | 10 +-- compiler/rustc_span/src/lib.rs | 4 +- compiler/rustc_type_ir/src/sty.rs | 72 +++++++------------ src/librustdoc/scrape_examples.rs | 4 +- .../deriving-encodable-decodable-box.rs | 6 +- ...riving-encodable-decodable-cell-refcell.rs | 6 +- src/test/ui-fulldeps/issue-14021.rs | 6 +- 23 files changed, 123 insertions(+), 140 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 9e6a186219b64..f2643d61fc8c9 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -31,7 +31,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::sync::Lrc; use rustc_data_structures::thin_vec::ThinVec; use rustc_macros::HashStable_Generic; -use rustc_serialize::{self, Decoder, Encoder}; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; @@ -2472,11 +2472,11 @@ rustc_index::newtype_index! { } } -impl rustc_serialize::Encodable for AttrId { +impl Encodable for AttrId { fn encode(&self, _s: &mut S) {} } -impl rustc_serialize::Decodable for AttrId { +impl Decodable for AttrId { fn decode(_: &mut D) -> AttrId { crate::attr::mk_attr_id() } diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 919df193d60b1..6c30923bc3d73 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -29,7 +29,8 @@ use rustc_middle::dep_graph::WorkProduct; use rustc_middle::middle::dependency_format::Dependencies; use rustc_middle::middle::exported_symbols::SymbolExportKind; use rustc_middle::ty::query::{ExternProviders, Providers}; -use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder}; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT}; use rustc_session::cstore::{self, CrateSource}; use rustc_session::utils::NativeLibKind; @@ -203,7 +204,7 @@ const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION"); impl CodegenResults { pub fn serialize_rlink(codegen_results: &CodegenResults) -> Vec { - let mut encoder = opaque::Encoder::new(); + let mut encoder = MemEncoder::new(); encoder.emit_raw_bytes(RLINK_MAGIC); // `emit_raw_bytes` is used to make sure that the version representation does not depend on // Encoder's inner representation of `u32`. @@ -230,7 +231,7 @@ impl CodegenResults { return Err(".rlink file was produced with encoding version {version_array}, but the current version is {RLINK_VERSION}".to_string()); } - let mut decoder = opaque::Decoder::new(&data[4..], 0); + let mut decoder = MemDecoder::new(&data[4..], 0); let rustc_version = decoder.read_str(); let current_version = RUSTC_VERSION.unwrap(); if rustc_version != current_version { diff --git a/compiler/rustc_data_structures/src/fingerprint.rs b/compiler/rustc_data_structures/src/fingerprint.rs index a032b039f34ef..5ff2d18dd2be3 100644 --- a/compiler/rustc_data_structures/src/fingerprint.rs +++ b/compiler/rustc_data_structures/src/fingerprint.rs @@ -1,5 +1,5 @@ use crate::stable_hasher; -use rustc_serialize::{Decodable, Encodable}; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use std::convert::TryInto; use std::hash::{Hash, Hasher}; @@ -142,14 +142,14 @@ impl stable_hasher::StableHasherResult for Fingerprint { impl_stable_hash_via_hash!(Fingerprint); -impl Encodable for Fingerprint { +impl Encodable for Fingerprint { #[inline] fn encode(&self, s: &mut E) { s.emit_raw_bytes(&self.to_le_bytes()); } } -impl Decodable for Fingerprint { +impl Decodable for Fingerprint { #[inline] fn decode(d: &mut D) -> Self { Fingerprint::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap()) @@ -184,7 +184,7 @@ impl std::fmt::Display for PackedFingerprint { } } -impl Encodable for PackedFingerprint { +impl Encodable for PackedFingerprint { #[inline] fn encode(&self, s: &mut E) { // Copy to avoid taking reference to packed field. @@ -193,7 +193,7 @@ impl Encodable for PackedFingerprint { } } -impl Decodable for PackedFingerprint { +impl Decodable for PackedFingerprint { #[inline] fn decode(d: &mut D) -> Self { Self(Fingerprint::decode(d)) diff --git a/compiler/rustc_incremental/src/persist/load.rs b/compiler/rustc_incremental/src/persist/load.rs index 9de14950aa8d3..9c325faae8058 100644 --- a/compiler/rustc_incremental/src/persist/load.rs +++ b/compiler/rustc_incremental/src/persist/load.rs @@ -4,7 +4,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::memmap::Mmap; use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId}; use rustc_middle::ty::OnDiskCache; -use rustc_serialize::opaque::Decoder; +use rustc_serialize::opaque::MemDecoder; use rustc_serialize::Decodable; use rustc_session::config::IncrementalStateAssertion; use rustc_session::Session; @@ -156,7 +156,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture { if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result { // Decode the list of work_products - let mut work_product_decoder = Decoder::new(&work_products_data[..], start_pos); + let mut work_product_decoder = MemDecoder::new(&work_products_data[..], start_pos); let work_products: Vec = Decodable::decode(&mut work_product_decoder); @@ -193,7 +193,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture { LoadResult::DataOutOfDate => LoadResult::DataOutOfDate, LoadResult::Error { message } => LoadResult::Error { message }, LoadResult::Ok { data: (bytes, start_pos) } => { - let mut decoder = Decoder::new(&bytes, start_pos); + let mut decoder = MemDecoder::new(&bytes, start_pos); let prev_commandline_args_hash = u64::decode(&mut decoder); if prev_commandline_args_hash != expected_hash { diff --git a/compiler/rustc_incremental/src/persist/save.rs b/compiler/rustc_incremental/src/persist/save.rs index 79836d66011a2..b34c7ad1f8a15 100644 --- a/compiler/rustc_incremental/src/persist/save.rs +++ b/compiler/rustc_incremental/src/persist/save.rs @@ -3,7 +3,7 @@ use rustc_data_structures::sync::join; use rustc_middle::dep_graph::{DepGraph, SerializedDepGraph, WorkProduct, WorkProductId}; use rustc_middle::ty::TyCtxt; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; -use rustc_serialize::Encodable as RustcEncodable; +use rustc_serialize::Encodable; use rustc_session::Session; use std::fs; diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 775ebb48402aa..03ac82b467b8e 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -26,7 +26,8 @@ use rustc_middle::ty::codec::TyDecoder; use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::GeneratorDiagnosticData; use rustc_middle::ty::{self, ParameterizedOverTcx, Ty, TyCtxt, Visibility}; -use rustc_serialize::{opaque, Decodable, Decoder}; +use rustc_serialize::opaque::MemDecoder; +use rustc_serialize::{Decodable, Decoder}; use rustc_session::cstore::{ CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib, }; @@ -154,7 +155,7 @@ struct ImportedSourceFile { } pub(super) struct DecodeContext<'a, 'tcx> { - opaque: opaque::Decoder<'a>, + opaque: MemDecoder<'a>, cdata: Option>, blob: &'a MetadataBlob, sess: Option<&'tcx Session>, @@ -186,7 +187,7 @@ pub(super) trait Metadata<'a, 'tcx>: Copy { fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> { let tcx = self.tcx(); DecodeContext { - opaque: opaque::Decoder::new(self.blob(), pos), + opaque: MemDecoder::new(self.blob(), pos), cdata: self.cdata(), blob: self.blob(), sess: self.sess().or(tcx.map(|tcx| tcx.sess)), @@ -418,7 +419,7 @@ impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> { where F: FnOnce(&mut Self) -> R, { - let new_opaque = opaque::Decoder::new(self.opaque.data, pos); + let new_opaque = MemDecoder::new(self.opaque.data, pos); let old_opaque = mem::replace(&mut self.opaque, new_opaque); let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode); let r = f(self); diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index e090b4c37e532..8867e008e42fa 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -27,7 +27,8 @@ use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams}; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt}; -use rustc_serialize::{opaque, Encodable, Encoder}; +use rustc_serialize::opaque::MemEncoder; +use rustc_serialize::{Encodable, Encoder}; use rustc_session::config::CrateType; use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib}; use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind}; @@ -43,7 +44,7 @@ use std::num::NonZeroUsize; use tracing::{debug, trace}; pub(super) struct EncodeContext<'a, 'tcx> { - opaque: opaque::Encoder, + opaque: MemEncoder, tcx: TyCtxt<'tcx>, feat: &'tcx rustc_feature::Features, @@ -2181,7 +2182,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata { } fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata { - let mut encoder = opaque::Encoder::new(); + let mut encoder = MemEncoder::new(); encoder.emit_raw_bytes(METADATA_HEADER); // Will be filled with the root position after encoding everything. diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index fb2ffe1d73d96..04f0847f5cccc 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -22,7 +22,7 @@ use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, ReprOptions, Ty}; use rustc_middle::ty::{GeneratorDiagnosticData, ParameterizedOverTcx, TyCtxt}; -use rustc_serialize::opaque::Encoder; +use rustc_serialize::opaque::MemEncoder; use rustc_session::config::SymbolManglingVersion; use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib}; use rustc_span::edition::Edition; @@ -323,7 +323,7 @@ macro_rules! define_tables { } impl TableBuilders { - fn encode(&self, buf: &mut Encoder) -> LazyTables { + fn encode(&self, buf: &mut MemEncoder) -> LazyTables { LazyTables { $($name: self.$name.encode(buf)),+ } diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index 8baa67a8f9fcf..5ab4269ae99ad 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -4,8 +4,8 @@ use rustc_data_structures::fingerprint::Fingerprint; use rustc_hir::def::{CtorKind, CtorOf}; use rustc_index::vec::Idx; use rustc_middle::ty::ParameterizedOverTcx; -use rustc_serialize::opaque::Encoder; -use rustc_serialize::Encoder as _; +use rustc_serialize::opaque::MemEncoder; +use rustc_serialize::Encoder; use rustc_span::hygiene::MacroKind; use std::convert::TryInto; use std::marker::PhantomData; @@ -281,7 +281,7 @@ where Some(value).write_to_bytes(&mut self.blocks[i]); } - pub(crate) fn encode(&self, buf: &mut Encoder) -> LazyTable + pub(crate) fn encode(&self, buf: &mut MemEncoder) -> LazyTable where Option: FixedSizeEncoding, { diff --git a/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs b/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs index 096bf8cbc158a..1279f5aee3691 100644 --- a/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs +++ b/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs @@ -3,7 +3,7 @@ use rustc_data_structures::graph::{ }; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::OnceCell; -use rustc_serialize as serialize; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; /// Helper type to cache the result of `graph::is_cyclic`. #[derive(Clone, Debug)] @@ -36,17 +36,17 @@ impl GraphIsCyclicCache { } } -impl serialize::Encodable for GraphIsCyclicCache { +impl Encodable for GraphIsCyclicCache { #[inline] fn encode(&self, s: &mut S) { - serialize::Encodable::encode(&(), s); + Encodable::encode(&(), s); } } -impl serialize::Decodable for GraphIsCyclicCache { +impl Decodable for GraphIsCyclicCache { #[inline] fn decode(d: &mut D) -> Self { - let () = serialize::Decodable::decode(d); + let () = Decodable::decode(d); Self::new() } } diff --git a/compiler/rustc_middle/src/mir/predecessors.rs b/compiler/rustc_middle/src/mir/predecessors.rs index 9bc0cb1138ff1..620cf7e336ba4 100644 --- a/compiler/rustc_middle/src/mir/predecessors.rs +++ b/compiler/rustc_middle/src/mir/predecessors.rs @@ -3,7 +3,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::OnceCell; use rustc_index::vec::IndexVec; -use rustc_serialize as serialize; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use smallvec::SmallVec; use crate::mir::{BasicBlock, BasicBlockData}; @@ -54,12 +54,12 @@ impl PredecessorCache { } } -impl serialize::Encodable for PredecessorCache { +impl Encodable for PredecessorCache { #[inline] fn encode(&self, _s: &mut S) {} } -impl serialize::Decodable for PredecessorCache { +impl Decodable for PredecessorCache { #[inline] fn decode(_: &mut D) -> Self { Self::new() diff --git a/compiler/rustc_middle/src/mir/switch_sources.rs b/compiler/rustc_middle/src/mir/switch_sources.rs index 4872a7835e3fa..99d13fcfef43e 100644 --- a/compiler/rustc_middle/src/mir/switch_sources.rs +++ b/compiler/rustc_middle/src/mir/switch_sources.rs @@ -5,7 +5,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_map::FxHashMap; use rustc_data_structures::sync::OnceCell; use rustc_index::vec::IndexVec; -use rustc_serialize as serialize; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use smallvec::SmallVec; use crate::mir::{BasicBlock, BasicBlockData, Terminator, TerminatorKind}; @@ -54,12 +54,12 @@ impl SwitchSourceCache { } } -impl serialize::Encodable for SwitchSourceCache { +impl Encodable for SwitchSourceCache { #[inline] fn encode(&self, _s: &mut S) {} } -impl serialize::Decodable for SwitchSourceCache { +impl Decodable for SwitchSourceCache { #[inline] fn decode(_: &mut D) -> Self { Self::new() diff --git a/compiler/rustc_middle/src/mir/traversal.rs b/compiler/rustc_middle/src/mir/traversal.rs index f745e55307ae2..7228e3f33b126 100644 --- a/compiler/rustc_middle/src/mir/traversal.rs +++ b/compiler/rustc_middle/src/mir/traversal.rs @@ -1,7 +1,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::OnceCell; use rustc_index::bit_set::BitSet; -use rustc_serialize as serialize; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use super::*; @@ -365,12 +365,12 @@ impl PostorderCache { } } -impl serialize::Encodable for PostorderCache { +impl Encodable for PostorderCache { #[inline] fn encode(&self, _s: &mut S) {} } -impl serialize::Decodable for PostorderCache { +impl Decodable for PostorderCache { #[inline] fn decode(_: &mut D) -> Self { Self::new() diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index 0e6435fdf7fde..0fb1d72829361 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -15,7 +15,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_query_system::dep_graph::DepContext; use rustc_query_system::query::{QueryCache, QueryContext, QuerySideEffects}; use rustc_serialize::{ - opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize}, + opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder}, Decodable, Decoder, Encodable, Encoder, }; use rustc_session::Session; @@ -159,7 +159,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> { // Wrap in a scope so we can borrow `data`. let footer: Footer = { - let mut decoder = opaque::Decoder::new(&data, start_pos); + let mut decoder = MemDecoder::new(&data, start_pos); // Decode the *position* of the footer, which can be found in the // last 8 bytes of the file. @@ -438,7 +438,7 @@ impl<'sess> OnDiskCache<'sess> { let serialized_data = self.serialized_data.read(); let mut decoder = CacheDecoder { tcx, - opaque: opaque::Decoder::new(serialized_data.as_deref().unwrap_or(&[]), pos.to_usize()), + opaque: MemDecoder::new(serialized_data.as_deref().unwrap_or(&[]), pos.to_usize()), source_map: self.source_map, file_index_to_file: &self.file_index_to_file, file_index_to_stable_id: &self.file_index_to_stable_id, @@ -459,7 +459,7 @@ impl<'sess> OnDiskCache<'sess> { /// will also handle things that contain `Ty` instances. pub struct CacheDecoder<'a, 'tcx> { tcx: TyCtxt<'tcx>, - opaque: opaque::Decoder<'a>, + opaque: MemDecoder<'a>, source_map: &'a SourceMap, file_index_to_file: &'a Lock>>, file_index_to_stable_id: &'a FxHashMap, @@ -511,7 +511,7 @@ trait DecoderWithPosition: Decoder { fn position(&self) -> usize; } -impl<'a> DecoderWithPosition for opaque::Decoder<'a> { +impl<'a> DecoderWithPosition for MemDecoder<'a> { fn position(&self) -> usize { self.position() } @@ -587,7 +587,7 @@ impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> { { debug_assert!(pos < self.opaque.data.len()); - let new_opaque = opaque::Decoder::new(self.opaque.data, pos); + let new_opaque = MemDecoder::new(self.opaque.data, pos); let old_opaque = mem::replace(&mut self.opaque, new_opaque); let r = f(self); self.opaque = old_opaque; diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index 2c44054e4c847..3b20ec70d73cb 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -19,7 +19,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sync::Lock; use rustc_index::vec::{Idx, IndexVec}; -use rustc_serialize::opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize}; +use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder}; use rustc_serialize::{Decodable, Decoder, Encodable}; use smallvec::SmallVec; use std::convert::TryInto; @@ -96,11 +96,11 @@ impl SerializedDepGraph { } } -impl<'a, K: DepKind + Decodable>> Decodable> +impl<'a, K: DepKind + Decodable>> Decodable> for SerializedDepGraph { #[instrument(level = "debug", skip(d))] - fn decode(d: &mut opaque::Decoder<'a>) -> SerializedDepGraph { + fn decode(d: &mut MemDecoder<'a>) -> SerializedDepGraph { let start_position = d.position(); // The last 16 bytes are the node count and edge count. diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 88e5239729708..366efe9cfa519 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -1,5 +1,5 @@ use crate::leb128::{self, max_leb128_len}; -use crate::serialize::{self, Decoder as _, Encoder as _}; +use crate::serialize::{Decodable, Decoder, Encodable, Encoder}; use std::convert::TryInto; use std::fs::File; use std::io::{self, Write}; @@ -11,13 +11,13 @@ use std::ptr; // Encoder // ----------------------------------------------------------------------------- -pub struct Encoder { +pub struct MemEncoder { pub data: Vec, } -impl Encoder { - pub fn new() -> Encoder { - Encoder { data: vec![] } +impl MemEncoder { + pub fn new() -> MemEncoder { + MemEncoder { data: vec![] } } #[inline] @@ -57,7 +57,7 @@ macro_rules! write_leb128 { /// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout const STR_SENTINEL: u8 = 0xC1; -impl serialize::Encoder for Encoder { +impl Encoder for MemEncoder { #[inline] fn emit_usize(&mut self, v: usize) { write_leb128!(self, v, usize, write_usize_leb128) @@ -158,7 +158,7 @@ pub type FileEncodeResult = Result; // `FileEncoder` encodes data to file via fixed-size buffer. // // When encoding large amounts of data to a file, using `FileEncoder` may be -// preferred over using `Encoder` to encode to a `Vec`, and then writing the +// preferred over using `MemEncoder` to encode to a `Vec`, and then writing the // `Vec` to file, as the latter uses as much memory as there is encoded data, // while the former uses the fixed amount of memory allocated to the buffer. // `FileEncoder` also has the advantage of not needing to reallocate as data @@ -429,7 +429,7 @@ macro_rules! file_encoder_write_leb128 { }}; } -impl serialize::Encoder for FileEncoder { +impl Encoder for FileEncoder { #[inline] fn emit_usize(&mut self, v: usize) { file_encoder_write_leb128!(self, v, usize, write_usize_leb128) @@ -529,15 +529,15 @@ impl serialize::Encoder for FileEncoder { // Decoder // ----------------------------------------------------------------------------- -pub struct Decoder<'a> { +pub struct MemDecoder<'a> { pub data: &'a [u8], position: usize, } -impl<'a> Decoder<'a> { +impl<'a> MemDecoder<'a> { #[inline] - pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> { - Decoder { data, position } + pub fn new(data: &'a [u8], position: usize) -> MemDecoder<'a> { + MemDecoder { data, position } } #[inline] @@ -560,7 +560,7 @@ macro_rules! read_leb128 { ($dec:expr, $fun:ident) => {{ leb128::$fun($dec.data, &mut $dec.position) }}; } -impl<'a> serialize::Decoder for Decoder<'a> { +impl<'a> Decoder for MemDecoder<'a> { #[inline] fn read_u128(&mut self) -> u128 { read_leb128!(self, read_u128_leb128) @@ -682,25 +682,25 @@ impl<'a> serialize::Decoder for Decoder<'a> { // Specialize encoding byte slices. This specialization also applies to encoding `Vec`s, etc., // since the default implementations call `encode` on their slices internally. -impl serialize::Encodable for [u8] { - fn encode(&self, e: &mut Encoder) { - serialize::Encoder::emit_usize(e, self.len()); +impl Encodable for [u8] { + fn encode(&self, e: &mut MemEncoder) { + Encoder::emit_usize(e, self.len()); e.emit_raw_bytes(self); } } -impl serialize::Encodable for [u8] { +impl Encodable for [u8] { fn encode(&self, e: &mut FileEncoder) { - serialize::Encoder::emit_usize(e, self.len()); + Encoder::emit_usize(e, self.len()); e.emit_raw_bytes(self); } } // Specialize decoding `Vec`. This specialization also applies to decoding `Box<[u8]>`s, etc., // since the default implementations call `decode` to produce a `Vec` internally. -impl<'a> serialize::Decodable> for Vec { - fn decode(d: &mut Decoder<'a>) -> Self { - let len = serialize::Decoder::read_usize(d); +impl<'a> Decodable> for Vec { + fn decode(d: &mut MemDecoder<'a>) -> Self { + let len = Decoder::read_usize(d); d.read_raw_bytes(len).to_owned() } } @@ -712,9 +712,9 @@ impl IntEncodedWithFixedSize { pub const ENCODED_SIZE: usize = 8; } -impl serialize::Encodable for IntEncodedWithFixedSize { +impl Encodable for IntEncodedWithFixedSize { #[inline] - fn encode(&self, e: &mut Encoder) { + fn encode(&self, e: &mut MemEncoder) { let _start_pos = e.position(); e.emit_raw_bytes(&self.0.to_le_bytes()); let _end_pos = e.position(); @@ -722,7 +722,7 @@ impl serialize::Encodable for IntEncodedWithFixedSize { } } -impl serialize::Encodable for IntEncodedWithFixedSize { +impl Encodable for IntEncodedWithFixedSize { #[inline] fn encode(&self, e: &mut FileEncoder) { let _start_pos = e.position(); @@ -732,9 +732,9 @@ impl serialize::Encodable for IntEncodedWithFixedSize { } } -impl<'a> serialize::Decodable> for IntEncodedWithFixedSize { +impl<'a> Decodable> for IntEncodedWithFixedSize { #[inline] - fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize { + fn decode(decoder: &mut MemDecoder<'a>) -> IntEncodedWithFixedSize { let _start_pos = decoder.position(); let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE); let value = u64::from_le_bytes(bytes.try_into().unwrap()); diff --git a/compiler/rustc_serialize/tests/opaque.rs b/compiler/rustc_serialize/tests/opaque.rs index 5ed6fc769cc29..3a695d0714ee1 100644 --- a/compiler/rustc_serialize/tests/opaque.rs +++ b/compiler/rustc_serialize/tests/opaque.rs @@ -1,7 +1,7 @@ #![allow(rustc::internal)] use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque::{Decoder, Encoder}; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; use rustc_serialize::{Decodable, Encodable}; use std::fmt::Debug; @@ -28,16 +28,18 @@ struct Struct { q: Option, } -fn check_round_trip + for<'a> Decodable> + PartialEq + Debug>( +fn check_round_trip< + T: Encodable + for<'a> Decodable> + PartialEq + Debug, +>( values: Vec, ) { - let mut encoder = Encoder::new(); + let mut encoder = MemEncoder::new(); for value in &values { Encodable::encode(value, &mut encoder); } let data = encoder.finish(); - let mut decoder = Decoder::new(&data[..], 0); + let mut decoder = MemDecoder::new(&data[..], 0); for value in values { let decoded = Decodable::decode(&mut decoder); diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 7f227217e3c2f..0b7020660e9a0 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1911,13 +1911,13 @@ impl_pos! { pub struct CharPos(pub usize); } -impl Encodable for BytePos { +impl Encodable for BytePos { fn encode(&self, s: &mut S) { s.emit_u32(self.0); } } -impl Decodable for BytePos { +impl Decodable for BytePos { fn decode(d: &mut D) -> BytePos { BytePos(d.read_u32()) } diff --git a/compiler/rustc_type_ir/src/sty.rs b/compiler/rustc_type_ir/src/sty.rs index a6a0d02c8ba99..9407218439993 100644 --- a/compiler/rustc_type_ir/src/sty.rs +++ b/compiler/rustc_type_ir/src/sty.rs @@ -14,7 +14,7 @@ use crate::UintTy; use self::TyKind::*; use rustc_data_structures::stable_hasher::HashStable; -use rustc_serialize::{Decodable, Encodable}; +use rustc_serialize::{Decodable, Decoder, Encodable}; /// Defines the kinds of types used by the type system. /// @@ -833,56 +833,34 @@ where I::AllocId: Decodable, { fn decode(d: &mut D) -> Self { - match rustc_serialize::Decoder::read_usize(d) { + match Decoder::read_usize(d) { 0 => Bool, 1 => Char, - 2 => Int(rustc_serialize::Decodable::decode(d)), - 3 => Uint(rustc_serialize::Decodable::decode(d)), - 4 => Float(rustc_serialize::Decodable::decode(d)), - 5 => Adt(rustc_serialize::Decodable::decode(d), rustc_serialize::Decodable::decode(d)), - 6 => Foreign(rustc_serialize::Decodable::decode(d)), + 2 => Int(Decodable::decode(d)), + 3 => Uint(Decodable::decode(d)), + 4 => Float(Decodable::decode(d)), + 5 => Adt(Decodable::decode(d), Decodable::decode(d)), + 6 => Foreign(Decodable::decode(d)), 7 => Str, - 8 => { - Array(rustc_serialize::Decodable::decode(d), rustc_serialize::Decodable::decode(d)) - } - 9 => Slice(rustc_serialize::Decodable::decode(d)), - 10 => RawPtr(rustc_serialize::Decodable::decode(d)), - 11 => Ref( - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - ), - 12 => { - FnDef(rustc_serialize::Decodable::decode(d), rustc_serialize::Decodable::decode(d)) - } - 13 => FnPtr(rustc_serialize::Decodable::decode(d)), - 14 => Dynamic( - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - ), - 15 => Closure( - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - ), - 16 => Generator( - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - ), - 17 => GeneratorWitness(rustc_serialize::Decodable::decode(d)), + 8 => Array(Decodable::decode(d), Decodable::decode(d)), + 9 => Slice(Decodable::decode(d)), + 10 => RawPtr(Decodable::decode(d)), + 11 => Ref(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)), + 12 => FnDef(Decodable::decode(d), Decodable::decode(d)), + 13 => FnPtr(Decodable::decode(d)), + 14 => Dynamic(Decodable::decode(d), Decodable::decode(d)), + 15 => Closure(Decodable::decode(d), Decodable::decode(d)), + 16 => Generator(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)), + 17 => GeneratorWitness(Decodable::decode(d)), 18 => Never, - 19 => Tuple(rustc_serialize::Decodable::decode(d)), - 20 => Projection(rustc_serialize::Decodable::decode(d)), - 21 => { - Opaque(rustc_serialize::Decodable::decode(d), rustc_serialize::Decodable::decode(d)) - } - 22 => Param(rustc_serialize::Decodable::decode(d)), - 23 => { - Bound(rustc_serialize::Decodable::decode(d), rustc_serialize::Decodable::decode(d)) - } - 24 => Placeholder(rustc_serialize::Decodable::decode(d)), - 25 => Infer(rustc_serialize::Decodable::decode(d)), - 26 => Error(rustc_serialize::Decodable::decode(d)), + 19 => Tuple(Decodable::decode(d)), + 20 => Projection(Decodable::decode(d)), + 21 => Opaque(Decodable::decode(d), Decodable::decode(d)), + 22 => Param(Decodable::decode(d)), + 23 => Bound(Decodable::decode(d), Decodable::decode(d)), + 24 => Placeholder(Decodable::decode(d)), + 25 => Infer(Decodable::decode(d)), + 26 => Error(Decodable::decode(d)), _ => panic!( "{}", format!( diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 81ce56b3342ed..f6c599297fcd6 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -17,7 +17,7 @@ use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; use rustc_middle::ty::{self, TyCtxt}; use rustc_serialize::{ - opaque::{Decoder, FileEncoder}, + opaque::{FileEncoder, MemDecoder}, Decodable, Encodable, }; use rustc_session::getopts; @@ -336,7 +336,7 @@ pub(crate) fn load_call_locations( let mut all_calls: AllCallLocations = FxHashMap::default(); for path in with_examples { let bytes = fs::read(&path).map_err(|e| format!("{} (for path {})", e, path))?; - let mut decoder = Decoder::new(&bytes, 0); + let mut decoder = MemDecoder::new(&bytes, 0); let calls = AllCallLocations::decode(&mut decoder); for (function, fn_calls) in calls.into_iter() { diff --git a/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs b/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs index 382fae4a08eb9..a4b911878e0d6 100644 --- a/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs +++ b/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs @@ -7,7 +7,7 @@ extern crate rustc_macros; extern crate rustc_serialize; use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; use rustc_serialize::{Decodable, Encodable, Encoder}; #[derive(Encodable, Decodable)] @@ -18,11 +18,11 @@ struct A { fn main() { let obj = A { foo: Box::new([true, false]) }; - let mut encoder = opaque::Encoder::new(); + let mut encoder = MemEncoder::new(); obj.encode(&mut encoder); let data = encoder.finish(); - let mut decoder = opaque::Decoder::new(&data, 0); + let mut decoder = MemDecoder::new(&data, 0); let obj2 = A::decode(&mut decoder); assert_eq!(obj.foo, obj2.foo); diff --git a/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs b/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs index 6097340a6e0c9..580c85f9b7848 100644 --- a/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs +++ b/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs @@ -9,7 +9,7 @@ extern crate rustc_macros; extern crate rustc_serialize; use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; use rustc_serialize::{Decodable, Encodable, Encoder}; use std::cell::{Cell, RefCell}; @@ -27,11 +27,11 @@ struct B { fn main() { let obj = B { foo: Cell::new(true), bar: RefCell::new(A { baz: 2 }) }; - let mut encoder = opaque::Encoder::new(); + let mut encoder = MemEncoder::new(); obj.encode(&mut encoder); let data = encoder.finish(); - let mut decoder = opaque::Decoder::new(&data, 0); + let mut decoder = MemDecoder::new(&data, 0); let obj2 = B::decode(&mut decoder); assert_eq!(obj.foo.get(), obj2.foo.get()); diff --git a/src/test/ui-fulldeps/issue-14021.rs b/src/test/ui-fulldeps/issue-14021.rs index 1a19ee0da595f..215dfaed7abbe 100644 --- a/src/test/ui-fulldeps/issue-14021.rs +++ b/src/test/ui-fulldeps/issue-14021.rs @@ -8,7 +8,7 @@ extern crate rustc_macros; extern crate rustc_serialize; use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; use rustc_serialize::{Decodable, Encodable, Encoder}; #[derive(Encodable, Decodable, PartialEq, Debug)] @@ -17,11 +17,11 @@ struct UnitLikeStruct; pub fn main() { let obj = UnitLikeStruct; - let mut encoder = opaque::Encoder::new(); + let mut encoder = MemEncoder::new(); obj.encode(&mut encoder); let data = encoder.finish(); - let mut decoder = opaque::Decoder::new(&data, 0); + let mut decoder = MemDecoder::new(&data, 0); let obj2 = UnitLikeStruct::decode(&mut decoder); assert_eq!(obj, obj2);