From 59481823675a7392e8160b659b0f7fa119df60fd Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 10 Aug 2016 19:39:12 +0200 Subject: [PATCH] Add Span field for Generics structs --- src/librustc/hir/fold.rs | 3 +- src/librustc/hir/lowering.rs | 1 + src/librustc/hir/mod.rs | 36 ++--------------------- src/librustc/hir/print.rs | 2 ++ src/librustc/infer/error_reporting.rs | 1 + src/librustc_typeck/lib.rs | 11 ++++--- src/libsyntax/ast.rs | 6 ++-- src/libsyntax/fold.rs | 3 +- src/libsyntax/parse/mod.rs | 5 ++-- src/libsyntax/parse/parser.rs | 8 +++-- src/libsyntax/print/pprust.rs | 2 ++ src/libsyntax_ext/deriving/generic/mod.rs | 3 +- src/libsyntax_ext/deriving/generic/ty.rs | 6 ++-- src/test/compile-fail/E0132.rs | 2 +- 14 files changed, 37 insertions(+), 52 deletions(-) diff --git a/src/librustc/hir/fold.rs b/src/librustc/hir/fold.rs index 0edfd16bdfd1b..0b362bac8882b 100644 --- a/src/librustc/hir/fold.rs +++ b/src/librustc/hir/fold.rs @@ -577,13 +577,14 @@ pub fn noop_fold_opt_lifetime(o_lt: Option, fld: &mut T) -> o_lt.map(|lt| fld.fold_lifetime(lt)) } -pub fn noop_fold_generics(Generics { ty_params, lifetimes, where_clause }: Generics, +pub fn noop_fold_generics(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 c2b211238b2f1..cb219bbe18ae8 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 d41cdfabdf4c0..f351384c2b89e 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, pub ty_params: HirVec, 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 { - if !self.is_parameterized() { - None - } else { - let mut span: Option = 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 66c1bc7642c56..1cbead123d871 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 9a6375719c1bc..03ad12d2d99f8 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 8a8232535c775..b24eb8cba1cf3 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 f8a5cb0b04a8e..968956d3391a0 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>, - 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, 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 b257ab98987dc..c566aa5661be0 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -698,12 +698,13 @@ pub fn noop_fold_opt_lifetime(o_lt: Option, fld: &mut T) o_lt.map(|lt| fld.fold_lifetime(lt)) } -pub fn noop_fold_generics(Generics {ty_params, lifetimes, where_clause}: Generics, +pub fn noop_fold_generics(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 cd1fdcfe9d130..eb59a8e24d3fd 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 126e8816d0559..19f44924067db 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 a77c678248b56..22b0bb2c07ad0 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 cd49e7ec9d2c6..6773088670f58 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 356c54fcf3120..210878b7c9f0e 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, ty_params: Vec) -> Generics { +fn mk_generics(lifetimes: Vec, ty_params: Vec, span: Span) + -> Generics { Generics { lifetimes: lifetimes, ty_params: P::from_vec(ty_params), @@ -215,6 +216,7 @@ fn mk_generics(lifetimes: Vec, ty_params: Vec) - 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 1a33fb24ca1a1..91ff6b85a42ce 100644 --- a/src/test/compile-fail/E0132.rs +++ b/src/test/compile-fail/E0132.rs @@ -11,7 +11,7 @@ #![feature(start)] #[start] -fn f() {} //~ ERROR E0132 +fn f< T >() {} //~ ERROR E0132 //~| NOTE start function cannot have type parameters fn main() {