5 source_map::{self, Span},
8 use crate::macros::MacroArg;
9 use crate::utils::{mk_sp, outer_attributes};
11 /// Spanned returns a span including attributes, if available.
12 pub(crate) trait Spanned {
13 fn span(&self) -> Span;
16 impl<T: Spanned> Spanned for ptr::P<T> {
17 fn span(&self) -> Span {
22 impl<T> Spanned for source_map::Spanned<T> {
23 fn span(&self) -> Span {
28 macro_rules! span_with_attrs_lo_hi {
29 ($this:ident, $lo:expr, $hi:expr) => {{
30 let attrs = outer_attributes(&$this.attrs);
34 mk_sp(attrs[0].span.lo(), $hi)
39 macro_rules! span_with_attrs {
41 span_with_attrs_lo_hi!($this, $this.span.lo(), $this.span.hi())
45 macro_rules! implement_spanned {
47 impl Spanned for $this {
48 fn span(&self) -> Span {
49 span_with_attrs!(self)
55 // Implement `Spanned` for structs with `attrs` field.
56 implement_spanned!(ast::Expr);
57 implement_spanned!(ast::Field);
58 implement_spanned!(ast::ForeignItem);
59 implement_spanned!(ast::Item);
60 implement_spanned!(ast::Local);
61 implement_spanned!(ast::TraitItem);
62 implement_spanned!(ast::ImplItem);
64 impl Spanned for ast::Stmt {
65 fn span(&self) -> Span {
67 ast::StmtKind::Local(ref local) => mk_sp(local.span().lo(), self.span.hi()),
68 ast::StmtKind::Item(ref item) => mk_sp(item.span().lo(), self.span.hi()),
69 ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
70 mk_sp(expr.span().lo(), self.span.hi())
72 ast::StmtKind::Mac(ref mac) => {
73 let (_, _, ref attrs) = **mac;
77 mk_sp(attrs[0].span.lo(), self.span.hi())
84 impl Spanned for ast::Pat {
85 fn span(&self) -> Span {
90 impl Spanned for ast::Ty {
91 fn span(&self) -> Span {
96 impl Spanned for ast::Arm {
97 fn span(&self) -> Span {
98 let lo = if self.attrs.is_empty() {
99 self.pats[0].span.lo()
101 self.attrs[0].span.lo()
103 span_with_attrs_lo_hi!(self, lo, self.body.span.hi())
107 impl Spanned for ast::Arg {
108 fn span(&self) -> Span {
109 if crate::items::is_named_arg(self) {
110 mk_sp(self.pat.span.lo(), self.ty.span.hi())
117 impl Spanned for ast::GenericParam {
118 fn span(&self) -> Span {
119 let lo = if self.attrs.is_empty() {
122 self.attrs[0].span.lo()
124 let hi = if self.bounds.is_empty() {
127 self.bounds.last().unwrap().span().hi()
129 let ty_hi = if let ast::GenericParamKind::Type {
130 default: Some(ref ty),
137 mk_sp(lo, max(hi, ty_hi))
141 impl Spanned for ast::StructField {
142 fn span(&self) -> Span {
143 span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi())
147 impl Spanned for ast::WherePredicate {
148 fn span(&self) -> Span {
150 ast::WherePredicate::BoundPredicate(ref p) => p.span,
151 ast::WherePredicate::RegionPredicate(ref p) => p.span,
152 ast::WherePredicate::EqPredicate(ref p) => p.span,
157 impl Spanned for ast::FunctionRetTy {
158 fn span(&self) -> Span {
160 ast::FunctionRetTy::Default(span) => span,
161 ast::FunctionRetTy::Ty(ref ty) => ty.span,
166 impl Spanned for ast::GenericArg {
167 fn span(&self) -> Span {
169 ast::GenericArg::Lifetime(ref lt) => lt.ident.span,
170 ast::GenericArg::Type(ref ty) => ty.span(),
171 ast::GenericArg::Const(ref _const) => _const.value.span(),
176 impl Spanned for ast::GenericBound {
177 fn span(&self) -> Span {
179 ast::GenericBound::Trait(ref ptr, _) => ptr.span,
180 ast::GenericBound::Outlives(ref l) => l.ident.span,
185 impl Spanned for MacroArg {
186 fn span(&self) -> Span {
188 MacroArg::Expr(ref expr) => expr.span(),
189 MacroArg::Ty(ref ty) => ty.span(),
190 MacroArg::Pat(ref pat) => pat.span(),
191 MacroArg::Item(ref item) => item.span(),
192 MacroArg::Keyword(_, span) => span,
197 impl Spanned for ast::NestedMetaItem {
198 fn span(&self) -> Span {