Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Span field for Generics structs #35591

Merged
merged 1 commit into from
Aug 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustc/hir/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
36 changes: 3 additions & 33 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -312,6 +313,7 @@ impl Generics {
id: DUMMY_NODE_ID,
predicates: HirVec::new(),
},
span: DUMMY_SP,
}
}

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/librustc/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -368,7 +369,8 @@ impl Default for Generics {
where_clause: WhereClause {
id: DUMMY_NODE_ID,
predicates: Vec::new(),
}
},
span: DUMMY_SP,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
}
Expand Down Expand Up @@ -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()?;
Expand All @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax_ext/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions src/libsyntax_ext/deriving/generic/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,16 @@ 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),
where_clause: ast::WhereClause {
id: ast::DUMMY_NODE_ID,
predicates: Vec::new(),
},
span: span,
}
}

Expand Down Expand Up @@ -257,7 +259,7 @@ impl<'a> LifetimeBounds<'a> {
}
})
.collect();
mk_generics(lifetimes, ty_params)
mk_generics(lifetimes, ty_params, span)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/E0132.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![feature(start)]

#[start]
fn f<T>() {} //~ ERROR E0132
fn f< T >() {} //~ ERROR E0132
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: #35264 (comment)

It definitely seems pretty minor imho

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is but it won't be a hack anymore and I prefer to avoid dark magic.

//~| NOTE start function cannot have type parameters

fn main() {
Expand Down