From ec6573f33b15275551af321df1a19cfd3d835982 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Sat, 3 Nov 2018 15:08:27 +0100 Subject: Make `newtype_index` safe --- src/librustc_data_structures/indexed_vec.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/librustc_data_structures') diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index a59bf9d530c..c35490ce35b 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -98,12 +98,18 @@ macro_rules! newtype_index { @max [$max:expr] @vis [$v:vis] @debug_format [$debug_format:tt]) => ( - #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)] + #[derive(Copy, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)] #[rustc_layout_scalar_valid_range_end($max)] $v struct $type { private: u32 } + impl Clone for $type { + fn clone(&self) -> Self { + *self + } + } + impl $type { $v const MAX_AS_U32: u32 = $max; @@ -145,7 +151,7 @@ macro_rules! newtype_index { #[inline] $v const unsafe fn from_u32_unchecked(value: u32) -> Self { - $type { private: value } + unsafe { $type { private: value } } } /// Extract value of this index as an integer. @@ -328,12 +334,17 @@ macro_rules! newtype_index { derive [$($derives:ident,)+] $($tokens:tt)*) => ( newtype_index!( - @derives [$($derives,)+ RustcDecodable, RustcEncodable,] + @derives [$($derives,)+ RustcEncodable,] @type [$type] @max [$max] @vis [$v] @debug_format [$debug_format] $($tokens)*); + impl Decodable for $type { + fn decode(d: &mut D) -> Result { + d.read_u32().into() + } + } ); // The case where no derives are added, but encodable is overridden. Don't @@ -360,12 +371,17 @@ macro_rules! newtype_index { @debug_format [$debug_format:tt] $($tokens:tt)*) => ( newtype_index!( - @derives [RustcDecodable, RustcEncodable,] + @derives [RustcEncodable,] @type [$type] @max [$max] @vis [$v] @debug_format [$debug_format] $($tokens)*); + impl Decodable for $type { + fn decode(d: &mut D) -> Result { + d.read_u32().map(Self::from) + } + } ); // Rewrite final without comma to one that includes comma -- cgit 1.4.1-3-g733a5 From 137a640d61a4c0fe3c88ae0062d1ae01faf8fbf5 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Mon, 19 Nov 2018 15:24:03 +0100 Subject: Automatically generate imports for newtype_index `Deserialize` impls --- src/librustc/dep_graph/graph.rs | 1 - src/librustc/dep_graph/serialized.rs | 1 - src/librustc/hir/mod.rs | 1 - src/librustc/middle/region.rs | 1 - src/librustc/mir/mod.rs | 2 +- src/librustc/ty/mod.rs | 2 +- src/librustc/ty/sty.rs | 2 +- src/librustc_data_structures/indexed_vec.rs | 24 ++++++++++++++-------- src/librustc_mir/borrow_check/location.rs | 1 - .../borrow_check/nll/constraints/mod.rs | 1 - .../borrow_check/nll/region_infer/values.rs | 1 - .../nll/type_check/liveness/liveness_map.rs | 1 - .../nll/type_check/liveness/local_use_map.rs | 1 - src/librustc_mir/build/mod.rs | 1 - src/librustc_target/abi/mod.rs | 1 - 15 files changed, 19 insertions(+), 22 deletions(-) (limited to 'src/librustc_data_structures') diff --git a/src/librustc/dep_graph/graph.rs b/src/librustc/dep_graph/graph.rs index 06287bda63a..4c94c993ab4 100644 --- a/src/librustc/dep_graph/graph.rs +++ b/src/librustc/dep_graph/graph.rs @@ -18,7 +18,6 @@ use std::env; use std::hash::Hash; use ty::{self, TyCtxt}; use util::common::{ProfileQueriesMsg, profq_msg}; -use serialize::{Decodable, Decoder}; use ich::{StableHashingContext, StableHashingContextProvider, Fingerprint}; diff --git a/src/librustc/dep_graph/serialized.rs b/src/librustc/dep_graph/serialized.rs index 992ebd0efb1..0c6c224fa91 100644 --- a/src/librustc/dep_graph/serialized.rs +++ b/src/librustc/dep_graph/serialized.rs @@ -13,7 +13,6 @@ use dep_graph::DepNode; use ich::Fingerprint; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; -use serialize::{Decodable, Decoder}; newtype_index! { pub struct SerializedDepNodeIndex { .. } diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 597a7738217..1674320165e 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -122,7 +122,6 @@ impl serialize::UseSpecializedDecodable for HirId { // hack to ensure that we don't try to access the private parts of `ItemLocalId` in this module mod item_local_id_inner { use rustc_data_structures::indexed_vec::Idx; - use serialize::{Decodable, Decoder}; /// An `ItemLocalId` uniquely identifies something within a given "item-like", /// that is within a hir::Item, hir::TraitItem, or hir::ImplItem. There is no /// guarantee that the numerical value of a given `ItemLocalId` corresponds to diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs index 1440d91b47e..35d1a4dd2cb 100644 --- a/src/librustc/middle/region.rs +++ b/src/librustc/middle/region.rs @@ -28,7 +28,6 @@ use syntax::ast; use syntax_pos::{Span, DUMMY_SP}; use ty::TyCtxt; use ty::query::Providers; -use serialize::{Decodable, Decoder}; use hir; use hir::Node; diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index 09b344cd38d..4a463c0d5a3 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -25,7 +25,7 @@ use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors}; use rustc_data_structures::indexed_vec::{Idx, IndexVec}; use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::MappedReadGuard; -use rustc_serialize::{self as serialize, Decodable, Decoder}; +use rustc_serialize::{self as serialize}; use smallvec::SmallVec; use std::borrow::Cow; use std::fmt::{self, Debug, Formatter, Write}; diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 8ff93cee250..4633ab11663 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -41,7 +41,7 @@ use util::nodemap::{NodeSet, DefIdMap, FxHashMap}; use arena::SyncDroplessArena; use session::DataTypeKind; -use serialize::{self, Encodable, Encoder, Decodable, Decoder}; +use serialize::{self, Encodable, Encoder}; use std::cell::RefCell; use std::cmp::{self, Ordering}; use std::fmt; diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index fa45b8c7768..1416cb17fea 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -29,7 +29,7 @@ use rustc_target::spec::abi; use syntax::ast::{self, Ident}; use syntax::symbol::{keywords, InternedString}; -use serialize::{self, Decodable, Decoder}; +use serialize; use hir; diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index c35490ce35b..6522dbe1179 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -340,11 +340,7 @@ macro_rules! newtype_index { @vis [$v] @debug_format [$debug_format] $($tokens)*); - impl Decodable for $type { - fn decode(d: &mut D) -> Result { - d.read_u32().into() - } - } + newtype_index!(@decodable $type); ); // The case where no derives are added, but encodable is overridden. Don't @@ -377,9 +373,21 @@ macro_rules! newtype_index { @vis [$v] @debug_format [$debug_format] $($tokens)*); - impl Decodable for $type { - fn decode(d: &mut D) -> Result { - d.read_u32().map(Self::from) + newtype_index!(@decodable $type); + ); + + (@decodable $type:ident) => ( + impl $type { + fn __decodable__impl__hack() { + mod __more_hacks_because__self_doesnt_work_in_functions { + extern crate serialize; + use self::serialize::{Decodable, Decoder}; + impl Decodable for super::$type { + fn decode(d: &mut D) -> Result { + d.read_u32().map(Self::from) + } + } + } } } ); diff --git a/src/librustc_mir/borrow_check/location.rs b/src/librustc_mir/borrow_check/location.rs index 2d593d85113..b3e159dd844 100644 --- a/src/librustc_mir/borrow_check/location.rs +++ b/src/librustc_mir/borrow_check/location.rs @@ -10,7 +10,6 @@ use rustc::mir::{BasicBlock, Location, Mir}; use rustc_data_structures::indexed_vec::{Idx, IndexVec}; -use rustc_serialize::{Decodable, Decoder}; /// Maps between a MIR Location, which identifies a particular /// statement within a basic block, to a "rich location", which diff --git a/src/librustc_mir/borrow_check/nll/constraints/mod.rs b/src/librustc_mir/borrow_check/nll/constraints/mod.rs index 37b11a77184..a873af8333a 100644 --- a/src/librustc_mir/borrow_check/nll/constraints/mod.rs +++ b/src/librustc_mir/borrow_check/nll/constraints/mod.rs @@ -13,7 +13,6 @@ use rustc::ty::RegionVid; use rustc_data_structures::graph::scc::Sccs; use rustc_data_structures::indexed_vec::{Idx, IndexVec}; use borrow_check::nll::type_check::Locations; -use rustc_serialize::{Decodable, Decoder}; use std::fmt; use std::ops::Deref; diff --git a/src/librustc_mir/borrow_check/nll/region_infer/values.rs b/src/librustc_mir/borrow_check/nll/region_infer/values.rs index 5ce80ca236d..69e2c896d33 100644 --- a/src/librustc_mir/borrow_check/nll/region_infer/values.rs +++ b/src/librustc_mir/borrow_check/nll/region_infer/values.rs @@ -16,7 +16,6 @@ use rustc_data_structures::indexed_vec::Idx; use rustc_data_structures::indexed_vec::IndexVec; use std::fmt::Debug; use std::rc::Rc; -use rustc_serialize::{Decodable, Decoder}; /// Maps between a `Location` and a `PointIndex` (and vice versa). crate struct RegionValueElements { diff --git a/src/librustc_mir/borrow_check/nll/type_check/liveness/liveness_map.rs b/src/librustc_mir/borrow_check/nll/type_check/liveness/liveness_map.rs index d1a1d2aea24..cc176cbc403 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/liveness/liveness_map.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/liveness/liveness_map.rs @@ -23,7 +23,6 @@ use rustc::ty::{RegionVid, TyCtxt}; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::indexed_vec::{Idx, IndexVec}; use util::liveness::LiveVariableMap; -use rustc_serialize::{Decodable, Decoder}; /// Map between Local and LiveVar indices: the purpose of this /// map is to define the subset of local variables for which we need diff --git a/src/librustc_mir/borrow_check/nll/type_check/liveness/local_use_map.rs b/src/librustc_mir/borrow_check/nll/type_check/liveness/local_use_map.rs index 9e0eaf61e94..4b39d58cd96 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/liveness/local_use_map.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/liveness/local_use_map.rs @@ -15,7 +15,6 @@ use rustc::mir::{Local, Location, Mir}; use rustc_data_structures::indexed_vec::{Idx, IndexVec}; use rustc_data_structures::vec_linked_list as vll; use util::liveness::{categorize, DefUse, LiveVariableMap}; -use rustc_serialize::{Decodable, Decoder}; /// A map that cross references each local with the locations where it /// is defined (assigned), used, or dropped. Used during liveness diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index 1a5a089c7aa..cc927df6350 100644 --- a/src/librustc_mir/build/mod.rs +++ b/src/librustc_mir/build/mod.rs @@ -32,7 +32,6 @@ use syntax::ast; use syntax::attr::{self, UnwindAttr}; use syntax::symbol::keywords; use syntax_pos::Span; -use rustc_serialize::{Decodable, Decoder}; use transform::MirSource; use util as mir_util; diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 27a4292543a..50ce0ad6915 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -17,7 +17,6 @@ use std::fmt; use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive}; use rustc_data_structures::indexed_vec::{Idx, IndexVec}; -use rustc_serialize::{Decodable, Decoder}; pub mod call; -- cgit 1.4.1-3-g733a5