2019-09-16 16:01:11 -05:00
|
|
|
#![allow(rustc::internal)]
|
2019-09-10 11:10:32 -05:00
|
|
|
|
2020-07-18 14:14:50 -05:00
|
|
|
use rustc_macros::{Decodable, Encodable};
|
2023-05-01 03:51:05 -05:00
|
|
|
use rustc_serialize::opaque::{MemDecoder, FileEncoder};
|
2022-06-16 01:00:25 -05:00
|
|
|
use rustc_serialize::{Decodable, Encodable};
|
2019-02-09 02:12:04 -06:00
|
|
|
use std::fmt::Debug;
|
2023-05-01 03:51:05 -05:00
|
|
|
use std::fs;
|
2019-02-09 02:12:04 -06:00
|
|
|
|
2020-07-18 14:14:50 -05:00
|
|
|
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
|
2019-02-09 02:12:04 -06:00
|
|
|
struct Struct {
|
|
|
|
a: (),
|
|
|
|
b: u8,
|
|
|
|
c: u16,
|
|
|
|
d: u32,
|
|
|
|
e: u64,
|
|
|
|
f: usize,
|
|
|
|
|
|
|
|
g: i8,
|
|
|
|
h: i16,
|
|
|
|
i: i32,
|
|
|
|
j: i64,
|
|
|
|
k: isize,
|
|
|
|
|
|
|
|
l: char,
|
|
|
|
m: String,
|
|
|
|
p: bool,
|
|
|
|
q: Option<u32>,
|
|
|
|
}
|
|
|
|
|
2022-06-13 23:52:01 -05:00
|
|
|
fn check_round_trip<
|
2023-05-01 03:51:05 -05:00
|
|
|
T: Encodable<FileEncoder> + for<'a> Decodable<MemDecoder<'a>> + PartialEq + Debug,
|
2022-06-13 23:52:01 -05:00
|
|
|
>(
|
2020-07-18 14:14:50 -05:00
|
|
|
values: Vec<T>,
|
|
|
|
) {
|
2023-05-01 03:51:05 -05:00
|
|
|
let tmpfile = tempfile::NamedTempFile::new().unwrap();
|
|
|
|
let tmpfile = tmpfile.path();
|
|
|
|
|
|
|
|
let mut encoder = FileEncoder::new(&tmpfile).unwrap();
|
2019-02-09 02:12:04 -06:00
|
|
|
for value in &values {
|
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<Vec<u8>, !>`.
- 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`.
2022-06-06 22:30:45 -05:00
|
|
|
Encodable::encode(value, &mut encoder);
|
2019-02-09 02:12:04 -06:00
|
|
|
}
|
2023-05-01 03:51:05 -05:00
|
|
|
encoder.finish().unwrap();
|
2019-02-09 02:12:04 -06:00
|
|
|
|
2023-05-01 03:51:05 -05:00
|
|
|
let data = fs::read(&tmpfile).unwrap();
|
2022-06-13 23:52:01 -05:00
|
|
|
let mut decoder = MemDecoder::new(&data[..], 0);
|
2019-02-09 02:12:04 -06:00
|
|
|
for value in values {
|
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.
2022-01-17 20:22:50 -06:00
|
|
|
let decoded = Decodable::decode(&mut decoder);
|
2019-02-09 02:12:04 -06:00
|
|
|
assert_eq!(value, decoded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unit() {
|
|
|
|
check_round_trip(vec![(), (), (), ()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_u8() {
|
|
|
|
let mut vec = vec![];
|
2020-04-06 16:09:17 -05:00
|
|
|
for i in u8::MIN..u8::MAX {
|
2019-02-09 02:12:04 -06:00
|
|
|
vec.push(i);
|
|
|
|
}
|
|
|
|
check_round_trip(vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_u16() {
|
2023-05-01 03:51:05 -05:00
|
|
|
for i in [u16::MIN, 111, 3333, 55555, u16::MAX] {
|
2019-02-09 02:12:04 -06:00
|
|
|
check_round_trip(vec![1, 2, 3, i, i, i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_u32() {
|
2020-04-06 16:09:17 -05:00
|
|
|
check_round_trip(vec![1, 2, 3, u32::MIN, 0, 1, u32::MAX, 2, 1]);
|
2019-02-09 02:12:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_u64() {
|
2020-04-06 16:09:17 -05:00
|
|
|
check_round_trip(vec![1, 2, 3, u64::MIN, 0, 1, u64::MAX, 2, 1]);
|
2019-02-09 02:12:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_usize() {
|
2020-04-06 16:09:17 -05:00
|
|
|
check_round_trip(vec![1, 2, 3, usize::MIN, 0, 1, usize::MAX, 2, 1]);
|
2019-02-09 02:12:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_i8() {
|
|
|
|
let mut vec = vec![];
|
2020-04-06 16:09:17 -05:00
|
|
|
for i in i8::MIN..i8::MAX {
|
2019-02-09 02:12:04 -06:00
|
|
|
vec.push(i);
|
|
|
|
}
|
|
|
|
check_round_trip(vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_i16() {
|
2023-05-01 03:51:05 -05:00
|
|
|
for i in [i16::MIN, -100, 0, 101, i16::MAX] {
|
2019-02-09 02:12:04 -06:00
|
|
|
check_round_trip(vec![-1, 2, -3, i, i, i, 2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_i32() {
|
2020-04-06 16:09:17 -05:00
|
|
|
check_round_trip(vec![-1, 2, -3, i32::MIN, 0, 1, i32::MAX, 2, 1]);
|
2019-02-09 02:12:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_i64() {
|
2020-04-06 16:09:17 -05:00
|
|
|
check_round_trip(vec![-1, 2, -3, i64::MIN, 0, 1, i64::MAX, 2, 1]);
|
2019-02-09 02:12:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_isize() {
|
2020-04-06 16:09:17 -05:00
|
|
|
check_round_trip(vec![-1, 2, -3, isize::MIN, 0, 1, isize::MAX, 2, 1]);
|
2019-02-09 02:12:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bool() {
|
|
|
|
check_round_trip(vec![false, true, true, false, false]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_char() {
|
|
|
|
let vec = vec!['a', 'b', 'c', 'd', 'A', 'X', ' ', '#', 'Ö', 'Ä', 'µ', '€'];
|
|
|
|
check_round_trip(vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string() {
|
|
|
|
let vec = vec![
|
|
|
|
"abcbuÖeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
|
|
|
|
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
|
|
|
|
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpapmaebn".to_string(),
|
|
|
|
"abcbuÖganeiovÄnameÜavmpßvmeabpnvapeapmaebn".to_string(),
|
|
|
|
"abcbuÖganeiÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
|
|
|
|
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpmaebn".to_string(),
|
|
|
|
"abcbuÖganeiovÄnameÜavmpßvmea€µnvapeapmaebn".to_string(),
|
|
|
|
];
|
|
|
|
|
|
|
|
check_round_trip(vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_option() {
|
|
|
|
check_round_trip(vec![Some(-1i8)]);
|
|
|
|
check_round_trip(vec![Some(-2i16)]);
|
|
|
|
check_round_trip(vec![Some(-3i32)]);
|
|
|
|
check_round_trip(vec![Some(-4i64)]);
|
|
|
|
check_round_trip(vec![Some(-5isize)]);
|
|
|
|
|
|
|
|
let none_i8: Option<i8> = None;
|
|
|
|
check_round_trip(vec![none_i8]);
|
|
|
|
|
|
|
|
let none_i16: Option<i16> = None;
|
|
|
|
check_round_trip(vec![none_i16]);
|
|
|
|
|
|
|
|
let none_i32: Option<i32> = None;
|
|
|
|
check_round_trip(vec![none_i32]);
|
|
|
|
|
|
|
|
let none_i64: Option<i64> = None;
|
|
|
|
check_round_trip(vec![none_i64]);
|
|
|
|
|
|
|
|
let none_isize: Option<isize> = None;
|
|
|
|
check_round_trip(vec![none_isize]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct() {
|
|
|
|
check_round_trip(vec![Struct {
|
|
|
|
a: (),
|
|
|
|
b: 10,
|
|
|
|
c: 11,
|
|
|
|
d: 12,
|
|
|
|
e: 13,
|
|
|
|
f: 14,
|
2019-12-22 16:42:04 -06:00
|
|
|
|
2019-02-09 02:12:04 -06:00
|
|
|
g: 15,
|
|
|
|
h: 16,
|
|
|
|
i: 17,
|
|
|
|
j: 18,
|
|
|
|
k: 19,
|
2019-12-22 16:42:04 -06:00
|
|
|
|
2019-02-09 02:12:04 -06:00
|
|
|
l: 'x',
|
|
|
|
m: "abc".to_string(),
|
|
|
|
p: false,
|
|
|
|
q: None,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
check_round_trip(vec![Struct {
|
|
|
|
a: (),
|
|
|
|
b: 101,
|
|
|
|
c: 111,
|
|
|
|
d: 121,
|
|
|
|
e: 131,
|
|
|
|
f: 141,
|
2019-12-22 16:42:04 -06:00
|
|
|
|
2019-02-09 02:12:04 -06:00
|
|
|
g: -15,
|
|
|
|
h: -16,
|
|
|
|
i: -17,
|
|
|
|
j: -18,
|
|
|
|
k: -19,
|
2019-12-22 16:42:04 -06:00
|
|
|
|
2019-02-09 02:12:04 -06:00
|
|
|
l: 'y',
|
|
|
|
m: "def".to_string(),
|
|
|
|
p: true,
|
|
|
|
q: Some(1234567),
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
2020-07-18 14:14:50 -05:00
|
|
|
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
|
2019-02-09 02:12:04 -06:00
|
|
|
enum Enum {
|
|
|
|
Variant1,
|
2023-04-05 16:46:09 -05:00
|
|
|
Variant2(usize, u32),
|
2019-02-09 02:12:04 -06:00
|
|
|
Variant3 { a: i32, b: char, c: bool },
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_enum() {
|
|
|
|
check_round_trip(vec![
|
|
|
|
Enum::Variant1,
|
2023-04-05 16:46:09 -05:00
|
|
|
Enum::Variant2(1, 25),
|
2019-02-09 02:12:04 -06:00
|
|
|
Enum::Variant3 { a: 3, b: 'b', c: false },
|
|
|
|
Enum::Variant3 { a: -4, b: 'f', c: true },
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sequence() {
|
|
|
|
let mut vec = vec![];
|
|
|
|
for i in -100i64..100i64 {
|
|
|
|
vec.push(i * 100000);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_round_trip(vec![vec]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hash_map() {
|
|
|
|
use std::collections::HashMap;
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
for i in -100i64..100i64 {
|
|
|
|
map.insert(i * 100000, i * 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_round_trip(vec![map]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuples() {
|
2023-04-05 16:46:09 -05:00
|
|
|
check_round_trip(vec![('x', (), false, 5u32)]);
|
|
|
|
check_round_trip(vec![(9i8, 10u16, 15i64)]);
|
2019-02-09 02:12:04 -06:00
|
|
|
check_round_trip(vec![(-12i16, 11u8, 12usize)]);
|
|
|
|
check_round_trip(vec![(1234567isize, 100000000000000u64, 99999999999999i64)]);
|
|
|
|
check_round_trip(vec![(String::new(), "some string".to_string())]);
|
|
|
|
}
|
2023-05-01 20:01:58 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unit_like_struct() {
|
|
|
|
#[derive(Encodable, Decodable, PartialEq, Debug)]
|
|
|
|
struct UnitLikeStruct;
|
|
|
|
|
|
|
|
check_round_trip(vec![UnitLikeStruct]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_box() {
|
|
|
|
#[derive(Encodable, Decodable, PartialEq, Debug)]
|
|
|
|
struct A {
|
|
|
|
foo: Box<[bool]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let obj = A { foo: Box::new([true, false]) };
|
|
|
|
check_round_trip(vec![obj]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cell() {
|
|
|
|
use std::cell::{Cell, RefCell};
|
|
|
|
|
|
|
|
#[derive(Encodable, Decodable, PartialEq, Debug)]
|
|
|
|
struct A {
|
|
|
|
baz: isize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Encodable, Decodable, PartialEq, Debug)]
|
|
|
|
struct B {
|
|
|
|
foo: Cell<bool>,
|
|
|
|
bar: RefCell<A>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let obj = B { foo: Cell::new(true), bar: RefCell::new(A { baz: 2 }) };
|
|
|
|
check_round_trip(vec![obj]);
|
|
|
|
}
|