diff options
| author | varkor <github@varkor.com> | 2018-05-28 13:33:28 +0100 |
|---|---|---|
| committer | varkor <github@varkor.com> | 2018-06-20 12:22:46 +0100 |
| commit | aed530a457dd937fa633dfe52cf07811196d3173 (patch) | |
| tree | d7848036bf766e1153e965028d8c4c5c8ecb5940 /src/libsyntax | |
| parent | a5328bc17b8d18083478554b3381d55183647f15 (diff) | |
| download | rust-aed530a457dd937fa633dfe52cf07811196d3173.tar.gz rust-aed530a457dd937fa633dfe52cf07811196d3173.zip | |
Lift bounds into GenericParam
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 45 | ||||
| -rw-r--r-- | src/libsyntax/ext/build.rs | 16 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 23 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 22 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 23 | ||||
| -rw-r--r-- | src/libsyntax/util/node_count.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 11 |
7 files changed, 73 insertions, 69 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index f589057218c..b082cde5df7 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -10,7 +10,7 @@ // The Rust abstract syntax tree. -pub use self::TyParamBound::*; +pub use self::ParamBound::*; pub use self::UnsafeSource::*; pub use self::GenericArgs::*; pub use symbol::{Ident, Symbol as Name}; @@ -269,44 +269,42 @@ pub const CRATE_NODE_ID: NodeId = NodeId(0); /// small, positive ids. pub const DUMMY_NODE_ID: NodeId = NodeId(!0); +/// A modifier on a bound, currently this is only used for `?Sized`, where the +/// modifier is `Maybe`. Negative bounds should also be handled here. +#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] +pub enum TraitBoundModifier { + None, + Maybe, +} + /// The AST represents all type param bounds as types. /// typeck::collect::compute_bounds matches these against /// the "special" built-in traits (see middle::lang_items) and /// detects Copy, Send and Sync. #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] -pub enum TyParamBound { +pub enum ParamBound { TraitTyParamBound(PolyTraitRef, TraitBoundModifier), - RegionTyParamBound(Lifetime) + Outlives(Lifetime) } -impl TyParamBound { +impl ParamBound { pub fn span(&self) -> Span { match self { &TraitTyParamBound(ref t, ..) => t.span, - &RegionTyParamBound(ref l) => l.ident.span, + &Outlives(ref l) => l.ident.span, } } } -/// A modifier on a bound, currently this is only used for `?Sized`, where the -/// modifier is `Maybe`. Negative bounds should also be handled here. -#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] -pub enum TraitBoundModifier { - None, - Maybe, -} - -pub type TyParamBounds = Vec<TyParamBound>; +pub type ParamBounds = Vec<ParamBound>; #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum GenericParamKind { /// A lifetime definition, e.g. `'a: 'b+'c+'d`. Lifetime { - bounds: Vec<Lifetime>, lifetime: Lifetime, }, Type { - bounds: TyParamBounds, default: Option<P<Ty>>, } } @@ -316,6 +314,7 @@ pub struct GenericParam { pub ident: Ident, pub id: NodeId, pub attrs: ThinVec<Attribute>, + pub bounds: ParamBounds, pub kind: GenericParamKind, } @@ -384,7 +383,7 @@ pub struct WhereBoundPredicate { /// The type being bounded pub bounded_ty: P<Ty>, /// Trait and lifetime bounds (`Clone+Send+'static`) - pub bounds: TyParamBounds, + pub bounds: ParamBounds, } /// A lifetime predicate. @@ -930,7 +929,7 @@ impl Expr { } } - fn to_bound(&self) -> Option<TyParamBound> { + fn to_bound(&self) -> Option<ParamBound> { match &self.node { ExprKind::Path(None, path) => Some(TraitTyParamBound(PolyTraitRef::new(Vec::new(), path.clone(), self.span), @@ -1355,7 +1354,7 @@ pub struct TraitItem { pub enum TraitItemKind { Const(P<Ty>, Option<P<Expr>>), Method(MethodSig, Option<P<Block>>), - Type(TyParamBounds, Option<P<Ty>>), + Type(ParamBounds, Option<P<Ty>>), Macro(Mac), } @@ -1540,10 +1539,10 @@ pub enum TyKind { Path(Option<QSelf>, Path), /// A trait object type `Bound1 + Bound2 + Bound3` /// where `Bound` is a trait or a lifetime. - TraitObject(TyParamBounds, TraitObjectSyntax), + TraitObject(ParamBounds, TraitObjectSyntax), /// An `impl Bound1 + Bound2 + Bound3` type /// where `Bound` is a trait or a lifetime. - ImplTrait(TyParamBounds), + ImplTrait(ParamBounds), /// No-op; kept solely so that we can pretty-print faithfully Paren(P<Ty>), /// Unused for now @@ -2064,11 +2063,11 @@ pub enum ItemKind { /// A Trait declaration (`trait` or `pub trait`). /// /// E.g. `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}` - Trait(IsAuto, Unsafety, Generics, TyParamBounds, Vec<TraitItem>), + Trait(IsAuto, Unsafety, Generics, ParamBounds, Vec<TraitItem>), /// Trait alias /// /// E.g. `trait Foo = Bar + Quux;` - TraitAlias(Generics, TyParamBounds), + TraitAlias(Generics, ParamBounds), /// An implementation. /// /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }` diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 695ad9c233f..ea151ca68a8 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -68,18 +68,18 @@ pub trait AstBuilder { span: Span, id: ast::Ident, attrs: Vec<ast::Attribute>, - bounds: ast::TyParamBounds, + bounds: ast::ParamBounds, default: Option<P<ast::Ty>>) -> ast::GenericParam; fn trait_ref(&self, path: ast::Path) -> ast::TraitRef; fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef; - fn typarambound(&self, path: ast::Path) -> ast::TyParamBound; + fn ty_param_bound(&self, path: ast::Path) -> ast::ParamBound; fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime; fn lifetime_def(&self, span: Span, ident: ast::Ident, attrs: Vec<ast::Attribute>, - bounds: Vec<ast::Lifetime>) + bounds: ast::ParamBounds) -> ast::GenericParam; // statements @@ -436,14 +436,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> { span: Span, ident: ast::Ident, attrs: Vec<ast::Attribute>, - bounds: ast::TyParamBounds, + bounds: ast::ParamBounds, default: Option<P<ast::Ty>>) -> ast::GenericParam { ast::GenericParam { ident: ident.with_span_pos(span), id: ast::DUMMY_NODE_ID, attrs: attrs.into(), + bounds, kind: ast::GenericParamKind::Type { - bounds, default, } } @@ -464,7 +464,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } } - fn typarambound(&self, path: ast::Path) -> ast::TyParamBound { + fn ty_param_bound(&self, path: ast::Path) -> ast::ParamBound { ast::TraitTyParamBound(self.poly_trait_ref(path.span, path), ast::TraitBoundModifier::None) } @@ -476,16 +476,16 @@ impl<'a> AstBuilder for ExtCtxt<'a> { span: Span, ident: ast::Ident, attrs: Vec<ast::Attribute>, - bounds: Vec<ast::Lifetime>) + bounds: ast::ParamBounds) -> ast::GenericParam { let lifetime = self.lifetime(span, ident); ast::GenericParam { ident: lifetime.ident, id: lifetime.id, attrs: attrs.into(), + bounds, kind: ast::GenericParamKind::Lifetime { lifetime, - bounds, } } } diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index ea147186b18..a0c69d83e84 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -268,17 +268,16 @@ pub trait Folder : Sized { noop_fold_interpolated(nt, self) } - fn fold_opt_bounds(&mut self, b: Option<TyParamBounds>) - -> Option<TyParamBounds> { + fn fold_opt_bounds(&mut self, b: Option<TyParamBounds>) -> Option<TyParamBounds> { noop_fold_opt_bounds(b, self) } - fn fold_bounds(&mut self, b: TyParamBounds) - -> TyParamBounds { + fn fold_bounds(&mut self, b: ParamBounds) + -> ParamBounds { noop_fold_bounds(b, self) } - fn fold_ty_param_bound(&mut self, tpb: TyParamBound) -> TyParamBound { + fn fold_ty_param_bound(&mut self, tpb: ParamBound) -> ParamBound { noop_fold_ty_param_bound(tpb, self) } @@ -678,12 +677,12 @@ pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> { }) } -pub fn noop_fold_ty_param_bound<T>(tpb: TyParamBound, fld: &mut T) - -> TyParamBound +pub fn noop_fold_ty_param_bound<T>(tpb: ParamBound, fld: &mut T) + -> ParamBound where T: Folder { match tpb { TraitTyParamBound(ty, modifier) => TraitTyParamBound(fld.fold_poly_trait_ref(ty), modifier), - RegionTyParamBound(lifetime) => RegionTyParamBound(noop_fold_lifetime(lifetime, fld)), + Outlives(lifetime) => Outlives(noop_fold_lifetime(lifetime, fld)), } } @@ -850,13 +849,13 @@ pub fn noop_fold_mt<T: Folder>(MutTy {ty, mutbl}: MutTy, folder: &mut T) -> MutT } } -pub fn noop_fold_opt_bounds<T: Folder>(b: Option<TyParamBounds>, folder: &mut T) - -> Option<TyParamBounds> { +pub fn noop_fold_opt_bounds<T: Folder>(b: Option<ParamBounds>, folder: &mut T) + -> Option<ParamBounds> { b.map(|bounds| folder.fold_bounds(bounds)) } -fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T) - -> TyParamBounds { +fn noop_fold_bounds<T: Folder>(bounds: ParamBounds, folder: &mut T) + -> ParamBounds { bounds.move_map(|bound| folder.fold_ty_param_bound(bound)) } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 203b529222d..ce79735fff5 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -10,7 +10,7 @@ use rustc_target::spec::abi::{self, Abi}; use ast::{AngleBracketedArgs, ParenthesizedArgData, AttrStyle, BareFnTy}; -use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier}; +use ast::{Outlives, TraitTyParamBound, TraitBoundModifier}; use ast::Unsafety; use ast::{Mod, AnonConst, Arg, Arm, Attribute, BindingMode, TraitItemKind}; use ast::Block; @@ -36,7 +36,7 @@ use ast::{VariantData, StructField}; use ast::StrStyle; use ast::SelfKind; use ast::{TraitItem, TraitRef, TraitObjectSyntax}; -use ast::{Ty, TyKind, TypeBinding, TyParamBounds}; +use ast::{Ty, TyKind, TypeBinding, ParamBounds}; use ast::{Visibility, VisibilityKind, WhereClause, CrateSugar}; use ast::{UseTree, UseTreeKind}; use ast::{BinOpKind, UnOp}; @@ -4735,7 +4735,7 @@ impl<'a> Parser<'a> { // LT_BOUND = LIFETIME (e.g. `'a`) // TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN) // TY_BOUND_NOPAREN = [?] [for<LT_PARAM_DEFS>] SIMPLE_PATH (e.g. `?for<'a: 'b> m::Trait<'a>`) - fn parse_ty_param_bounds_common(&mut self, allow_plus: bool) -> PResult<'a, TyParamBounds> { + fn parse_ty_param_bounds_common(&mut self, allow_plus: bool) -> PResult<'a, ParamBounds> { let mut bounds = Vec::new(); loop { // This needs to be syncronized with `Token::can_begin_bound`. @@ -4752,7 +4752,7 @@ impl<'a> Parser<'a> { self.span_err(question_span, "`?` may only modify trait bounds, not lifetime bounds"); } - bounds.push(RegionTyParamBound(self.expect_lifetime())); + bounds.push(Outlives(RegionTyParamBound(self.expect_lifetime()))); if has_parens { self.expect(&token::CloseDelim(token::Paren))?; self.span_err(self.prev_span, @@ -4784,7 +4784,7 @@ impl<'a> Parser<'a> { return Ok(bounds); } - fn parse_ty_param_bounds(&mut self) -> PResult<'a, TyParamBounds> { + fn parse_ty_param_bounds(&mut self) -> PResult<'a, ParamBounds> { self.parse_ty_param_bounds_common(true) } @@ -4823,17 +4823,17 @@ impl<'a> Parser<'a> { Ok(GenericParam { ident, - attrs: preceding_attrs.into(), id: ast::DUMMY_NODE_ID, + attrs: preceding_attrs.into(), + bounds, kind: GenericParamKind::Type { - bounds, default, } }) } /// Parses the following grammar: - /// TraitItemAssocTy = Ident ["<"...">"] [":" [TyParamBounds]] ["where" ...] ["=" Ty] + /// TraitItemAssocTy = Ident ["<"...">"] [":" [ParamBounds]] ["where" ...] ["=" Ty] fn parse_trait_item_assoc_ty(&mut self) -> PResult<'a, (Ident, TraitItemKind, ast::Generics)> { let ident = self.parse_ident()?; @@ -4868,7 +4868,9 @@ impl<'a> Parser<'a> { let lifetime = self.expect_lifetime(); // Parse lifetime parameter. let bounds = if self.eat(&token::Colon) { - self.parse_lt_param_bounds() + self.parse_lt_param_bounds().iter() + .map(|bound| ast::ParamBound::Outlives(*bound)) + .collect() } else { Vec::new() }; @@ -4876,9 +4878,9 @@ impl<'a> Parser<'a> { ident: lifetime.ident, id: lifetime.id, attrs: attrs.into(), + bounds, kind: ast::GenericParamKind::Lifetime { lifetime, - bounds, } }); if seen_ty_param { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 84a4a51b716..c8d139c7de9 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -12,7 +12,7 @@ pub use self::AnnNode::*; use rustc_target::spec::abi::{self, Abi}; use ast::{self, BlockCheckMode, PatKind, RangeEnd, RangeSyntax}; -use ast::{SelfKind, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier}; +use ast::{SelfKind, Outlives, TraitTyParamBound, TraitBoundModifier}; use ast::{Attribute, MacDelimiter, GenericArg}; use util::parser::{self, AssocOp, Fixity}; use attr; @@ -292,7 +292,7 @@ pub fn ty_to_string(ty: &ast::Ty) -> String { to_string(|s| s.print_type(ty)) } -pub fn bounds_to_string(bounds: &[ast::TyParamBound]) -> String { +pub fn bounds_to_string(bounds: &[ast::ParamBound]) -> String { to_string(|s| s.print_bounds("", bounds)) } @@ -1178,7 +1178,7 @@ impl<'a> State<'a> { fn print_associated_type(&mut self, ident: ast::Ident, - bounds: Option<&ast::TyParamBounds>, + bounds: Option<&ast::ParamBounds>, ty: Option<&ast::Ty>) -> io::Result<()> { self.word_space("type")?; @@ -2811,7 +2811,7 @@ impl<'a> State<'a> { pub fn print_bounds(&mut self, prefix: &str, - bounds: &[ast::TyParamBound]) + bounds: &[ast::ParamBound]) -> io::Result<()> { if !bounds.is_empty() { self.s.word(prefix)?; @@ -2833,7 +2833,7 @@ impl<'a> State<'a> { } self.print_poly_trait_ref(tref)?; } - RegionTyParamBound(lt) => { + Outlives(lt) => { self.print_lifetime(lt)?; } } @@ -2879,14 +2879,19 @@ impl<'a> State<'a> { self.commasep(Inconsistent, &generic_params, |s, param| { match param.kind { - ast::GenericParamKind::Lifetime { ref bounds, ref lifetime } => { + ast::GenericParamKind::Lifetime { ref lifetime } => { s.print_outer_attributes_inline(¶m.attrs)?; - s.print_lifetime_bounds(lifetime, bounds) + s.print_lifetime_bounds(lifetime, ¶m.bounds.iter().map(|bound| { + match bound { + ast::ParamBound::Outlives(lt) => *lt, + _ => panic!(), + } + }).collect::<Vec<_>>().as_slice()) }, - ast::GenericParamKind::Type { ref bounds, ref default } => { + ast::GenericParamKind::Type { ref default } => { s.print_outer_attributes_inline(¶m.attrs)?; s.print_ident(param.ident)?; - s.print_bounds(":", bounds)?; + s.print_bounds(":", ¶m.bounds)?; match default { Some(ref default) => { s.s.space()?; diff --git a/src/libsyntax/util/node_count.rs b/src/libsyntax/util/node_count.rs index 95ae9f9bcf8..485775765ab 100644 --- a/src/libsyntax/util/node_count.rs +++ b/src/libsyntax/util/node_count.rs @@ -95,7 +95,7 @@ impl<'ast> Visitor<'ast> for NodeCounter { self.count += 1; walk_trait_ref(self, t) } - fn visit_ty_param_bound(&mut self, bounds: &TyParamBound) { + fn visit_ty_param_bound(&mut self, bounds: &ParamBound) { self.count += 1; walk_ty_param_bound(self, bounds) } diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index e12369a522d..4e0c417d4fb 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -86,7 +86,7 @@ pub trait Visitor<'ast>: Sized { fn visit_trait_item(&mut self, ti: &'ast TraitItem) { walk_trait_item(self, ti) } fn visit_impl_item(&mut self, ii: &'ast ImplItem) { walk_impl_item(self, ii) } fn visit_trait_ref(&mut self, t: &'ast TraitRef) { walk_trait_ref(self, t) } - fn visit_ty_param_bound(&mut self, bounds: &'ast TyParamBound) { + fn visit_ty_param_bound(&mut self, bounds: &'ast ParamBound) { walk_ty_param_bound(self, bounds) } fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) { @@ -479,31 +479,30 @@ pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) { // Empty! } -pub fn walk_ty_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a TyParamBound) { +pub fn walk_ty_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a ParamBound) { match *bound { TraitTyParamBound(ref typ, ref modifier) => { visitor.visit_poly_trait_ref(typ, modifier); } - RegionTyParamBound(ref lifetime) => { + Outlives(ref lifetime) => { visitor.visit_lifetime(lifetime); } } } pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) { + visitor.visit_ident(param.ident); match param.kind { GenericParamKind::Lifetime { ref bounds, ref lifetime } => { - visitor.visit_ident(param.ident); walk_list!(visitor, visit_lifetime, bounds); - walk_list!(visitor, visit_attribute, param.attrs.iter()); } GenericParamKind::Type { ref bounds, ref default } => { visitor.visit_ident(t.ident); walk_list!(visitor, visit_ty_param_bound, bounds); walk_list!(visitor, visit_ty, default); - walk_list!(visitor, visit_attribute, param.attrs.iter()); } } + walk_list!(visitor, visit_attribute, param.attrs.iter()); } pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) { |
