Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 970eaf6

Browse files
committed
rustc_span: By-value interface for ctxt update
1 parent 8545b60 commit 970eaf6

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl MutVisitor for Marker {
3030
// it's some advanced case with macro-generated macros. So if we cache the marked version
3131
// of that context once, we'll typically have a 100% cache hit rate after that.
3232
let Marker(expn_id, transparency, ref mut cache) = *self;
33-
span.update_ctxt(|ctxt| {
33+
*span = span.map_ctxt(|ctxt| {
3434
*cache
3535
.entry(ctxt)
3636
.or_insert_with(|| ctxt.apply_mark(expn_id.to_expn_id(), transparency))

compiler/rustc_span/src/lib.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ impl SpanData {
520520
pub fn with_hi(&self, hi: BytePos) -> Span {
521521
Span::new(self.lo, hi, self.ctxt, self.parent)
522522
}
523-
/// Avoid if possible, `Span::update_ctxt` should be preferred.
523+
/// Avoid if possible, `Span::map_ctxt` should be preferred.
524524
#[inline]
525525
fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
526526
Span::new(self.lo, self.hi, ctxt, self.parent)
@@ -577,9 +577,8 @@ impl Span {
577577
self.data().with_hi(hi)
578578
}
579579
#[inline]
580-
pub fn with_ctxt(mut self, ctxt: SyntaxContext) -> Span {
581-
self.update_ctxt(|_| ctxt);
582-
self
580+
pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
581+
self.map_ctxt(|_| ctxt)
583582
}
584583
#[inline]
585584
pub fn parent(self) -> Option<LocalDefId> {
@@ -1060,9 +1059,8 @@ impl Span {
10601059
}
10611060

10621061
#[inline]
1063-
pub fn apply_mark(mut self, expn_id: ExpnId, transparency: Transparency) -> Span {
1064-
self.update_ctxt(|ctxt| ctxt.apply_mark(expn_id, transparency));
1065-
self
1062+
pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> Span {
1063+
self.map_ctxt(|ctxt| ctxt.apply_mark(expn_id, transparency))
10661064
}
10671065

10681066
#[inline]
@@ -1110,15 +1108,13 @@ impl Span {
11101108
}
11111109

11121110
#[inline]
1113-
pub fn normalize_to_macros_2_0(mut self) -> Span {
1114-
self.update_ctxt(|ctxt| ctxt.normalize_to_macros_2_0());
1115-
self
1111+
pub fn normalize_to_macros_2_0(self) -> Span {
1112+
self.map_ctxt(|ctxt| ctxt.normalize_to_macros_2_0())
11161113
}
11171114

11181115
#[inline]
1119-
pub fn normalize_to_macro_rules(mut self) -> Span {
1120-
self.update_ctxt(|ctxt| ctxt.normalize_to_macro_rules());
1121-
self
1116+
pub fn normalize_to_macro_rules(self) -> Span {
1117+
self.map_ctxt(|ctxt| ctxt.normalize_to_macro_rules())
11221118
}
11231119
}
11241120

compiler/rustc_span/src/span_encoding.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,20 @@ macro_rules! match_span_kind {
192192
if $span.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
193193
if $span.len_with_tag_or_marker & PARENT_TAG == 0 {
194194
// Inline-context format.
195-
let $span1: &mut InlineCtxt = unsafe { transmute(&mut *$span) };
195+
let $span1: InlineCtxt = unsafe { transmute($span) };
196196
$arm1
197197
} else {
198198
// Inline-parent format.
199-
let $span2: &mut InlineParent = unsafe { transmute(&mut *$span) };
199+
let $span2: InlineParent = unsafe { transmute($span) };
200200
$arm2
201201
}
202202
} else if $span.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
203203
// Partially-interned format.
204-
let $span3: &mut PartiallyInterned = unsafe { transmute(&mut *$span) };
204+
let $span3: PartiallyInterned = unsafe { transmute($span) };
205205
$arm3
206206
} else {
207207
// Interned format.
208-
let $span4: &mut Interned = unsafe { transmute(&mut *$span) };
208+
let $span4: Interned = unsafe { transmute($span) };
209209
$arm4
210210
}
211211
};
@@ -273,9 +273,9 @@ impl Span {
273273
/// Internal function to translate between an encoded span and the expanded representation.
274274
/// This function must not be used outside the incremental engine.
275275
#[inline]
276-
pub fn data_untracked(mut self) -> SpanData {
276+
pub fn data_untracked(self) -> SpanData {
277277
match_span_kind! {
278-
&mut self,
278+
self,
279279
InlineCtxt(span) => span.data(),
280280
InlineParent(span) => span.data(),
281281
PartiallyInterned(span) => span.data(),
@@ -304,7 +304,7 @@ impl Span {
304304
// update doesn't change format. All non-inline or format changing scenarios require accessing
305305
// interner and can fall back to `Span::new`.
306306
#[inline]
307-
pub fn update_ctxt(&mut self, update: impl FnOnce(SyntaxContext) -> SyntaxContext) {
307+
pub fn map_ctxt(self, update: impl FnOnce(SyntaxContext) -> SyntaxContext) -> Span {
308308
let (updated_ctxt32, data);
309309
match_span_kind! {
310310
self,
@@ -313,8 +313,7 @@ impl Span {
313313
update(SyntaxContext::from_u32(span.ctxt as u32)).as_u32();
314314
// Any small new context including zero will preserve the format.
315315
if updated_ctxt32 <= MAX_CTXT {
316-
span.ctxt = updated_ctxt32 as u16;
317-
return;
316+
return InlineCtxt::span(span.lo, span.len, updated_ctxt32 as u16);
318317
}
319318
data = span.data();
320319
},
@@ -323,7 +322,7 @@ impl Span {
323322
// Only if the new context is zero the format will be preserved.
324323
if updated_ctxt32 == 0 {
325324
// Do nothing.
326-
return;
325+
return self;
327326
}
328327
data = span.data();
329328
},
@@ -332,8 +331,7 @@ impl Span {
332331
// Any small new context excluding zero will preserve the format.
333332
// Zero may change the format to `InlineParent` if parent and len are small enough.
334333
if updated_ctxt32 <= MAX_CTXT && updated_ctxt32 != 0 {
335-
span.ctxt = updated_ctxt32 as u16;
336-
return;
334+
return PartiallyInterned::span(span.index, updated_ctxt32 as u16);
337335
}
338336
data = span.data();
339337
},
@@ -344,15 +342,15 @@ impl Span {
344342
}
345343

346344
// We could not keep the span in the same inline format, fall back to the complete logic.
347-
*self = data.with_ctxt(SyntaxContext::from_u32(updated_ctxt32));
345+
data.with_ctxt(SyntaxContext::from_u32(updated_ctxt32))
348346
}
349347

350348
// Returns either syntactic context, if it can be retrieved without taking the interner lock,
351349
// or an index into the interner if it cannot.
352350
#[inline]
353-
fn inline_ctxt(mut self) -> Result<SyntaxContext, usize> {
351+
fn inline_ctxt(self) -> Result<SyntaxContext, usize> {
354352
match_span_kind! {
355-
&mut self,
353+
self,
356354
InlineCtxt(span) => Ok(SyntaxContext::from_u32(span.ctxt as u32)),
357355
InlineParent(_span) => Ok(SyntaxContext::root()),
358356
PartiallyInterned(span) => Ok(SyntaxContext::from_u32(span.ctxt as u32)),

0 commit comments

Comments
 (0)