Skip to content

Commit 56c96d7

Browse files
Rollup merge of rust-lang#112706 - WaffleLapkin:syntax_context_is_root, r=petrochenkov
Add `SyntaxContext::is_root` Makes the code a tad nicer.
2 parents a5f8859 + 73c5c97 commit 56c96d7

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

compiler/rustc_expand/src/mbe/quoted.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_session::parse::{feature_err, ParseSess};
99
use rustc_span::symbol::{kw, sym, Ident};
1010

1111
use rustc_span::edition::Edition;
12-
use rustc_span::{Span, SyntaxContext};
12+
use rustc_span::Span;
1313

1414
const VALID_FRAGMENT_NAMES_MSG: &str = "valid fragment specifiers are \
1515
`ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, \
@@ -72,7 +72,7 @@ pub(super) fn parse(
7272
// `SyntaxContext::root()` from a foreign crate will
7373
// have the edition of that crate (which we manually
7474
// retrieve via the `edition` parameter).
75-
if span.ctxt() == SyntaxContext::root() {
75+
if span.ctxt().is_root() {
7676
edition
7777
} else {
7878
span.edition()

compiler/rustc_middle/src/mir/spanview.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_middle::hir;
33
use rustc_middle::mir::*;
44
use rustc_middle::ty::TyCtxt;
55
use rustc_session::config::MirSpanview;
6-
use rustc_span::{BytePos, Pos, Span, SyntaxContext};
6+
use rustc_span::{BytePos, Pos, Span};
77

88
use std::cmp;
99
use std::io::{self, Write};
@@ -327,7 +327,7 @@ fn compute_block_span(data: &BasicBlockData<'_>, body_span: Span) -> Span {
327327
let mut span = data.terminator().source_info.span;
328328
for statement_span in data.statements.iter().map(|statement| statement.source_info.span) {
329329
// Only combine Spans from the root context, and within the function's body_span.
330-
if statement_span.ctxt() == SyntaxContext::root() && body_span.contains(statement_span) {
330+
if statement_span.ctxt().is_root() && body_span.contains(statement_span) {
331331
span = span.to(statement_span);
332332
}
333333
}

compiler/rustc_span/src/hygiene.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl HygieneData {
507507
self.normalize_to_macro_rules(call_site_ctxt)
508508
};
509509

510-
if call_site_ctxt == SyntaxContext::root() {
510+
if call_site_ctxt.is_root() {
511511
return self.apply_mark_internal(ctxt, expn_id, transparency);
512512
}
513513

@@ -671,12 +671,17 @@ impl SyntaxContext {
671671
}
672672

673673
#[inline]
674-
pub(crate) fn as_u32(self) -> u32 {
674+
pub const fn is_root(self) -> bool {
675+
self.0 == SyntaxContext::root().as_u32()
676+
}
677+
678+
#[inline]
679+
pub(crate) const fn as_u32(self) -> u32 {
675680
self.0
676681
}
677682

678683
#[inline]
679-
pub(crate) fn from_u32(raw: u32) -> SyntaxContext {
684+
pub(crate) const fn from_u32(raw: u32) -> SyntaxContext {
680685
SyntaxContext(raw)
681686
}
682687

@@ -1500,7 +1505,7 @@ impl<CTX: HashStableContext> HashStable<CTX> for SyntaxContext {
15001505
const TAG_EXPANSION: u8 = 0;
15011506
const TAG_NO_EXPANSION: u8 = 1;
15021507

1503-
if *self == SyntaxContext::root() {
1508+
if self.is_root() {
15041509
TAG_NO_EXPANSION.hash_stable(ctx, hasher);
15051510
} else {
15061511
TAG_EXPANSION.hash_stable(ctx, hasher);

compiler/rustc_span/src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -826,9 +826,9 @@ impl Span {
826826
// Return the macro span on its own to avoid weird diagnostic output. It is preferable to
827827
// have an incomplete span than a completely nonsensical one.
828828
if span_data.ctxt != end_data.ctxt {
829-
if span_data.ctxt == SyntaxContext::root() {
829+
if span_data.ctxt.is_root() {
830830
return end;
831-
} else if end_data.ctxt == SyntaxContext::root() {
831+
} else if end_data.ctxt.is_root() {
832832
return self;
833833
}
834834
// Both spans fall within a macro.
@@ -837,7 +837,7 @@ impl Span {
837837
Span::new(
838838
cmp::min(span_data.lo, end_data.lo),
839839
cmp::max(span_data.hi, end_data.hi),
840-
if span_data.ctxt == SyntaxContext::root() { end_data.ctxt } else { span_data.ctxt },
840+
if span_data.ctxt.is_root() { end_data.ctxt } else { span_data.ctxt },
841841
if span_data.parent == end_data.parent { span_data.parent } else { None },
842842
)
843843
}
@@ -855,7 +855,7 @@ impl Span {
855855
Span::new(
856856
span.hi,
857857
end.lo,
858-
if end.ctxt == SyntaxContext::root() { end.ctxt } else { span.ctxt },
858+
if end.ctxt.is_root() { end.ctxt } else { span.ctxt },
859859
if span.parent == end.parent { span.parent } else { None },
860860
)
861861
}
@@ -879,9 +879,9 @@ impl Span {
879879
// Return the macro span on its own to avoid weird diagnostic output. It is preferable to
880880
// have an incomplete span than a completely nonsensical one.
881881
if span_data.ctxt != end_data.ctxt {
882-
if span_data.ctxt == SyntaxContext::root() {
882+
if span_data.ctxt.is_root() {
883883
return end;
884-
} else if end_data.ctxt == SyntaxContext::root() {
884+
} else if end_data.ctxt.is_root() {
885885
return self;
886886
}
887887
// Both spans fall within a macro.
@@ -890,7 +890,7 @@ impl Span {
890890
Span::new(
891891
span_data.lo,
892892
end_data.lo,
893-
if end_data.ctxt == SyntaxContext::root() { end_data.ctxt } else { span_data.ctxt },
893+
if end_data.ctxt.is_root() { end_data.ctxt } else { span_data.ctxt },
894894
if span_data.parent == end_data.parent { span_data.parent } else { None },
895895
)
896896
}

0 commit comments

Comments
 (0)