diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index 5d175a3ade9..72395bd31ec 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -216,7 +216,7 @@ macro_rules! encode_fields { $( $enc.emit_struct_field( stringify!($name), - idx, + idx == 0, |enc| $name.encode(enc), )?; idx += 1; @@ -229,7 +229,7 @@ macro_rules! encode_fields { // Special-case encoder to skip tool_metadata if not set impl Encodable for Diagnostic { fn encode(&self, s: &mut E) -> Result<(), E::Error> { - s.emit_struct("diagnostic", 7, |s| { + s.emit_struct(false, |s| { let mut idx = 0; idx = encode_fields!( diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index 72bd4804e98..7bc669f2b00 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -43,12 +43,9 @@ fn decodable_body( let decode_body = match s.variants() { [vi] => { let construct = vi.construct(|field, index| decode_field(field, index, true)); - let n_fields = vi.ast().fields.len(); quote! { ::rustc_serialize::Decoder::read_struct( __decoder, - #ty_name, - #n_fields, |__decoder| { ::std::result::Result::Ok(#construct) }, ) } @@ -77,7 +74,6 @@ fn decodable_body( quote! { ::rustc_serialize::Decoder::read_enum( __decoder, - #ty_name, |__decoder| { ::rustc_serialize::Decoder::read_enum_variant( __decoder, @@ -128,7 +124,7 @@ fn decode_field(field: &syn::Field, index: usize, is_struct: bool) -> proc_macro quote! { match ::rustc_serialize::Decoder::#decode_method( - __decoder, #opt_field_name #index, #decode_inner_method) { + __decoder, #opt_field_name #decode_inner_method) { ::std::result::Result::Ok(__res) => __res, ::std::result::Result::Err(__err) => return ::std::result::Result::Err(__err), } @@ -183,7 +179,6 @@ fn encodable_body( } }); - let ty_name = s.ast().ident.to_string(); let encode_body = match s.variants() { [_] => { let mut field_idx = 0usize; @@ -197,11 +192,12 @@ fn encodable_body( .ident .as_ref() .map_or_else(|| field_idx.to_string(), |i| i.to_string()); + let first = field_idx == 0; let result = quote! { match ::rustc_serialize::Encoder::emit_struct_field( __encoder, #field_name, - #field_idx, + #first, |__encoder| ::rustc_serialize::Encodable::<#encoder_ty>::encode(#bind_ident, __encoder), ) { @@ -215,8 +211,9 @@ fn encodable_body( }) .collect::() }); + let no_fields = field_idx == 0; quote! { - ::rustc_serialize::Encoder::emit_struct(__encoder, #ty_name, #field_idx, |__encoder| { + ::rustc_serialize::Encoder::emit_struct(__encoder, #no_fields, |__encoder| { ::std::result::Result::Ok(match *self { #encode_inner }) }) } @@ -232,10 +229,11 @@ fn encodable_body( .iter() .map(|binding| { let bind_ident = &binding.binding; + let first = field_idx == 0; let result = quote! { match ::rustc_serialize::Encoder::emit_enum_variant_arg( __encoder, - #field_idx, + #first, |__encoder| ::rustc_serialize::Encodable::<#encoder_ty>::encode(#bind_ident, __encoder), ) { @@ -260,7 +258,7 @@ fn encodable_body( result }); quote! { - ::rustc_serialize::Encoder::emit_enum(__encoder, #ty_name, |__encoder| { + ::rustc_serialize::Encoder::emit_enum(__encoder, |__encoder| { match *self { #encode_inner } diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index 6a84a28be66..73c00fc49ba 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -122,21 +122,21 @@ fn decode(d: &mut opaque::Decoder<'a>) -> Result, String> let mut edge_list_data = Vec::with_capacity(edge_count); for _index in 0..node_count { - d.read_struct("NodeInfo", 3, |d| { - let dep_node: DepNode = d.read_struct_field("node", 0, Decodable::decode)?; + d.read_struct(|d| { + let dep_node: DepNode = d.read_struct_field("node", Decodable::decode)?; let _i: SerializedDepNodeIndex = nodes.push(dep_node); debug_assert_eq!(_i.index(), _index); let fingerprint: Fingerprint = - d.read_struct_field("fingerprint", 1, Decodable::decode)?; + d.read_struct_field("fingerprint", Decodable::decode)?; let _i: SerializedDepNodeIndex = fingerprints.push(fingerprint); debug_assert_eq!(_i.index(), _index); - d.read_struct_field("edges", 2, |d| { + d.read_struct_field("edges", |d| { d.read_seq(|d, len| { let start = edge_list_data.len().try_into().unwrap(); - for e in 0..len { - let edge = d.read_seq_elt(e, Decodable::decode)?; + for _ in 0..len { + let edge = d.read_seq_elt(Decodable::decode)?; edge_list_data.push(edge); } let end = edge_list_data.len().try_into().unwrap(); diff --git a/compiler/rustc_serialize/src/collection_impls.rs b/compiler/rustc_serialize/src/collection_impls.rs index ae6d27e037b..80a7f650188 100644 --- a/compiler/rustc_serialize/src/collection_impls.rs +++ b/compiler/rustc_serialize/src/collection_impls.rs @@ -21,8 +21,8 @@ fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut vec = SmallVec::with_capacity(len); // FIXME(#48994) - could just be collected into a Result - for i in 0..len { - vec.push(d.read_seq_elt(i, |d| Decodable::decode(d))?); + for _ in 0..len { + vec.push(d.read_seq_elt(|d| Decodable::decode(d))?); } Ok(vec) }) @@ -44,8 +44,8 @@ impl> Decodable for LinkedList { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut list = LinkedList::new(); - for i in 0..len { - list.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))?); + for _ in 0..len { + list.push_back(d.read_seq_elt(|d| Decodable::decode(d))?); } Ok(list) }) @@ -67,8 +67,8 @@ impl> Decodable for VecDeque { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut deque: VecDeque = VecDeque::with_capacity(len); - for i in 0..len { - deque.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))?); + for _ in 0..len { + deque.push_back(d.read_seq_elt(|d| Decodable::decode(d))?); } Ok(deque) }) @@ -84,7 +84,7 @@ fn encode(&self, e: &mut S) -> Result<(), S::Error> { e.emit_map(self.len(), |e| { for (i, (key, val)) in self.iter().enumerate() { e.emit_map_elt_key(i, |e| key.encode(e))?; - e.emit_map_elt_val(i, |e| val.encode(e))?; + e.emit_map_elt_val(|e| val.encode(e))?; } Ok(()) }) @@ -99,9 +99,9 @@ impl Decodable for BTreeMap fn decode(d: &mut D) -> Result, D::Error> { d.read_map(|d, len| { let mut map = BTreeMap::new(); - for i in 0..len { - let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?; - let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?; + for _ in 0..len { + let key = d.read_map_elt_key(|d| Decodable::decode(d))?; + let val = d.read_map_elt_val(|d| Decodable::decode(d))?; map.insert(key, val); } Ok(map) @@ -130,8 +130,8 @@ impl Decodable for BTreeSet fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut set = BTreeSet::new(); - for i in 0..len { - set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?); + for _ in 0..len { + set.insert(d.read_seq_elt(|d| Decodable::decode(d))?); } Ok(set) }) @@ -148,7 +148,7 @@ fn encode(&self, e: &mut E) -> Result<(), E::Error> { e.emit_map(self.len(), |e| { for (i, (key, val)) in self.iter().enumerate() { e.emit_map_elt_key(i, |e| key.encode(e))?; - e.emit_map_elt_val(i, |e| val.encode(e))?; + e.emit_map_elt_val(|e| val.encode(e))?; } Ok(()) }) @@ -165,9 +165,9 @@ fn decode(d: &mut D) -> Result, D::Error> { d.read_map(|d, len| { let state = Default::default(); let mut map = HashMap::with_capacity_and_hasher(len, state); - for i in 0..len { - let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?; - let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?; + for _ in 0..len { + let key = d.read_map_elt_key(|d| Decodable::decode(d))?; + let val = d.read_map_elt_val(|d| Decodable::decode(d))?; map.insert(key, val); } Ok(map) @@ -209,8 +209,8 @@ fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let state = Default::default(); let mut set = HashSet::with_capacity_and_hasher(len, state); - for i in 0..len { - set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?); + for _ in 0..len { + set.insert(d.read_seq_elt(|d| Decodable::decode(d))?); } Ok(set) }) @@ -227,7 +227,7 @@ fn encode(&self, e: &mut E) -> Result<(), E::Error> { e.emit_map(self.len(), |e| { for (i, (key, val)) in self.iter().enumerate() { e.emit_map_elt_key(i, |e| key.encode(e))?; - e.emit_map_elt_val(i, |e| val.encode(e))?; + e.emit_map_elt_val(|e| val.encode(e))?; } Ok(()) }) @@ -244,9 +244,9 @@ fn decode(d: &mut D) -> Result, D::Error> { d.read_map(|d, len| { let state = Default::default(); let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state); - for i in 0..len { - let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?; - let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?; + for _ in 0..len { + let key = d.read_map_elt_key(|d| Decodable::decode(d))?; + let val = d.read_map_elt_val(|d| Decodable::decode(d))?; map.insert(key, val); } Ok(map) @@ -278,8 +278,8 @@ fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let state = Default::default(); let mut set = indexmap::IndexSet::with_capacity_and_hasher(len, state); - for i in 0..len { - set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?); + for _ in 0..len { + set.insert(d.read_seq_elt(|d| Decodable::decode(d))?); } Ok(set) }) diff --git a/compiler/rustc_serialize/src/json.rs b/compiler/rustc_serialize/src/json.rs index 78a102c5c23..b79adb6f7bc 100644 --- a/compiler/rustc_serialize/src/json.rs +++ b/compiler/rustc_serialize/src/json.rs @@ -560,7 +560,7 @@ fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error> { Ok(()) } - fn emit_enum(&mut self, _name: &str, f: F) -> EncodeResult + fn emit_enum(&mut self, f: F) -> EncodeResult where F: FnOnce(&mut Encoder<'a>) -> EncodeResult, { @@ -589,46 +589,20 @@ fn emit_enum_variant(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> } } - fn emit_enum_variant_arg(&mut self, idx: usize, f: F) -> EncodeResult + fn emit_enum_variant_arg(&mut self, first: bool, f: F) -> EncodeResult where F: FnOnce(&mut Encoder<'a>) -> EncodeResult, { if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); } - if idx != 0 { + if !first { write!(self.writer, ",")?; } f(self) } - fn emit_enum_struct_variant( - &mut self, - name: &str, - id: usize, - cnt: usize, - f: F, - ) -> EncodeResult - where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - if self.is_emitting_map_key { - return Err(EncoderError::BadHashmapKey); - } - self.emit_enum_variant(name, id, cnt, f) - } - - fn emit_enum_struct_variant_field(&mut self, _: &str, idx: usize, f: F) -> EncodeResult - where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - if self.is_emitting_map_key { - return Err(EncoderError::BadHashmapKey); - } - self.emit_enum_variant_arg(idx, f) - } - - fn emit_struct(&mut self, _: &str, _: usize, f: F) -> EncodeResult + fn emit_struct(&mut self, _: bool, f: F) -> EncodeResult where F: FnOnce(&mut Encoder<'a>) -> EncodeResult, { @@ -641,14 +615,14 @@ fn emit_struct(&mut self, _: &str, _: usize, f: F) -> EncodeResult Ok(()) } - fn emit_struct_field(&mut self, name: &str, idx: usize, f: F) -> EncodeResult + fn emit_struct_field(&mut self, name: &str, first: bool, f: F) -> EncodeResult where F: FnOnce(&mut Encoder<'a>) -> EncodeResult, { if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); } - if idx != 0 { + if !first { write!(self.writer, ",")?; } escape_str(self.writer, name)?; @@ -675,25 +649,6 @@ fn emit_tuple_arg(&mut self, idx: usize, f: F) -> EncodeResult self.emit_seq_elt(idx, f) } - fn emit_tuple_struct(&mut self, _name: &str, len: usize, f: F) -> EncodeResult - where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - if self.is_emitting_map_key { - return Err(EncoderError::BadHashmapKey); - } - self.emit_seq(len, f) - } - fn emit_tuple_struct_arg(&mut self, idx: usize, f: F) -> EncodeResult - where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - if self.is_emitting_map_key { - return Err(EncoderError::BadHashmapKey); - } - self.emit_seq_elt(idx, f) - } - fn emit_option(&mut self, f: F) -> EncodeResult where F: FnOnce(&mut Encoder<'a>) -> EncodeResult, @@ -774,7 +729,7 @@ fn emit_map_elt_key(&mut self, idx: usize, f: F) -> EncodeResult Ok(()) } - fn emit_map_elt_val(&mut self, _idx: usize, f: F) -> EncodeResult + fn emit_map_elt_val(&mut self, f: F) -> EncodeResult where F: FnOnce(&mut Encoder<'a>) -> EncodeResult, { @@ -892,7 +847,7 @@ fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error> { Ok(()) } - fn emit_enum(&mut self, _name: &str, f: F) -> EncodeResult + fn emit_enum(&mut self, f: F) -> EncodeResult where F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, { @@ -930,54 +885,28 @@ fn emit_enum_variant(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> } } - fn emit_enum_variant_arg(&mut self, idx: usize, f: F) -> EncodeResult + fn emit_enum_variant_arg(&mut self, first: bool, f: F) -> EncodeResult where F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, { if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); } - if idx != 0 { + if !first { writeln!(self.writer, ",")?; } spaces(self.writer, self.curr_indent)?; f(self) } - fn emit_enum_struct_variant( - &mut self, - name: &str, - id: usize, - cnt: usize, - f: F, - ) -> EncodeResult + fn emit_struct(&mut self, no_fields: bool, f: F) -> EncodeResult where F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, { if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); } - self.emit_enum_variant(name, id, cnt, f) - } - - fn emit_enum_struct_variant_field(&mut self, _: &str, idx: usize, f: F) -> EncodeResult - where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if self.is_emitting_map_key { - return Err(EncoderError::BadHashmapKey); - } - self.emit_enum_variant_arg(idx, f) - } - - fn emit_struct(&mut self, _: &str, len: usize, f: F) -> EncodeResult - where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if self.is_emitting_map_key { - return Err(EncoderError::BadHashmapKey); - } - if len == 0 { + if no_fields { write!(self.writer, "{{}}")?; } else { write!(self.writer, "{{")?; @@ -991,14 +920,14 @@ fn emit_struct(&mut self, _: &str, len: usize, f: F) -> EncodeResult Ok(()) } - fn emit_struct_field(&mut self, name: &str, idx: usize, f: F) -> EncodeResult + fn emit_struct_field(&mut self, name: &str, first: bool, f: F) -> EncodeResult where F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, { if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); } - if idx == 0 { + if first { writeln!(self.writer)?; } else { writeln!(self.writer, ",")?; @@ -1028,25 +957,6 @@ fn emit_tuple_arg(&mut self, idx: usize, f: F) -> EncodeResult self.emit_seq_elt(idx, f) } - fn emit_tuple_struct(&mut self, _: &str, len: usize, f: F) -> EncodeResult - where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if self.is_emitting_map_key { - return Err(EncoderError::BadHashmapKey); - } - self.emit_seq(len, f) - } - fn emit_tuple_struct_arg(&mut self, idx: usize, f: F) -> EncodeResult - where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if self.is_emitting_map_key { - return Err(EncoderError::BadHashmapKey); - } - self.emit_seq_elt(idx, f) - } - fn emit_option(&mut self, f: F) -> EncodeResult where F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, @@ -1149,7 +1059,7 @@ fn emit_map_elt_key(&mut self, idx: usize, f: F) -> EncodeResult Ok(()) } - fn emit_map_elt_val(&mut self, _idx: usize, f: F) -> EncodeResult + fn emit_map_elt_val(&mut self, f: F) -> EncodeResult where F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, { @@ -2373,7 +2283,7 @@ fn read_raw_bytes_into(&mut self, s: &mut [u8]) -> Result<(), Self::Error> { Ok(()) } - fn read_enum(&mut self, _name: &str, f: F) -> DecodeResult + fn read_enum(&mut self, f: F) -> DecodeResult where F: FnOnce(&mut Decoder) -> DecodeResult, { @@ -2410,33 +2320,14 @@ fn read_enum_variant(&mut self, names: &[&str], mut f: F) -> DecodeResult< f(self, idx) } - fn read_enum_variant_arg(&mut self, _idx: usize, f: F) -> DecodeResult + fn read_enum_variant_arg(&mut self, f: F) -> DecodeResult where F: FnOnce(&mut Decoder) -> DecodeResult, { f(self) } - fn read_enum_struct_variant(&mut self, names: &[&str], f: F) -> DecodeResult - where - F: FnMut(&mut Decoder, usize) -> DecodeResult, - { - self.read_enum_variant(names, f) - } - - fn read_enum_struct_variant_field( - &mut self, - _name: &str, - idx: usize, - f: F, - ) -> DecodeResult - where - F: FnOnce(&mut Decoder) -> DecodeResult, - { - self.read_enum_variant_arg(idx, f) - } - - fn read_struct(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult + fn read_struct(&mut self, f: F) -> DecodeResult where F: FnOnce(&mut Decoder) -> DecodeResult, { @@ -2445,7 +2336,7 @@ fn read_struct(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult< Ok(value) } - fn read_struct_field(&mut self, name: &str, _idx: usize, f: F) -> DecodeResult + fn read_struct_field(&mut self, name: &str, f: F) -> DecodeResult where F: FnOnce(&mut Decoder) -> DecodeResult, { @@ -2483,25 +2374,11 @@ fn read_tuple(&mut self, tuple_len: usize, f: F) -> DecodeResult }) } - fn read_tuple_arg(&mut self, idx: usize, f: F) -> DecodeResult + fn read_tuple_arg(&mut self, f: F) -> DecodeResult where F: FnOnce(&mut Decoder) -> DecodeResult, { - self.read_seq_elt(idx, f) - } - - fn read_tuple_struct(&mut self, _name: &str, len: usize, f: F) -> DecodeResult - where - F: FnOnce(&mut Decoder) -> DecodeResult, - { - self.read_tuple(len, f) - } - - fn read_tuple_struct_arg(&mut self, idx: usize, f: F) -> DecodeResult - where - F: FnOnce(&mut Decoder) -> DecodeResult, - { - self.read_tuple_arg(idx, f) + self.read_seq_elt(f) } fn read_option(&mut self, mut f: F) -> DecodeResult @@ -2527,7 +2404,7 @@ fn read_seq(&mut self, f: F) -> DecodeResult f(self, len) } - fn read_seq_elt(&mut self, _idx: usize, f: F) -> DecodeResult + fn read_seq_elt(&mut self, f: F) -> DecodeResult where F: FnOnce(&mut Decoder) -> DecodeResult, { @@ -2547,14 +2424,14 @@ fn read_map(&mut self, f: F) -> DecodeResult f(self, len) } - fn read_map_elt_key(&mut self, _idx: usize, f: F) -> DecodeResult + fn read_map_elt_key(&mut self, f: F) -> DecodeResult where F: FnOnce(&mut Decoder) -> DecodeResult, { f(self) } - fn read_map_elt_val(&mut self, _idx: usize, f: F) -> DecodeResult + fn read_map_elt_val(&mut self, f: F) -> DecodeResult where F: FnOnce(&mut Decoder) -> DecodeResult, { diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index d3e5f306970..bb3c537ef19 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -37,7 +37,7 @@ pub trait Encoder { // Compound types: #[inline] - fn emit_enum(&mut self, _name: &str, f: F) -> Result<(), Self::Error> + fn emit_enum(&mut self, f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error>, { @@ -59,40 +59,7 @@ fn emit_enum_variant( } #[inline] - fn emit_enum_variant_arg(&mut self, _a_idx: usize, f: F) -> Result<(), Self::Error> - where - F: FnOnce(&mut Self) -> Result<(), Self::Error>, - { - f(self) - } - - fn emit_enum_struct_variant( - &mut self, - v_name: &str, - v_id: usize, - len: usize, - f: F, - ) -> Result<(), Self::Error> - where - F: FnOnce(&mut Self) -> Result<(), Self::Error>, - { - self.emit_enum_variant(v_name, v_id, len, f) - } - - fn emit_enum_struct_variant_field( - &mut self, - _f_name: &str, - f_idx: usize, - f: F, - ) -> Result<(), Self::Error> - where - F: FnOnce(&mut Self) -> Result<(), Self::Error>, - { - self.emit_enum_variant_arg(f_idx, f) - } - - #[inline] - fn emit_struct(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Self::Error> + fn emit_enum_variant_arg(&mut self, _first: bool, f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error>, { @@ -100,12 +67,15 @@ fn emit_struct(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Self: } #[inline] - fn emit_struct_field( - &mut self, - _f_name: &str, - _f_idx: usize, - f: F, - ) -> Result<(), Self::Error> + fn emit_struct(&mut self, _no_fields: bool, f: F) -> Result<(), Self::Error> + where + F: FnOnce(&mut Self) -> Result<(), Self::Error>, + { + f(self) + } + + #[inline] + fn emit_struct_field(&mut self, _f_name: &str, _first: bool, f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error>, { @@ -128,26 +98,12 @@ fn emit_tuple_arg(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> f(self) } - fn emit_tuple_struct(&mut self, _name: &str, len: usize, f: F) -> Result<(), Self::Error> - where - F: FnOnce(&mut Self) -> Result<(), Self::Error>, - { - self.emit_tuple(len, f) - } - - fn emit_tuple_struct_arg(&mut self, f_idx: usize, f: F) -> Result<(), Self::Error> - where - F: FnOnce(&mut Self) -> Result<(), Self::Error>, - { - self.emit_tuple_arg(f_idx, f) - } - // Specialized types: fn emit_option(&mut self, f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error>, { - self.emit_enum("Option", f) + self.emit_enum(f) } #[inline] @@ -195,7 +151,7 @@ fn emit_map_elt_key(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> } #[inline] - fn emit_map_elt_val(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> + fn emit_map_elt_val(&mut self, f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error>, { @@ -229,7 +185,7 @@ pub trait Decoder { // Compound types: #[inline] - fn read_enum(&mut self, _name: &str, f: F) -> Result + fn read_enum(&mut self, f: F) -> Result where F: FnOnce(&mut Self) -> Result, { @@ -246,34 +202,7 @@ fn read_enum_variant(&mut self, _names: &[&str], mut f: F) -> Result(&mut self, _a_idx: usize, f: F) -> Result - where - F: FnOnce(&mut Self) -> Result, - { - f(self) - } - - fn read_enum_struct_variant(&mut self, names: &[&str], f: F) -> Result - where - F: FnMut(&mut Self, usize) -> Result, - { - self.read_enum_variant(names, f) - } - - fn read_enum_struct_variant_field( - &mut self, - _f_name: &str, - f_idx: usize, - f: F, - ) -> Result - where - F: FnOnce(&mut Self) -> Result, - { - self.read_enum_variant_arg(f_idx, f) - } - - #[inline] - fn read_struct(&mut self, _s_name: &str, _len: usize, f: F) -> Result + fn read_enum_variant_arg(&mut self, f: F) -> Result where F: FnOnce(&mut Self) -> Result, { @@ -281,12 +210,15 @@ fn read_struct(&mut self, _s_name: &str, _len: usize, f: F) -> Result( - &mut self, - _f_name: &str, - _f_idx: usize, - f: F, - ) -> Result + fn read_struct(&mut self, f: F) -> Result + where + F: FnOnce(&mut Self) -> Result, + { + f(self) + } + + #[inline] + fn read_struct_field(&mut self, _f_name: &str, f: F) -> Result where F: FnOnce(&mut Self) -> Result, { @@ -302,33 +234,19 @@ fn read_tuple(&mut self, _len: usize, f: F) -> Result } #[inline] - fn read_tuple_arg(&mut self, _a_idx: usize, f: F) -> Result + fn read_tuple_arg(&mut self, f: F) -> Result where F: FnOnce(&mut Self) -> Result, { f(self) } - fn read_tuple_struct(&mut self, _s_name: &str, len: usize, f: F) -> Result - where - F: FnOnce(&mut Self) -> Result, - { - self.read_tuple(len, f) - } - - fn read_tuple_struct_arg(&mut self, a_idx: usize, f: F) -> Result - where - F: FnOnce(&mut Self) -> Result, - { - self.read_tuple_arg(a_idx, f) - } - // Specialized types: fn read_option(&mut self, mut f: F) -> Result where F: FnMut(&mut Self, bool) -> Result, { - self.read_enum("Option", move |this| { + self.read_enum(move |this| { this.read_enum_variant(&["None", "Some"], move |this, idx| match idx { 0 => f(this, false), 1 => f(this, true), @@ -346,7 +264,7 @@ fn read_seq(&mut self, f: F) -> Result } #[inline] - fn read_seq_elt(&mut self, _idx: usize, f: F) -> Result + fn read_seq_elt(&mut self, f: F) -> Result where F: FnOnce(&mut Self) -> Result, { @@ -362,7 +280,7 @@ fn read_map(&mut self, f: F) -> Result } #[inline] - fn read_map_elt_key(&mut self, _idx: usize, f: F) -> Result + fn read_map_elt_key(&mut self, f: F) -> Result where F: FnOnce(&mut Self) -> Result, { @@ -370,7 +288,7 @@ fn read_map_elt_key(&mut self, _idx: usize, f: F) -> Result(&mut self, _idx: usize, f: F) -> Result + fn read_map_elt_val(&mut self, f: F) -> Result where F: FnOnce(&mut Self) -> Result, { @@ -550,8 +468,8 @@ impl> Decodable for Vec { default fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut v = Vec::with_capacity(len); - for i in 0..len { - v.push(d.read_seq_elt(i, |d| Decodable::decode(d))?); + for _ in 0..len { + v.push(d.read_seq_elt(|d| Decodable::decode(d))?); } Ok(v) }) @@ -571,7 +489,7 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { assert!(len == N); let mut v = [0u8; N]; for i in 0..len { - v[i] = d.read_seq_elt(i, |d| Decodable::decode(d))?; + v[i] = d.read_seq_elt(|d| Decodable::decode(d))?; } Ok(v) }) @@ -615,12 +533,12 @@ fn decode(d: &mut D) -> Result, D::Error> { impl, T2: Encodable> Encodable for Result { fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_enum("Result", |s| match *self { + s.emit_enum(|s| match *self { Ok(ref v) => { - s.emit_enum_variant("Ok", 0, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s))) + s.emit_enum_variant("Ok", 0, 1, |s| s.emit_enum_variant_arg(true, |s| v.encode(s))) } Err(ref v) => { - s.emit_enum_variant("Err", 1, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s))) + s.emit_enum_variant("Err", 1, 1, |s| s.emit_enum_variant_arg(true, |s| v.encode(s))) } }) } @@ -628,10 +546,10 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { impl, T2: Decodable> Decodable for Result { fn decode(d: &mut D) -> Result, D::Error> { - d.read_enum("Result", |d| { + d.read_enum(|d| { d.read_enum_variant(&["Ok", "Err"], |d, disr| match disr { - 0 => Ok(Ok(d.read_enum_variant_arg(0, |d| T1::decode(d))?)), - 1 => Ok(Err(d.read_enum_variant_arg(0, |d| T2::decode(d))?)), + 0 => Ok(Ok(d.read_enum_variant_arg(|d| T1::decode(d))?)), + 1 => Ok(Err(d.read_enum_variant_arg(|d| T2::decode(d))?)), _ => { panic!( "Encountered invalid discriminant while \ @@ -668,8 +586,7 @@ impl),+> Decodable for ($($name,)+) { fn decode(d: &mut D) -> Result<($($name,)+), D::Error> { let len: usize = count!($($name)+); d.read_tuple(len, |d| { - let mut i = 0; - let ret = ($(d.read_tuple_arg({ i+=1; i-1 }, |d| -> Result<$name, D::Error> { + let ret = ($(d.read_tuple_arg(|d| -> Result<$name, D::Error> { Decodable::decode(d) })?,)+); Ok(ret) diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index aeb3234315f..02b20d03a57 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -295,20 +295,20 @@ pub fn is_top_level_module(self) -> bool { impl Encodable for DefId { default fn encode(&self, s: &mut E) -> Result<(), E::Error> { - s.emit_struct("DefId", 2, |s| { - s.emit_struct_field("krate", 0, |s| self.krate.encode(s))?; + s.emit_struct(false, |s| { + s.emit_struct_field("krate", true, |s| self.krate.encode(s))?; - s.emit_struct_field("index", 1, |s| self.index.encode(s)) + s.emit_struct_field("index", false, |s| self.index.encode(s)) }) } } impl Decodable for DefId { default fn decode(d: &mut D) -> Result { - d.read_struct("DefId", 2, |d| { + d.read_struct(|d| { Ok(DefId { - krate: d.read_struct_field("krate", 0, Decodable::decode)?, - index: d.read_struct_field("index", 1, Decodable::decode)?, + krate: d.read_struct_field("krate", Decodable::decode)?, + index: d.read_struct_field("index", Decodable::decode)?, }) }) } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index dc15c531d1e..74a5eded650 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -146,11 +146,12 @@ fn hash(&self, state: &mut H) { // an added assert statement impl Encodable for RealFileName { fn encode(&self, encoder: &mut S) -> Result<(), S::Error> { - encoder.emit_enum("RealFileName", |encoder| match *self { + encoder.emit_enum(|encoder| match *self { RealFileName::LocalPath(ref local_path) => { encoder.emit_enum_variant("LocalPath", 0, 1, |encoder| { Ok({ - encoder.emit_enum_variant_arg(0, |encoder| local_path.encode(encoder))?; + encoder + .emit_enum_variant_arg(true, |encoder| local_path.encode(encoder))?; }) }) } @@ -161,8 +162,10 @@ fn encode(&self, encoder: &mut S) -> Result<(), S::Error> { // if they have been remapped by --remap-path-prefix assert!(local_path.is_none()); Ok({ - encoder.emit_enum_variant_arg(0, |encoder| local_path.encode(encoder))?; - encoder.emit_enum_variant_arg(1, |encoder| virtual_name.encode(encoder))?; + encoder + .emit_enum_variant_arg(true, |encoder| local_path.encode(encoder))?; + encoder + .emit_enum_variant_arg(false, |encoder| virtual_name.encode(encoder))?; }) }), }) @@ -827,17 +830,17 @@ fn default() -> Self { impl Encodable for Span { default fn encode(&self, s: &mut E) -> Result<(), E::Error> { let span = self.data(); - s.emit_struct("Span", 2, |s| { - s.emit_struct_field("lo", 0, |s| span.lo.encode(s))?; - s.emit_struct_field("hi", 1, |s| span.hi.encode(s)) + s.emit_struct(false, |s| { + s.emit_struct_field("lo", true, |s| span.lo.encode(s))?; + s.emit_struct_field("hi", false, |s| span.hi.encode(s)) }) } } impl Decodable for Span { default fn decode(s: &mut D) -> Result { - s.read_struct("Span", 2, |d| { - let lo = d.read_struct_field("lo", 0, Decodable::decode)?; - let hi = d.read_struct_field("hi", 1, Decodable::decode)?; + s.read_struct(|d| { + 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())) }) @@ -1234,12 +1237,12 @@ pub struct SourceFile { impl Encodable for SourceFile { fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_struct("SourceFile", 8, |s| { - s.emit_struct_field("name", 0, |s| self.name.encode(s))?; - s.emit_struct_field("src_hash", 2, |s| self.src_hash.encode(s))?; - s.emit_struct_field("start_pos", 3, |s| self.start_pos.encode(s))?; - s.emit_struct_field("end_pos", 4, |s| self.end_pos.encode(s))?; - s.emit_struct_field("lines", 5, |s| { + s.emit_struct(false, |s| { + s.emit_struct_field("name", true, |s| self.name.encode(s))?; + s.emit_struct_field("src_hash", false, |s| self.src_hash.encode(s))?; + s.emit_struct_field("start_pos", false, |s| self.start_pos.encode(s))?; + s.emit_struct_field("end_pos", false, |s| self.end_pos.encode(s))?; + s.emit_struct_field("lines", false, |s| { let lines = &self.lines[..]; // Store the length. s.emit_u32(lines.len() as u32)?; @@ -1297,25 +1300,24 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { Ok(()) })?; - s.emit_struct_field("multibyte_chars", 6, |s| self.multibyte_chars.encode(s))?; - s.emit_struct_field("non_narrow_chars", 7, |s| self.non_narrow_chars.encode(s))?; - s.emit_struct_field("name_hash", 8, |s| self.name_hash.encode(s))?; - s.emit_struct_field("normalized_pos", 9, |s| self.normalized_pos.encode(s))?; - s.emit_struct_field("cnum", 10, |s| self.cnum.encode(s)) + s.emit_struct_field("multibyte_chars", false, |s| self.multibyte_chars.encode(s))?; + s.emit_struct_field("non_narrow_chars", false, |s| self.non_narrow_chars.encode(s))?; + s.emit_struct_field("name_hash", false, |s| self.name_hash.encode(s))?; + s.emit_struct_field("normalized_pos", false, |s| self.normalized_pos.encode(s))?; + s.emit_struct_field("cnum", false, |s| self.cnum.encode(s)) }) } } impl Decodable for SourceFile { fn decode(d: &mut D) -> Result { - d.read_struct("SourceFile", 8, |d| { - let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?; + d.read_struct(|d| { + let name: FileName = d.read_struct_field("name", |d| Decodable::decode(d))?; let src_hash: SourceFileHash = - d.read_struct_field("src_hash", 2, |d| Decodable::decode(d))?; - let start_pos: BytePos = - d.read_struct_field("start_pos", 3, |d| Decodable::decode(d))?; - let end_pos: BytePos = d.read_struct_field("end_pos", 4, |d| Decodable::decode(d))?; - let lines: Vec = d.read_struct_field("lines", 5, |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 = d.read_struct_field("lines", |d| { let num_lines: u32 = Decodable::decode(d)?; let mut lines = Vec::with_capacity(num_lines as usize); @@ -1344,13 +1346,13 @@ fn decode(d: &mut D) -> Result { Ok(lines) })?; let multibyte_chars: Vec = - d.read_struct_field("multibyte_chars", 6, |d| Decodable::decode(d))?; + d.read_struct_field("multibyte_chars", |d| Decodable::decode(d))?; let non_narrow_chars: Vec = - d.read_struct_field("non_narrow_chars", 7, |d| Decodable::decode(d))?; - let name_hash: u128 = d.read_struct_field("name_hash", 8, |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 = - d.read_struct_field("normalized_pos", 9, |d| Decodable::decode(d))?; - let cnum: CrateNum = d.read_struct_field("cnum", 10, |d| Decodable::decode(d))?; + d.read_struct_field("normalized_pos", |d| Decodable::decode(d))?; + let cnum: CrateNum = d.read_struct_field("cnum", |d| Decodable::decode(d))?; Ok(SourceFile { name, start_pos,