From c4f91bb28102e88d4ab94e8d3b2dbc25e02dfa53 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sat, 18 Jul 2020 20:14:50 +0100 Subject: [PATCH] Fix rustc_serialize unit tests --- Cargo.lock | 1 + src/librustc_serialize/Cargo.toml | 3 +++ src/librustc_serialize/json.rs | 21 ++++++++++++--------- src/librustc_serialize/tests/json.rs | 21 +++++++++++---------- src/librustc_serialize/tests/opaque.rs | 11 +++++++---- 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 564c3da5223..c1fa3bef07c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3877,6 +3877,7 @@ name = "rustc_serialize" version = "0.0.0" dependencies = [ "indexmap", + "rustc_macros", "smallvec 1.4.0", ] diff --git a/src/librustc_serialize/Cargo.toml b/src/librustc_serialize/Cargo.toml index 84206df50cc..939e6a59ba0 100644 --- a/src/librustc_serialize/Cargo.toml +++ b/src/librustc_serialize/Cargo.toml @@ -11,3 +11,6 @@ path = "lib.rs" [dependencies] indexmap = "1" smallvec = { version = "1.0", features = ["union", "may_dangle"] } + +[dev-dependencies] +rustc_macros = { path = "../librustc_macros" } diff --git a/src/librustc_serialize/json.rs b/src/librustc_serialize/json.rs index dafeb053d83..6c8965aa2e3 100644 --- a/src/librustc_serialize/json.rs +++ b/src/librustc_serialize/json.rs @@ -47,17 +47,17 @@ //! //! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via //! the serialization API. -//! To be able to encode a piece of data, it must implement the `serialize::RustcEncodable` trait. -//! To be able to decode a piece of data, it must implement the `serialize::RustcDecodable` trait. +//! To be able to encode a piece of data, it must implement the `serialize::Encodable` trait. +//! To be able to decode a piece of data, it must implement the `serialize::Decodable` trait. //! The Rust compiler provides an annotation to automatically generate the code for these traits: -//! `#[derive(RustcDecodable, RustcEncodable)]` +//! `#[derive(Decodable, Encodable)]` //! //! The JSON API provides an enum `json::Json` and a trait `ToJson` to encode objects. //! The `ToJson` trait provides a `to_json` method to convert an object into a `json::Json` value. //! A `json::Json` value can be encoded as a string or buffer using the functions described above. //! You can also use the `json::Encoder` object, which implements the `Encoder` trait. //! -//! When using `ToJson` the `RustcEncodable` trait implementation is not mandatory. +//! When using `ToJson` the `Encodable` trait implementation is not mandatory. //! //! # Examples of use //! @@ -68,10 +68,11 @@ //! //! ```rust //! # #![feature(rustc_private)] +//! use rustc_macros::{Decodable, Encodable}; //! use rustc_serialize::json; //! //! // Automatically generate `Decodable` and `Encodable` trait implementations -//! #[derive(RustcDecodable, RustcEncodable)] +//! #[derive(Decodable, Encodable)] //! pub struct TestStruct { //! data_int: u8, //! data_str: String, @@ -100,6 +101,7 @@ //! //! ```rust //! # #![feature(rustc_private)] +//! use rustc_macros::Encodable; //! use rustc_serialize::json::{self, ToJson, Json}; //! //! // A custom data structure @@ -115,8 +117,8 @@ //! } //! } //! -//! // Only generate `RustcEncodable` trait implementation -//! #[derive(RustcEncodable)] +//! // Only generate `Encodable` trait implementation +//! #[derive(Encodable)] //! pub struct ComplexNumRecord { //! uid: u8, //! dsc: String, @@ -137,11 +139,12 @@ //! //! ```rust //! # #![feature(rustc_private)] +//! use rustc_macros::Decodable; //! use std::collections::BTreeMap; //! use rustc_serialize::json::{self, Json, ToJson}; //! -//! // Only generate `RustcDecodable` trait implementation -//! #[derive(RustcDecodable)] +//! // Only generate `Decodable` trait implementation +//! #[derive(Decodable)] //! pub struct TestStruct { //! data_int: u8, //! data_str: String, diff --git a/src/librustc_serialize/tests/json.rs b/src/librustc_serialize/tests/json.rs index 59c481edbca..e3a823127d9 100644 --- a/src/librustc_serialize/tests/json.rs +++ b/src/librustc_serialize/tests/json.rs @@ -9,6 +9,7 @@ use json::{ from_str, DecodeResult, Decoder, DecoderError, Encoder, EncoderError, Json, JsonEvent, Parser, StackElement, }; +use rustc_macros::{Decodable, Encodable}; use rustc_serialize::json; use rustc_serialize::{Decodable, Encodable}; @@ -17,7 +18,7 @@ use std::io::prelude::*; use std::string; use Animal::*; -#[derive(RustcDecodable, Eq, PartialEq, Debug)] +#[derive(Decodable, Eq, PartialEq, Debug)] struct OptionData { opt: Option, } @@ -48,20 +49,20 @@ fn test_decode_option_malformed() { ); } -#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)] +#[derive(PartialEq, Encodable, Decodable, Debug)] enum Animal { Dog, Frog(string::String, isize), } -#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)] +#[derive(PartialEq, Encodable, Decodable, Debug)] struct Inner { a: (), b: usize, c: Vec, } -#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)] +#[derive(PartialEq, Encodable, Decodable, Debug)] struct Outer { inner: Vec, } @@ -568,7 +569,7 @@ fn test_decode_struct() { ); } -#[derive(RustcDecodable)] +#[derive(Decodable)] struct FloatStruct { f: f64, a: Vec, @@ -616,7 +617,7 @@ fn test_multiline_errors() { assert_eq!(from_str("{\n \"foo\":\n \"bar\""), Err(SyntaxError(EOFWhileParsingObject, 3, 8))); } -#[derive(RustcDecodable)] +#[derive(Decodable)] #[allow(dead_code)] struct DecodeStruct { x: f64, @@ -624,12 +625,12 @@ struct DecodeStruct { z: string::String, w: Vec, } -#[derive(RustcDecodable)] +#[derive(Decodable)] enum DecodeEnum { A(f64), B(string::String), } -fn check_err(to_parse: &'static str, expected: DecoderError) { +fn check_err>(to_parse: &'static str, expected: DecoderError) { let res: DecodeResult = match from_str(to_parse) { Err(e) => Err(ParseError(e)), Ok(json) => Decodable::decode(&mut Decoder::new(json)), @@ -933,7 +934,7 @@ fn test_prettyencoder_indent_level_param() { #[test] fn test_hashmap_with_enum_key() { use std::collections::HashMap; - #[derive(RustcEncodable, Eq, Hash, PartialEq, RustcDecodable, Debug)] + #[derive(Encodable, Eq, Hash, PartialEq, Decodable, Debug)] enum Enum { Foo, #[allow(dead_code)] @@ -1254,7 +1255,7 @@ fn test_to_json() { #[test] fn test_encode_hashmap_with_arbitrary_key() { use std::collections::HashMap; - #[derive(PartialEq, Eq, Hash, RustcEncodable)] + #[derive(PartialEq, Eq, Hash, Encodable)] struct ArbitraryType(usize); let mut hm: HashMap = HashMap::new(); hm.insert(ArbitraryType(1), true); diff --git a/src/librustc_serialize/tests/opaque.rs b/src/librustc_serialize/tests/opaque.rs index c8273917007..13b3676a56c 100644 --- a/src/librustc_serialize/tests/opaque.rs +++ b/src/librustc_serialize/tests/opaque.rs @@ -1,10 +1,11 @@ #![allow(rustc::internal)] +use rustc_macros::{Decodable, Encodable}; use rustc_serialize::opaque::{Decoder, Encoder}; use rustc_serialize::{Decodable, Encodable}; use std::fmt::Debug; -#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(PartialEq, Clone, Debug, Encodable, Decodable)] struct Struct { a: (), b: u8, @@ -27,11 +28,13 @@ struct Struct { q: Option, } -fn check_round_trip(values: Vec) { +fn check_round_trip + for<'a> Decodable> + PartialEq + Debug>( + values: Vec, +) { let mut encoder = Encoder::new(Vec::new()); for value in &values { - Encodable::encode(&value, &mut encoder).unwrap(); + Encodable::encode(value, &mut encoder).unwrap(); } let data = encoder.into_inner(); @@ -225,7 +228,7 @@ fn test_struct() { }]); } -#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(PartialEq, Clone, Debug, Encodable, Decodable)] enum Enum { Variant1, Variant2(usize, f32),