Skip to content

Commit 3186e31

Browse files
committed
Revert dc08bc5.
1 parent 7f51a1b commit 3186e31

File tree

13 files changed

+111
-51
lines changed

13 files changed

+111
-51
lines changed

compiler/rustc_codegen_ssa/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl CodegenResults {
210210
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
211211
encoder.emit_str(RUSTC_VERSION.unwrap());
212212
Encodable::encode(codegen_results, &mut encoder);
213-
encoder.finish()
213+
encoder.finish().unwrap()
214214
}
215215

216216
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, String> {

compiler/rustc_incremental/src/persist/save.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_data_structures::sync::join;
33
use rustc_middle::dep_graph::{DepGraph, SerializedDepGraph, WorkProduct, WorkProductId};
44
use rustc_middle::ty::TyCtxt;
55
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
6-
use rustc_serialize::Encodable as RustcEncodable;
6+
use rustc_serialize::{Encodable as RustcEncodable, Encoder};
77
use rustc_session::Session;
88
use std::fs;
99

compiler/rustc_metadata/src/rmeta/encoder.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ macro_rules! encoder_methods {
9393
}
9494

9595
impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
96+
type Ok = <opaque::Encoder as Encoder>::Ok;
97+
type Err = <opaque::Encoder as Encoder>::Err;
98+
9699
encoder_methods! {
97100
emit_usize(usize);
98101
emit_u128(u128);
@@ -115,6 +118,10 @@ impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
115118
emit_str(&str);
116119
emit_raw_bytes(&[u8]);
117120
}
121+
122+
fn finish(self) -> Result<Self::Ok, Self::Err> {
123+
self.opaque.finish()
124+
}
118125
}
119126

120127
impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyValue<T> {
@@ -2216,7 +2223,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
22162223
// culminating in the `CrateRoot` which points to all of it.
22172224
let root = ecx.encode_crate_root();
22182225

2219-
let mut result = ecx.opaque.finish();
2226+
let mut result = ecx.opaque.finish().unwrap();
22202227

22212228
// Encode the root position.
22222229
let header = METADATA_HEADER.len();

compiler/rustc_query_impl/src/on_disk_cache.rs

+66-26
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use rustc_span::hygiene::{
2525
use rustc_span::source_map::{SourceMap, StableSourceFileId};
2626
use rustc_span::CachingSourceMapView;
2727
use rustc_span::{BytePos, ExpnData, ExpnHash, Pos, SourceFile, Span};
28-
use std::io;
2928
use std::mem;
3029

3130
const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE;
@@ -808,10 +807,21 @@ impl_ref_decoder! {<'tcx>
808807

809808
//- ENCODING -------------------------------------------------------------------
810809

810+
pub trait OpaqueEncoder: Encoder {
811+
fn position(&self) -> usize;
812+
}
813+
814+
impl OpaqueEncoder for FileEncoder {
815+
#[inline]
816+
fn position(&self) -> usize {
817+
FileEncoder::position(self)
818+
}
819+
}
820+
811821
/// An encoder that can write to the incremental compilation cache.
812-
pub struct CacheEncoder<'a, 'tcx> {
822+
pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
813823
tcx: TyCtxt<'tcx>,
814-
encoder: FileEncoder,
824+
encoder: E,
815825
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
816826
predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
817827
interpret_allocs: FxIndexSet<interpret::AllocId>,
@@ -820,7 +830,10 @@ pub struct CacheEncoder<'a, 'tcx> {
820830
hygiene_context: &'a HygieneEncodeContext,
821831
}
822832

823-
impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
833+
impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E>
834+
where
835+
E: OpaqueEncoder,
836+
{
824837
fn source_file_index(&mut self, source_file: Lrc<SourceFile>) -> SourceFileIndex {
825838
self.file_to_file_index[&(&*source_file as *const SourceFile)]
826839
}
@@ -839,27 +852,32 @@ impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
839852
let end_pos = self.position();
840853
((end_pos - start_pos) as u64).encode(self);
841854
}
842-
843-
fn finish(self) -> Result<usize, io::Error> {
844-
self.encoder.finish()
845-
}
846855
}
847856

848-
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for SyntaxContext {
849-
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
857+
impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for SyntaxContext
858+
where
859+
E: OpaqueEncoder,
860+
{
861+
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) {
850862
rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s);
851863
}
852864
}
853865

854-
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for ExpnId {
855-
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
866+
impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for ExpnId
867+
where
868+
E: OpaqueEncoder,
869+
{
870+
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) {
856871
s.hygiene_context.schedule_expn_data_for_encoding(*self);
857872
self.expn_hash().encode(s);
858873
}
859874
}
860875

861-
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Span {
862-
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
876+
impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for Span
877+
where
878+
E: OpaqueEncoder,
879+
{
880+
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) {
863881
let span_data = self.data_untracked();
864882
span_data.ctxt.encode(s);
865883
span_data.parent.encode(s);
@@ -902,7 +920,10 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Span {
902920
}
903921
}
904922

905-
impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> {
923+
impl<'a, 'tcx, E> TyEncoder for CacheEncoder<'a, 'tcx, E>
924+
where
925+
E: OpaqueEncoder,
926+
{
906927
type I = TyCtxt<'tcx>;
907928
const CLEAR_CROSS_CRATE: bool = false;
908929

@@ -922,20 +943,29 @@ impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> {
922943
}
923944
}
924945

925-
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for CrateNum {
926-
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
946+
impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for CrateNum
947+
where
948+
E: OpaqueEncoder,
949+
{
950+
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) {
927951
s.tcx.stable_crate_id(*self).encode(s);
928952
}
929953
}
930954

931-
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for DefId {
932-
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
955+
impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for DefId
956+
where
957+
E: OpaqueEncoder,
958+
{
959+
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) {
933960
s.tcx.def_path_hash(*self).encode(s);
934961
}
935962
}
936963

937-
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for DefIndex {
938-
fn encode(&self, _: &mut CacheEncoder<'a, 'tcx>) {
964+
impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for DefIndex
965+
where
966+
E: OpaqueEncoder,
967+
{
968+
fn encode(&self, _: &mut CacheEncoder<'a, 'tcx, E>) {
939969
bug!("encoding `DefIndex` without context");
940970
}
941971
}
@@ -949,7 +979,13 @@ macro_rules! encoder_methods {
949979
}
950980
}
951981

952-
impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
982+
impl<'a, 'tcx, E> Encoder for CacheEncoder<'a, 'tcx, E>
983+
where
984+
E: OpaqueEncoder,
985+
{
986+
type Ok = E::Ok;
987+
type Err = E::Err;
988+
953989
encoder_methods! {
954990
emit_usize(usize);
955991
emit_u128(u128);
@@ -972,26 +1008,30 @@ impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
9721008
emit_str(&str);
9731009
emit_raw_bytes(&[u8]);
9741010
}
1011+
1012+
fn finish(self) -> Result<E::Ok, E::Err> {
1013+
self.encoder.finish()
1014+
}
9751015
}
9761016

9771017
// This ensures that the `Encodable<opaque::FileEncoder>::encode` specialization for byte slices
9781018
// is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`.
9791019
// Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder`
9801020
// and the encoding traits currently work.
981-
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for [u8] {
982-
fn encode(&self, e: &mut CacheEncoder<'a, 'tcx>) {
1021+
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx, FileEncoder>> for [u8] {
1022+
fn encode(&self, e: &mut CacheEncoder<'a, 'tcx, FileEncoder>) {
9831023
self.encode(&mut e.encoder);
9841024
}
9851025
}
9861026

9871027
pub fn encode_query_results<'a, 'tcx, CTX, Q>(
9881028
tcx: CTX,
989-
encoder: &mut CacheEncoder<'a, 'tcx>,
1029+
encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>,
9901030
query_result_index: &mut EncodedDepNodeIndex,
9911031
) where
9921032
CTX: QueryContext + 'tcx,
9931033
Q: super::QueryDescription<CTX>,
994-
Q::Value: Encodable<CacheEncoder<'a, 'tcx>>,
1034+
Q::Value: Encodable<CacheEncoder<'a, 'tcx, FileEncoder>>,
9951035
{
9961036
let _timer = tcx
9971037
.dep_context()

compiler/rustc_query_impl/src/plumbing.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_query_system::query::{QueryContext, QueryJobId, QueryMap, QuerySideEff
1212
use rustc_data_structures::sync::Lock;
1313
use rustc_data_structures::thin_vec::ThinVec;
1414
use rustc_errors::{Diagnostic, Handler};
15+
use rustc_serialize::opaque;
1516

1617
use std::any::Any;
1718
use std::num::NonZeroU64;
@@ -139,7 +140,7 @@ impl<'tcx> QueryCtxt<'tcx> {
139140

140141
pub(super) fn encode_query_results(
141142
self,
142-
encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>,
143+
encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx, opaque::FileEncoder>,
143144
query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
144145
) {
145146
macro_rules! encode_queries {

compiler/rustc_query_system/src/dep_graph/serialized.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_data_structures::profiling::SelfProfilerRef;
2020
use rustc_data_structures::sync::Lock;
2121
use rustc_index::vec::{Idx, IndexVec};
2222
use rustc_serialize::opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize};
23-
use rustc_serialize::{Decodable, Decoder, Encodable};
23+
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
2424
use smallvec::SmallVec;
2525
use std::convert::TryInto;
2626

compiler/rustc_serialize/src/opaque.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ impl Encoder {
2424
pub fn position(&self) -> usize {
2525
self.data.len()
2626
}
27-
28-
pub fn finish(self) -> Vec<u8> {
29-
self.data
30-
}
3127
}
3228

3329
macro_rules! write_leb128 {
@@ -58,6 +54,9 @@ macro_rules! write_leb128 {
5854
const STR_SENTINEL: u8 = 0xC1;
5955

6056
impl serialize::Encoder for Encoder {
57+
type Ok = Vec<u8>;
58+
type Err = !;
59+
6160
#[inline]
6261
fn emit_usize(&mut self, v: usize) {
6362
write_leb128!(self, v, usize, write_usize_leb128)
@@ -151,6 +150,10 @@ impl serialize::Encoder for Encoder {
151150
fn emit_raw_bytes(&mut self, s: &[u8]) {
152151
self.data.extend_from_slice(s);
153152
}
153+
154+
fn finish(self) -> Result<Self::Ok, Self::Err> {
155+
Ok(self.data)
156+
}
154157
}
155158

156159
pub type FileEncodeResult = Result<usize, io::Error>;
@@ -386,13 +389,6 @@ impl FileEncoder {
386389
}
387390
}
388391
}
389-
390-
pub fn finish(mut self) -> Result<usize, io::Error> {
391-
self.flush();
392-
393-
let res = std::mem::replace(&mut self.res, Ok(()));
394-
res.map(|()| self.position())
395-
}
396392
}
397393

398394
impl Drop for FileEncoder {
@@ -430,6 +426,9 @@ macro_rules! file_encoder_write_leb128 {
430426
}
431427

432428
impl serialize::Encoder for FileEncoder {
429+
type Ok = usize;
430+
type Err = io::Error;
431+
433432
#[inline]
434433
fn emit_usize(&mut self, v: usize) {
435434
file_encoder_write_leb128!(self, v, usize, write_usize_leb128)
@@ -523,6 +522,13 @@ impl serialize::Encoder for FileEncoder {
523522
fn emit_raw_bytes(&mut self, s: &[u8]) {
524523
self.write_all(s);
525524
}
525+
526+
fn finish(mut self) -> Result<usize, io::Error> {
527+
self.flush();
528+
529+
let res = std::mem::replace(&mut self.res, Ok(()));
530+
res.map(|()| self.position())
531+
}
526532
}
527533

528534
// -----------------------------------------------------------------------------

compiler/rustc_serialize/src/serialize.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ use std::sync::Arc;
1818
/// is pervasive and has non-trivial cost. Instead, impls of this trait must
1919
/// implement a delayed error handling strategy. If a failure occurs, they
2020
/// should record this internally, and all subsequent encoding operations can
21-
/// be processed or ignored, whichever is appropriate. Then they should provide
22-
/// a `finish` method that finishes up encoding. If the encoder is fallible,
23-
/// `finish` should return a `Result` that indicates success or failure.
21+
/// be processed or ignored, whichever is appropriate. Then when `finish()` is
22+
/// called, an error result should be returned to indicate the failure. If no
23+
/// failures occurred, then `finish()` should return a success result.
2424
pub trait Encoder {
25+
type Ok;
26+
type Err;
27+
2528
// Primitive types:
2629
fn emit_usize(&mut self, v: usize);
2730
fn emit_u128(&mut self, v: u128);
@@ -61,6 +64,9 @@ pub trait Encoder {
6164
fn emit_fieldless_enum_variant<const ID: usize>(&mut self) {
6265
self.emit_usize(ID)
6366
}
67+
68+
// Consume the encoder, getting the result.
69+
fn finish(self) -> Result<Self::Ok, Self::Err>;
6470
}
6571

6672
// Note: all the methods in this trait are infallible, which may be surprising.

compiler/rustc_serialize/tests/opaque.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use rustc_macros::{Decodable, Encodable};
44
use rustc_serialize::opaque::{Decoder, Encoder};
5-
use rustc_serialize::{Decodable, Encodable};
5+
use rustc_serialize::{Decodable, Encodable, Encoder as EncoderTrait};
66
use std::fmt::Debug;
77

88
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
@@ -36,7 +36,7 @@ fn check_round_trip<T: Encodable<Encoder> + for<'a> Decodable<Decoder<'a>> + Par
3636
Encodable::encode(value, &mut encoder);
3737
}
3838

39-
let data = encoder.finish();
39+
let data = encoder.finish().unwrap();
4040
let mut decoder = Decoder::new(&data[..], 0);
4141

4242
for value in values {

src/librustdoc/scrape_examples.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::hir::nested_filter;
1818
use rustc_middle::ty::{self, TyCtxt};
1919
use rustc_serialize::{
2020
opaque::{Decoder, FileEncoder},
21-
Decodable, Encodable,
21+
Decodable, Encodable, Encoder,
2222
};
2323
use rustc_session::getopts;
2424
use rustc_span::{

src/test/ui-fulldeps/deriving-encodable-decodable-box.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() {
2020

2121
let mut encoder = opaque::Encoder::new();
2222
obj.encode(&mut encoder);
23-
let data = encoder.finish();
23+
let data = encoder.finish().unwrap();
2424

2525
let mut decoder = opaque::Decoder::new(&data, 0);
2626
let obj2 = A::decode(&mut decoder);

0 commit comments

Comments
 (0)