Make Decodable
and Decoder
infallible.
`Decoder` has two impls: - opaque: this impl is already partly infallible, i.e. in some places it currently panics on failure (e.g. if the input is too short, or on a bad `Result` discriminant), and in some places it returns an error (e.g. on a bad `Option` discriminant). The number of places where either happens is surprisingly small, just because the binary representation has very little redundancy and a lot of input reading can occur even on malformed data. - json: this impl is fully fallible, but it's only used (a) for the `.rlink` file production, and there's a `FIXME` comment suggesting it should change to a binary format, and (b) in a few tests in non-fundamental ways. Indeed #85993 is open to remove it entirely. And the top-level places in the compiler that call into decoding just abort on error anyway. So the fallibility is providing little value, and getting rid of it leads to some non-trivial performance improvements. Much of this commit is pretty boring and mechanical. Some notes about a few interesting parts: - The commit removes `Decoder::{Error,error}`. - `InternIteratorElement::intern_with`: the impl for `T` now has the same optimization for small counts that the impl for `Result<T, E>` has, because it's now much hotter. - Decodable impls for SmallVec, LinkedList, VecDeque now all use `collect`, which is nice; the one for `Vec` uses unsafe code, because that gave better perf on some benchmarks.
This commit is contained in:
parent
88600a6d7f
commit
416399dc10
39 changed files with 726 additions and 781 deletions
|
@ -47,8 +47,8 @@ impl<E: Encoder> Encodable<E> for CrateNum {
|
|||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for CrateNum {
|
||||
default fn decode(d: &mut D) -> Result<CrateNum, D::Error> {
|
||||
Ok(CrateNum::from_u32(d.read_u32()?))
|
||||
default fn decode(d: &mut D) -> CrateNum {
|
||||
CrateNum::from_u32(d.read_u32())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -209,7 +209,7 @@ impl<E: Encoder> Encodable<E> for DefIndex {
|
|||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for DefIndex {
|
||||
default fn decode(_: &mut D) -> Result<DefIndex, D::Error> {
|
||||
default fn decode(_: &mut D) -> DefIndex {
|
||||
panic!("cannot decode `DefIndex` with `{}`", std::any::type_name::<D>());
|
||||
}
|
||||
}
|
||||
|
@ -298,12 +298,10 @@ impl<E: Encoder> Encodable<E> for DefId {
|
|||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for DefId {
|
||||
default fn decode(d: &mut D) -> Result<DefId, D::Error> {
|
||||
d.read_struct(|d| {
|
||||
Ok(DefId {
|
||||
krate: d.read_struct_field("krate", Decodable::decode)?,
|
||||
index: d.read_struct_field("index", Decodable::decode)?,
|
||||
})
|
||||
default fn decode(d: &mut D) -> DefId {
|
||||
d.read_struct(|d| DefId {
|
||||
krate: d.read_struct_field("krate", Decodable::decode),
|
||||
index: d.read_struct_field("index", Decodable::decode),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -378,8 +376,8 @@ impl<E: Encoder> Encodable<E> for LocalDefId {
|
|||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for LocalDefId {
|
||||
fn decode(d: &mut D) -> Result<LocalDefId, D::Error> {
|
||||
DefId::decode(d).map(|d| d.expect_local())
|
||||
fn decode(d: &mut D) -> LocalDefId {
|
||||
DefId::decode(d).expect_local()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1314,19 +1314,16 @@ pub fn decode_expn_id(
|
|||
// to track which `SyntaxContext`s we have already decoded.
|
||||
// The provided closure will be invoked to deserialize a `SyntaxContextData`
|
||||
// if we haven't already seen the id of the `SyntaxContext` we are deserializing.
|
||||
pub fn decode_syntax_context<
|
||||
D: Decoder,
|
||||
F: FnOnce(&mut D, u32) -> Result<SyntaxContextData, D::Error>,
|
||||
>(
|
||||
pub fn decode_syntax_context<D: Decoder, F: FnOnce(&mut D, u32) -> SyntaxContextData>(
|
||||
d: &mut D,
|
||||
context: &HygieneDecodeContext,
|
||||
decode_data: F,
|
||||
) -> Result<SyntaxContext, D::Error> {
|
||||
let raw_id: u32 = Decodable::decode(d)?;
|
||||
) -> SyntaxContext {
|
||||
let raw_id: u32 = Decodable::decode(d);
|
||||
if raw_id == 0 {
|
||||
debug!("decode_syntax_context: deserialized root");
|
||||
// The root is special
|
||||
return Ok(SyntaxContext::root());
|
||||
return SyntaxContext::root();
|
||||
}
|
||||
|
||||
let outer_ctxts = &context.remapped_ctxts;
|
||||
|
@ -1334,7 +1331,7 @@ pub fn decode_syntax_context<
|
|||
// Ensure that the lock() temporary is dropped early
|
||||
{
|
||||
if let Some(ctxt) = outer_ctxts.lock().get(raw_id as usize).copied().flatten() {
|
||||
return Ok(ctxt);
|
||||
return ctxt;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1364,7 +1361,7 @@ pub fn decode_syntax_context<
|
|||
|
||||
// Don't try to decode data while holding the lock, since we need to
|
||||
// be able to recursively decode a SyntaxContext
|
||||
let mut ctxt_data = decode_data(d, raw_id)?;
|
||||
let mut ctxt_data = decode_data(d, raw_id);
|
||||
// Reset `dollar_crate_name` so that it will be updated by `update_dollar_crate_names`
|
||||
// We don't care what the encoding crate set this to - we want to resolve it
|
||||
// from the perspective of the current compilation session
|
||||
|
@ -1380,7 +1377,7 @@ pub fn decode_syntax_context<
|
|||
assert_eq!(dummy.dollar_crate_name, kw::Empty);
|
||||
});
|
||||
|
||||
Ok(new_ctxt)
|
||||
new_ctxt
|
||||
}
|
||||
|
||||
fn for_all_ctxts_in<E, F: FnMut(u32, SyntaxContext, &SyntaxContextData) -> Result<(), E>>(
|
||||
|
@ -1422,13 +1419,13 @@ impl<E: Encoder> Encodable<E> for ExpnId {
|
|||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for LocalExpnId {
|
||||
fn decode(d: &mut D) -> Result<Self, D::Error> {
|
||||
ExpnId::decode(d).map(ExpnId::expect_local)
|
||||
fn decode(d: &mut D) -> Self {
|
||||
ExpnId::expect_local(ExpnId::decode(d))
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for ExpnId {
|
||||
default fn decode(_: &mut D) -> Result<Self, D::Error> {
|
||||
default fn decode(_: &mut D) -> Self {
|
||||
panic!("cannot decode `ExpnId` with `{}`", std::any::type_name::<D>());
|
||||
}
|
||||
}
|
||||
|
@ -1451,7 +1448,7 @@ impl<E: Encoder> Encodable<E> for SyntaxContext {
|
|||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for SyntaxContext {
|
||||
default fn decode(_: &mut D) -> Result<Self, D::Error> {
|
||||
default fn decode(_: &mut D) -> Self {
|
||||
panic!("cannot decode `SyntaxContext` with `{}`", std::any::type_name::<D>());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -975,12 +975,12 @@ impl<E: Encoder> Encodable<E> for Span {
|
|||
}
|
||||
}
|
||||
impl<D: Decoder> Decodable<D> for Span {
|
||||
default fn decode(s: &mut D) -> Result<Span, D::Error> {
|
||||
default fn decode(s: &mut D) -> Span {
|
||||
s.read_struct(|d| {
|
||||
let lo = d.read_struct_field("lo", Decodable::decode)?;
|
||||
let hi = d.read_struct_field("hi", Decodable::decode)?;
|
||||
let lo = d.read_struct_field("lo", Decodable::decode);
|
||||
let hi = d.read_struct_field("hi", Decodable::decode);
|
||||
|
||||
Ok(Span::new(lo, hi, SyntaxContext::root(), None))
|
||||
Span::new(lo, hi, SyntaxContext::root(), None)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1448,30 +1448,30 @@ impl<S: Encoder> Encodable<S> for SourceFile {
|
|||
}
|
||||
|
||||
impl<D: Decoder> Decodable<D> for SourceFile {
|
||||
fn decode(d: &mut D) -> Result<SourceFile, D::Error> {
|
||||
fn decode(d: &mut D) -> SourceFile {
|
||||
d.read_struct(|d| {
|
||||
let name: FileName = d.read_struct_field("name", |d| Decodable::decode(d))?;
|
||||
let name: FileName = d.read_struct_field("name", |d| Decodable::decode(d));
|
||||
let src_hash: SourceFileHash =
|
||||
d.read_struct_field("src_hash", |d| Decodable::decode(d))?;
|
||||
let start_pos: BytePos = d.read_struct_field("start_pos", |d| Decodable::decode(d))?;
|
||||
let end_pos: BytePos = d.read_struct_field("end_pos", |d| Decodable::decode(d))?;
|
||||
d.read_struct_field("src_hash", |d| Decodable::decode(d));
|
||||
let start_pos: BytePos = d.read_struct_field("start_pos", |d| Decodable::decode(d));
|
||||
let end_pos: BytePos = d.read_struct_field("end_pos", |d| Decodable::decode(d));
|
||||
let lines: Vec<BytePos> = d.read_struct_field("lines", |d| {
|
||||
let num_lines: u32 = Decodable::decode(d)?;
|
||||
let num_lines: u32 = Decodable::decode(d);
|
||||
let mut lines = Vec::with_capacity(num_lines as usize);
|
||||
|
||||
if num_lines > 0 {
|
||||
// Read the number of bytes used per diff.
|
||||
let bytes_per_diff: u8 = Decodable::decode(d)?;
|
||||
let bytes_per_diff: u8 = Decodable::decode(d);
|
||||
|
||||
// Read the first element.
|
||||
let mut line_start: BytePos = Decodable::decode(d)?;
|
||||
let mut line_start: BytePos = Decodable::decode(d);
|
||||
lines.push(line_start);
|
||||
|
||||
for _ in 1..num_lines {
|
||||
let diff = match bytes_per_diff {
|
||||
1 => d.read_u8()? as u32,
|
||||
2 => d.read_u16()? as u32,
|
||||
4 => d.read_u32()?,
|
||||
1 => d.read_u8() as u32,
|
||||
2 => d.read_u16() as u32,
|
||||
4 => d.read_u32(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
|
@ -1481,17 +1481,17 @@ impl<D: Decoder> Decodable<D> for SourceFile {
|
|||
}
|
||||
}
|
||||
|
||||
Ok(lines)
|
||||
})?;
|
||||
lines
|
||||
});
|
||||
let multibyte_chars: Vec<MultiByteChar> =
|
||||
d.read_struct_field("multibyte_chars", |d| Decodable::decode(d))?;
|
||||
d.read_struct_field("multibyte_chars", |d| Decodable::decode(d));
|
||||
let non_narrow_chars: Vec<NonNarrowChar> =
|
||||
d.read_struct_field("non_narrow_chars", |d| Decodable::decode(d))?;
|
||||
let name_hash: u128 = d.read_struct_field("name_hash", |d| Decodable::decode(d))?;
|
||||
d.read_struct_field("non_narrow_chars", |d| Decodable::decode(d));
|
||||
let name_hash: u128 = d.read_struct_field("name_hash", |d| Decodable::decode(d));
|
||||
let normalized_pos: Vec<NormalizedPos> =
|
||||
d.read_struct_field("normalized_pos", |d| Decodable::decode(d))?;
|
||||
let cnum: CrateNum = d.read_struct_field("cnum", |d| Decodable::decode(d))?;
|
||||
Ok(SourceFile {
|
||||
d.read_struct_field("normalized_pos", |d| Decodable::decode(d));
|
||||
let cnum: CrateNum = d.read_struct_field("cnum", |d| Decodable::decode(d));
|
||||
SourceFile {
|
||||
name,
|
||||
start_pos,
|
||||
end_pos,
|
||||
|
@ -1506,7 +1506,7 @@ impl<D: Decoder> Decodable<D> for SourceFile {
|
|||
normalized_pos,
|
||||
name_hash,
|
||||
cnum,
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1949,8 +1949,8 @@ impl<S: rustc_serialize::Encoder> Encodable<S> for BytePos {
|
|||
}
|
||||
|
||||
impl<D: rustc_serialize::Decoder> Decodable<D> for BytePos {
|
||||
fn decode(d: &mut D) -> Result<BytePos, D::Error> {
|
||||
Ok(BytePos(d.read_u32()?))
|
||||
fn decode(d: &mut D) -> BytePos {
|
||||
BytePos(d.read_u32())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1755,8 +1755,8 @@ impl<S: Encoder> Encodable<S> for Symbol {
|
|||
|
||||
impl<D: Decoder> Decodable<D> for Symbol {
|
||||
#[inline]
|
||||
fn decode(d: &mut D) -> Result<Symbol, D::Error> {
|
||||
Ok(Symbol::intern(&d.read_str()?))
|
||||
fn decode(d: &mut D) -> Symbol {
|
||||
Symbol::intern(&d.read_str())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue