about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2024-06-12 01:58:29 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2024-06-13 19:29:24 +0300
commit6fea953267298e98ee630e595efcfe1c3a287976 (patch)
tree7ac386752b1d9413415c1468612d673592af36ee
parent4440f50996e12611e82735b57a0be143b18e9ea3 (diff)
downloadrust-6fea953267298e98ee630e595efcfe1c3a287976.tar.gz
rust-6fea953267298e98ee630e595efcfe1c3a287976.zip
rustc_span: By-value interface for ctxt update
-rw-r--r--compiler/rustc_expand/src/mbe/transcribe.rs2
-rw-r--r--compiler/rustc_span/src/lib.rs22
-rw-r--r--compiler/rustc_span/src/span_encoding.rs28
3 files changed, 23 insertions, 29 deletions
diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs
index 25e961d6009..5aa9242fa16 100644
--- a/compiler/rustc_expand/src/mbe/transcribe.rs
+++ b/compiler/rustc_expand/src/mbe/transcribe.rs
@@ -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))
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index 7b4506d33ca..b7ffe6c618a 100644
--- a/compiler/rustc_span/src/lib.rs
+++ b/compiler/rustc_span/src/lib.rs
@@ -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)
@@ -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> {
@@ -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]
@@ -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())
     }
 }
 
diff --git a/compiler/rustc_span/src/span_encoding.rs b/compiler/rustc_span/src/span_encoding.rs
index 8227597ac6f..69d7b4ca8c2 100644
--- a/compiler/rustc_span/src/span_encoding.rs
+++ b/compiler/rustc_span/src/span_encoding.rs
@@ -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
         }
     };
@@ -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(),
@@ -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,
@@ -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();
             },
@@ -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();
             },
@@ -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();
             },
@@ -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)),