Skip to content

Commit 801026e

Browse files
committed
Refactor how to get the span of a function header
1 parent d242a8b commit 801026e

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
@@ -2285,6 +2285,33 @@ pub struct FnSig {
22852285
pub span: Span,
22862286
}
22872287

2288+
impl FnSig {
2289+
/// Return a span encompassing the header, or where to insert it if empty.
2290+
pub fn header_span(&self) -> Span {
2291+
match self.header.ext {
2292+
Extern::Implicit(span) | Extern::Explicit(_, span) => {
2293+
return self.span.with_hi(span.hi());
2294+
}
2295+
Extern::None => {}
2296+
}
2297+
2298+
match self.header.safety {
2299+
Safety::Unsafe(span) | Safety::Safe(span) => return self.span.with_hi(span.hi()),
2300+
Safety::Default => {}
2301+
};
2302+
2303+
if let Some(coroutine_kind) = self.header.coroutine_kind {
2304+
return self.span.with_hi(coroutine_kind.span().hi());
2305+
}
2306+
2307+
if let Const::Yes(span) = self.header.constness {
2308+
return self.span.with_hi(span.hi());
2309+
}
2310+
2311+
self.span.shrink_to_lo()
2312+
}
2313+
}
2314+
22882315
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
22892316
#[derive(Encodable, Decodable, HashStable_Generic)]
22902317
pub enum FloatTy {
@@ -3634,12 +3661,12 @@ impl Extern {
36343661
/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
36353662
#[derive(Clone, Copy, Encodable, Decodable, Debug, Walkable)]
36363663
pub struct FnHeader {
3637-
/// Whether this is `unsafe`, or has a default safety.
3638-
pub safety: Safety,
3639-
/// Whether this is `async`, `gen`, or nothing.
3640-
pub coroutine_kind: Option<CoroutineKind>,
36413664
/// The `const` keyword, if any
36423665
pub constness: Const,
3666+
/// Whether this is `async`, `gen`, or nothing.
3667+
pub coroutine_kind: Option<CoroutineKind>,
3668+
/// Whether this is `unsafe`, or has a default safety.
3669+
pub safety: Safety,
36433670
/// The `extern` keyword and corresponding ABI string, if any.
36443671
pub ext: Extern,
36453672
}
@@ -3653,38 +3680,6 @@ impl FnHeader {
36533680
|| matches!(constness, Const::Yes(_))
36543681
|| !matches!(ext, Extern::None)
36553682
}
3656-
3657-
/// Return a span encompassing the header, or none if all options are default.
3658-
pub fn span(&self) -> Option<Span> {
3659-
fn append(a: &mut Option<Span>, b: Span) {
3660-
*a = match a {
3661-
None => Some(b),
3662-
Some(x) => Some(x.to(b)),
3663-
}
3664-
}
3665-
3666-
let mut full_span = None;
3667-
3668-
match self.safety {
3669-
Safety::Unsafe(span) | Safety::Safe(span) => append(&mut full_span, span),
3670-
Safety::Default => {}
3671-
};
3672-
3673-
if let Some(coroutine_kind) = self.coroutine_kind {
3674-
append(&mut full_span, coroutine_kind.span());
3675-
}
3676-
3677-
if let Const::Yes(span) = self.constness {
3678-
append(&mut full_span, span);
3679-
}
3680-
3681-
match self.ext {
3682-
Extern::Implicit(span) | Extern::Explicit(_, span) => append(&mut full_span, span),
3683-
Extern::None => {}
3684-
}
3685-
3686-
full_span
3687-
}
36883683
}
36893684

36903685
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
@@ -455,7 +455,7 @@ impl<'a> AstValidator<'a> {
455455
}
456456

457457
if !spans.is_empty() {
458-
let header_span = sig.header.span().unwrap_or(sig.span.shrink_to_lo());
458+
let header_span = sig.header_span();
459459
let suggestion_span = header_span.shrink_to_hi().to(sig.decl.output.span());
460460
let padding = if header_span.is_empty() { "" } else { " " };
461461

0 commit comments

Comments
 (0)