Skip to content

Commit aed530a

Browse files
committed
Lift bounds into GenericParam
1 parent a5328bc commit aed530a

File tree

33 files changed

+339
-344
lines changed

33 files changed

+339
-344
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ pub trait Visitor<'v> : Sized {
314314
fn visit_trait_ref(&mut self, t: &'v TraitRef) {
315315
walk_trait_ref(self, t)
316316
}
317-
fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
317+
fn visit_ty_param_bound(&mut self, bounds: &'v ParamBound) {
318318
walk_ty_param_bound(self, bounds)
319319
}
320320
fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: TraitBoundModifier) {
@@ -731,12 +731,12 @@ pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v
731731
walk_list!(visitor, visit_attribute, &foreign_item.attrs);
732732
}
733733

734-
pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v TyParamBound) {
734+
pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v ParamBound) {
735735
match *bound {
736736
TraitTyParamBound(ref typ, modifier) => {
737737
visitor.visit_poly_trait_ref(typ, modifier);
738738
}
739-
RegionTyParamBound(ref lifetime) => {
739+
Outlives(ref lifetime) => {
740740
visitor.visit_lifetime(lifetime);
741741
}
742742
}
@@ -759,11 +759,11 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
759759
}
760760
GenericParamKind::Type { name, ref bounds, ref default, ref attrs, .. } => {
761761
visitor.visit_name(param.span, name);
762-
walk_list!(visitor, visit_ty_param_bound, bounds);
763762
walk_list!(visitor, visit_ty, default);
764763
walk_list!(visitor, visit_attribute, attrs.iter());
765764
}
766765
}
766+
walk_list!(visitor, visit_ty_param_bound, &param.bounds);
767767
}
768768

769769
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {

src/librustc/hir/lowering.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,9 @@ impl<'a> LoweringContext<'a> {
701701
id: def_node_id,
702702
span,
703703
pure_wrt_drop: false,
704+
bounds: vec![].into(),
704705
kind: hir::GenericParamKind::Lifetime {
705706
name: hir_name,
706-
bounds: vec![].into(),
707707
in_band: true,
708708
lifetime: hir::Lifetime {
709709
id: def_node_id,
@@ -1127,7 +1127,7 @@ impl<'a> LoweringContext<'a> {
11271127
Some(self.lower_poly_trait_ref(ty, itctx))
11281128
}
11291129
TraitTyParamBound(_, TraitBoundModifier::Maybe) => None,
1130-
RegionTyParamBound(ref lifetime) => {
1130+
Outlives(ref lifetime) => {
11311131
if lifetime_bound.is_none() {
11321132
lifetime_bound = Some(self.lower_lifetime(lifetime));
11331133
}
@@ -1246,16 +1246,16 @@ impl<'a> LoweringContext<'a> {
12461246
span,
12471247
);
12481248

1249-
let hir_bounds = self.lower_bounds(bounds, itctx);
1249+
let hir_bounds = self.lower_param_bounds(bounds, itctx);
12501250
// Set the name to `impl Bound1 + Bound2`
12511251
let name = Symbol::intern(&pprust::ty_to_string(t));
12521252
self.in_band_ty_params.push(hir::GenericParam {
12531253
id: def_node_id,
12541254
span,
12551255
pure_wrt_drop: false,
1256+
bounds: hir_bounds,
12561257
kind: hir::GenericParamKind::Type {
12571258
name,
1258-
bounds: hir_bounds,
12591259
default: None,
12601260
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
12611261
attrs: P::new(),
@@ -1299,7 +1299,7 @@ impl<'a> LoweringContext<'a> {
12991299
&mut self,
13001300
exist_ty_id: NodeId,
13011301
parent_index: DefIndex,
1302-
bounds: &hir::TyParamBounds,
1302+
bounds: &hir::ParamBounds,
13031303
) -> (HirVec<hir::Lifetime>, HirVec<hir::GenericParam>) {
13041304
// This visitor walks over impl trait bounds and creates defs for all lifetimes which
13051305
// appear in the bounds, excluding lifetimes that are created within the bounds.
@@ -1420,9 +1420,9 @@ impl<'a> LoweringContext<'a> {
14201420
id: def_node_id,
14211421
span: lifetime.span,
14221422
pure_wrt_drop: false,
1423+
bounds: vec![].into(),
14231424
kind: hir::GenericParamKind::Lifetime {
14241425
name,
1425-
bounds: vec![].into(),
14261426
in_band: false,
14271427
lifetime: hir::Lifetime {
14281428
id: def_node_id,
@@ -1882,18 +1882,18 @@ impl<'a> LoweringContext<'a> {
18821882
})
18831883
}
18841884

1885-
fn lower_ty_param_bound(
1885+
fn lower_param_bound(
18861886
&mut self,
1887-
tpb: &TyParamBound,
1887+
tpb: &ParamBound,
18881888
itctx: ImplTraitContext,
1889-
) -> hir::TyParamBound {
1889+
) -> hir::ParamBound {
18901890
match *tpb {
18911891
TraitTyParamBound(ref ty, modifier) => hir::TraitTyParamBound(
18921892
self.lower_poly_trait_ref(ty, itctx),
18931893
self.lower_trait_bound_modifier(modifier),
18941894
),
1895-
RegionTyParamBound(ref lifetime) => {
1896-
hir::RegionTyParamBound(self.lower_lifetime(lifetime))
1895+
Outlives(ref lifetime) => {
1896+
hir::Outlives(self.lower_lifetime(lifetime))
18971897
}
18981898
}
18991899
}
@@ -1935,19 +1935,20 @@ impl<'a> LoweringContext<'a> {
19351935
fn lower_generic_params(
19361936
&mut self,
19371937
params: &Vec<GenericParam>,
1938-
add_bounds: &NodeMap<Vec<TyParamBound>>,
1938+
add_bounds: &NodeMap<Vec<ParamBound>>,
19391939
itctx: ImplTraitContext,
19401940
) -> hir::HirVec<hir::GenericParam> {
19411941
params.iter().map(|param| self.lower_generic_param(param, add_bounds, itctx)).collect()
19421942
}
19431943

19441944
fn lower_generic_param(&mut self,
19451945
param: &GenericParam,
1946-
add_bounds: &NodeMap<Vec<TyParamBound>>,
1946+
add_bounds: &NodeMap<Vec<ParamBound>>,
19471947
itctx: ImplTraitContext)
19481948
-> hir::GenericParam {
1949+
let mut bounds = self.lower_param_bounds(&param.bounds, itctx);
19491950
match param.kind {
1950-
GenericParamKind::Lifetime { ref bounds, ref lifetime } => {
1951+
GenericParamKind::Lifetime { ref lifetime } => {
19511952
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
19521953
self.is_collecting_in_band_lifetimes = false;
19531954

@@ -1956,9 +1957,9 @@ impl<'a> LoweringContext<'a> {
19561957
id: lifetime.id,
19571958
span: lifetime.span,
19581959
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
1960+
bounds,
19591961
kind: hir::GenericParamKind::Lifetime {
19601962
name: lifetime.name,
1961-
bounds: bounds.iter().map(|lt| self.lower_lifetime(lt)).collect(),
19621963
in_band: false,
19631964
lifetime,
19641965
}
@@ -1968,7 +1969,7 @@ impl<'a> LoweringContext<'a> {
19681969

19691970
param
19701971
}
1971-
GenericParamKind::Type { ref bounds, ref default } => {
1972+
GenericParamKind::Type { ref default, .. } => {
19721973
let mut name = self.lower_ident(param.ident);
19731974

19741975
// Don't expose `Self` (recovered "keyword used as ident" parse error).
@@ -1978,21 +1979,20 @@ impl<'a> LoweringContext<'a> {
19781979
name = Symbol::gensym("Self");
19791980
}
19801981

1981-
let mut bounds = self.lower_bounds(bounds, itctx);
19821982
let add_bounds = add_bounds.get(&param.id).map_or(&[][..], |x| &x);
19831983
if !add_bounds.is_empty() {
19841984
bounds = bounds.into_iter()
1985-
.chain(self.lower_bounds(add_bounds, itctx).into_iter())
1985+
.chain(self.lower_param_bounds(add_bounds, itctx).into_iter())
19861986
.collect();
19871987
}
19881988

19891989
hir::GenericParam {
19901990
id: self.lower_node_id(param.id).node_id,
19911991
span: param.ident.span,
19921992
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
1993+
bounds,
19931994
kind: hir::GenericParamKind::Type {
19941995
name,
1995-
bounds,
19961996
default: default.as_ref().map(|x| {
19971997
self.lower_ty(x, ImplTraitContext::Disallowed)
19981998
}),
@@ -2107,7 +2107,7 @@ impl<'a> LoweringContext<'a> {
21072107
// Ignore `?Trait` bounds.
21082108
// Tthey were copied into type parameters already.
21092109
TraitTyParamBound(_, TraitBoundModifier::Maybe) => None,
2110-
_ => Some(this.lower_ty_param_bound(
2110+
_ => Some(this.lower_param_bound(
21112111
bound,
21122112
ImplTraitContext::Disallowed,
21132113
)),
@@ -2228,15 +2228,9 @@ impl<'a> LoweringContext<'a> {
22282228
}
22292229
}
22302230

2231-
fn lower_bounds(
2232-
&mut self,
2233-
bounds: &[TyParamBound],
2234-
itctx: ImplTraitContext,
2235-
) -> hir::TyParamBounds {
2236-
bounds
2237-
.iter()
2238-
.map(|bound| self.lower_ty_param_bound(bound, itctx))
2239-
.collect()
2231+
fn lower_param_bounds(&mut self, bounds: &[ParamBound], itctx: ImplTraitContext)
2232+
-> hir::ParamBounds {
2233+
bounds.iter().map(|bound| self.lower_param_bound(bound, itctx)).collect()
22402234
}
22412235

22422236
fn lower_block(&mut self, b: &Block, targeted_by_break: bool) -> P<hir::Block> {
@@ -2422,7 +2416,7 @@ impl<'a> LoweringContext<'a> {
24222416
)
24232417
}
24242418
ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref items) => {
2425-
let bounds = self.lower_bounds(bounds, ImplTraitContext::Disallowed);
2419+
let bounds = self.lower_param_bounds(bounds, ImplTraitContext::Disallowed);
24262420
let items = items
24272421
.iter()
24282422
.map(|item| self.lower_trait_item_ref(item))
@@ -2437,7 +2431,7 @@ impl<'a> LoweringContext<'a> {
24372431
}
24382432
ItemKind::TraitAlias(ref generics, ref bounds) => hir::ItemTraitAlias(
24392433
self.lower_generics(generics, ImplTraitContext::Disallowed),
2440-
self.lower_bounds(bounds, ImplTraitContext::Disallowed),
2434+
self.lower_param_bounds(bounds, ImplTraitContext::Disallowed),
24412435
),
24422436
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
24432437
}
@@ -2664,7 +2658,7 @@ impl<'a> LoweringContext<'a> {
26642658
TraitItemKind::Type(ref bounds, ref default) => (
26652659
self.lower_generics(&i.generics, ImplTraitContext::Disallowed),
26662660
hir::TraitItemKind::Type(
2667-
self.lower_bounds(bounds, ImplTraitContext::Disallowed),
2661+
self.lower_param_bounds(bounds, ImplTraitContext::Disallowed),
26682662
default
26692663
.as_ref()
26702664
.map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),

src/librustc/hir/mod.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use self::Mutability::*;
2222
pub use self::PrimTy::*;
2323
pub use self::Stmt_::*;
2424
pub use self::Ty_::*;
25-
pub use self::TyParamBound::*;
25+
pub use self::ParamBound::*;
2626
pub use self::UnOp::*;
2727
pub use self::UnsafeSource::*;
2828
pub use self::Visibility::{Public, Inherited};
@@ -416,41 +416,42 @@ impl GenericArgs {
416416
}
417417
}
418418

419+
/// A modifier on a bound, currently this is only used for `?Sized`, where the
420+
/// modifier is `Maybe`. Negative bounds should also be handled here.
421+
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
422+
pub enum TraitBoundModifier {
423+
None,
424+
Maybe,
425+
}
426+
427+
pub type Outlives = Lifetime;
428+
419429
/// The AST represents all type param bounds as types.
420430
/// typeck::collect::compute_bounds matches these against
421431
/// the "special" built-in traits (see middle::lang_items) and
422432
/// detects Copy, Send and Sync.
423433
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
424-
pub enum TyParamBound {
434+
pub enum ParamBound {
425435
TraitTyParamBound(PolyTraitRef, TraitBoundModifier),
426-
RegionTyParamBound(Lifetime),
436+
Outlives(Lifetime),
427437
}
428438

429-
impl TyParamBound {
439+
impl ParamBound {
430440
pub fn span(&self) -> Span {
431441
match self {
432442
&TraitTyParamBound(ref t, ..) => t.span,
433-
&RegionTyParamBound(ref l) => l.span,
443+
&Outlives(ref l) => l.span,
434444
}
435445
}
436446
}
437447

438-
/// A modifier on a bound, currently this is only used for `?Sized`, where the
439-
/// modifier is `Maybe`. Negative bounds should also be handled here.
440-
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
441-
pub enum TraitBoundModifier {
442-
None,
443-
Maybe,
444-
}
445-
446-
pub type TyParamBounds = HirVec<TyParamBound>;
448+
pub type ParamBounds = HirVec<ParamBound>;
447449

448450
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
449451
pub enum GenericParamKind {
450452
/// A lifetime definition, eg `'a: 'b + 'c + 'd`.
451453
Lifetime {
452454
name: LifetimeName,
453-
bounds: HirVec<Lifetime>,
454455
// Indicates that the lifetime definition was synthetically added
455456
// as a result of an in-band lifetime usage like:
456457
// `fn foo(x: &'a u8) -> &'a u8 { x }`
@@ -460,7 +461,6 @@ pub enum GenericParamKind {
460461
},
461462
Type {
462463
name: Name,
463-
bounds: TyParamBounds,
464464
default: Option<P<Ty>>,
465465
synthetic: Option<SyntheticTyParamKind>,
466466
attrs: HirVec<Attribute>,
@@ -470,6 +470,7 @@ pub enum GenericParamKind {
470470
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
471471
pub struct GenericParam {
472472
pub id: NodeId,
473+
pub bounds: ParamBounds,
473474
pub span: Span,
474475
pub pure_wrt_drop: bool,
475476

@@ -587,7 +588,7 @@ pub struct WhereBoundPredicate {
587588
/// The type being bounded
588589
pub bounded_ty: P<Ty>,
589590
/// Trait and lifetime bounds (`Clone+Send+'static`)
590-
pub bounds: TyParamBounds,
591+
pub bounds: ParamBounds,
591592
}
592593

593594
/// A lifetime predicate, e.g. `'a: 'b+'c`
@@ -1554,7 +1555,7 @@ pub enum TraitItemKind {
15541555
Method(MethodSig, TraitMethod),
15551556
/// An associated type with (possibly empty) bounds and optional concrete
15561557
/// type
1557-
Type(TyParamBounds, Option<P<Ty>>),
1558+
Type(ParamBounds, Option<P<Ty>>),
15581559
}
15591560

15601561
// The bodies for items are stored "out of line", in a separate
@@ -1639,7 +1640,7 @@ pub struct BareFnTy {
16391640
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
16401641
pub struct ExistTy {
16411642
pub generics: Generics,
1642-
pub bounds: TyParamBounds,
1643+
pub bounds: ParamBounds,
16431644
pub impl_trait_fn: Option<DefId>,
16441645
}
16451646

@@ -2048,9 +2049,9 @@ pub enum Item_ {
20482049
/// A union definition, e.g. `union Foo<A, B> {x: A, y: B}`
20492050
ItemUnion(VariantData, Generics),
20502051
/// Represents a Trait Declaration
2051-
ItemTrait(IsAuto, Unsafety, Generics, TyParamBounds, HirVec<TraitItemRef>),
2052+
ItemTrait(IsAuto, Unsafety, Generics, ParamBounds, HirVec<TraitItemRef>),
20522053
/// Represents a Trait Alias Declaration
2053-
ItemTraitAlias(Generics, TyParamBounds),
2054+
ItemTraitAlias(Generics, ParamBounds),
20542055

20552056
/// An implementation, eg `impl<A> Trait for Foo { .. }`
20562057
ItemImpl(Unsafety,

0 commit comments

Comments
 (0)