提交 5aee959e 编写于 作者: N Niko Matsakis

make field always private, add `From` impls

上级 c46f185f
......@@ -53,7 +53,7 @@ pub fn as_usize(&self) -> usize {
}
pub fn as_u32(&self) -> u32 {
self.0
u32::from(*self)
}
pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
......
......@@ -102,7 +102,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::Local {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
......@@ -112,7 +111,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::BasicBlock {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
......@@ -122,7 +120,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::Field {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
......@@ -133,7 +130,6 @@ impl<'a> HashStable<StableHashingContext<'a>>
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
......@@ -143,7 +139,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::Promoted {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
......
......@@ -143,7 +143,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionVid {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
......@@ -153,7 +152,6 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for ty::CanonicalVar {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'gcx>,
hasher: &mut StableHasher<W>) {
use rustc_data_structures::indexed_vec::Idx;
self.index().hash_stable(hcx, hasher);
}
}
......@@ -774,7 +772,6 @@ fn hash_stable<W: StableHasherResult>(&self,
FnPtrAddrCast
});
impl_stable_hash_for!(tuple_struct ::middle::region::FirstStatementIndex { idx });
impl_stable_hash_for!(struct ::middle::region::Scope { id, code });
impl<'a> ToStableHashKey<StableHashingContext<'a>> for region::Scope {
......
......@@ -73,8 +73,6 @@
use syntax_pos::{Pos, Span};
use errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
use rustc_data_structures::indexed_vec::Idx;
mod note;
mod need_type_info;
......
......@@ -16,7 +16,7 @@
use super::{MiscVariable, RegionVariableOrigin, SubregionOrigin};
use super::unify_key;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::unify as ut;
use ty::{self, Ty, TyCtxt};
......
......@@ -49,8 +49,8 @@ fn unify_values(value1: &Self, value2: &Self) -> Result<Self, NoError> {
impl UnifyKey for ty::RegionVid {
type Value = RegionVidKey;
fn index(&self) -> u32 { self.0 }
fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid(i) }
fn index(&self) -> u32 { u32::from(*self) }
fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid::from(i) }
fn tag() -> &'static str { "RegionVid" }
}
......
......@@ -161,11 +161,12 @@ pub struct BlockRemainder {
newtype_index! {
pub struct FirstStatementIndex {
pub idx
MAX = SCOPE_DATA_REMAINDER_MAX
}
}
impl_stable_hash_for!(tuple_struct ::middle::region::FirstStatementIndex { idx });
impl From<ScopeData> for Scope {
#[inline]
fn from(scope_data: ScopeData) -> Self {
......
......@@ -1179,7 +1179,6 @@ pub struct FloatVid {
newtype_index! {
pub struct RegionVid {
pub idx
DEBUG_FORMAT = custom,
}
}
......@@ -1190,18 +1189,6 @@ fn index(self) -> usize {
}
}
impl From<usize> for RegionVid {
fn from(i: usize) -> RegionVid {
RegionVid::new(i)
}
}
impl From<RegionVid> for usize {
fn from(vid: RegionVid) -> usize {
Idx::index(vid)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub enum InferTy {
TyVar(TyVid),
......
......@@ -26,7 +26,6 @@
use std::fmt;
use std::usize;
use rustc_data_structures::indexed_vec::Idx;
use rustc_target::spec::abi::Abi;
use syntax::ast::CRATE_NODE_ID;
use syntax::symbol::{Symbol, InternedString};
......
......@@ -32,7 +32,7 @@
use std::iter;
use rustc_data_structures::bitvec::BitArray;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::indexed_vec::IndexVec;
pub use self::constant::codegen_static_initializer;
......
......@@ -13,7 +13,6 @@
use rustc::mir::interpret::{ConstValue, ScalarMaybeUndef};
use rustc::ty;
use rustc::ty::layout::{self, Align, LayoutOf, TyLayout};
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::sync::Lrc;
use base;
......
......@@ -13,7 +13,6 @@
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, Size};
use rustc::mir;
use rustc::mir::tcx::PlaceTy;
use rustc_data_structures::indexed_vec::Idx;
use base;
use builder::Builder;
use common::{CodegenCx, C_undef, C_usize, C_u8, C_u32, C_uint, C_null, C_uint_big};
......
......@@ -49,12 +49,20 @@ fn index(self) -> usize { self as usize }
}
/// Creates a struct type `S` that can be used as an index with
/// `IndexVec` and so on. This struct can be constructed via `S::new`
/// (given a `usize`) and converted to a usize with the `index()`
/// method (from the `Idx` trait). Internally, the index uses a u32,
/// so the index must not exceed `u32::MAX`. You can also customize
/// things like the `Debug` impl, what traits are derived, and so
/// forth.
/// `IndexVec` and so on.
///
/// There are two ways of interacting with these indices:
///
/// - The `From` impls are the preferred way. So you can do
/// `S::from(v)` with a `usize` or `u32`. And you can convert back
/// to an integer with `u32::from(s)`.
///
/// - Alternatively, you can use the methods `S::new(v)` and `s.index()`
/// to create/return a value.
///
/// Internally, the index uses a u32, so the index must not exceed
/// `u32::MAX`. You can also customize things like the `Debug` impl,
/// what traits are derived, and so forth via the macro.
#[macro_export]
macro_rules! newtype_index {
// ---- public rules ----
......@@ -84,13 +92,20 @@ fn index(self) -> usize { self as usize }
// Base case, user-defined constants (if any) have already been defined
(@derives [$($derives:ident,)*]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
@debug_format [$debug_format:tt]) => (
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
$v struct $type($($pub)* u32);
$v struct $type(u32);
impl $type {
/// Extract value of this index as an integer.
#[inline]
$v fn index(self) -> usize {
<Self as Idx>::index(self)
}
}
impl Idx for $type {
#[inline]
......@@ -134,6 +149,30 @@ fn add_usize(&self, u: usize) -> Option<Self> {
}
}
impl From<$type> for u32 {
fn from(v: $type) -> u32 {
v.0
}
}
impl From<$type> for usize {
fn from(v: $type) -> usize {
v.0 as usize
}
}
impl From<usize> for $type {
fn from(v: usize) -> Self {
Self::new(v)
}
}
impl From<u32> for $type {
fn from(v: u32) -> Self {
Self::new(v as usize)
}
}
newtype_index!(
@handle_debug
@derives [$($derives,)*]
......@@ -177,47 +216,14 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
@debug_format [$debug_format]);
);
// Handle the case where someone wants to make the internal field public
(@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
@debug_format [$debug_format:tt]
pub idx
$($tokens:tt)*) => (
newtype_index!(
@pub [pub]
@type [$type]
@max [$max]
@vis [$v]
@debug_format [$debug_format]
$($tokens)*);
);
// The default case is that the internal field is private
(@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
@debug_format [$debug_format:tt]
$($tokens:tt)*) => (
newtype_index!(
@pub []
@type [$type]
@max [$max]
@vis [$v]
@debug_format [$debug_format]
$($tokens)*);
);
// Append comma to end of derives list if it's missing
(@pub [$($pub:tt)*]
@type [$type:ident]
(@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
@debug_format [$debug_format:tt]
derive [$($derives:ident),*]
$($tokens:tt)*) => (
newtype_index!(
@pub [$($pub)*]
@type [$type]
@max [$max]
@vis [$v]
......@@ -228,8 +234,7 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
// By not including the @derives marker in this list nor in the default args, we can force it
// to come first if it exists. When encodable is custom, just use the derives list as-is.
(@pub [$($pub:tt)*]
@type [$type:ident]
(@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
@debug_format [$debug_format:tt]
......@@ -238,7 +243,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
$($tokens:tt)*) => (
newtype_index!(
@derives [$($derives,)+]
@pub [$($pub)*]
@type [$type]
@max [$max]
@vis [$v]
......@@ -248,8 +252,7 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
// By not including the @derives marker in this list nor in the default args, we can force it
// to come first if it exists. When encodable isn't custom, add serialization traits by default.
(@pub [$($pub:tt)*]
@type [$type:ident]
(@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
@debug_format [$debug_format:tt]
......@@ -257,7 +260,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
$($tokens:tt)*) => (
newtype_index!(
@derives [$($derives,)+ RustcDecodable, RustcEncodable,]
@pub [$($pub)*]
@type [$type]
@max [$max]
@vis [$v]
......@@ -267,8 +269,7 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
// The case where no derives are added, but encodable is overridden. Don't
// derive serialization traits
(@pub [$($pub:tt)*]
@type [$type:ident]
(@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
@debug_format [$debug_format:tt]
......@@ -276,7 +277,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
$($tokens:tt)*) => (
newtype_index!(
@derives []
@pub [$($pub)*]
@type [$type]
@max [$max]
@vis [$v]
......@@ -285,15 +285,13 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
);
// The case where no derives are added, add serialization derives by default
(@pub [$($pub:tt)*]
@type [$type:ident]
(@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
@debug_format [$debug_format:tt]
$($tokens:tt)*) => (
newtype_index!(
@derives [RustcDecodable, RustcEncodable,]
@pub [$($pub)*]
@type [$type]
@max [$max]
@vis [$v]
......@@ -303,7 +301,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
// Rewrite final without comma to one that includes comma
(@derives [$($derives:ident,)*]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
......@@ -311,7 +308,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
$name:ident = $constant:expr) => (
newtype_index!(
@derives [$($derives,)*]
@pub [$($pub)*]
@type [$type]
@max [$max]
@vis [$v]
......@@ -321,7 +317,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
// Rewrite final const without comma to one that includes comma
(@derives [$($derives:ident,)*]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$_max:expr]
@vis [$v:vis]
......@@ -330,7 +325,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
const $name:ident = $constant:expr) => (
newtype_index!(
@derives [$($derives,)*]
@pub [$($pub)*]
@type [$type]
@max [$max]
@vis [$v]
......@@ -340,7 +334,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
// Replace existing default for max
(@derives [$($derives:ident,)*]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$_max:expr]
@vis [$v:vis]
......@@ -349,7 +342,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
$($tokens:tt)*) => (
newtype_index!(
@derives [$($derives,)*]
@pub [$($pub)*]
@type [$type]
@max [$max]
@vis [$v]
......@@ -359,7 +351,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
// Replace existing default for debug_format
(@derives [$($derives:ident,)*]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
......@@ -368,7 +359,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
$($tokens:tt)*) => (
newtype_index!(
@derives [$($derives,)*]
@pub [$($pub)*]
@type [$type]
@max [$max]
@vis [$v]
......@@ -378,7 +368,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
// Assign a user-defined constant
(@derives [$($derives:ident,)*]
@pub [$($pub:tt)*]
@type [$type:ident]
@max [$max:expr]
@vis [$v:vis]
......@@ -390,7 +379,6 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
pub const $name: $type = $type($constant);
newtype_index!(
@derives [$($derives,)*]
@pub [$($pub)*]
@type [$type]
@max [$max]
@vis [$v]
......
......@@ -16,7 +16,6 @@
use rustc::mir::{ProjectionElem, Rvalue, Statement, StatementKind};
use rustc::ty;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::sync::Lrc;
use rustc_errors::DiagnosticBuilder;
use syntax_pos::Span;
......
......@@ -12,7 +12,6 @@
use rustc::mir::*;
use rustc::ty;
use rustc_errors::DiagnosticBuilder;
use rustc_data_structures::indexed_vec::Idx;
use syntax_pos::Span;
use borrow_check::MirBorrowckCtxt;
......
......@@ -100,18 +100,6 @@ fn index(self) -> usize {
}
}
impl From<usize> for LocationIndex {
fn from(i: usize) -> LocationIndex {
LocationIndex::new(i)
}
}
impl From<LocationIndex> for usize {
fn from(vid: LocationIndex) -> usize {
Idx::index(vid)
}
}
struct FactWriter<'w> {
location_table: &'w LocationTable,
dir: &'w Path,
......
......@@ -15,7 +15,6 @@
use super::*;
use borrow_check::nll::constraints::OutlivesConstraint;
use dot::{self, IntoCow};
use rustc_data_structures::indexed_vec::Idx;
use std::borrow::Cow;
use std::io::{self, Write};
......
......@@ -42,7 +42,6 @@
use transform::{MirPass, MirSource};
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::indexed_vec::Idx;
macro_rules! span_mirbug {
($context:expr, $elem:expr, $($message:tt)*) => ({
......
......@@ -20,7 +20,7 @@
use rustc::ty::subst::Kind;
use rustc::ty::{self, CanonicalTy, CanonicalVar, RegionVid, Ty, TyCtxt};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc_data_structures::indexed_vec::IndexVec;
use std::mem;
pub(super) fn sub_types<'tcx>(
......
......@@ -95,7 +95,6 @@
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::mir::*;
use syntax_pos::{Span};
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::fx::FxHashMap;
#[derive(Debug)]
......
......@@ -19,7 +19,7 @@
use rustc::ty::{self, TyCtxt, Instance, query::TyCtxtAt};
use rustc::ty::layout::{LayoutOf, TyLayout};
use rustc::ty::subst::Subst;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::indexed_vec::IndexVec;
use syntax::ast::Mutability;
use syntax::source_map::Span;
......
......@@ -13,7 +13,6 @@
use rustc::mir::{BasicBlock, Location};
use rustc_data_structures::indexed_set::{HybridIdxSet, IdxSet, Iter};
use rustc_data_structures::indexed_vec::Idx;
use dataflow::{BitDenotation, BlockSets, DataflowResults};
use dataflow::move_paths::{HasMoveData, MovePathIndex};
......
......@@ -13,7 +13,6 @@
use syntax::ast::NodeId;
use rustc::mir::{BasicBlock, Mir};
use rustc_data_structures::bitslice::bits_to_string;
use rustc_data_structures::indexed_vec::Idx;
use dot;
use dot::IntoCow;
......
......@@ -16,7 +16,7 @@
use rustc::{mir, ty};
use rustc::ty::layout::{self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt};
use rustc_data_structures::indexed_vec::Idx;
use rustc::mir::interpret::{
GlobalId, AllocId,
ConstValue, Pointer, Scalar, ScalarMaybeUndef,
......
......@@ -18,7 +18,6 @@
use rustc::mir;
use rustc::ty::{self, Ty};
use rustc::ty::layout::{self, Size, Align, LayoutOf, TyLayout, HasDataLayout};
use rustc_data_structures::indexed_vec::Idx;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult};
use rustc::mir::interpret::{
......
......@@ -26,7 +26,7 @@
use transform::{MirPass, MirSource};
use syntax::source_map::{Span, DUMMY_SP};
use rustc::ty::subst::Substs;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::indexed_vec::IndexVec;
use rustc::ty::ParamEnv;
use rustc::ty::layout::{
LayoutOf, TyLayout, LayoutError,
......@@ -133,7 +133,6 @@ fn use_ecx<F, T>(
self.ecx.tcx.span = source_info.span;
let lint_root = match self.mir.source_scope_local_data {
ClearCrossCrate::Set(ref ivs) => {
use rustc_data_structures::indexed_vec::Idx;
//FIXME(#51314): remove this check
if source_info.scope.index() >= ivs.len() {
return None;
......
......@@ -19,7 +19,6 @@
use rustc::mir::*;
use rustc::util::nodemap::FxHashMap;
use rustc_data_structures::indexed_set::IdxSet;
use rustc_data_structures::indexed_vec::Idx;
use transform::{MirPass, MirSource};
use util::patch::MirPatch;
use util::elaborate_drops::{DropFlagState, Unwind, elaborate_drop};
......
......@@ -16,7 +16,7 @@
use rustc_data_structures::bitvec::BitArray;
use rustc_data_structures::indexed_set::IdxSet;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::fx::FxHashSet;
use rustc::hir;
use rustc::hir::def_id::DefId;
......
......@@ -15,7 +15,6 @@
use rustc::ty::{self, TyCtxt};
use rustc::mir::{self, Mir, Location};
use rustc_data_structures::indexed_set::IdxSet;
use rustc_data_structures::indexed_vec::Idx;
use transform::{MirPass, MirSource};
use dataflow::{do_dataflow, DebugFormatted};
......
......@@ -15,8 +15,6 @@
use std::fmt::Debug;
use std::io::{self, Write};
use rustc_data_structures::indexed_vec::Idx;
use super::pretty::dump_mir_def_ids;
/// Write a graphviz DOT graph of a list of MIRs.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册