From 3186e311e526e3dad197dacc91c2d84cde2be846 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 10 Jun 2022 11:58:29 +1000 Subject: [PATCH] Revert dc08bc51f2c58a0f5f815a07f9bb3d671153b5a1. --- 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, 111 insertions(+), 51 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 919df193d60..771157dcad9 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() + encoder.finish().unwrap() } 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 79836d66011..9341a742925 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; diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index e090b4c37e5..3285273ba90 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -93,6 +93,9 @@ 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); @@ -115,6 +118,10 @@ 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 { @@ -2216,7 +2223,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(); + let mut result = ecx.opaque.finish().unwrap(); // 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 0e6435fdf7f..c2c876f7f1a 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -25,7 +25,6 @@ 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; @@ -808,10 +807,21 @@ 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> { +pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> { tcx: TyCtxt<'tcx>, - encoder: FileEncoder, + encoder: E, type_shorthands: FxHashMap, usize>, predicate_shorthands: FxHashMap, usize>, interpret_allocs: FxIndexSet, @@ -820,7 +830,10 @@ pub struct CacheEncoder<'a, 'tcx> { hygiene_context: &'a HygieneEncodeContext, } -impl<'a, 'tcx> CacheEncoder<'a, 'tcx> { +impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E> +where + E: OpaqueEncoder, +{ fn source_file_index(&mut self, source_file: Lrc) -> SourceFileIndex { self.file_to_file_index[&(&*source_file as *const SourceFile)] } @@ -839,27 +852,32 @@ impl<'a, 'tcx> CacheEncoder<'a, 'tcx> { let end_pos = self.position(); ((end_pos - start_pos) as u64).encode(self); } - - fn finish(self) -> Result { - self.encoder.finish() - } } -impl<'a, 'tcx> Encodable> for SyntaxContext { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { +impl<'a, 'tcx, E> Encodable> for SyntaxContext +where + E: OpaqueEncoder, +{ + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s); } } -impl<'a, 'tcx> Encodable> for ExpnId { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { +impl<'a, 'tcx, E> Encodable> for ExpnId +where + E: OpaqueEncoder, +{ + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { s.hygiene_context.schedule_expn_data_for_encoding(*self); self.expn_hash().encode(s); } } -impl<'a, 'tcx> Encodable> for Span { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { +impl<'a, 'tcx, E> Encodable> for Span +where + E: OpaqueEncoder, +{ + 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); @@ -902,7 +920,10 @@ impl<'a, 'tcx> Encodable> for Span { } } -impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> { +impl<'a, 'tcx, E> TyEncoder for CacheEncoder<'a, 'tcx, E> +where + E: OpaqueEncoder, +{ type I = TyCtxt<'tcx>; const CLEAR_CROSS_CRATE: bool = false; @@ -922,20 +943,29 @@ impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> { } } -impl<'a, 'tcx> Encodable> for CrateNum { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { +impl<'a, 'tcx, E> Encodable> for CrateNum +where + E: OpaqueEncoder, +{ + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { s.tcx.stable_crate_id(*self).encode(s); } } -impl<'a, 'tcx> Encodable> for DefId { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { +impl<'a, 'tcx, E> Encodable> for DefId +where + E: OpaqueEncoder, +{ + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { s.tcx.def_path_hash(*self).encode(s); } } -impl<'a, 'tcx> Encodable> for DefIndex { - fn encode(&self, _: &mut CacheEncoder<'a, 'tcx>) { +impl<'a, 'tcx, E> Encodable> for DefIndex +where + E: OpaqueEncoder, +{ + fn encode(&self, _: &mut CacheEncoder<'a, 'tcx, E>) { bug!("encoding `DefIndex` without context"); } } @@ -949,7 +979,13 @@ macro_rules! encoder_methods { } } -impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> { +impl<'a, 'tcx, E> Encoder for CacheEncoder<'a, 'tcx, E> +where + E: OpaqueEncoder, +{ + type Ok = E::Ok; + type Err = E::Err; + encoder_methods! { emit_usize(usize); emit_u128(u128); @@ -972,26 +1008,30 @@ impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> { 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>) { +impl<'a, 'tcx> Encodable> for [u8] { + fn encode(&self, e: &mut CacheEncoder<'a, 'tcx, FileEncoder>) { self.encode(&mut e.encoder); } } pub fn encode_query_results<'a, 'tcx, CTX, Q>( tcx: CTX, - encoder: &mut CacheEncoder<'a, 'tcx>, + encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>, 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 66f4508f6b4..87aedc6542d 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -12,6 +12,7 @@ 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; @@ -139,7 +140,7 @@ impl<'tcx> QueryCtxt<'tcx> { pub(super) fn encode_query_results( self, - encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>, + encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx, opaque::FileEncoder>, 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 2c44054e4c8..7fde9c0119b 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; diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 88e52397297..b2dbf937eb7 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -24,10 +24,6 @@ impl Encoder { pub fn position(&self) -> usize { self.data.len() } - - pub fn finish(self) -> Vec { - self.data - } } macro_rules! write_leb128 { @@ -58,6 +54,9 @@ 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) @@ -151,6 +150,10 @@ 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; @@ -386,13 +389,6 @@ 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 { @@ -430,6 +426,9 @@ 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) @@ -523,6 +522,13 @@ 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 36585b8d77e..98bb18581f5 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -18,10 +18,13 @@ 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 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. +/// 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 Ok; + type Err; + // Primitive types: fn emit_usize(&mut self, v: usize); fn emit_u128(&mut self, v: u128); @@ -61,6 +64,9 @@ 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 5ed6fc769cc..703b7f5e7a5 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)] @@ -36,7 +36,7 @@ fn check_round_trip + for<'a> Decodable> + Par Encodable::encode(value, &mut encoder); } - let data = encoder.finish(); + let data = encoder.finish().unwrap(); 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 81ce56b3342..242f926967c 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::{ diff --git a/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs b/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs index 382fae4a08e..a09deeec4f1 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(); + let data = encoder.finish().unwrap(); 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 6097340a6e0..9b6fb0e5806 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(); + let data = encoder.finish().unwrap(); 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 1a19ee0da59..4241456367e 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(); + let data = encoder.finish().unwrap(); let mut decoder = opaque::Decoder::new(&data, 0); let obj2 = UnitLikeStruct::decode(&mut decoder);