From 0c5de8633c8ea5d73606103db5049529c9b5d034 Mon Sep 17 00:00:00 2001 From: Paul Faria Date: Tue, 26 Sep 2017 22:24:19 -0400 Subject: [PATCH 1/9] Store a new Region value every time we create a new region variable --- src/librustc_mir/transform/nll/mod.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/librustc_mir/transform/nll/mod.rs b/src/librustc_mir/transform/nll/mod.rs index bd02788df16..0ed0321a948 100644 --- a/src/librustc_mir/transform/nll/mod.rs +++ b/src/librustc_mir/transform/nll/mod.rs @@ -15,12 +15,14 @@ use rustc::mir::{Mir, Location, Rvalue, BasicBlock, Statement, StatementKind}; use rustc::mir::visit::{MutVisitor, Lookup}; use rustc::mir::transform::{MirPass, MirSource}; use rustc::infer::{self, InferCtxt}; +use rustc::util::nodemap::FxHashSet; use syntax_pos::DUMMY_SP; use std::collections::HashMap; #[allow(dead_code)] struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> { lookup_map: HashMap, + regions: Vec, infcx: InferCtxt<'a, 'gcx, 'tcx>, } @@ -29,6 +31,7 @@ impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> { NLLVisitor { infcx, lookup_map: HashMap::new(), + regions: vec![], } } @@ -36,8 +39,9 @@ impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> { self.lookup_map } - fn renumber_regions(&self, value: &T) -> T where T: TypeFoldable<'tcx> { + fn renumber_regions(&mut self, value: &T) -> T where T: TypeFoldable<'tcx> { self.infcx.tcx.fold_regions(value, &mut false, |_region, _depth| { + self.regions.push(Region::default()); self.infcx.next_region_var(infer::MiscVariable(DUMMY_SP)) }) } @@ -143,4 +147,9 @@ impl MirPass for NLL { let _results = visitor.into_results(); }) } -} \ No newline at end of file +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +struct Region { + points: FxHashSet, +} From f5cef21569115d41171114ec07bb144a3875afc3 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 27 Sep 2017 14:29:11 -0300 Subject: [PATCH 2/9] Convert regions to IndexVec --- src/librustc_mir/transform/nll/mod.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/librustc_mir/transform/nll/mod.rs b/src/librustc_mir/transform/nll/mod.rs index 0ed0321a948..f06441f77e2 100644 --- a/src/librustc_mir/transform/nll/mod.rs +++ b/src/librustc_mir/transform/nll/mod.rs @@ -16,13 +16,14 @@ use rustc::mir::visit::{MutVisitor, Lookup}; use rustc::mir::transform::{MirPass, MirSource}; use rustc::infer::{self, InferCtxt}; use rustc::util::nodemap::FxHashSet; +use rustc_data_structures::indexed_vec::{IndexVec, Idx}; use syntax_pos::DUMMY_SP; use std::collections::HashMap; #[allow(dead_code)] struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> { lookup_map: HashMap, - regions: Vec, + regions: IndexVec, infcx: InferCtxt<'a, 'gcx, 'tcx>, } @@ -31,7 +32,7 @@ impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> { NLLVisitor { infcx, lookup_map: HashMap::new(), - regions: vec![], + regions: IndexVec::new(), } } @@ -153,3 +154,19 @@ impl MirPass for NLL { struct Region { points: FxHashSet, } + +#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Debug)] +pub struct RegionIndex(pub u32); + +impl Idx for RegionIndex { + #[inline] + fn new(idx: usize) -> Self { + assert!(idx <= ::std::u32::MAX as usize); + RegionIndex(idx as u32) + } + + #[inline] + fn index(self) -> usize { + self.0 as usize + } +} From 3d230af80b01ebe62d27de8436832d0e7ab9ed94 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 27 Sep 2017 19:47:41 -0300 Subject: [PATCH 3/9] Move newtype_index to rustc_data_structures --- src/librustc/lib.rs | 2 +- src/librustc/mir/mod.rs | 24 -------------------- src/librustc_data_structures/indexed_vec.rs | 25 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 1e90aa47267..3322142c9cf 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -71,7 +71,7 @@ extern crate graphviz; extern crate libc; extern crate owning_ref; extern crate rustc_back; -extern crate rustc_data_structures; +#[macro_use] extern crate rustc_data_structures; extern crate serialize; extern crate rustc_const_math; extern crate rustc_errors as errors; diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index ba221ef6ae1..b909269e153 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -43,30 +43,6 @@ pub mod visit; pub mod transform; pub mod traversal; -macro_rules! newtype_index { - ($name:ident, $debug_name:expr) => ( - #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, - RustcEncodable, RustcDecodable)] - pub struct $name(u32); - - impl Idx for $name { - fn new(value: usize) -> Self { - assert!(value < (u32::MAX) as usize); - $name(value as u32) - } - fn index(self) -> usize { - self.0 as usize - } - } - - impl Debug for $name { - fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { - write!(fmt, "{}{}", $debug_name, self.0) - } - } - ) -} - /// Types for locals type LocalDecls<'tcx> = IndexVec>; diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index 1d0e88ee328..7674018075c 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -38,6 +38,31 @@ impl Idx for u32 { fn index(self) -> usize { self as usize } } +#[macro_export] +macro_rules! newtype_index { + ($name:ident, $debug_name:expr) => ( + #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, + RustcEncodable, RustcDecodable)] + pub struct $name(u32); + + impl Idx for $name { + fn new(value: usize) -> Self { + assert!(value < (u32::MAX) as usize); + $name(value as u32) + } + fn index(self) -> usize { + self.0 as usize + } + } + + impl Debug for $name { + fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + write!(fmt, "{}{}", $debug_name, self.0) + } + } + ) +} + #[derive(Clone, PartialEq, Eq)] pub struct IndexVec { pub raw: Vec, From c8549a158648db323690fbb42356042f6b5d956b Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 27 Sep 2017 22:18:38 -0300 Subject: [PATCH 4/9] Generate Idx in nll using newtype_index! --- src/Cargo.lock | 1 + src/librustc_mir/Cargo.toml | 1 + src/librustc_mir/lib.rs | 3 ++- src/librustc_mir/transform/nll/mod.rs | 18 +++--------------- 4 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index 26be463f6bb..77e33855f23 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -1668,6 +1668,7 @@ dependencies = [ "rustc_const_math 0.0.0", "rustc_data_structures 0.0.0", "rustc_errors 0.0.0", + "serialize 0.0.0", "syntax 0.0.0", "syntax_pos 0.0.0", ] diff --git a/src/librustc_mir/Cargo.toml b/src/librustc_mir/Cargo.toml index 936fd5a774d..b7a576babeb 100644 --- a/src/librustc_mir/Cargo.toml +++ b/src/librustc_mir/Cargo.toml @@ -17,5 +17,6 @@ rustc_const_eval = { path = "../librustc_const_eval" } rustc_const_math = { path = "../librustc_const_math" } rustc_data_structures = { path = "../librustc_data_structures" } rustc_errors = { path = "../librustc_errors" } +serialize = { path = "../libserialize" } syntax = { path = "../libsyntax" } syntax_pos = { path = "../libsyntax_pos" } diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs index d0b9849986b..553fec20217 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -30,7 +30,8 @@ extern crate bitflags; extern crate graphviz as dot; #[macro_use] extern crate rustc; -extern crate rustc_data_structures; +#[macro_use] extern crate rustc_data_structures; +extern crate serialize as rustc_serialize; extern crate rustc_errors; #[macro_use] extern crate syntax; diff --git a/src/librustc_mir/transform/nll/mod.rs b/src/librustc_mir/transform/nll/mod.rs index f06441f77e2..c6acc53f5fa 100644 --- a/src/librustc_mir/transform/nll/mod.rs +++ b/src/librustc_mir/transform/nll/mod.rs @@ -19,6 +19,8 @@ use rustc::util::nodemap::FxHashSet; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; use syntax_pos::DUMMY_SP; use std::collections::HashMap; +use std::fmt::{self, Debug, Formatter}; +use std::u32; #[allow(dead_code)] struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> { @@ -155,18 +157,4 @@ struct Region { points: FxHashSet, } -#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Debug)] -pub struct RegionIndex(pub u32); - -impl Idx for RegionIndex { - #[inline] - fn new(idx: usize) -> Self { - assert!(idx <= ::std::u32::MAX as usize); - RegionIndex(idx as u32) - } - - #[inline] - fn index(self) -> usize { - self.0 as usize - } -} +newtype_index!(RegionIndex, "region_index"); From 9af7426b47a99f5f4b06c79d1e294e424eb74a1f Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 28 Sep 2017 12:30:13 -0300 Subject: [PATCH 5/9] Make newtype_index macro use full path to resolve constants --- src/librustc_data_structures/indexed_vec.rs | 6 +++--- src/librustc_mir/transform/nll/mod.rs | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index 7674018075c..01fb2930687 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -47,7 +47,7 @@ macro_rules! newtype_index { impl Idx for $name { fn new(value: usize) -> Self { - assert!(value < (u32::MAX) as usize); + assert!(value < (::std::u32::MAX) as usize); $name(value as u32) } fn index(self) -> usize { @@ -55,8 +55,8 @@ macro_rules! newtype_index { } } - impl Debug for $name { - fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + impl ::std::fmt::Debug for $name { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { write!(fmt, "{}{}", $debug_name, self.0) } } diff --git a/src/librustc_mir/transform/nll/mod.rs b/src/librustc_mir/transform/nll/mod.rs index c6acc53f5fa..7ef8e3bdbd5 100644 --- a/src/librustc_mir/transform/nll/mod.rs +++ b/src/librustc_mir/transform/nll/mod.rs @@ -19,8 +19,6 @@ use rustc::util::nodemap::FxHashSet; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; use syntax_pos::DUMMY_SP; use std::collections::HashMap; -use std::fmt::{self, Debug, Formatter}; -use std::u32; #[allow(dead_code)] struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> { From 3502bec0322829d8bbe61b65c62bb796814e4bd3 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 3 Oct 2017 14:19:56 -0300 Subject: [PATCH 6/9] Make newtype_index get debug_name using reflection --- src/librustc_data_structures/indexed_vec.rs | 4 ++++ src/librustc_mir/lib.rs | 1 + src/librustc_mir/transform/nll/mod.rs | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index 01fb2930687..82882f94440 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -40,6 +40,10 @@ impl Idx for u32 { #[macro_export] macro_rules! newtype_index { + ($name:ident) => ( + newtype_index!($name, unsafe { ::std::intrinsics::type_name::<$name>() }); + ); + ($name:ident, $debug_name:expr) => ( #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, RustcEncodable, RustcDecodable)] diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs index 553fec20217..3339e7a2c30 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -18,6 +18,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment! #![feature(box_patterns)] #![feature(box_syntax)] +#![feature(core_intrinsics)] #![feature(i128_type)] #![feature(rustc_diagnostic_macros)] #![feature(placement_in_syntax)] diff --git a/src/librustc_mir/transform/nll/mod.rs b/src/librustc_mir/transform/nll/mod.rs index 7ef8e3bdbd5..d4a5354c78f 100644 --- a/src/librustc_mir/transform/nll/mod.rs +++ b/src/librustc_mir/transform/nll/mod.rs @@ -155,4 +155,4 @@ struct Region { points: FxHashSet, } -newtype_index!(RegionIndex, "region_index"); +newtype_index!(RegionIndex); From b5a5556dd4807ee97cb51ab395d5dbfc858152dd Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 28 Sep 2017 15:56:28 -0300 Subject: [PATCH 7/9] Generate DepNodeIndexNew using newtype_index macro --- src/librustc/dep_graph/graph.rs | 2 +- src/librustc/lib.rs | 1 + src/librustc_data_structures/indexed_vec.rs | 7 +++++++ src/librustc_mir/lib.rs | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/librustc/dep_graph/graph.rs b/src/librustc/dep_graph/graph.rs index 2d2558fd815..5057b1c6065 100644 --- a/src/librustc/dep_graph/graph.rs +++ b/src/librustc/dep_graph/graph.rs @@ -406,7 +406,7 @@ impl DepGraph { for (current_dep_node_index, edges) in current_dep_graph.edges.iter_enumerated() { let start = edge_list_data.len() as u32; // This should really just be a memcpy :/ - edge_list_data.extend(edges.iter().map(|i| SerializedDepNodeIndex(i.index))); + edge_list_data.extend(edges.iter().map(|i| SerializedDepNodeIndex(i.index() as u32))); let end = edge_list_data.len() as u32; debug_assert_eq!(current_dep_node_index.index(), edge_list_indices.len()); diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 3322142c9cf..015dbbb7aff 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -43,6 +43,7 @@ #![feature(box_patterns)] #![feature(box_syntax)] #![feature(conservative_impl_trait)] +#![feature(const_fn)] #![feature(core_intrinsics)] #![feature(i128_type)] #![cfg_attr(windows, feature(libc))] diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index 82882f94440..ce2468fb9f1 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -49,6 +49,13 @@ macro_rules! newtype_index { RustcEncodable, RustcDecodable)] pub struct $name(u32); + impl $name { + // HACK use for constants + pub const fn const_new(x: u32) -> Self { + $name(x) + } + } + impl Idx for $name { fn new(value: usize) -> Self { assert!(value < (::std::u32::MAX) as usize); diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs index 3339e7a2c30..7e4206e14c5 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -18,6 +18,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment! #![feature(box_patterns)] #![feature(box_syntax)] +#![feature(const_fn)] #![feature(core_intrinsics)] #![feature(i128_type)] #![feature(rustc_diagnostic_macros)] From 8ee16f4352f3621be75084167185c1f9617859eb Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 28 Sep 2017 16:11:06 -0300 Subject: [PATCH 8/9] Generate SerializedDepNodeIndex using newtype_index macro --- src/librustc/dep_graph/graph.rs | 2 +- src/librustc/dep_graph/serialized.rs | 18 +----------------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/src/librustc/dep_graph/graph.rs b/src/librustc/dep_graph/graph.rs index 5057b1c6065..cb4126245af 100644 --- a/src/librustc/dep_graph/graph.rs +++ b/src/librustc/dep_graph/graph.rs @@ -406,7 +406,7 @@ impl DepGraph { for (current_dep_node_index, edges) in current_dep_graph.edges.iter_enumerated() { let start = edge_list_data.len() as u32; // This should really just be a memcpy :/ - edge_list_data.extend(edges.iter().map(|i| SerializedDepNodeIndex(i.index() as u32))); + edge_list_data.extend(edges.iter().map(|i| SerializedDepNodeIndex::new(i.index()))); let end = edge_list_data.len() as u32; debug_assert_eq!(current_dep_node_index.index(), edge_list_indices.len()); diff --git a/src/librustc/dep_graph/serialized.rs b/src/librustc/dep_graph/serialized.rs index 7275a740e76..c96040ab9b6 100644 --- a/src/librustc/dep_graph/serialized.rs +++ b/src/librustc/dep_graph/serialized.rs @@ -14,23 +14,7 @@ use dep_graph::DepNode; use ich::Fingerprint; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; -/// The index of a DepNode in the SerializedDepGraph::nodes array. -#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Debug, - RustcEncodable, RustcDecodable)] -pub struct SerializedDepNodeIndex(pub u32); - -impl Idx for SerializedDepNodeIndex { - #[inline] - fn new(idx: usize) -> Self { - assert!(idx <= ::std::u32::MAX as usize); - SerializedDepNodeIndex(idx as u32) - } - - #[inline] - fn index(self) -> usize { - self.0 as usize - } -} +newtype_index!(SerializedDepNodeIndex); /// Data for use when recompiling the **current crate**. #[derive(Debug, RustcEncodable, RustcDecodable)] From 271a492cb2028090c0198e893c18024c19bb6fc7 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 28 Sep 2017 17:08:28 -0300 Subject: [PATCH 9/9] Generate ScopeId using newtype_index macro --- src/librustc_data_structures/indexed_vec.rs | 3 ++- src/librustc_mir/build/mod.rs | 14 +------------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index ce2468fb9f1..4b7f55eba06 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -51,7 +51,8 @@ macro_rules! newtype_index { impl $name { // HACK use for constants - pub const fn const_new(x: u32) -> Self { + #[allow(unused)] + const fn const_new(x: u32) -> Self { $name(x) } } diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index 68ef646184c..46a5e5abbdd 100644 --- a/src/librustc_mir/build/mod.rs +++ b/src/librustc_mir/build/mod.rs @@ -311,19 +311,7 @@ struct CFG<'tcx> { basic_blocks: IndexVec>, } -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct ScopeId(u32); - -impl Idx for ScopeId { - fn new(index: usize) -> ScopeId { - assert!(index < (u32::MAX as usize)); - ScopeId(index as u32) - } - - fn index(self) -> usize { - self.0 as usize - } -} +newtype_index!(ScopeId); /////////////////////////////////////////////////////////////////////////// /// The `BlockAnd` "monad" packages up the new basic block along with a