about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2014-08-05 22:59:24 -0400
committerNiko Matsakis <niko@alum.mit.edu>2014-08-07 07:23:59 -0400
commitfcab98038c3b466d9ecd00b0f27e9c748e7acbde (patch)
tree345deba110cec778935c4d2fa1b7f9150c90f7c3 /src/libsyntax
parent1a53c001170f8084ce850498d5e8f22b5e7da72c (diff)
downloadrust-fcab98038c3b466d9ecd00b0f27e9c748e7acbde.tar.gz
rust-fcab98038c3b466d9ecd00b0f27e9c748e7acbde.zip
Temporary bootstrapping hack: introduce syntax for r egion bounds like `'b:'a`,
meaning `'b outlives 'a`. Syntax currently does nothing but is needed for full
fix to #5763. To use this syntax, the issue_5763_bootstrap feature guard is
required.
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ast.rs12
-rw-r--r--src/libsyntax/ast_util.rs2
-rw-r--r--src/libsyntax/ext/build.rs16
-rw-r--r--src/libsyntax/ext/deriving/generic/mod.rs8
-rw-r--r--src/libsyntax/ext/deriving/generic/ty.rs15
-rw-r--r--src/libsyntax/fold.rs29
-rw-r--r--src/libsyntax/parse/parser.rs91
-rw-r--r--src/libsyntax/print/pprust.rs20
-rw-r--r--src/libsyntax/visit.rs4
9 files changed, 149 insertions, 48 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index 2d39a47a85e..fc000d2357e 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -160,6 +160,12 @@ pub struct Lifetime {
     pub name: Name
 }
 
+#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
+pub struct LifetimeDef {
+    pub lifetime: Lifetime,
+    pub bounds: Vec<Lifetime>
+}
+
 /// A "Path" is essentially Rust's notion of a name; for instance:
 /// std::cmp::PartialEq  .  It's represented as a sequence of identifiers,
 /// along with a bunch of supporting information.
@@ -231,7 +237,7 @@ pub struct TyParam {
 /// of a function, enum, trait, etc.
 #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
 pub struct Generics {
-    pub lifetimes: Vec<Lifetime>,
+    pub lifetimes: Vec<LifetimeDef>,
     pub ty_params: OwnedSlice<TyParam>,
 }
 
@@ -861,7 +867,7 @@ impl fmt::Show for Onceness {
 /// Represents the type of a closure
 #[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
 pub struct ClosureTy {
-    pub lifetimes: Vec<Lifetime>,
+    pub lifetimes: Vec<LifetimeDef>,
     pub fn_style: FnStyle,
     pub onceness: Onceness,
     pub decl: P<FnDecl>,
@@ -876,7 +882,7 @@ pub struct ClosureTy {
 pub struct BareFnTy {
     pub fn_style: FnStyle,
     pub abi: Abi,
-    pub lifetimes: Vec<Lifetime>,
+    pub lifetimes: Vec<LifetimeDef>,
     pub decl: P<FnDecl>
 }
 
diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs
index e58187fe30d..cf2b5bc4063 100644
--- a/src/libsyntax/ast_util.rs
+++ b/src/libsyntax/ast_util.rs
@@ -369,7 +369,7 @@ impl<'a, O: IdVisitingOperation> IdVisitor<'a, O> {
             self.operation.visit_id(type_parameter.id)
         }
         for lifetime in generics.lifetimes.iter() {
-            self.operation.visit_id(lifetime.id)
+            self.operation.visit_id(lifetime.lifetime.id)
         }
     }
 }
diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs
index d500600e25d..0e687c02c1d 100644
--- a/src/libsyntax/ext/build.rs
+++ b/src/libsyntax/ext/build.rs
@@ -73,6 +73,11 @@ pub trait AstBuilder {
     fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
     fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
     fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
+    fn lifetime_def(&self,
+                    span: Span,
+                    name: ast::Name,
+                    bounds: Vec<ast::Lifetime>)
+                    -> ast::LifetimeDef;
 
     // statements
     fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt>;
@@ -456,6 +461,17 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
         ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
     }
 
+    fn lifetime_def(&self,
+                    span: Span,
+                    name: ast::Name,
+                    bounds: Vec<ast::Lifetime>)
+                    -> ast::LifetimeDef {
+        ast::LifetimeDef {
+            lifetime: self.lifetime(span, name),
+            bounds: bounds
+        }
+    }
+
     fn stmt_expr(&self, expr: Gc<ast::Expr>) -> Gc<ast::Stmt> {
         box(GC) respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID))
     }
diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs
index 9225e4414c4..5842ca4a0d5 100644
--- a/src/libsyntax/ext/deriving/generic/mod.rs
+++ b/src/libsyntax/ext/deriving/generic/mod.rs
@@ -395,7 +395,7 @@ impl<'a> TraitDef<'a> {
         let mut ty_params = ty_params.into_vec();
 
         // Copy the lifetimes
-        lifetimes.extend(generics.lifetimes.iter().map(|l| *l));
+        lifetimes.extend(generics.lifetimes.iter().map(|l| (*l).clone()));
 
         // Create the type parameters.
         ty_params.extend(generics.ty_params.iter().map(|ty_param| {
@@ -429,7 +429,11 @@ impl<'a> TraitDef<'a> {
             cx.ty_ident(self.span, ty_param.ident)
         });
 
-        let self_lifetimes = generics.lifetimes.clone();
+        let self_lifetimes: Vec<ast::Lifetime> =
+            generics.lifetimes
+            .iter()
+            .map(|ld| ld.lifetime)
+            .collect();
 
         // Create the type of `self`.
         let self_type = cx.ty_path(
diff --git a/src/libsyntax/ext/deriving/generic/ty.rs b/src/libsyntax/ext/deriving/generic/ty.rs
index 5d8b5660698..2130b6f4e9d 100644
--- a/src/libsyntax/ext/deriving/generic/ty.rs
+++ b/src/libsyntax/ext/deriving/generic/ty.rs
@@ -174,7 +174,9 @@ impl<'a> Ty<'a> {
                 let self_params = self_generics.ty_params.map(|ty_param| {
                     cx.ty_ident(span, ty_param.ident)
                 });
-                let lifetimes = self_generics.lifetimes.clone();
+                let lifetimes = self_generics.lifetimes.iter()
+                                                       .map(|d| d.lifetime)
+                                                       .collect();
 
                 cx.path_all(span, false, vec!(self_ty), lifetimes,
                             self_params.into_vec())
@@ -200,7 +202,7 @@ fn mk_ty_param(cx: &ExtCtxt, span: Span, name: &str,
     cx.typaram(span, cx.ident_of(name), bounds, unbound, None)
 }
 
-fn mk_generics(lifetimes: Vec<ast::Lifetime>, ty_params: Vec<ast::TyParam> ) -> Generics {
+fn mk_generics(lifetimes: Vec<ast::LifetimeDef>, ty_params: Vec<ast::TyParam> ) -> Generics {
     Generics {
         lifetimes: lifetimes,
         ty_params: OwnedSlice::from_vec(ty_params)
@@ -210,7 +212,7 @@ fn mk_generics(lifetimes: Vec<ast::Lifetime>, ty_params: Vec<ast::TyParam> ) ->
 /// Lifetimes and bounds on type parameters
 #[deriving(Clone)]
 pub struct LifetimeBounds<'a> {
-    pub lifetimes: Vec<&'a str>,
+    pub lifetimes: Vec<(&'a str, Vec<&'a str>)>,
     pub bounds: Vec<(&'a str, Option<ast::TyParamBound>, Vec<Path<'a>>)>,
 }
 
@@ -226,8 +228,11 @@ impl<'a> LifetimeBounds<'a> {
                        self_ty: Ident,
                        self_generics: &Generics)
                        -> Generics {
-        let lifetimes = self.lifetimes.iter().map(|lt| {
-            cx.lifetime(span, cx.ident_of(*lt).name)
+        let lifetimes = self.lifetimes.iter().map(|&(ref lt, ref bounds)| {
+            let bounds =
+                bounds.iter().map(
+                    |b| cx.lifetime(span, cx.ident_of(*b).name)).collect();
+            cx.lifetime_def(span, cx.ident_of(*lt).name, bounds)
         }).collect();
         let ty_params = self.bounds.iter().map(|t| {
             match t {
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index 2f82702ece4..80325c64349 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -163,6 +163,10 @@ pub trait Folder {
         noop_fold_lifetime(l, self)
     }
 
+    fn fold_lifetime_def(&mut self, l: &LifetimeDef) -> LifetimeDef {
+        noop_fold_lifetime_def(l, self)
+    }
+
     fn fold_attribute(&mut self, at: Attribute) -> Attribute {
         noop_fold_attribute(at, self)
     }
@@ -187,6 +191,10 @@ pub trait Folder {
         noop_fold_lifetimes(lts, self)
     }
 
+    fn fold_lifetime_defs(&mut self, lts: &[LifetimeDef]) -> Vec<LifetimeDef> {
+        noop_fold_lifetime_defs(lts, self)
+    }
+
     fn fold_ty_param(&mut self, tp: &TyParam) -> TyParam {
         noop_fold_ty_param(tp, self)
     }
@@ -337,7 +345,7 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
                 onceness: f.onceness,
                 bounds: fld.fold_opt_bounds(&f.bounds),
                 decl: fld.fold_fn_decl(&*f.decl),
-                lifetimes: f.lifetimes.iter().map(|l| fld.fold_lifetime(l)).collect(),
+                lifetimes: fld.fold_lifetime_defs(f.lifetimes.as_slice()),
             }, fld.fold_opt_lifetime(region))
         }
         TyProc(ref f) => {
@@ -346,12 +354,12 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
                 onceness: f.onceness,
                 bounds: fld.fold_opt_bounds(&f.bounds),
                 decl: fld.fold_fn_decl(&*f.decl),
-                lifetimes: f.lifetimes.iter().map(|l| fld.fold_lifetime(l)).collect(),
+                lifetimes: fld.fold_lifetime_defs(f.lifetimes.as_slice()),
             })
         }
         TyBareFn(ref f) => {
             TyBareFn(box(GC) BareFnTy {
-                lifetimes: f.lifetimes.iter().map(|l| fld.fold_lifetime(l)).collect(),
+                lifetimes: fld.fold_lifetime_defs(f.lifetimes.as_slice()),
                 fn_style: f.fn_style,
                 abi: f.abi,
                 decl: fld.fold_fn_decl(&*f.decl)
@@ -665,10 +673,23 @@ pub fn noop_fold_lifetime<T: Folder>(l: &Lifetime, fld: &mut T) -> Lifetime {
     }
 }
 
+pub fn noop_fold_lifetime_def<T: Folder>(l: &LifetimeDef, fld: &mut T)
+                                         -> LifetimeDef
+{
+    LifetimeDef {
+        lifetime: fld.fold_lifetime(&l.lifetime),
+        bounds: fld.fold_lifetimes(l.bounds.as_slice()),
+    }
+}
+
 pub fn noop_fold_lifetimes<T: Folder>(lts: &[Lifetime], fld: &mut T) -> Vec<Lifetime> {
     lts.iter().map(|l| fld.fold_lifetime(l)).collect()
 }
 
+pub fn noop_fold_lifetime_defs<T: Folder>(lts: &[LifetimeDef], fld: &mut T) -> Vec<LifetimeDef> {
+    lts.iter().map(|l| fld.fold_lifetime_def(l)).collect()
+}
+
 pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: &Option<Lifetime>, fld: &mut T)
                                       -> Option<Lifetime> {
     o_lt.as_ref().map(|lt| fld.fold_lifetime(lt))
@@ -676,7 +697,7 @@ pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: &Option<Lifetime>, fld: &mut T)
 
 pub fn noop_fold_generics<T: Folder>(generics: &Generics, fld: &mut T) -> Generics {
     Generics {ty_params: fld.fold_ty_params(generics.ty_params.as_slice()),
-              lifetimes: fld.fold_lifetimes(generics.lifetimes.as_slice())}
+              lifetimes: fld.fold_lifetime_defs(generics.lifetimes.as_slice())}
 }
 
 pub fn noop_fold_struct_def<T: Folder>(struct_def: Gc<StructDef>,
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index c9fba355c4d..7ea000d3aac 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -1053,10 +1053,10 @@ impl<'a> Parser<'a> {
 
         */
 
-        let lifetimes = if self.eat(&token::LT) {
-            let lifetimes = self.parse_lifetimes();
+        let lifetime_defs = if self.eat(&token::LT) {
+            let lifetime_defs = self.parse_lifetime_defs();
             self.expect_gt();
-            lifetimes
+            lifetime_defs
         } else {
             Vec::new()
         };
@@ -1082,7 +1082,7 @@ impl<'a> Parser<'a> {
             onceness: Once,
             bounds: bounds,
             decl: decl,
-            lifetimes: lifetimes,
+            lifetimes: lifetime_defs,
         })
     }
 
@@ -1096,7 +1096,7 @@ impl<'a> Parser<'a> {
           |        |      |    |      |      Return type
           |        |      |    |  Closure bounds
           |        |      |  Argument types
-          |        |    Lifetimes
+          |        |    Lifetime defs
           |     Once-ness (a.k.a., affine)
         Function Style
 
@@ -1105,11 +1105,11 @@ impl<'a> Parser<'a> {
         let fn_style = self.parse_unsafety();
         let onceness = if self.eat_keyword(keywords::Once) {Once} else {Many};
 
-        let lifetimes = if self.eat(&token::LT) {
-            let lifetimes = self.parse_lifetimes();
+        let lifetime_defs = if self.eat(&token::LT) {
+            let lifetime_defs = self.parse_lifetime_defs();
             self.expect_gt();
 
-            lifetimes
+            lifetime_defs
         } else {
             Vec::new()
         };
@@ -1164,7 +1164,7 @@ impl<'a> Parser<'a> {
                 onceness: onceness,
                 bounds: bounds,
                 decl: decl,
-                lifetimes: lifetimes,
+                lifetimes: lifetime_defs,
             }, region)
         }
     }
@@ -1179,7 +1179,7 @@ impl<'a> Parser<'a> {
 
     /// Parse a function type (following the 'fn')
     pub fn parse_ty_fn_decl(&mut self, allow_variadic: bool)
-                            -> (P<FnDecl>, Vec<ast::Lifetime>) {
+                            -> (P<FnDecl>, Vec<ast::LifetimeDef>) {
         /*
 
         (fn) <'lt> (S) -> T
@@ -1187,13 +1187,13 @@ impl<'a> Parser<'a> {
                |    |     |
                |    |   Return type
                |  Argument types
-           Lifetimes
+           Lifetime_defs
 
         */
-        let lifetimes = if self.eat(&token::LT) {
-            let lifetimes = self.parse_lifetimes();
+        let lifetime_defs = if self.eat(&token::LT) {
+            let lifetime_defs = self.parse_lifetime_defs();
             self.expect_gt();
-            lifetimes
+            lifetime_defs
         } else {
             Vec::new()
         };
@@ -1206,7 +1206,7 @@ impl<'a> Parser<'a> {
             cf: ret_style,
             variadic: variadic
         });
-        (decl, lifetimes)
+        (decl, lifetime_defs)
     }
 
     /// Parse the methods in a trait declaration
@@ -1770,31 +1770,34 @@ impl<'a> Parser<'a> {
         }
     }
 
-    // matches lifetimes = ( lifetime ) | ( lifetime , lifetimes )
-    // actually, it matches the empty one too, but putting that in there
-    // messes up the grammar....
-    pub fn parse_lifetimes(&mut self) -> Vec<ast::Lifetime> {
+    pub fn parse_lifetime_defs(&mut self) -> Vec<ast::LifetimeDef> {
         /*!
-         *
-         * Parses zero or more comma separated lifetimes.
-         * Expects each lifetime to be followed by either
-         * a comma or `>`.  Used when parsing type parameter
-         * lists, where we expect something like `<'a, 'b, T>`.
+         * Parses `lifetime_defs = [ lifetime_defs { ',' lifetime_defs } ]`
+         * where `lifetime_def  = lifetime [':' lifetimes]`
          */
 
         let mut res = Vec::new();
         loop {
             match self.token {
                 token::LIFETIME(_) => {
-                    res.push(self.parse_lifetime());
+                    let lifetime = self.parse_lifetime();
+                    let bounds =
+                        if self.eat(&token::COLON) {
+                            self.parse_lifetimes(token::BINOP(token::PLUS))
+                        } else {
+                            Vec::new()
+                        };
+                    res.push(ast::LifetimeDef { lifetime: lifetime,
+                                                bounds: bounds });
                 }
+
                 _ => {
                     return res;
                 }
             }
 
             match self.token {
-                token::COMMA => { self.bump();}
+                token::COMMA => { self.bump(); }
                 token::GT => { return res; }
                 token::BINOP(token::SHR) => { return res; }
                 _ => {
@@ -1807,6 +1810,36 @@ impl<'a> Parser<'a> {
         }
     }
 
+    // matches lifetimes = ( lifetime ) | ( lifetime , lifetimes )
+    // actually, it matches the empty one too, but putting that in there
+    // messes up the grammar....
+    pub fn parse_lifetimes(&mut self, sep: token::Token) -> Vec<ast::Lifetime> {
+        /*!
+         * Parses zero or more comma separated lifetimes.
+         * Expects each lifetime to be followed by either
+         * a comma or `>`.  Used when parsing type parameter
+         * lists, where we expect something like `<'a, 'b, T>`.
+         */
+
+        let mut res = Vec::new();
+        loop {
+            match self.token {
+                token::LIFETIME(_) => {
+                    res.push(self.parse_lifetime());
+                }
+                _ => {
+                    return res;
+                }
+            }
+
+            if self.token != sep {
+                return res;
+            }
+
+            self.bump();
+        }
+    }
+
     pub fn token_is_mutability(tok: &token::Token) -> bool {
         token::is_keyword(keywords::Mut, tok) ||
         token::is_keyword(keywords::Const, tok)
@@ -3664,7 +3697,7 @@ impl<'a> Parser<'a> {
     /// where   typaramseq = ( typaram ) | ( typaram , typaramseq )
     pub fn parse_generics(&mut self) -> ast::Generics {
         if self.eat(&token::LT) {
-            let lifetimes = self.parse_lifetimes();
+            let lifetime_defs = self.parse_lifetime_defs();
             let mut seen_default = false;
             let ty_params = self.parse_seq_to_gt(Some(token::COMMA), |p| {
                 p.forbid_lifetime();
@@ -3678,14 +3711,14 @@ impl<'a> Parser<'a> {
                 }
                 ty_param
             });
-            ast::Generics { lifetimes: lifetimes, ty_params: ty_params }
+            ast::Generics { lifetimes: lifetime_defs, ty_params: ty_params }
         } else {
             ast_util::empty_generics()
         }
     }
 
     fn parse_generic_values_after_lt(&mut self) -> (Vec<ast::Lifetime>, Vec<P<Ty>> ) {
-        let lifetimes = self.parse_lifetimes();
+        let lifetimes = self.parse_lifetimes(token::COMMA);
         let result = self.parse_seq_to_gt(
             Some(token::COMMA),
             |p| {
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index ab0269f807a..d60e4cb3b54 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -2082,10 +2082,26 @@ impl<'a> State<'a> {
     }
 
     pub fn print_lifetime(&mut self,
-                          lifetime: &ast::Lifetime) -> IoResult<()> {
+                          lifetime: &ast::Lifetime)
+                          -> IoResult<()>
+    {
         self.print_name(lifetime.name)
     }
 
+    pub fn print_lifetime_def(&mut self,
+                              lifetime: &ast::LifetimeDef)
+                              -> IoResult<()>
+    {
+        try!(self.print_lifetime(&lifetime.lifetime));
+        let mut sep = ":";
+        for v in lifetime.bounds.iter() {
+            try!(word(&mut self.s, sep));
+            try!(self.print_lifetime(v));
+            sep = "+";
+        }
+        Ok(())
+    }
+
     pub fn print_generics(&mut self,
                           generics: &ast::Generics) -> IoResult<()> {
         let total = generics.lifetimes.len() + generics.ty_params.len();
@@ -2102,7 +2118,7 @@ impl<'a> State<'a> {
                 |s, &idx| {
                     if idx < generics.lifetimes.len() {
                         let lifetime = generics.lifetimes.get(idx);
-                        s.print_lifetime(lifetime)
+                        s.print_lifetime_def(lifetime)
                     } else {
                         let idx = idx - generics.lifetimes.len();
                         let param = generics.ty_params.get(idx);
diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs
index 733c4bfc2b0..647e81db1f1 100644
--- a/src/libsyntax/visit.rs
+++ b/src/libsyntax/visit.rs
@@ -122,7 +122,7 @@ pub trait Visitor<E: Clone> {
     fn visit_lifetime_ref(&mut self, _lifetime: &Lifetime, _e: E) {
         /*! Visits a reference to a lifetime */
     }
-    fn visit_lifetime_decl(&mut self, _lifetime: &Lifetime, _e: E) {
+    fn visit_lifetime_decl(&mut self, _lifetime: &LifetimeDef, _e: E) {
         /*! Visits a declaration of a lifetime */
     }
     fn visit_explicit_self(&mut self, es: &ExplicitSelf, e: E) {
@@ -424,7 +424,7 @@ pub fn walk_ty<E: Clone, V: Visitor<E>>(visitor: &mut V, typ: &Ty, env: E) {
 }
 
 fn walk_lifetime_decls<E: Clone, V: Visitor<E>>(visitor: &mut V,
-                                                lifetimes: &Vec<Lifetime>,
+                                                lifetimes: &Vec<LifetimeDef>,
                                                 env: E) {
     for l in lifetimes.iter() {
         visitor.visit_lifetime_decl(l, env.clone());