Skip to content

Commit 1a97162

Browse files
committed
Auto merge of rust-lang#94732 - nnethercote:infallible-encoder, r=bjorn3
Make `Encodable` and `Encoder` infallible. A follow-up to rust-lang#93066. r? `@ghost`
2 parents e45d997 + b983e42 commit 1a97162

File tree

48 files changed

+705
-855
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+705
-855
lines changed

compiler/rustc_ast/src/ast.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
3131
use rustc_data_structures::sync::Lrc;
3232
use rustc_data_structures::thin_vec::ThinVec;
3333
use rustc_macros::HashStable_Generic;
34-
use rustc_serialize::{self, Decoder, Encoder};
34+
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
3535
use rustc_span::source_map::{respan, Spanned};
3636
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3737
use rustc_span::{Span, DUMMY_SP};
@@ -2472,13 +2472,11 @@ rustc_index::newtype_index! {
24722472
}
24732473
}
24742474

2475-
impl<S: Encoder> rustc_serialize::Encodable<S> for AttrId {
2476-
fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
2477-
Ok(())
2478-
}
2475+
impl<S: Encoder> Encodable<S> for AttrId {
2476+
fn encode(&self, _s: &mut S) {}
24792477
}
24802478

2481-
impl<D: Decoder> rustc_serialize::Decodable<D> for AttrId {
2479+
impl<D: Decoder> Decodable<D> for AttrId {
24822480
fn decode(_: &mut D) -> AttrId {
24832481
crate::attr::mk_attr_id()
24842482
}

compiler/rustc_ast/src/ptr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ impl<D: Decoder, T: 'static + Decodable<D>> Decodable<D> for P<T> {
121121
}
122122

123123
impl<S: Encoder, T: Encodable<S>> Encodable<S> for P<T> {
124-
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
125-
(**self).encode(s)
124+
fn encode(&self, s: &mut S) {
125+
(**self).encode(s);
126126
}
127127
}
128128

@@ -191,8 +191,8 @@ impl<'a, T> IntoIterator for &'a P<[T]> {
191191
}
192192

193193
impl<S: Encoder, T: Encodable<S>> Encodable<S> for P<[T]> {
194-
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
195-
Encodable::encode(&**self, s)
194+
fn encode(&self, s: &mut S) {
195+
Encodable::encode(&**self, s);
196196
}
197197
}
198198

compiler/rustc_ast/src/tokenstream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ impl fmt::Debug for LazyTokenStream {
142142
}
143143

144144
impl<S: Encoder> Encodable<S> for LazyTokenStream {
145-
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
145+
fn encode(&self, s: &mut S) {
146146
// Used by AST json printing.
147-
Encodable::encode(&self.create_token_stream(), s)
147+
Encodable::encode(&self.create_token_stream(), s);
148148
}
149149
}
150150

compiler/rustc_codegen_ssa/src/lib.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ use rustc_middle::dep_graph::WorkProduct;
2929
use rustc_middle::middle::dependency_format::Dependencies;
3030
use rustc_middle::middle::exported_symbols::SymbolExportKind;
3131
use rustc_middle::ty::query::{ExternProviders, Providers};
32-
use rustc_serialize::{opaque, Decodable, Decoder, Encoder};
32+
use rustc_serialize::opaque::{MemDecoder, MemEncoder};
33+
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
3334
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
3435
use rustc_session::cstore::{self, CrateSource};
3536
use rustc_session::utils::NativeLibKind;
@@ -203,16 +204,14 @@ const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");
203204

204205
impl CodegenResults {
205206
pub fn serialize_rlink(codegen_results: &CodegenResults) -> Vec<u8> {
206-
let mut encoder = opaque::Encoder::new(vec![]);
207-
encoder.emit_raw_bytes(RLINK_MAGIC).unwrap();
207+
let mut encoder = MemEncoder::new();
208+
encoder.emit_raw_bytes(RLINK_MAGIC);
208209
// `emit_raw_bytes` is used to make sure that the version representation does not depend on
209210
// Encoder's inner representation of `u32`.
210-
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes()).unwrap();
211-
encoder.emit_str(RUSTC_VERSION.unwrap()).unwrap();
212-
213-
let mut encoder = rustc_serialize::opaque::Encoder::new(encoder.into_inner());
214-
rustc_serialize::Encodable::encode(codegen_results, &mut encoder).unwrap();
215-
encoder.into_inner()
211+
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
212+
encoder.emit_str(RUSTC_VERSION.unwrap());
213+
Encodable::encode(codegen_results, &mut encoder);
214+
encoder.finish()
216215
}
217216

218217
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, String> {
@@ -232,7 +231,7 @@ impl CodegenResults {
232231
return Err(".rlink file was produced with encoding version {version_array}, but the current version is {RLINK_VERSION}".to_string());
233232
}
234233

235-
let mut decoder = opaque::Decoder::new(&data[4..], 0);
234+
let mut decoder = MemDecoder::new(&data[4..], 0);
236235
let rustc_version = decoder.read_str();
237236
let current_version = RUSTC_VERSION.unwrap();
238237
if rustc_version != current_version {

compiler/rustc_data_structures/src/fingerprint.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::stable_hasher;
2-
use rustc_serialize::{Decodable, Encodable};
2+
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
33
use std::convert::TryInto;
44
use std::hash::{Hash, Hasher};
55

@@ -142,15 +142,14 @@ impl stable_hasher::StableHasherResult for Fingerprint {
142142

143143
impl_stable_hash_via_hash!(Fingerprint);
144144

145-
impl<E: rustc_serialize::Encoder> Encodable<E> for Fingerprint {
145+
impl<E: Encoder> Encodable<E> for Fingerprint {
146146
#[inline]
147-
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
148-
s.emit_raw_bytes(&self.to_le_bytes())?;
149-
Ok(())
147+
fn encode(&self, s: &mut E) {
148+
s.emit_raw_bytes(&self.to_le_bytes());
150149
}
151150
}
152151

153-
impl<D: rustc_serialize::Decoder> Decodable<D> for Fingerprint {
152+
impl<D: Decoder> Decodable<D> for Fingerprint {
154153
#[inline]
155154
fn decode(d: &mut D) -> Self {
156155
Fingerprint::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap())
@@ -185,16 +184,16 @@ impl std::fmt::Display for PackedFingerprint {
185184
}
186185
}
187186

188-
impl<E: rustc_serialize::Encoder> Encodable<E> for PackedFingerprint {
187+
impl<E: Encoder> Encodable<E> for PackedFingerprint {
189188
#[inline]
190-
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
189+
fn encode(&self, s: &mut E) {
191190
// Copy to avoid taking reference to packed field.
192191
let copy = self.0;
193-
copy.encode(s)
192+
copy.encode(s);
194193
}
195194
}
196195

197-
impl<D: rustc_serialize::Decoder> Decodable<D> for PackedFingerprint {
196+
impl<D: Decoder> Decodable<D> for PackedFingerprint {
198197
#[inline]
199198
fn decode(d: &mut D) -> Self {
200199
Self(Fingerprint::decode(d))

compiler/rustc_data_structures/src/svh.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ impl fmt::Display for Svh {
4949
}
5050

5151
impl<S: Encoder> Encodable<S> for Svh {
52-
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
53-
s.emit_u64(self.as_u64().to_le())
52+
fn encode(&self, s: &mut S) {
53+
s.emit_u64(self.as_u64().to_le());
5454
}
5555
}
5656

compiler/rustc_incremental/src/persist/file_format.rs

+20-29
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,20 @@ const HEADER_FORMAT_VERSION: u16 = 0;
3030
/// the Git commit hash.
3131
const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");
3232

33-
pub(crate) fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) -> FileEncodeResult {
34-
stream.emit_raw_bytes(FILE_MAGIC)?;
35-
stream.emit_raw_bytes(&[
36-
(HEADER_FORMAT_VERSION >> 0) as u8,
37-
(HEADER_FORMAT_VERSION >> 8) as u8,
38-
])?;
33+
pub(crate) fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) {
34+
stream.emit_raw_bytes(FILE_MAGIC);
35+
stream
36+
.emit_raw_bytes(&[(HEADER_FORMAT_VERSION >> 0) as u8, (HEADER_FORMAT_VERSION >> 8) as u8]);
3937

4038
let rustc_version = rustc_version(nightly_build);
4139
assert_eq!(rustc_version.len(), (rustc_version.len() as u8) as usize);
42-
stream.emit_raw_bytes(&[rustc_version.len() as u8])?;
43-
stream.emit_raw_bytes(rustc_version.as_bytes())
40+
stream.emit_raw_bytes(&[rustc_version.len() as u8]);
41+
stream.emit_raw_bytes(rustc_version.as_bytes());
4442
}
4543

4644
pub(crate) fn save_in<F>(sess: &Session, path_buf: PathBuf, name: &str, encode: F)
4745
where
48-
F: FnOnce(&mut FileEncoder) -> FileEncodeResult,
46+
F: FnOnce(FileEncoder) -> FileEncodeResult,
4947
{
5048
debug!("save: storing data in {}", path_buf.display());
5149

@@ -80,28 +78,21 @@ where
8078
}
8179
};
8280

83-
if let Err(err) = write_file_header(&mut encoder, sess.is_nightly_build()) {
84-
sess.err(&format!("failed to write {} header to `{}`: {}", name, path_buf.display(), err));
85-
return;
86-
}
87-
88-
if let Err(err) = encode(&mut encoder) {
89-
sess.err(&format!("failed to write {} to `{}`: {}", name, path_buf.display(), err));
90-
return;
91-
}
81+
write_file_header(&mut encoder, sess.is_nightly_build());
9282

93-
if let Err(err) = encoder.flush() {
94-
sess.err(&format!("failed to flush {} to `{}`: {}", name, path_buf.display(), err));
95-
return;
83+
match encode(encoder) {
84+
Ok(position) => {
85+
sess.prof.artifact_size(
86+
&name.replace(' ', "_"),
87+
path_buf.file_name().unwrap().to_string_lossy(),
88+
position as u64,
89+
);
90+
debug!("save: data written to disk successfully");
91+
}
92+
Err(err) => {
93+
sess.err(&format!("failed to write {} to `{}`: {}", name, path_buf.display(), err));
94+
}
9695
}
97-
98-
sess.prof.artifact_size(
99-
&name.replace(' ', "_"),
100-
path_buf.file_name().unwrap().to_string_lossy(),
101-
encoder.position() as u64,
102-
);
103-
104-
debug!("save: data written to disk successfully");
10596
}
10697

10798
/// Reads the contents of a file with a file header as defined in this module.

compiler/rustc_incremental/src/persist/load.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::fx::FxHashMap;
44
use rustc_data_structures::memmap::Mmap;
55
use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId};
66
use rustc_middle::ty::OnDiskCache;
7-
use rustc_serialize::opaque::Decoder;
7+
use rustc_serialize::opaque::MemDecoder;
88
use rustc_serialize::Decodable;
99
use rustc_session::config::IncrementalStateAssertion;
1010
use rustc_session::Session;
@@ -156,7 +156,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
156156

157157
if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result {
158158
// Decode the list of work_products
159-
let mut work_product_decoder = Decoder::new(&work_products_data[..], start_pos);
159+
let mut work_product_decoder = MemDecoder::new(&work_products_data[..], start_pos);
160160
let work_products: Vec<SerializedWorkProduct> =
161161
Decodable::decode(&mut work_product_decoder);
162162

@@ -193,7 +193,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
193193
LoadResult::DataOutOfDate => LoadResult::DataOutOfDate,
194194
LoadResult::Error { message } => LoadResult::Error { message },
195195
LoadResult::Ok { data: (bytes, start_pos) } => {
196-
let mut decoder = Decoder::new(&bytes, start_pos);
196+
let mut decoder = MemDecoder::new(&bytes, start_pos);
197197
let prev_commandline_args_hash = u64::decode(&mut decoder);
198198

199199
if prev_commandline_args_hash != expected_hash {

compiler/rustc_incremental/src/persist/save.rs

+8-21
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;
77
use rustc_session::Session;
88
use std::fs;
99

@@ -96,8 +96,9 @@ pub fn save_work_product_index(
9696
debug!("save_work_product_index()");
9797
dep_graph.assert_ignored();
9898
let path = work_products_path(sess);
99-
file_format::save_in(sess, path, "work product index", |e| {
100-
encode_work_product_index(&new_work_products, e)
99+
file_format::save_in(sess, path, "work product index", |mut e| {
100+
encode_work_product_index(&new_work_products, &mut e);
101+
e.finish()
101102
});
102103

103104
// 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(
123124
fn encode_work_product_index(
124125
work_products: &FxHashMap<WorkProductId, WorkProduct>,
125126
encoder: &mut FileEncoder,
126-
) -> FileEncodeResult {
127+
) {
127128
let serialized_products: Vec<_> = work_products
128129
.iter()
129130
.map(|(id, work_product)| SerializedWorkProduct {
@@ -135,7 +136,7 @@ fn encode_work_product_index(
135136
serialized_products.encode(encoder)
136137
}
137138

138-
fn encode_query_cache(tcx: TyCtxt<'_>, encoder: &mut FileEncoder) -> FileEncodeResult {
139+
fn encode_query_cache(tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult {
139140
tcx.sess.time("incr_comp_serialize_result_cache", || tcx.serialize_query_result_cache(encoder))
140141
}
141142

@@ -170,24 +171,10 @@ pub fn build_dep_graph(
170171
}
171172
};
172173

173-
if let Err(err) = file_format::write_file_header(&mut encoder, sess.is_nightly_build()) {
174-
sess.err(&format!(
175-
"failed to write dependency graph header to `{}`: {}",
176-
path_buf.display(),
177-
err
178-
));
179-
return None;
180-
}
174+
file_format::write_file_header(&mut encoder, sess.is_nightly_build());
181175

182176
// First encode the commandline arguments hash
183-
if let Err(err) = sess.opts.dep_tracking_hash(false).encode(&mut encoder) {
184-
sess.err(&format!(
185-
"failed to write dependency graph hash `{}`: {}",
186-
path_buf.display(),
187-
err
188-
));
189-
return None;
190-
}
177+
sess.opts.dep_tracking_hash(false).encode(&mut encoder);
191178

192179
Some(DepGraph::new(
193180
&sess.prof,

compiler/rustc_index/src/vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pub struct IndexVec<I: Idx, T> {
6060
unsafe impl<I: Idx, T> Send for IndexVec<I, T> where T: Send {}
6161

6262
impl<S: Encoder, I: Idx, T: Encodable<S>> Encodable<S> for IndexVec<I, T> {
63-
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
64-
Encodable::encode(&self.raw, s)
63+
fn encode(&self, s: &mut S) {
64+
Encodable::encode(&self.raw, s);
6565
}
6666
}
6767

compiler/rustc_macros/src/newtype.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ impl Parse for Newtype {
137137
}
138138
}
139139
impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for #name {
140-
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
141-
e.emit_u32(self.private)
140+
fn encode(&self, e: &mut E) {
141+
e.emit_u32(self.private);
142142
}
143143
}
144144
}

0 commit comments

Comments
 (0)