Skip to content

Commit 45e33a3

Browse files
committed
Refactor how to get the span of a function header
1 parent 1b0bc59 commit 45e33a3

File tree

2 files changed

+32
-37
lines changed

2 files changed

+32
-37
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,33 @@ pub struct FnSig {
22272227
pub span: Span,
22282228
}
22292229

2230+
impl FnSig {
2231+
/// Return a span encompassing the header, or where to insert it if empty.
2232+
pub fn header_span(&self) -> Span {
2233+
match self.header.ext {
2234+
Extern::Implicit(span) | Extern::Explicit(_, span) => {
2235+
return self.span.with_hi(span.hi());
2236+
}
2237+
Extern::None => {}
2238+
}
2239+
2240+
match self.header.safety {
2241+
Safety::Unsafe(span) | Safety::Safe(span) => return self.span.with_hi(span.hi()),
2242+
Safety::Default => {}
2243+
};
2244+
2245+
if let Some(coroutine_kind) = self.header.coroutine_kind {
2246+
return self.span.with_hi(coroutine_kind.span().hi());
2247+
}
2248+
2249+
if let Const::Yes(span) = self.header.constness {
2250+
return self.span.with_hi(span.hi());
2251+
}
2252+
2253+
self.span.shrink_to_lo()
2254+
}
2255+
}
2256+
22302257
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
22312258
#[derive(Encodable, Decodable, HashStable_Generic)]
22322259
pub enum FloatTy {
@@ -3571,12 +3598,12 @@ impl Extern {
35713598
/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
35723599
#[derive(Clone, Copy, Encodable, Decodable, Debug)]
35733600
pub struct FnHeader {
3574-
/// Whether this is `unsafe`, or has a default safety.
3575-
pub safety: Safety,
3576-
/// Whether this is `async`, `gen`, or nothing.
3577-
pub coroutine_kind: Option<CoroutineKind>,
35783601
/// The `const` keyword, if any
35793602
pub constness: Const,
3603+
/// Whether this is `async`, `gen`, or nothing.
3604+
pub coroutine_kind: Option<CoroutineKind>,
3605+
/// Whether this is `unsafe`, or has a default safety.
3606+
pub safety: Safety,
35803607
/// The `extern` keyword and corresponding ABI string, if any.
35813608
pub ext: Extern,
35823609
}
@@ -3590,38 +3617,6 @@ impl FnHeader {
35903617
|| matches!(constness, Const::Yes(_))
35913618
|| !matches!(ext, Extern::None)
35923619
}
3593-
3594-
/// Return a span encompassing the header, or none if all options are default.
3595-
pub fn span(&self) -> Option<Span> {
3596-
fn append(a: &mut Option<Span>, b: Span) {
3597-
*a = match a {
3598-
None => Some(b),
3599-
Some(x) => Some(x.to(b)),
3600-
}
3601-
}
3602-
3603-
let mut full_span = None;
3604-
3605-
match self.safety {
3606-
Safety::Unsafe(span) | Safety::Safe(span) => append(&mut full_span, span),
3607-
Safety::Default => {}
3608-
};
3609-
3610-
if let Some(coroutine_kind) = self.coroutine_kind {
3611-
append(&mut full_span, coroutine_kind.span());
3612-
}
3613-
3614-
if let Const::Yes(span) = self.constness {
3615-
append(&mut full_span, span);
3616-
}
3617-
3618-
match self.ext {
3619-
Extern::Implicit(span) | Extern::Explicit(_, span) => append(&mut full_span, span),
3620-
Extern::None => {}
3621-
}
3622-
3623-
full_span
3624-
}
36253620
}
36263621

36273622
impl Default for FnHeader {

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl<'a> AstValidator<'a> {
460460
}
461461

462462
if !spans.is_empty() {
463-
let header_span = sig.header.span().unwrap_or(sig.span.shrink_to_lo());
463+
let header_span = sig.header_span();
464464
let suggestion_span = header_span.shrink_to_hi().to(sig.decl.output.span());
465465
let padding = if header_span.is_empty() { "" } else { " " };
466466

0 commit comments

Comments
 (0)