Rollup merge of #35591 - GuillaumeGomez:generics_span, r=jntrmr

Add Span field for Generics structs
This commit is contained in:
Jeffrey Seyfried 2016-08-28 10:30:01 +00:00
commit b833e8d0a0
14 changed files with 37 additions and 52 deletions

View File

@ -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),
}
}

View File

@ -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,
}
}

View File

@ -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

View File

@ -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,

View File

@ -1297,6 +1297,7 @@ impl<'a, 'gcx, 'tcx> Rebuilder<'a, 'gcx, 'tcx> {
lifetimes: lifetimes.into(),
ty_params: ty_params,
where_clause: where_clause,
span: generics.span,
}
}

View File

@ -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;

View File

@ -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,
}
}
}

View File

@ -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),
}
}

View File

@ -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 {

View File

@ -725,8 +725,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)))
}
}
}
@ -4293,6 +4293,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()?;
@ -4315,7 +4316,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())

View File

@ -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,

View File

@ -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.

View File

@ -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)
}
}

View File

@ -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() {