Skip to content

Commit

Permalink
rustc_span: By-value interface for ctxt update
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jun 13, 2024
1 parent 4440f50 commit 6fea953
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl MutVisitor for Marker {
// 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.
let Marker(expn_id, transparency, ref mut cache) = *self;
span.update_ctxt(|ctxt| {
*span = span.map_ctxt(|ctxt| {
*cache
.entry(ctxt)
.or_insert_with(|| ctxt.apply_mark(expn_id.to_expn_id(), transparency))
Expand Down
22 changes: 9 additions & 13 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ impl SpanData {
pub fn with_hi(&self, hi: BytePos) -> Span {
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]
fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
Span::new(self.lo, self.hi, ctxt, self.parent)
Expand Down Expand Up @@ -577,9 +577,8 @@ impl Span {
self.data().with_hi(hi)
}
#[inline]
pub fn with_ctxt(mut self, ctxt: SyntaxContext) -> Span {
self.update_ctxt(|_| ctxt);
self
pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
self.map_ctxt(|_| ctxt)
}
#[inline]
pub fn parent(self) -> Option<LocalDefId> {
Expand Down Expand Up @@ -1060,9 +1059,8 @@ impl Span {
}

#[inline]
pub fn apply_mark(mut self, expn_id: ExpnId, transparency: Transparency) -> Span {
self.update_ctxt(|ctxt| ctxt.apply_mark(expn_id, transparency));
self
pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> Span {
self.map_ctxt(|ctxt| ctxt.apply_mark(expn_id, transparency))
}

#[inline]
Expand Down Expand Up @@ -1110,15 +1108,13 @@ impl Span {
}

#[inline]
pub fn normalize_to_macros_2_0(mut self) -> Span {
self.update_ctxt(|ctxt| ctxt.normalize_to_macros_2_0());
self
pub fn normalize_to_macros_2_0(self) -> Span {
self.map_ctxt(|ctxt| ctxt.normalize_to_macros_2_0())
}

#[inline]
pub fn normalize_to_macro_rules(mut self) -> Span {
self.update_ctxt(|ctxt| ctxt.normalize_to_macro_rules());
self
pub fn normalize_to_macro_rules(self) -> Span {
self.map_ctxt(|ctxt| ctxt.normalize_to_macro_rules())
}
}

Expand Down
28 changes: 13 additions & 15 deletions compiler/rustc_span/src/span_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 & PARENT_TAG == 0 {
// Inline-context format.
let $span1: &mut InlineCtxt = unsafe { transmute(&mut *$span) };
let $span1: InlineCtxt = unsafe { transmute($span) };
$arm1
} else {
// Inline-parent format.
let $span2: &mut InlineParent = unsafe { transmute(&mut *$span) };
let $span2: InlineParent = unsafe { transmute($span) };
$arm2
}
} else if $span.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
// Partially-interned format.
let $span3: &mut PartiallyInterned = unsafe { transmute(&mut *$span) };
let $span3: PartiallyInterned = unsafe { transmute($span) };
$arm3
} else {
// Interned format.
let $span4: &mut Interned = unsafe { transmute(&mut *$span) };
let $span4: Interned = unsafe { transmute($span) };
$arm4
}
};
Expand Down Expand Up @@ -273,9 +273,9 @@ impl Span {
/// Internal function to translate between an encoded span and the expanded representation.
/// This function must not be used outside the incremental engine.
#[inline]
pub fn data_untracked(mut self) -> SpanData {
pub fn data_untracked(self) -> SpanData {
match_span_kind! {
&mut self,
self,
InlineCtxt(span) => span.data(),
InlineParent(span) => span.data(),
PartiallyInterned(span) => span.data(),
Expand Down Expand Up @@ -304,7 +304,7 @@ impl Span {
// update doesn't change format. All non-inline or format changing scenarios require accessing
// interner and can fall back to `Span::new`.
#[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);
match_span_kind! {
self,
Expand All @@ -313,8 +313,7 @@ impl Span {
update(SyntaxContext::from_u32(span.ctxt as u32)).as_u32();
// Any small new context including zero will preserve the format.
if updated_ctxt32 <= MAX_CTXT {
span.ctxt = updated_ctxt32 as u16;
return;
return InlineCtxt::span(span.lo, span.len, updated_ctxt32 as u16);
}
data = span.data();
},
Expand All @@ -323,7 +322,7 @@ impl Span {
// Only if the new context is zero the format will be preserved.
if updated_ctxt32 == 0 {
// Do nothing.
return;
return self;
}
data = span.data();
},
Expand All @@ -332,8 +331,7 @@ impl Span {
// Any small new context excluding zero will preserve the format.
// Zero may change the format to `InlineParent` if parent and len are small enough.
if updated_ctxt32 <= MAX_CTXT && updated_ctxt32 != 0 {
span.ctxt = updated_ctxt32 as u16;
return;
return PartiallyInterned::span(span.index, updated_ctxt32 as u16);
}
data = span.data();
},
Expand All @@ -344,15 +342,15 @@ impl Span {
}

// 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,
// or an index into the interner if it cannot.
#[inline]
fn inline_ctxt(mut self) -> Result<SyntaxContext, usize> {
fn inline_ctxt(self) -> Result<SyntaxContext, usize> {
match_span_kind! {
&mut self,
self,
InlineCtxt(span) => Ok(SyntaxContext::from_u32(span.ctxt as u32)),
InlineParent(_span) => Ok(SyntaxContext::root()),
PartiallyInterned(span) => Ok(SyntaxContext::from_u32(span.ctxt as u32)),
Expand Down

0 comments on commit 6fea953

Please sign in to comment.