about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2016-08-10 19:39:12 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2016-08-18 18:23:36 +0200
commit59481823675a7392e8160b659b0f7fa119df60fd (patch)
tree01405802c217ec5d3d8aa6c2dd9cd46e95c6fdda /src
parent7ac11cad3fe85163dd8b0ca1f63af492509f9bfe (diff)
downloadrust-59481823675a7392e8160b659b0f7fa119df60fd.tar.gz
rust-59481823675a7392e8160b659b0f7fa119df60fd.zip
Add Span field for Generics structs
Diffstat (limited to 'src')
-rw-r--r--src/librustc/hir/fold.rs3
-rw-r--r--src/librustc/hir/lowering.rs1
-rw-r--r--src/librustc/hir/mod.rs36
-rw-r--r--src/librustc/hir/print.rs2
-rw-r--r--src/librustc/infer/error_reporting.rs1
-rw-r--r--src/librustc_typeck/lib.rs11
-rw-r--r--src/libsyntax/ast.rs6
-rw-r--r--src/libsyntax/fold.rs3
-rw-r--r--src/libsyntax/parse/mod.rs5
-rw-r--r--src/libsyntax/parse/parser.rs8
-rw-r--r--src/libsyntax/print/pprust.rs2
-rw-r--r--src/libsyntax_ext/deriving/generic/mod.rs3
-rw-r--r--src/libsyntax_ext/deriving/generic/ty.rs6
-rw-r--r--src/test/compile-fail/E0132.rs2
14 files changed, 37 insertions, 52 deletions
diff --git a/src/librustc/hir/fold.rs b/src/librustc/hir/fold.rs
index 0edfd16bdfd..0b362bac888 100644
--- a/src/librustc/hir/fold.rs
+++ b/src/librustc/hir/fold.rs
@@ -577,13 +577,14 @@ pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: Option<Lifetime>, fld: &mut T) ->
     o_lt.map(|lt| fld.fold_lifetime(lt))
 }
 
-pub fn noop_fold_generics<T: Folder>(Generics { ty_params, lifetimes, where_clause }: Generics,
+pub fn noop_fold_generics<T: Folder>(Generics {ty_params, lifetimes, where_clause, span}: Generics,
                                      fld: &mut T)
                                      -> Generics {
     Generics {
         ty_params: fld.fold_ty_params(ty_params),
         lifetimes: fld.fold_lifetime_defs(lifetimes),
         where_clause: fld.fold_where_clause(where_clause),
+        span: fld.new_span(span),
     }
 }
 
diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs
index c2b211238b2..cb219bbe18a 100644
--- a/src/librustc/hir/lowering.rs
+++ b/src/librustc/hir/lowering.rs
@@ -466,6 +466,7 @@ impl<'a> LoweringContext<'a> {
             ty_params: self.lower_ty_params(&g.ty_params),
             lifetimes: self.lower_lifetime_defs(&g.lifetimes),
             where_clause: self.lower_where_clause(&g.where_clause),
+            span: g.span,
         }
     }
 
diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs
index d41cdfabdf4..f351384c2b8 100644
--- a/src/librustc/hir/mod.rs
+++ b/src/librustc/hir/mod.rs
@@ -36,7 +36,7 @@ use hir::def::Def;
 use hir::def_id::DefId;
 use util::nodemap::{NodeMap, FnvHashSet};
 
-use syntax_pos::{BytePos, mk_sp, Span, ExpnId};
+use syntax_pos::{mk_sp, Span, ExpnId, DUMMY_SP};
 use syntax::codemap::{self, respan, Spanned};
 use syntax::abi::Abi;
 use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, AsmDialect};
@@ -301,6 +301,7 @@ pub struct Generics {
     pub lifetimes: HirVec<LifetimeDef>,
     pub ty_params: HirVec<TyParam>,
     pub where_clause: WhereClause,
+    pub span: Span,
 }
 
 impl Generics {
@@ -312,6 +313,7 @@ impl Generics {
                 id: DUMMY_NODE_ID,
                 predicates: HirVec::new(),
             },
+            span: DUMMY_SP,
         }
     }
 
@@ -326,38 +328,6 @@ impl Generics {
     pub fn is_parameterized(&self) -> bool {
         self.is_lt_parameterized() || self.is_type_parameterized()
     }
-
-    // Does return a span which includes lifetimes and type parameters,
-    // not where clause.
-    pub fn span(&self) -> Option<Span> {
-        if !self.is_parameterized() {
-            None
-        } else {
-            let mut span: Option<Span> = None;
-            for lifetime in self.lifetimes.iter() {
-                if let Some(ref mut span) = span {
-                    let life_span = lifetime.lifetime.span;
-                    span.hi = if span.hi > life_span.hi { span.hi } else { life_span.hi };
-                    span.lo = if span.lo < life_span.lo { span.lo } else { life_span.lo };
-                } else {
-                    span = Some(lifetime.lifetime.span.clone());
-                }
-            }
-            for ty_param in self.ty_params.iter() {
-                if let Some(ref mut span) = span {
-                    span.lo = if span.lo < ty_param.span.lo { span.lo } else { ty_param.span.lo };
-                    span.hi = if span.hi > ty_param.span.hi { span.hi } else { ty_param.span.hi };
-                } else {
-                    span = Some(ty_param.span.clone());
-                }
-            }
-            if let Some(ref mut span) = span {
-                span.lo = span.lo - BytePos(1);
-                span.hi = span.hi + BytePos(1);
-            }
-            span
-        }
-    }
 }
 
 /// A `where` clause in a definition
diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs
index 66c1bc7642c..1cbead123d8 100644
--- a/src/librustc/hir/print.rs
+++ b/src/librustc/hir/print.rs
@@ -523,6 +523,7 @@ impl<'a> State<'a> {
                         id: ast::DUMMY_NODE_ID,
                         predicates: hir::HirVec::new(),
                     },
+                    span: syntax_pos::DUMMY_SP,
                 };
                 self.print_ty_fn(f.abi, f.unsafety, &f.decl, None, &generics)?;
             }
@@ -2224,6 +2225,7 @@ impl<'a> State<'a> {
                 id: ast::DUMMY_NODE_ID,
                 predicates: hir::HirVec::new(),
             },
+            span: syntax_pos::DUMMY_SP,
         };
         self.print_fn(decl,
                       unsafety,
diff --git a/src/librustc/infer/error_reporting.rs b/src/librustc/infer/error_reporting.rs
index 9a6375719c1..03ad12d2d99 100644
--- a/src/librustc/infer/error_reporting.rs
+++ b/src/librustc/infer/error_reporting.rs
@@ -1293,6 +1293,7 @@ impl<'a, 'gcx, 'tcx> Rebuilder<'a, 'gcx, 'tcx> {
             lifetimes: lifetimes.into(),
             ty_params: ty_params,
             where_clause: where_clause,
+            span: generics.span,
         }
     }
 
diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs
index 8a8232535c7..b24eb8cba1c 100644
--- a/src/librustc_typeck/lib.rs
+++ b/src/librustc_typeck/lib.rs
@@ -216,10 +216,10 @@ fn check_main_fn_ty(ccx: &CrateCtxt,
                 Some(hir_map::NodeItem(it)) => {
                     match it.node {
                         hir::ItemFn(_, _, _, _, ref generics, _) => {
-                            if let Some(gen_span) = generics.span() {
-                                struct_span_err!(ccx.tcx.sess, gen_span, E0131,
+                            if generics.is_parameterized() {
+                                struct_span_err!(ccx.tcx.sess, generics.span, E0131,
                                          "main function is not allowed to have type parameters")
-                                    .span_label(gen_span,
+                                    .span_label(generics.span,
                                                 &format!("main cannot have type parameters"))
                                     .emit();
                                 return;
@@ -269,10 +269,9 @@ fn check_start_fn_ty(ccx: &CrateCtxt,
                     match it.node {
                         hir::ItemFn(_,_,_,_,ref ps,_)
                         if ps.is_parameterized() => {
-                            let sp = if let Some(sp) = ps.span() { sp } else { start_span };
-                            struct_span_err!(tcx.sess, sp, E0132,
+                            struct_span_err!(tcx.sess, ps.span, E0132,
                                 "start function is not allowed to have type parameters")
-                                .span_label(sp,
+                                .span_label(ps.span,
                                             &format!("start function cannot have type parameters"))
                                 .emit();
                             return;
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index f8a5cb0b04a..968956d3391 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -336,7 +336,7 @@ pub struct TyParam {
     pub id: NodeId,
     pub bounds: TyParamBounds,
     pub default: Option<P<Ty>>,
-    pub span: Span
+    pub span: Span,
 }
 
 /// Represents lifetimes and type parameters attached to a declaration
@@ -346,6 +346,7 @@ pub struct Generics {
     pub lifetimes: Vec<LifetimeDef>,
     pub ty_params: P<[TyParam]>,
     pub where_clause: WhereClause,
+    pub span: Span,
 }
 
 impl Generics {
@@ -368,7 +369,8 @@ impl Default for Generics {
             where_clause: WhereClause {
                 id: DUMMY_NODE_ID,
                 predicates: Vec::new(),
-            }
+            },
+            span: DUMMY_SP,
         }
     }
 }
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index b257ab98987..c566aa5661b 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -698,12 +698,13 @@ pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: Option<Lifetime>, fld: &mut T)
     o_lt.map(|lt| fld.fold_lifetime(lt))
 }
 
-pub fn noop_fold_generics<T: Folder>(Generics {ty_params, lifetimes, where_clause}: Generics,
+pub fn noop_fold_generics<T: Folder>(Generics {ty_params, lifetimes, where_clause, span}: Generics,
                                      fld: &mut T) -> Generics {
     Generics {
         ty_params: fld.fold_ty_params(ty_params),
         lifetimes: fld.fold_lifetime_defs(lifetimes),
         where_clause: fld.fold_where_clause(where_clause),
+        span: fld.new_span(span),
     }
 }
 
diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs
index cd1fdcfe9d1..eb59a8e24d3 100644
--- a/src/libsyntax/parse/mod.rs
+++ b/src/libsyntax/parse/mod.rs
@@ -674,7 +674,7 @@ pub fn integer_lit(s: &str,
 mod tests {
     use super::*;
     use std::rc::Rc;
-    use syntax_pos::{Span, BytePos, Pos, NO_EXPANSION};
+    use syntax_pos::{self, Span, BytePos, Pos, NO_EXPANSION};
     use codemap::Spanned;
     use ast::{self, PatKind};
     use abi::Abi;
@@ -945,7 +945,8 @@ mod tests {
                                         where_clause: ast::WhereClause {
                                             id: ast::DUMMY_NODE_ID,
                                             predicates: Vec::new(),
-                                        }
+                                        },
+                                        span: syntax_pos::DUMMY_SP,
                                     },
                                     P(ast::Block {
                                         stmts: vec!(ast::Stmt {
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 126e8816d05..19f44924067 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -716,8 +716,8 @@ impl<'a> Parser<'a> {
                 let gt_str = Parser::token_to_string(&token::Gt);
                 let this_token_str = self.this_token_to_string();
                 Err(self.fatal(&format!("expected `{}`, found `{}`",
-                                   gt_str,
-                                   this_token_str)))
+                                        gt_str,
+                                        this_token_str)))
             }
         }
     }
@@ -4251,6 +4251,7 @@ impl<'a> Parser<'a> {
     /// where   typaramseq = ( typaram ) | ( typaram , typaramseq )
     pub fn parse_generics(&mut self) -> PResult<'a, ast::Generics> {
         maybe_whole!(self, NtGenerics);
+        let span_lo = self.span.lo;
 
         if self.eat(&token::Lt) {
             let lifetime_defs = self.parse_lifetime_defs()?;
@@ -4273,7 +4274,8 @@ impl<'a> Parser<'a> {
                 where_clause: WhereClause {
                     id: ast::DUMMY_NODE_ID,
                     predicates: Vec::new(),
-                }
+                },
+                span: mk_sp(span_lo, self.last_span.hi),
             })
         } else {
             Ok(ast::Generics::default())
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index a77c678248b..22b0bb2c07a 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -1001,6 +1001,7 @@ impl<'a> State<'a> {
                         id: ast::DUMMY_NODE_ID,
                         predicates: Vec::new(),
                     },
+                    span: syntax_pos::DUMMY_SP,
                 };
                 try!(self.print_ty_fn(f.abi,
                                  f.unsafety,
@@ -2982,6 +2983,7 @@ impl<'a> State<'a> {
                 id: ast::DUMMY_NODE_ID,
                 predicates: Vec::new(),
             },
+            span: syntax_pos::DUMMY_SP,
         };
         try!(self.print_fn(decl,
                       unsafety,
diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs
index cd49e7ec9d2..6773088670f 100644
--- a/src/libsyntax_ext/deriving/generic/mod.rs
+++ b/src/libsyntax_ext/deriving/generic/mod.rs
@@ -488,7 +488,7 @@ impl<'a> TraitDef<'a> {
             }
         });
 
-        let Generics { mut lifetimes, ty_params, mut where_clause } = self.generics
+        let Generics { mut lifetimes, ty_params, mut where_clause, span } = self.generics
             .to_generics(cx, self.span, type_ident, generics);
         let mut ty_params = ty_params.into_vec();
 
@@ -590,6 +590,7 @@ impl<'a> TraitDef<'a> {
             lifetimes: lifetimes,
             ty_params: P::from_vec(ty_params),
             where_clause: where_clause,
+            span: span,
         };
 
         // Create the reference to the trait.
diff --git a/src/libsyntax_ext/deriving/generic/ty.rs b/src/libsyntax_ext/deriving/generic/ty.rs
index 356c54fcf31..210878b7c9f 100644
--- a/src/libsyntax_ext/deriving/generic/ty.rs
+++ b/src/libsyntax_ext/deriving/generic/ty.rs
@@ -207,7 +207,8 @@ fn mk_ty_param(cx: &ExtCtxt,
     cx.typaram(span, cx.ident_of(name), bounds, None)
 }
 
-fn mk_generics(lifetimes: Vec<ast::LifetimeDef>, ty_params: Vec<ast::TyParam>) -> Generics {
+fn mk_generics(lifetimes: Vec<ast::LifetimeDef>, ty_params: Vec<ast::TyParam>, span: Span)
+               -> Generics {
     Generics {
         lifetimes: lifetimes,
         ty_params: P::from_vec(ty_params),
@@ -215,6 +216,7 @@ fn mk_generics(lifetimes: Vec<ast::LifetimeDef>, ty_params: Vec<ast::TyParam>) -
             id: ast::DUMMY_NODE_ID,
             predicates: Vec::new(),
         },
+        span: span,
     }
 }
 
@@ -257,7 +259,7 @@ impl<'a> LifetimeBounds<'a> {
                 }
             })
             .collect();
-        mk_generics(lifetimes, ty_params)
+        mk_generics(lifetimes, ty_params, span)
     }
 }
 
diff --git a/src/test/compile-fail/E0132.rs b/src/test/compile-fail/E0132.rs
index 1a33fb24ca1..91ff6b85a42 100644
--- a/src/test/compile-fail/E0132.rs
+++ b/src/test/compile-fail/E0132.rs
@@ -11,7 +11,7 @@
 #![feature(start)]
 
 #[start]
-fn f<T>() {} //~ ERROR E0132
+fn f< T >() {} //~ ERROR E0132
              //~| NOTE start function cannot have type parameters
 
 fn main() {