Skip to content

Commit e3da2a9

Browse files
committed
Improve OwnedSlice and use it in HIR
1 parent 29ea4ee commit e3da2a9

File tree

9 files changed

+69
-34
lines changed

9 files changed

+69
-34
lines changed

src/librustc/middle/infer/error_reporting.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1153,11 +1153,11 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
11531153
}
11541154

11551155
fn rebuild_ty_params(&self,
1156-
ty_params: P<[hir::TyParam]>,
1156+
ty_params: hir::HirVec<hir::TyParam>,
11571157
lifetime: hir::Lifetime,
11581158
region_names: &HashSet<ast::Name>)
1159-
-> P<[hir::TyParam]> {
1160-
ty_params.map(|ty_param| {
1159+
-> hir::HirVec<hir::TyParam> {
1160+
ty_params.iter().map(|ty_param| {
11611161
let bounds = self.rebuild_ty_param_bounds(ty_param.bounds.clone(),
11621162
lifetime,
11631163
region_names);
@@ -1168,15 +1168,15 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
11681168
default: ty_param.default.clone(),
11691169
span: ty_param.span,
11701170
}
1171-
})
1171+
}).collect()
11721172
}
11731173

11741174
fn rebuild_ty_param_bounds(&self,
11751175
ty_param_bounds: hir::TyParamBounds,
11761176
lifetime: hir::Lifetime,
11771177
region_names: &HashSet<ast::Name>)
11781178
-> hir::TyParamBounds {
1179-
ty_param_bounds.map(|tpb| {
1179+
ty_param_bounds.iter().map(|tpb| {
11801180
match tpb {
11811181
&hir::RegionTyParamBound(lt) => {
11821182
// FIXME -- it's unclear whether I'm supposed to
@@ -1212,7 +1212,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
12121212
}, modifier)
12131213
}
12141214
}
1215-
})
1215+
}).collect()
12161216
}
12171217

12181218
fn rebuild_expl_self(&self,
@@ -1248,7 +1248,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
12481248
add: &Vec<hir::Lifetime>,
12491249
keep: &HashSet<ast::Name>,
12501250
remove: &HashSet<ast::Name>,
1251-
ty_params: P<[hir::TyParam]>,
1251+
ty_params: hir::HirVec<hir::TyParam>,
12521252
where_clause: hir::WhereClause)
12531253
-> hir::Generics {
12541254
let mut lifetimes = Vec::new();
@@ -1498,10 +1498,10 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
14981498
}
14991499
}
15001500
}
1501-
let new_types = data.types.map(|t| {
1501+
let new_types = data.types.iter().map(|t| {
15021502
self.rebuild_arg_ty_or_output(&**t, lifetime, anon_nums, region_names)
1503-
});
1504-
let new_bindings = data.bindings.map(|b| {
1503+
}).collect();
1504+
let new_bindings = data.bindings.iter().map(|b| {
15051505
hir::TypeBinding {
15061506
id: b.id,
15071507
name: b.name,
@@ -1511,7 +1511,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
15111511
region_names),
15121512
span: b.span
15131513
}
1514-
});
1514+
}).collect();
15151515
hir::AngleBracketedParameters(hir::AngleBracketedParameterData {
15161516
lifetimes: new_lts.into(),
15171517
types: new_types,

src/librustc_front/fold.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub trait Folder : Sized {
210210
noop_fold_ty_param(tp, self)
211211
}
212212

213-
fn fold_ty_params(&mut self, tps: P<[TyParam]>) -> P<[TyParam]> {
213+
fn fold_ty_params(&mut self, tps: HirVec<TyParam>) -> HirVec<TyParam> {
214214
noop_fold_ty_params(tps, self)
215215
}
216216

@@ -575,9 +575,9 @@ pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam {
575575
}
576576
}
577577

578-
pub fn noop_fold_ty_params<T: Folder>(tps: P<[TyParam]>,
578+
pub fn noop_fold_ty_params<T: Folder>(tps: HirVec<TyParam>,
579579
fld: &mut T)
580-
-> P<[TyParam]> {
580+
-> HirVec<TyParam> {
581581
tps.move_map(|tp| fld.fold_ty_param(tp))
582582
}
583583

src/librustc_front/hir.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use serialize::{Encodable, Decodable, Encoder, Decoder};
5656
/// It can be `Vec`, `P<[T]>` or potentially `Box<[T]>`, or some other container with similar
5757
/// behavior. Unlike AST, HIR is mostly a static structure, so we can use an owned slice instead
5858
/// of `Vec` to avoid keeping extra capacity.
59-
pub type HirVec<T> = Vec<T>;
59+
pub type HirVec<T> = P<[T]>;
6060

6161
macro_rules! hir_vec {
6262
($elem:expr; $n:expr) => (
@@ -208,8 +208,8 @@ impl PathParameters {
208208
pub fn none() -> PathParameters {
209209
AngleBracketedParameters(AngleBracketedParameterData {
210210
lifetimes: HirVec::new(),
211-
types: P::empty(),
212-
bindings: P::empty(),
211+
types: HirVec::new(),
212+
bindings: HirVec::new(),
213213
})
214214
}
215215

@@ -282,10 +282,10 @@ pub struct AngleBracketedParameterData {
282282
/// The lifetime parameters for this path segment.
283283
pub lifetimes: HirVec<Lifetime>,
284284
/// The type parameters for this path segment, if present.
285-
pub types: P<[P<Ty>]>,
285+
pub types: HirVec<P<Ty>>,
286286
/// Bindings (equality constraints) on associated types, if present.
287287
/// E.g., `Foo<A=Bar>`.
288-
pub bindings: P<[TypeBinding]>,
288+
pub bindings: HirVec<TypeBinding>,
289289
}
290290

291291
impl AngleBracketedParameterData {
@@ -325,7 +325,7 @@ pub enum TraitBoundModifier {
325325
Maybe,
326326
}
327327

328-
pub type TyParamBounds = P<[TyParamBound]>;
328+
pub type TyParamBounds = HirVec<TyParamBound>;
329329

330330
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
331331
pub struct TyParam {
@@ -341,7 +341,7 @@ pub struct TyParam {
341341
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
342342
pub struct Generics {
343343
pub lifetimes: HirVec<LifetimeDef>,
344-
pub ty_params: P<[TyParam]>,
344+
pub ty_params: HirVec<TyParam>,
345345
pub where_clause: WhereClause,
346346
}
347347

src/librustc_front/lowering.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ pub fn lower_ty_param(lctx: &LoweringContext, tp: &TyParam) -> hir::TyParam {
434434

435435
pub fn lower_ty_params(lctx: &LoweringContext,
436436
tps: &P<[TyParam]>)
437-
-> P<[hir::TyParam]> {
437+
-> hir::HirVec<hir::TyParam> {
438438
tps.iter().map(|tp| lower_ty_param(lctx, tp)).collect()
439439
}
440440

@@ -1772,19 +1772,19 @@ fn path_ident(span: Span, id: hir::Ident) -> hir::Path {
17721772
}
17731773

17741774
fn path(span: Span, strs: Vec<hir::Ident>) -> hir::Path {
1775-
path_all(span, false, strs, hir::HirVec::new(), Vec::new(), Vec::new())
1775+
path_all(span, false, strs, hir::HirVec::new(), hir::HirVec::new(), hir::HirVec::new())
17761776
}
17771777

17781778
fn path_global(span: Span, strs: Vec<hir::Ident>) -> hir::Path {
1779-
path_all(span, true, strs, hir::HirVec::new(), Vec::new(), Vec::new())
1779+
path_all(span, true, strs, hir::HirVec::new(), hir::HirVec::new(), hir::HirVec::new())
17801780
}
17811781

17821782
fn path_all(sp: Span,
17831783
global: bool,
17841784
mut idents: Vec<hir::Ident>,
17851785
lifetimes: hir::HirVec<hir::Lifetime>,
1786-
types: Vec<P<hir::Ty>>,
1787-
bindings: Vec<hir::TypeBinding>)
1786+
types: hir::HirVec<P<hir::Ty>>,
1787+
bindings: hir::HirVec<hir::TypeBinding>)
17881788
-> hir::Path {
17891789
let last_identifier = idents.pop().unwrap();
17901790
let mut segments: Vec<hir::PathSegment> = idents.into_iter()
@@ -1799,8 +1799,8 @@ fn path_all(sp: Span,
17991799
identifier: last_identifier,
18001800
parameters: hir::AngleBracketedParameters(hir::AngleBracketedParameterData {
18011801
lifetimes: lifetimes,
1802-
types: P::from_vec(types),
1803-
bindings: P::from_vec(bindings),
1802+
types: types,
1803+
bindings: bindings,
18041804
}),
18051805
});
18061806
hir::Path {

src/librustc_front/print/pprust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl<'a> State<'a> {
518518
hir::TyBareFn(ref f) => {
519519
let generics = hir::Generics {
520520
lifetimes: f.lifetimes.clone(),
521-
ty_params: P::empty(),
521+
ty_params: hir::HirVec::new(),
522522
where_clause: hir::WhereClause {
523523
id: ast::DUMMY_NODE_ID,
524524
predicates: hir::HirVec::new(),
@@ -2257,7 +2257,7 @@ impl<'a> State<'a> {
22572257
}
22582258
let generics = hir::Generics {
22592259
lifetimes: hir::HirVec::new(),
2260-
ty_params: P::empty(),
2260+
ty_params: hir::HirVec::new(),
22612261
where_clause: hir::WhereClause {
22622262
id: ast::DUMMY_NODE_ID,
22632263
predicates: hir::HirVec::new(),

src/librustc_front/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ pub fn is_path(e: P<Expr>) -> bool {
335335
pub fn empty_generics() -> Generics {
336336
Generics {
337337
lifetimes: HirVec::new(),
338-
ty_params: P::empty(),
338+
ty_params: HirVec::new(),
339339
where_clause: WhereClause {
340340
id: DUMMY_NODE_ID,
341341
predicates: HirVec::new(),
@@ -353,8 +353,8 @@ pub fn ident_to_path(s: Span, ident: Ident) -> Path {
353353
identifier: ident,
354354
parameters: hir::AngleBracketedParameters(hir::AngleBracketedParameterData {
355355
lifetimes: HirVec::new(),
356-
types: P::empty(),
357-
bindings: P::empty(),
356+
types: HirVec::new(),
357+
bindings: HirVec::new(),
358358
}),
359359
}],
360360
}

src/librustc_mir/hair/cx/to_ref.rs

+10
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,13 @@ impl<'a,'tcx:'a,T,U> ToRef for &'tcx Vec<T>
6161
self.iter().map(|expr| expr.to_ref()).collect()
6262
}
6363
}
64+
65+
impl<'a,'tcx:'a,T,U> ToRef for &'tcx P<[T]>
66+
where &'tcx T: ToRef<Output=U>
67+
{
68+
type Output = Vec<U>;
69+
70+
fn to_ref(self) -> Vec<U> {
71+
self.iter().map(|expr| expr.to_ref()).collect()
72+
}
73+
}

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4906,7 +4906,7 @@ pub fn may_break(cx: &ty::ctxt, id: ast::NodeId, b: &hir::Block) -> bool {
49064906
}
49074907

49084908
pub fn check_bounds_are_used<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
4909-
tps: &P<[hir::TyParam]>,
4909+
tps: &[hir::TyParam],
49104910
ty: Ty<'tcx>) {
49114911
debug!("check_bounds_are_used(n_tps={}, ty={:?})",
49124912
tps.len(), ty);

src/libsyntax/ptr.rs

+25
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ impl<T:fmt::Debug> fmt::Debug for P<[T]> {
130130
}
131131

132132
impl<T> P<[T]> {
133+
pub fn new() -> P<[T]> {
134+
P::empty()
135+
}
136+
133137
pub fn empty() -> P<[T]> {
134138
P { ptr: Default::default() }
135139
}
@@ -177,12 +181,33 @@ impl<T: Clone> Clone for P<[T]> {
177181
}
178182
}
179183

184+
impl<T> From<Vec<T>> for P<[T]> {
185+
fn from(v: Vec<T>) -> Self {
186+
P::from_vec(v)
187+
}
188+
}
189+
190+
impl<T> Into<Vec<T>> for P<[T]> {
191+
fn into(self) -> Vec<T> {
192+
self.into_vec()
193+
}
194+
}
195+
180196
impl<T> FromIterator<T> for P<[T]> {
181197
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> P<[T]> {
182198
P::from_vec(iter.into_iter().collect())
183199
}
184200
}
185201

202+
impl<T> IntoIterator for P<[T]> {
203+
type Item = T;
204+
type IntoIter = vec::IntoIter<T>;
205+
206+
fn into_iter(self) -> Self::IntoIter {
207+
self.into_vec().into_iter()
208+
}
209+
}
210+
186211
impl<'a, T> IntoIterator for &'a P<[T]> {
187212
type Item = &'a T;
188213
type IntoIter = slice::Iter<'a, T>;

0 commit comments

Comments
 (0)