// except according to those terms.
use syntax::ast;
-use syntax::codemap::Span;
+use syntax::source_map::Span;
use macros::MacroArg;
use utils::{mk_sp, outer_attributes};
+use std::cmp::max;
+
/// Spanned returns a span including attributes, if available.
pub trait Spanned {
fn span(&self) -> Span;
}
macro_rules! span_with_attrs_lo_hi {
- ($this: ident, $lo: expr, $hi: expr) => {{
+ ($this:ident, $lo:expr, $hi:expr) => {{
let attrs = outer_attributes(&$this.attrs);
if attrs.is_empty() {
mk_sp($lo, $hi)
}
macro_rules! span_with_attrs {
- ($this: ident) => {
+ ($this:ident) => {
span_with_attrs_lo_hi!($this, $this.span.lo(), $this.span.hi())
};
}
macro_rules! implement_spanned {
- ($this: ty) => {
+ ($this:ty) => {
impl Spanned for $this {
fn span(&self) -> Span {
span_with_attrs!(self)
impl Spanned for ast::Arm {
fn span(&self) -> Span {
- let lo = if let Some(sp) = self.beginning_vert {
- sp.lo()
- } else {
+ let lo = if self.attrs.is_empty() {
self.pats[0].span.lo()
+ } else {
+ self.attrs[0].span.lo()
};
span_with_attrs_lo_hi!(self, lo, self.body.span.hi())
}
impl Spanned for ast::GenericParam {
fn span(&self) -> Span {
- match *self {
- ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.span(),
- ast::GenericParam::Type(ref ty) => ty.span(),
- }
+ let lo = if self.attrs.is_empty() {
+ self.ident.span.lo()
+ } else {
+ self.attrs[0].span.lo()
+ };
+ let hi = if self.bounds.is_empty() {
+ self.ident.span.hi()
+ } else {
+ self.bounds.last().unwrap().span().hi()
+ };
+ let ty_hi = if let ast::GenericParamKind::Type {
+ default: Some(ref ty),
+ } = self.kind
+ {
+ ty.span().hi()
+ } else {
+ hi
+ };
+ mk_sp(lo, max(hi, ty_hi))
}
}
}
}
-impl Spanned for ast::TyParam {
+impl Spanned for ast::GenericArg {
fn span(&self) -> Span {
- // Note that ty.span is the span for ty.ident, not the whole item.
- let lo = if self.attrs.is_empty() {
- self.span.lo()
- } else {
- self.attrs[0].span.lo()
- };
- if let Some(ref def) = self.default {
- return mk_sp(lo, def.span.hi());
- }
- if self.bounds.is_empty() {
- return mk_sp(lo, self.span.hi());
+ match *self {
+ ast::GenericArg::Lifetime(ref lt) => lt.ident.span,
+ ast::GenericArg::Type(ref ty) => ty.span(),
}
- let hi = self.bounds[self.bounds.len() - 1].span().hi();
- mk_sp(lo, hi)
}
}
-impl Spanned for ast::TyParamBound {
+impl Spanned for ast::GenericBound {
fn span(&self) -> Span {
match *self {
- ast::TyParamBound::TraitTyParamBound(ref ptr, _) => ptr.span,
- ast::TyParamBound::RegionTyParamBound(ref l) => l.span,
+ ast::GenericBound::Trait(ref ptr, _) => ptr.span,
+ ast::GenericBound::Outlives(ref l) => l.ident.span,
}
}
}
-impl Spanned for ast::LifetimeDef {
- fn span(&self) -> Span {
- let hi = if self.bounds.is_empty() {
- self.lifetime.span.hi()
- } else {
- self.bounds[self.bounds.len() - 1].span.hi()
- };
- mk_sp(self.lifetime.span.lo(), hi)
- }
-}
-
impl Spanned for MacroArg {
fn span(&self) -> Span {
match *self {
MacroArg::Expr(ref expr) => expr.span(),
MacroArg::Ty(ref ty) => ty.span(),
MacroArg::Pat(ref pat) => pat.span(),
+ MacroArg::Item(ref item) => item.span(),
}
}
}