3 use rustc_ast::{ast, ptr};
4 use rustc_span::{source_map, Span};
6 use crate::macros::MacroArg;
7 use crate::utils::{mk_sp, outer_attributes};
9 /// Spanned returns a span including attributes, if available.
10 pub(crate) trait Spanned {
11 fn span(&self) -> Span;
14 impl<T: Spanned> Spanned for ptr::P<T> {
15 fn span(&self) -> Span {
20 impl<T> Spanned for source_map::Spanned<T> {
21 fn span(&self) -> Span {
26 macro_rules! span_with_attrs_lo_hi {
27 ($this:ident, $lo:expr, $hi:expr) => {{
28 let attrs = outer_attributes(&$this.attrs);
32 mk_sp(attrs[0].span.lo(), $hi)
37 macro_rules! span_with_attrs {
39 span_with_attrs_lo_hi!($this, $this.span.lo(), $this.span.hi())
43 macro_rules! implement_spanned {
45 impl Spanned for $this {
46 fn span(&self) -> Span {
47 span_with_attrs!(self)
53 // Implement `Spanned` for structs with `attrs` field.
54 implement_spanned!(ast::AssocItem);
55 implement_spanned!(ast::Expr);
56 implement_spanned!(ast::ExprField);
57 implement_spanned!(ast::ForeignItem);
58 implement_spanned!(ast::Item);
59 implement_spanned!(ast::Local);
61 impl Spanned for ast::Stmt {
62 fn span(&self) -> Span {
64 ast::StmtKind::Local(ref local) => mk_sp(local.span().lo(), self.span.hi()),
65 ast::StmtKind::Item(ref item) => mk_sp(item.span().lo(), self.span.hi()),
66 ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
67 mk_sp(expr.span().lo(), self.span.hi())
69 ast::StmtKind::MacCall(ref mac_stmt) => {
70 if mac_stmt.attrs.is_empty() {
73 mk_sp(mac_stmt.attrs[0].span.lo(), self.span.hi())
76 ast::StmtKind::Empty => self.span,
81 impl Spanned for ast::Pat {
82 fn span(&self) -> Span {
87 impl Spanned for ast::Ty {
88 fn span(&self) -> Span {
93 impl Spanned for ast::Arm {
94 fn span(&self) -> Span {
95 let lo = if self.attrs.is_empty() {
98 self.attrs[0].span.lo()
100 span_with_attrs_lo_hi!(self, lo, self.body.span.hi())
104 impl Spanned for ast::Param {
105 fn span(&self) -> Span {
106 if crate::items::is_named_param(self) {
107 mk_sp(crate::items::span_lo_for_param(self), self.ty.span.hi())
114 impl Spanned for ast::GenericParam {
115 fn span(&self) -> Span {
116 let lo = match self.kind {
117 _ if !self.attrs.is_empty() => self.attrs[0].span.lo(),
118 ast::GenericParamKind::Const { kw_span, .. } => kw_span.lo(),
119 _ => self.ident.span.lo(),
121 let hi = if self.bounds.is_empty() {
124 self.bounds.last().unwrap().span().hi()
126 let ty_hi = if let ast::GenericParamKind::Type {
127 default: Some(ref ty),
129 | ast::GenericParamKind::Const { ref ty, .. } = self.kind
135 mk_sp(lo, max(hi, ty_hi))
139 impl Spanned for ast::FieldDef {
140 fn span(&self) -> Span {
141 span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi())
145 impl Spanned for ast::WherePredicate {
146 fn span(&self) -> Span {
148 ast::WherePredicate::BoundPredicate(ref p) => p.span,
149 ast::WherePredicate::RegionPredicate(ref p) => p.span,
150 ast::WherePredicate::EqPredicate(ref p) => p.span,
155 impl Spanned for ast::FnRetTy {
156 fn span(&self) -> Span {
158 ast::FnRetTy::Default(span) => span,
159 ast::FnRetTy::Ty(ref ty) => ty.span,
164 impl Spanned for ast::GenericArg {
165 fn span(&self) -> Span {
167 ast::GenericArg::Lifetime(ref lt) => lt.ident.span,
168 ast::GenericArg::Type(ref ty) => ty.span(),
169 ast::GenericArg::Const(ref _const) => _const.value.span(),
174 impl Spanned for ast::GenericBound {
175 fn span(&self) -> Span {
177 ast::GenericBound::Trait(ref ptr, _) => ptr.span,
178 ast::GenericBound::Outlives(ref l) => l.ident.span,
183 impl Spanned for MacroArg {
184 fn span(&self) -> Span {
186 MacroArg::Expr(ref expr) => expr.span(),
187 MacroArg::Ty(ref ty) => ty.span(),
188 MacroArg::Pat(ref pat) => pat.span(),
189 MacroArg::Item(ref item) => item.span(),
190 MacroArg::Keyword(_, span) => span,
195 impl Spanned for ast::NestedMetaItem {
196 fn span(&self) -> Span {