rustc_span: By-value interface for ctxt update

This commit is contained in:
Vadim Petrochenkov 2024-06-12 01:58:29 +03:00
parent 4440f50996
commit 6fea953267
3 changed files with 23 additions and 29 deletions

View File

@ -30,7 +30,7 @@ impl MutVisitor for Marker {
// it's some advanced case with macro-generated macros. So if we cache the marked version // it's some advanced case with macro-generated macros. So if we cache the marked version
// of that context once, we'll typically have a 100% cache hit rate after that. // of that context once, we'll typically have a 100% cache hit rate after that.
let Marker(expn_id, transparency, ref mut cache) = *self; let Marker(expn_id, transparency, ref mut cache) = *self;
span.update_ctxt(|ctxt| { *span = span.map_ctxt(|ctxt| {
*cache *cache
.entry(ctxt) .entry(ctxt)
.or_insert_with(|| ctxt.apply_mark(expn_id.to_expn_id(), transparency)) .or_insert_with(|| ctxt.apply_mark(expn_id.to_expn_id(), transparency))

View File

@ -520,7 +520,7 @@ impl SpanData {
pub fn with_hi(&self, hi: BytePos) -> Span { pub fn with_hi(&self, hi: BytePos) -> Span {
Span::new(self.lo, hi, self.ctxt, self.parent) Span::new(self.lo, hi, self.ctxt, self.parent)
} }
/// Avoid if possible, `Span::update_ctxt` should be preferred. /// Avoid if possible, `Span::map_ctxt` should be preferred.
#[inline] #[inline]
fn with_ctxt(&self, ctxt: SyntaxContext) -> Span { fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
Span::new(self.lo, self.hi, ctxt, self.parent) Span::new(self.lo, self.hi, ctxt, self.parent)
@ -577,9 +577,8 @@ impl Span {
self.data().with_hi(hi) self.data().with_hi(hi)
} }
#[inline] #[inline]
pub fn with_ctxt(mut self, ctxt: SyntaxContext) -> Span { pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
self.update_ctxt(|_| ctxt); self.map_ctxt(|_| ctxt)
self
} }
#[inline] #[inline]
pub fn parent(self) -> Option<LocalDefId> { pub fn parent(self) -> Option<LocalDefId> {
@ -1060,9 +1059,8 @@ impl Span {
} }
#[inline] #[inline]
pub fn apply_mark(mut self, expn_id: ExpnId, transparency: Transparency) -> Span { pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> Span {
self.update_ctxt(|ctxt| ctxt.apply_mark(expn_id, transparency)); self.map_ctxt(|ctxt| ctxt.apply_mark(expn_id, transparency))
self
} }
#[inline] #[inline]
@ -1110,15 +1108,13 @@ impl Span {
} }
#[inline] #[inline]
pub fn normalize_to_macros_2_0(mut self) -> Span { pub fn normalize_to_macros_2_0(self) -> Span {
self.update_ctxt(|ctxt| ctxt.normalize_to_macros_2_0()); self.map_ctxt(|ctxt| ctxt.normalize_to_macros_2_0())
self
} }
#[inline] #[inline]
pub fn normalize_to_macro_rules(mut self) -> Span { pub fn normalize_to_macro_rules(self) -> Span {
self.update_ctxt(|ctxt| ctxt.normalize_to_macro_rules()); self.map_ctxt(|ctxt| ctxt.normalize_to_macro_rules())
self
} }
} }

View File

@ -192,20 +192,20 @@ macro_rules! match_span_kind {
if $span.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER { if $span.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
if $span.len_with_tag_or_marker & PARENT_TAG == 0 { if $span.len_with_tag_or_marker & PARENT_TAG == 0 {
// Inline-context format. // Inline-context format.
let $span1: &mut InlineCtxt = unsafe { transmute(&mut *$span) }; let $span1: InlineCtxt = unsafe { transmute($span) };
$arm1 $arm1
} else { } else {
// Inline-parent format. // Inline-parent format.
let $span2: &mut InlineParent = unsafe { transmute(&mut *$span) }; let $span2: InlineParent = unsafe { transmute($span) };
$arm2 $arm2
} }
} else if $span.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER { } else if $span.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
// Partially-interned format. // Partially-interned format.
let $span3: &mut PartiallyInterned = unsafe { transmute(&mut *$span) }; let $span3: PartiallyInterned = unsafe { transmute($span) };
$arm3 $arm3
} else { } else {
// Interned format. // Interned format.
let $span4: &mut Interned = unsafe { transmute(&mut *$span) }; let $span4: Interned = unsafe { transmute($span) };
$arm4 $arm4
} }
}; };
@ -273,9 +273,9 @@ impl Span {
/// Internal function to translate between an encoded span and the expanded representation. /// Internal function to translate between an encoded span and the expanded representation.
/// This function must not be used outside the incremental engine. /// This function must not be used outside the incremental engine.
#[inline] #[inline]
pub fn data_untracked(mut self) -> SpanData { pub fn data_untracked(self) -> SpanData {
match_span_kind! { match_span_kind! {
&mut self, self,
InlineCtxt(span) => span.data(), InlineCtxt(span) => span.data(),
InlineParent(span) => span.data(), InlineParent(span) => span.data(),
PartiallyInterned(span) => span.data(), PartiallyInterned(span) => span.data(),
@ -304,7 +304,7 @@ impl Span {
// update doesn't change format. All non-inline or format changing scenarios require accessing // update doesn't change format. All non-inline or format changing scenarios require accessing
// interner and can fall back to `Span::new`. // interner and can fall back to `Span::new`.
#[inline] #[inline]
pub fn update_ctxt(&mut self, update: impl FnOnce(SyntaxContext) -> SyntaxContext) { pub fn map_ctxt(self, update: impl FnOnce(SyntaxContext) -> SyntaxContext) -> Span {
let (updated_ctxt32, data); let (updated_ctxt32, data);
match_span_kind! { match_span_kind! {
self, self,
@ -313,8 +313,7 @@ impl Span {
update(SyntaxContext::from_u32(span.ctxt as u32)).as_u32(); update(SyntaxContext::from_u32(span.ctxt as u32)).as_u32();
// Any small new context including zero will preserve the format. // Any small new context including zero will preserve the format.
if updated_ctxt32 <= MAX_CTXT { if updated_ctxt32 <= MAX_CTXT {
span.ctxt = updated_ctxt32 as u16; return InlineCtxt::span(span.lo, span.len, updated_ctxt32 as u16);
return;
} }
data = span.data(); data = span.data();
}, },
@ -323,7 +322,7 @@ impl Span {
// Only if the new context is zero the format will be preserved. // Only if the new context is zero the format will be preserved.
if updated_ctxt32 == 0 { if updated_ctxt32 == 0 {
// Do nothing. // Do nothing.
return; return self;
} }
data = span.data(); data = span.data();
}, },
@ -332,8 +331,7 @@ impl Span {
// Any small new context excluding zero will preserve the format. // Any small new context excluding zero will preserve the format.
// Zero may change the format to `InlineParent` if parent and len are small enough. // Zero may change the format to `InlineParent` if parent and len are small enough.
if updated_ctxt32 <= MAX_CTXT && updated_ctxt32 != 0 { if updated_ctxt32 <= MAX_CTXT && updated_ctxt32 != 0 {
span.ctxt = updated_ctxt32 as u16; return PartiallyInterned::span(span.index, updated_ctxt32 as u16);
return;
} }
data = span.data(); data = span.data();
}, },
@ -344,15 +342,15 @@ impl Span {
} }
// We could not keep the span in the same inline format, fall back to the complete logic. // We could not keep the span in the same inline format, fall back to the complete logic.
*self = data.with_ctxt(SyntaxContext::from_u32(updated_ctxt32)); data.with_ctxt(SyntaxContext::from_u32(updated_ctxt32))
} }
// Returns either syntactic context, if it can be retrieved without taking the interner lock, // Returns either syntactic context, if it can be retrieved without taking the interner lock,
// or an index into the interner if it cannot. // or an index into the interner if it cannot.
#[inline] #[inline]
fn inline_ctxt(mut self) -> Result<SyntaxContext, usize> { fn inline_ctxt(self) -> Result<SyntaxContext, usize> {
match_span_kind! { match_span_kind! {
&mut self, self,
InlineCtxt(span) => Ok(SyntaxContext::from_u32(span.ctxt as u32)), InlineCtxt(span) => Ok(SyntaxContext::from_u32(span.ctxt as u32)),
InlineParent(_span) => Ok(SyntaxContext::root()), InlineParent(_span) => Ok(SyntaxContext::root()),
PartiallyInterned(span) => Ok(SyntaxContext::from_u32(span.ctxt as u32)), PartiallyInterned(span) => Ok(SyntaxContext::from_u32(span.ctxt as u32)),