rustfmt_nightly/
spanned.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::cmp::max;

use rustc_ast::{ast, ptr};
use rustc_span::{Span, source_map};

use crate::macros::MacroArg;
use crate::patterns::RangeOperand;
use crate::utils::{mk_sp, outer_attributes};

/// Spanned returns a span including attributes, if available.
pub(crate) trait Spanned {
    fn span(&self) -> Span;
}

impl<T: Spanned> Spanned for ptr::P<T> {
    fn span(&self) -> Span {
        (**self).span()
    }
}

impl<T> Spanned for source_map::Spanned<T> {
    fn span(&self) -> Span {
        self.span
    }
}

macro_rules! span_with_attrs_lo_hi {
    ($this:ident, $lo:expr, $hi:expr) => {{
        let attrs = outer_attributes(&$this.attrs);
        if attrs.is_empty() {
            mk_sp($lo, $hi)
        } else {
            mk_sp(attrs[0].span.lo(), $hi)
        }
    }};
}

macro_rules! span_with_attrs {
    ($this:ident) => {
        span_with_attrs_lo_hi!($this, $this.span.lo(), $this.span.hi())
    };
}

macro_rules! implement_spanned {
    ($this:ty) => {
        impl Spanned for $this {
            fn span(&self) -> Span {
                span_with_attrs!(self)
            }
        }
    };
}

// Implement `Spanned` for structs with `attrs` field.
implement_spanned!(ast::AssocItem);
implement_spanned!(ast::Expr);
implement_spanned!(ast::ExprField);
implement_spanned!(ast::ForeignItem);
implement_spanned!(ast::Item);
implement_spanned!(ast::Local);

impl Spanned for ast::Stmt {
    fn span(&self) -> Span {
        match self.kind {
            ast::StmtKind::Let(ref local) => mk_sp(local.span().lo(), self.span.hi()),
            ast::StmtKind::Item(ref item) => mk_sp(item.span().lo(), self.span.hi()),
            ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
                mk_sp(expr.span().lo(), self.span.hi())
            }
            ast::StmtKind::MacCall(ref mac_stmt) => {
                if mac_stmt.attrs.is_empty() {
                    self.span
                } else {
                    mk_sp(mac_stmt.attrs[0].span.lo(), self.span.hi())
                }
            }
            ast::StmtKind::Empty => self.span,
        }
    }
}

impl Spanned for ast::Pat {
    fn span(&self) -> Span {
        self.span
    }
}

impl Spanned for ast::Ty {
    fn span(&self) -> Span {
        self.span
    }
}

impl Spanned for ast::Arm {
    fn span(&self) -> Span {
        let lo = if self.attrs.is_empty() {
            self.pat.span.lo()
        } else {
            self.attrs[0].span.lo()
        };
        let hi = if let Some(body) = &self.body {
            body.span.hi()
        } else {
            self.pat.span.hi()
        };
        span_with_attrs_lo_hi!(self, lo, hi)
    }
}

impl Spanned for ast::Param {
    fn span(&self) -> Span {
        if crate::items::is_named_param(self) {
            mk_sp(crate::items::span_lo_for_param(self), self.ty.span.hi())
        } else {
            self.ty.span
        }
    }
}

impl Spanned for ast::GenericParam {
    fn span(&self) -> Span {
        let lo = match self.kind {
            _ if !self.attrs.is_empty() => self.attrs[0].span.lo(),
            ast::GenericParamKind::Const { kw_span, .. } => kw_span.lo(),
            _ => self.ident.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),
        }
        | ast::GenericParamKind::Const { ref ty, .. } = self.kind
        {
            ty.span().hi()
        } else {
            hi
        };
        mk_sp(lo, max(hi, ty_hi))
    }
}

impl Spanned for ast::FieldDef {
    fn span(&self) -> Span {
        span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi())
    }
}

impl Spanned for ast::WherePredicate {
    fn span(&self) -> Span {
        self.span
    }
}

impl Spanned for ast::FnRetTy {
    fn span(&self) -> Span {
        match *self {
            ast::FnRetTy::Default(span) => span,
            ast::FnRetTy::Ty(ref ty) => ty.span,
        }
    }
}

impl Spanned for ast::GenericArg {
    fn span(&self) -> Span {
        match *self {
            ast::GenericArg::Lifetime(ref lt) => lt.ident.span,
            ast::GenericArg::Type(ref ty) => ty.span(),
            ast::GenericArg::Const(ref _const) => _const.value.span(),
        }
    }
}

impl Spanned for ast::GenericBound {
    fn span(&self) -> Span {
        match *self {
            ast::GenericBound::Trait(ref ptr) => ptr.span,
            ast::GenericBound::Outlives(ref l) => l.ident.span,
            ast::GenericBound::Use(_, span) => span,
        }
    }
}

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(),
            MacroArg::Keyword(_, span) => span,
        }
    }
}

impl Spanned for ast::MetaItemInner {
    fn span(&self) -> Span {
        self.span()
    }
}

impl Spanned for ast::PreciseCapturingArg {
    fn span(&self) -> Span {
        match self {
            ast::PreciseCapturingArg::Lifetime(lt) => lt.ident.span,
            ast::PreciseCapturingArg::Arg(path, _) => path.span,
        }
    }
}

impl<'a> Spanned for RangeOperand<'a> {
    fn span(&self) -> Span {
        self.span
    }
}