Skip to content

Commit ec6573f

Browse files
committed
Make newtype_index safe
1 parent 02b2232 commit ec6573f

File tree

14 files changed

+33
-7
lines changed

14 files changed

+33
-7
lines changed

src/librustc/dep_graph/graph.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::env;
1818
use std::hash::Hash;
1919
use ty::{self, TyCtxt};
2020
use util::common::{ProfileQueriesMsg, profq_msg};
21+
use serialize::{Decodable, Decoder};
2122

2223
use ich::{StableHashingContext, StableHashingContextProvider, Fingerprint};
2324

src/librustc/dep_graph/serialized.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use dep_graph::DepNode;
1414
use ich::Fingerprint;
1515
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
16+
use serialize::{Decodable, Decoder};
1617

1718
newtype_index! {
1819
pub struct SerializedDepNodeIndex { .. }

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#![feature(in_band_lifetimes)]
7070
#![feature(crate_visibility_modifier)]
7171
#![feature(transpose_result)]
72+
#![cfg_attr(not(stage0), feature(min_const_unsafe_fn))]
7273

7374
#![recursion_limit="512"]
7475

src/librustc/middle/region.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use syntax::ast;
2828
use syntax_pos::{Span, DUMMY_SP};
2929
use ty::TyCtxt;
3030
use ty::query::Providers;
31+
use serialize::{Decodable, Decoder};
3132

3233
use hir;
3334
use hir::Node;

src/librustc/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
2525
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
2626
use rustc_data_structures::sync::Lrc;
2727
use rustc_data_structures::sync::MappedReadGuard;
28-
use rustc_serialize as serialize;
28+
use rustc_serialize::{self as serialize, Decodable, Decoder};
2929
use smallvec::SmallVec;
3030
use std::borrow::Cow;
3131
use std::fmt::{self, Debug, Formatter, Write};

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use util::nodemap::{NodeSet, DefIdMap, FxHashMap};
4141
use arena::SyncDroplessArena;
4242
use session::DataTypeKind;
4343

44-
use serialize::{self, Encodable, Encoder};
44+
use serialize::{self, Encodable, Encoder, Decodable, Decoder};
4545
use std::cell::RefCell;
4646
use std::cmp::{self, Ordering};
4747
use std::fmt;

src/librustc/ty/sty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_target::spec::abi;
2929
use syntax::ast::{self, Ident};
3030
use syntax::symbol::{keywords, InternedString};
3131

32-
use serialize;
32+
use serialize::{self, Decodable, Decoder};
3333

3434
use hir;
3535

src/librustc_data_structures/indexed_vec.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,18 @@ macro_rules! newtype_index {
9898
@max [$max:expr]
9999
@vis [$v:vis]
100100
@debug_format [$debug_format:tt]) => (
101-
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
101+
#[derive(Copy, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
102102
#[rustc_layout_scalar_valid_range_end($max)]
103103
$v struct $type {
104104
private: u32
105105
}
106106

107+
impl Clone for $type {
108+
fn clone(&self) -> Self {
109+
*self
110+
}
111+
}
112+
107113
impl $type {
108114
$v const MAX_AS_U32: u32 = $max;
109115

@@ -145,7 +151,7 @@ macro_rules! newtype_index {
145151

146152
#[inline]
147153
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
148-
$type { private: value }
154+
unsafe { $type { private: value } }
149155
}
150156

151157
/// Extract value of this index as an integer.
@@ -328,12 +334,17 @@ macro_rules! newtype_index {
328334
derive [$($derives:ident,)+]
329335
$($tokens:tt)*) => (
330336
newtype_index!(
331-
@derives [$($derives,)+ RustcDecodable, RustcEncodable,]
337+
@derives [$($derives,)+ RustcEncodable,]
332338
@type [$type]
333339
@max [$max]
334340
@vis [$v]
335341
@debug_format [$debug_format]
336342
$($tokens)*);
343+
impl Decodable for $type {
344+
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
345+
d.read_u32().into()
346+
}
347+
}
337348
);
338349

339350
// The case where no derives are added, but encodable is overridden. Don't
@@ -360,12 +371,17 @@ macro_rules! newtype_index {
360371
@debug_format [$debug_format:tt]
361372
$($tokens:tt)*) => (
362373
newtype_index!(
363-
@derives [RustcDecodable, RustcEncodable,]
374+
@derives [RustcEncodable,]
364375
@type [$type]
365376
@max [$max]
366377
@vis [$v]
367378
@debug_format [$debug_format]
368379
$($tokens)*);
380+
impl Decodable for $type {
381+
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
382+
d.read_u32().map(Self::from)
383+
}
384+
}
369385
);
370386

371387
// Rewrite final without comma to one that includes comma

src/librustc_mir/borrow_check/location.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use rustc::mir::{BasicBlock, Location, Mir};
1212
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
13+
use rustc_serialize::{Decodable, Decoder};
1314

1415
/// Maps between a MIR Location, which identifies a particular
1516
/// statement within a basic block, to a "rich location", which

src/librustc_mir/borrow_check/nll/constraints/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc::ty::RegionVid;
1313
use rustc_data_structures::graph::scc::Sccs;
1414
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
1515
use borrow_check::nll::type_check::Locations;
16+
use rustc_serialize::{Decodable, Decoder};
1617

1718
use std::fmt;
1819
use std::ops::Deref;

0 commit comments

Comments
 (0)