Skip to content

Commit 42541f5

Browse files
committed
Refactor how to get the span of a function header
1 parent 889701d commit 42541f5

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
/// A constraint on an associated item.
22892316
///
22902317
/// ### Examples
@@ -3535,12 +3562,12 @@ impl Extern {
35353562
/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
35363563
#[derive(Clone, Copy, Encodable, Decodable, Debug, Walkable)]
35373564
pub struct FnHeader {
3538-
/// Whether this is `unsafe`, or has a default safety.
3539-
pub safety: Safety,
3540-
/// Whether this is `async`, `gen`, or nothing.
3541-
pub coroutine_kind: Option<CoroutineKind>,
35423565
/// The `const` keyword, if any
35433566
pub constness: Const,
3567+
/// Whether this is `async`, `gen`, or nothing.
3568+
pub coroutine_kind: Option<CoroutineKind>,
3569+
/// Whether this is `unsafe`, or has a default safety.
3570+
pub safety: Safety,
35443571
/// The `extern` keyword and corresponding ABI string, if any.
35453572
pub ext: Extern,
35463573
}
@@ -3554,38 +3581,6 @@ impl FnHeader {
35543581
|| matches!(constness, Const::Yes(_))
35553582
|| !matches!(ext, Extern::None)
35563583
}
3557-
3558-
/// Return a span encompassing the header, or none if all options are default.
3559-
pub fn span(&self) -> Option<Span> {
3560-
fn append(a: &mut Option<Span>, b: Span) {
3561-
*a = match a {
3562-
None => Some(b),
3563-
Some(x) => Some(x.to(b)),
3564-
}
3565-
}
3566-
3567-
let mut full_span = None;
3568-
3569-
match self.safety {
3570-
Safety::Unsafe(span) | Safety::Safe(span) => append(&mut full_span, span),
3571-
Safety::Default => {}
3572-
};
3573-
3574-
if let Some(coroutine_kind) = self.coroutine_kind {
3575-
append(&mut full_span, coroutine_kind.span());
3576-
}
3577-
3578-
if let Const::Yes(span) = self.constness {
3579-
append(&mut full_span, span);
3580-
}
3581-
3582-
match self.ext {
3583-
Extern::Implicit(span) | Extern::Explicit(_, span) => append(&mut full_span, span),
3584-
Extern::None => {}
3585-
}
3586-
3587-
full_span
3588-
}
35893584
}
35903585

35913586
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)