1use rustc_data_structures::fx::FxIndexSet;
2use rustc_data_structures::stable_hasher::RawSpan;
3// This code is very hot and uses lots of arithmetic, avoid overflow checks for performance.
4// See https://github.com/rust-lang/rust/pull/119440#issuecomment-1874255727
5use rustc_serialize::int_overflow::DebugStrictAdd;
67use crate::def_id::{DefIndex, LocalDefId};
8use crate::hygiene::SyntaxContext;
9use crate::{BytePos, SPAN_TRACK, SpanData};
1011/// A compressed span.
12///
13/// [`SpanData`] is 16 bytes, which is too big to stick everywhere. `Span` only
14/// takes up 8 bytes, with less space for the length, parent and context. The
15/// vast majority (99.9%+) of `SpanData` instances can be made to fit within
16/// those 8 bytes. Any `SpanData` whose fields don't fit into a `Span` are
17/// stored in a separate interner table, and the `Span` will index into that
18/// table. Interning is rare enough that the cost is low, but common enough
19/// that the code is exercised regularly.
20///
21/// An earlier version of this code used only 4 bytes for `Span`, but that was
22/// slower because only 80--90% of spans could be stored inline (even less in
23/// very large crates) and so the interner was used a lot more. That version of
24/// the code also predated the storage of parents.
25///
26/// There are four different span forms.
27///
28/// Inline-context format (requires non-huge length, non-huge context, and no parent):
29/// - `span.lo_or_index == span_data.lo`
30/// - `span.len_with_tag_or_marker == len == span_data.hi - span_data.lo` (must be `<= MAX_LEN`)
31/// - `span.ctxt_or_parent_or_marker == span_data.ctxt` (must be `<= MAX_CTXT`)
32///
33/// Inline-parent format (requires non-huge length, root context, and non-huge parent):
34/// - `span.lo_or_index == span_data.lo`
35/// - `span.len_with_tag_or_marker & !PARENT_TAG == len == span_data.hi - span_data.lo`
36/// (must be `<= MAX_LEN`)
37/// - `span.len_with_tag_or_marker` has top bit (`PARENT_TAG`) set
38/// - `span.ctxt_or_parent_or_marker == span_data.parent` (must be `<= MAX_CTXT`)
39///
40/// Partially-interned format (requires non-huge context):
41/// - `span.lo_or_index == index` (indexes into the interner table)
42/// - `span.len_with_tag_or_marker == BASE_LEN_INTERNED_MARKER`
43/// - `span.ctxt_or_parent_or_marker == span_data.ctxt` (must be `<= MAX_CTXT`)
44///
45/// Fully-interned format (all cases not covered above):
46/// - `span.lo_or_index == index` (indexes into the interner table)
47/// - `span.len_with_tag_or_marker == BASE_LEN_INTERNED_MARKER`
48/// - `span.ctxt_or_parent_or_marker == CTXT_INTERNED_MARKER`
49///
50/// The partially-interned form requires looking in the interning table for
51/// lo and length, but the context is stored inline as well as interned.
52/// This is useful because context lookups are often done in isolation, and
53/// inline lookups are quicker.
54///
55/// Notes about the choice of field sizes:
56/// - `lo` is 32 bits in both `Span` and `SpanData`, which means that `lo`
57/// values never cause interning. The number of bits needed for `lo`
58/// depends on the crate size. 32 bits allows up to 4 GiB of code in a crate.
59/// Having no compression on this field means there is no performance cliff
60/// if a crate exceeds a particular size.
61/// - `len` is ~15 bits in `Span` (a u16, minus 1 bit for PARENT_TAG) and 32
62/// bits in `SpanData`, which means that large `len` values will cause
63/// interning. The number of bits needed for `len` does not depend on the
64/// crate size. The most common numbers of bits for `len` are from 0 to 7,
65/// with a peak usually at 3 or 4, and then it drops off quickly from 8
66/// onwards. 15 bits is enough for 99.99%+ of cases, but larger values
67/// (sometimes 20+ bits) might occur dozens of times in a typical crate.
68/// - `ctxt_or_parent_or_marker` is 16 bits in `Span` and two 32 bit fields in
69/// `SpanData`, which means intering will happen if `ctxt` is large, if
70/// `parent` is large, or if both values are non-zero. The number of bits
71/// needed for `ctxt` values depend partly on the crate size and partly on
72/// the form of the code. No crates in `rustc-perf` need more than 15 bits
73/// for `ctxt_or_parent_or_marker`, but larger crates might need more than 16
74/// bits. The number of bits needed for `parent` hasn't been measured,
75/// because `parent` isn't currently used by default.
76///
77/// In order to reliably use parented spans in incremental compilation,
78/// accesses to `lo` and `hi` must introduce a dependency to the parent definition's span.
79/// This is performed using the callback `SPAN_TRACK` to access the query engine.
80#[derive(#[automatically_derived]
impl ::core::clone::Clone for Span {
#[inline]
fn clone(&self) -> Span {
let _: ::core::clone::AssertParamIsClone<u32>;
let _: ::core::clone::AssertParamIsClone<u16>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Span { }Copy, #[automatically_derived]
impl ::core::cmp::Eq for Span {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<u32>;
let _: ::core::cmp::AssertParamIsEq<u16>;
}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for Span {
#[inline]
fn eq(&self, other: &Span) -> bool {
self.lo_or_index == other.lo_or_index &&
self.len_with_tag_or_marker == other.len_with_tag_or_marker &&
self.ctxt_or_parent_or_marker == other.ctxt_or_parent_or_marker
}
}PartialEq, #[automatically_derived]
impl ::core::hash::Hash for Span {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.lo_or_index, state);
::core::hash::Hash::hash(&self.len_with_tag_or_marker, state);
::core::hash::Hash::hash(&self.ctxt_or_parent_or_marker, state)
}
}Hash)]
81#[rustc_pass_by_value]
82pub struct Span {
83 lo_or_index: u32,
84 len_with_tag_or_marker: u16,
85 ctxt_or_parent_or_marker: u16,
86}
8788// Convenience structures for all span formats.
89#[derive(#[automatically_derived]
impl ::core::clone::Clone for InlineCtxt {
#[inline]
fn clone(&self) -> InlineCtxt {
let _: ::core::clone::AssertParamIsClone<u32>;
let _: ::core::clone::AssertParamIsClone<u16>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for InlineCtxt { }Copy)]
90struct InlineCtxt {
91 lo: u32,
92 len: u16,
93 ctxt: u16,
94}
9596#[derive(#[automatically_derived]
impl ::core::clone::Clone for InlineParent {
#[inline]
fn clone(&self) -> InlineParent {
let _: ::core::clone::AssertParamIsClone<u32>;
let _: ::core::clone::AssertParamIsClone<u16>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for InlineParent { }Copy)]
97struct InlineParent {
98 lo: u32,
99 len_with_tag: u16,
100 parent: u16,
101}
102103#[derive(#[automatically_derived]
impl ::core::clone::Clone for PartiallyInterned {
#[inline]
fn clone(&self) -> PartiallyInterned {
let _: ::core::clone::AssertParamIsClone<u32>;
let _: ::core::clone::AssertParamIsClone<u16>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PartiallyInterned { }Copy)]
104struct PartiallyInterned {
105 index: u32,
106 ctxt: u16,
107}
108109#[derive(#[automatically_derived]
impl ::core::clone::Clone for Interned {
#[inline]
fn clone(&self) -> Interned {
let _: ::core::clone::AssertParamIsClone<u32>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Interned { }Copy)]
110struct Interned {
111 index: u32,
112}
113114impl InlineCtxt {
115#[inline]
116fn data(self) -> SpanData {
117let len = self.len as u32;
118if true {
if !(len <= MAX_LEN) {
::core::panicking::panic("assertion failed: len <= MAX_LEN")
};
};debug_assert!(len <= MAX_LEN);
119SpanData {
120 lo: BytePos(self.lo),
121 hi: BytePos(self.lo.debug_strict_add(len)),
122 ctxt: SyntaxContext::from_u16(self.ctxt),
123 parent: None,
124 }
125 }
126#[inline]
127fn span(lo: u32, len: u16, ctxt: u16) -> Span {
128Span { lo_or_index: lo, len_with_tag_or_marker: len, ctxt_or_parent_or_marker: ctxt }
129 }
130#[inline]
131fn from_span(span: Span) -> InlineCtxt {
132let (lo, len, ctxt) =
133 (span.lo_or_index, span.len_with_tag_or_marker, span.ctxt_or_parent_or_marker);
134InlineCtxt { lo, len, ctxt }
135 }
136}
137138impl InlineParent {
139#[inline]
140fn data(self) -> SpanData {
141let len = (self.len_with_tag & !PARENT_TAG) as u32;
142if true {
if !(len <= MAX_LEN) {
::core::panicking::panic("assertion failed: len <= MAX_LEN")
};
};debug_assert!(len <= MAX_LEN);
143SpanData {
144 lo: BytePos(self.lo),
145 hi: BytePos(self.lo.debug_strict_add(len)),
146 ctxt: SyntaxContext::root(),
147 parent: Some(LocalDefId { local_def_index: DefIndex::from_u16(self.parent) }),
148 }
149 }
150#[inline]
151fn span(lo: u32, len: u16, parent: u16) -> Span {
152let (lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker) =
153 (lo, PARENT_TAG | len, parent);
154Span { lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker }
155 }
156#[inline]
157fn from_span(span: Span) -> InlineParent {
158let (lo, len_with_tag, parent) =
159 (span.lo_or_index, span.len_with_tag_or_marker, span.ctxt_or_parent_or_marker);
160InlineParent { lo, len_with_tag, parent }
161 }
162}
163164impl PartiallyInterned {
165#[inline]
166fn data(self) -> SpanData {
167SpanData {
168 ctxt: SyntaxContext::from_u16(self.ctxt),
169 ..with_span_interner(|interner| interner.spans[self.index as usize])
170 }
171 }
172#[inline]
173fn span(index: u32, ctxt: u16) -> Span {
174let (lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker) =
175 (index, BASE_LEN_INTERNED_MARKER, ctxt);
176Span { lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker }
177 }
178#[inline]
179fn from_span(span: Span) -> PartiallyInterned {
180PartiallyInterned { index: span.lo_or_index, ctxt: span.ctxt_or_parent_or_marker }
181 }
182}
183184impl Interned {
185#[inline]
186fn data(self) -> SpanData {
187with_span_interner(|interner| interner.spans[self.index as usize])
188 }
189#[inline]
190fn span(index: u32) -> Span {
191let (lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker) =
192 (index, BASE_LEN_INTERNED_MARKER, CTXT_INTERNED_MARKER);
193Span { lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker }
194 }
195#[inline]
196fn from_span(span: Span) -> Interned {
197Interned { index: span.lo_or_index }
198 }
199}
200201// This code is very hot, and converting span to an enum and matching on it doesn't optimize away
202// properly. So we are using a macro emulating such a match, but expand it directly to an if-else
203// chain.
204macro_rules!match_span_kind {
205 (
206$span:expr,
207 InlineCtxt($span1:ident) => $arm1:expr,
208 InlineParent($span2:ident) => $arm2:expr,
209 PartiallyInterned($span3:ident) => $arm3:expr,
210 Interned($span4:ident) => $arm4:expr,
211 ) => {
212if $span.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
213if $span.len_with_tag_or_marker & PARENT_TAG == 0 {
214// Inline-context format.
215let $span1 = InlineCtxt::from_span($span);
216$arm1
217} else {
218// Inline-parent format.
219let $span2 = InlineParent::from_span($span);
220$arm2
221}
222 } else if $span.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
223// Partially-interned format.
224let $span3 = PartiallyInterned::from_span($span);
225$arm3
226} else {
227// Interned format.
228let $span4 = Interned::from_span($span);
229$arm4
230}
231 };
232}
233234// `MAX_LEN` is chosen so that `PARENT_TAG | MAX_LEN` is distinct from
235// `BASE_LEN_INTERNED_MARKER`. (If `MAX_LEN` was 1 higher, this wouldn't be true.)
236const MAX_LEN: u32 = 0b0111_1111_1111_1110;
237const MAX_CTXT: u32 = 0b0111_1111_1111_1110;
238const PARENT_TAG: u16 = 0b1000_0000_0000_0000;
239const BASE_LEN_INTERNED_MARKER: u16 = 0b1111_1111_1111_1111;
240const CTXT_INTERNED_MARKER: u16 = 0b1111_1111_1111_1111;
241242/// The dummy span has zero position, length, and context, and no parent.
243pub const DUMMY_SP: Span =
244Span { lo_or_index: 0, len_with_tag_or_marker: 0, ctxt_or_parent_or_marker: 0 };
245246impl Span {
247#[inline]
248pub fn new(
249mut lo: BytePos,
250mut hi: BytePos,
251 ctxt: SyntaxContext,
252 parent: Option<LocalDefId>,
253 ) -> Self {
254if lo > hi {
255 std::mem::swap(&mut lo, &mut hi);
256 }
257258// Small len and ctxt may enable one of fully inline formats (or may not).
259let (len, ctxt32) = (hi.0 - lo.0, ctxt.as_u32());
260if len <= MAX_LEN && ctxt32 <= MAX_CTXT {
261match parent {
262None => return InlineCtxt::span(lo.0, lenas u16, ctxt32as u16),
263Some(parent) => {
264let parent32 = parent.local_def_index.as_u32();
265if ctxt32 == 0 && parent32 <= MAX_CTXT {
266return InlineParent::span(lo.0, lenas u16, parent32as u16);
267 }
268 }
269 }
270 }
271272// Otherwise small ctxt may enable the partially inline format.
273let index = |ctxt| {
274with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt, parent }))
275 };
276if ctxt32 <= MAX_CTXT {
277// Interned ctxt should never be read, so it can use any value.
278PartiallyInterned::span(index(SyntaxContext::from_u32(u32::MAX)), ctxt32as u16)
279 } else {
280Interned::span(index(ctxt))
281 }
282 }
283284#[inline]
285pub fn data(self) -> SpanData {
286let data = self.data_untracked();
287if let Some(parent) = data.parent {
288 (*SPAN_TRACK)(parent);
289 }
290data291 }
292293/// Internal function to translate between an encoded span and the expanded representation.
294 /// This function must not be used outside the incremental engine.
295#[inline]
296pub fn data_untracked(self) -> SpanData {
297if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
let span = InlineCtxt::from_span(self);
span.data()
} else { let span = InlineParent::from_span(self); span.data() }
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
let span = PartiallyInterned::from_span(self);
span.data()
} else { let span = Interned::from_span(self); span.data() }match_span_kind! {
298self,
299 InlineCtxt(span) => span.data(),
300 InlineParent(span) => span.data(),
301 PartiallyInterned(span) => span.data(),
302 Interned(span) => span.data(),
303 }304 }
305306/// Returns `true` if this span comes from any kind of macro, desugaring or inlining.
307#[inline]
308pub fn from_expansion(self) -> bool {
309let ctxt = if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
let span = InlineCtxt::from_span(self);
SyntaxContext::from_u16(span.ctxt)
} else {
let _span = InlineParent::from_span(self);
SyntaxContext::root()
}
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
let span = PartiallyInterned::from_span(self);
SyntaxContext::from_u16(span.ctxt)
} else {
let _span = Interned::from_span(self);
SyntaxContext::from_u16(CTXT_INTERNED_MARKER)
}match_span_kind! {
310self,
311// All branches here, except `InlineParent`, actually return `span.ctxt_or_parent_or_marker`.
312 // Since `Interned` is selected if the field contains `CTXT_INTERNED_MARKER` returning that value
313 // as the context allows the compiler to optimize out the branch that selects between either
314 // `Interned` and `PartiallyInterned`.
315 //
316 // Interned contexts can never be the root context and `CTXT_INTERNED_MARKER` has a different value
317 // than the root context so this works for checking is this is an expansion.
318InlineCtxt(span) => SyntaxContext::from_u16(span.ctxt),
319 InlineParent(_span) => SyntaxContext::root(),
320 PartiallyInterned(span) => SyntaxContext::from_u16(span.ctxt),
321 Interned(_span) => SyntaxContext::from_u16(CTXT_INTERNED_MARKER),
322 };
323 !ctxt.is_root()
324 }
325326/// Returns `true` if this is a dummy span with any hygienic context.
327#[inline]
328pub fn is_dummy(self) -> bool {
329if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
330// Inline-context or inline-parent format.
331let lo = self.lo_or_index;
332let len = (self.len_with_tag_or_marker & !PARENT_TAG) as u32;
333if true {
if !(len <= MAX_LEN) {
::core::panicking::panic("assertion failed: len <= MAX_LEN")
};
};debug_assert!(len <= MAX_LEN);
334lo == 0 && len == 0
335} else {
336// Fully-interned or partially-interned format.
337let index = self.lo_or_index;
338let data = with_span_interner(|interner| interner.spans[indexas usize]);
339data.lo == BytePos(0) && data.hi == BytePos(0)
340 }
341 }
342343#[inline]
344pub fn map_ctxt(self, map: impl FnOnce(SyntaxContext) -> SyntaxContext) -> Span {
345let data = if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
let span = InlineCtxt::from_span(self);
{
let new_ctxt = map(SyntaxContext::from_u16(span.ctxt));
let new_ctxt32 = new_ctxt.as_u32();
return if new_ctxt32 <= MAX_CTXT {
InlineCtxt::span(span.lo, span.len, new_ctxt32 as u16)
} else { span.data().with_ctxt(new_ctxt) };
}
} else { let span = InlineParent::from_span(self); span.data() }
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
let span = PartiallyInterned::from_span(self);
span.data()
} else { let span = Interned::from_span(self); span.data() }match_span_kind! {
346self,
347 InlineCtxt(span) => {
348// This format occurs 1-2 orders of magnitude more often than others (#125017),
349 // so it makes sense to micro-optimize it to avoid `span.data()` and `Span::new()`.
350let new_ctxt = map(SyntaxContext::from_u16(span.ctxt));
351let new_ctxt32 = new_ctxt.as_u32();
352return if new_ctxt32 <= MAX_CTXT {
353// Any small new context including zero will preserve the format.
354InlineCtxt::span(span.lo, span.len, new_ctxt32 as u16)
355 } else {
356 span.data().with_ctxt(new_ctxt)
357 };
358 },
359 InlineParent(span) => span.data(),
360 PartiallyInterned(span) => span.data(),
361 Interned(span) => span.data(),
362 };
363364data.with_ctxt(map(data.ctxt))
365 }
366367// Returns either syntactic context, if it can be retrieved without taking the interner lock,
368 // or an index into the interner if it cannot.
369#[inline]
370fn inline_ctxt(self) -> Result<SyntaxContext, usize> {
371if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
let span = InlineCtxt::from_span(self);
Ok(SyntaxContext::from_u16(span.ctxt))
} else {
let _span = InlineParent::from_span(self);
Ok(SyntaxContext::root())
}
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
let span = PartiallyInterned::from_span(self);
Ok(SyntaxContext::from_u16(span.ctxt))
} else { let span = Interned::from_span(self); Err(span.index as usize) }match_span_kind! {
372self,
373 InlineCtxt(span) => Ok(SyntaxContext::from_u16(span.ctxt)),
374 InlineParent(_span) => Ok(SyntaxContext::root()),
375 PartiallyInterned(span) => Ok(SyntaxContext::from_u16(span.ctxt)),
376 Interned(span) => Err(span.index as usize),
377 }378 }
379380/// This function is used as a fast path when decoding the full `SpanData` is not necessary.
381 /// It's a cut-down version of `data_untracked`.
382#[cfg_attr(not(test), rustc_diagnostic_item = "SpanCtxt")]
383 #[inline]
384pub fn ctxt(self) -> SyntaxContext {
385self.inline_ctxt()
386 .unwrap_or_else(|index| with_span_interner(|interner| interner.spans[index].ctxt))
387 }
388389#[inline]
390pub fn eq_ctxt(self, other: Span) -> bool {
391match (self.inline_ctxt(), other.inline_ctxt()) {
392 (Ok(ctxt1), Ok(ctxt2)) => ctxt1 == ctxt2,
393// If `inline_ctxt` returns `Ok` the context is <= MAX_CTXT.
394 // If it returns `Err` the span is fully interned and the context is > MAX_CTXT.
395 // As these do not overlap an `Ok` and `Err` result cannot have an equal context.
396(Ok(_), Err(_)) | (Err(_), Ok(_)) => false,
397 (Err(index1), Err(index2)) => with_span_interner(|interner| {
398interner.spans[index1].ctxt == interner.spans[index2].ctxt
399 }),
400 }
401 }
402403#[inline]
404pub fn with_parent(self, parent: Option<LocalDefId>) -> Span {
405let data = if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
let span = InlineCtxt::from_span(self);
{
match parent {
None => return self,
Some(parent) => {
let parent32 = parent.local_def_index.as_u32();
if span.ctxt == 0 && parent32 <= MAX_CTXT {
return InlineParent::span(span.lo, span.len,
parent32 as u16);
}
}
}
span.data()
}
} else { let span = InlineParent::from_span(self); span.data() }
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
let span = PartiallyInterned::from_span(self);
span.data()
} else { let span = Interned::from_span(self); span.data() }match_span_kind! {
406self,
407 InlineCtxt(span) => {
408// This format occurs 1-2 orders of magnitude more often than others (#126544),
409 // so it makes sense to micro-optimize it to avoid `span.data()` and `Span::new()`.
410 // Copypaste from `Span::new`, the small len & ctxt conditions are known to hold.
411match parent {
412None => return self,
413Some(parent) => {
414let parent32 = parent.local_def_index.as_u32();
415if span.ctxt == 0 && parent32 <= MAX_CTXT {
416return InlineParent::span(span.lo, span.len, parent32 as u16);
417 }
418 }
419 }
420 span.data()
421 },
422 InlineParent(span) => span.data(),
423 PartiallyInterned(span) => span.data(),
424 Interned(span) => span.data(),
425 };
426427if let Some(old_parent) = data.parent {
428 (*SPAN_TRACK)(old_parent);
429 }
430data.with_parent(parent)
431 }
432433#[inline]
434pub fn parent(self) -> Option<LocalDefId> {
435let interned_parent =
436 |index: u32| with_span_interner(|interner| interner.spans[indexas usize].parent);
437if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
let _span = InlineCtxt::from_span(self);
None
} else {
let span = InlineParent::from_span(self);
Some(LocalDefId { local_def_index: DefIndex::from_u16(span.parent) })
}
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
let span = PartiallyInterned::from_span(self);
interned_parent(span.index)
} else { let span = Interned::from_span(self); interned_parent(span.index) }match_span_kind! {
438self,
439 InlineCtxt(_span) => None,
440 InlineParent(span) => Some(LocalDefId { local_def_index: DefIndex::from_u16(span.parent) }),
441 PartiallyInterned(span) => interned_parent(span.index),
442 Interned(span) => interned_parent(span.index),
443 }444 }
445446#[inline]
447pub(crate) fn to_raw_span(self) -> RawSpan {
448// Field order must match `from_raw_span`.
449RawSpan(self.lo_or_index, self.len_with_tag_or_marker, self.ctxt_or_parent_or_marker)
450 }
451452#[inline]
453pub fn from_raw_span(RawSpan(a, b, c): RawSpan) -> Span {
454// Field order must match `to_raw_span`.
455Span { lo_or_index: a, len_with_tag_or_marker: b, ctxt_or_parent_or_marker: c }
456 }
457}
458459#[derive(#[automatically_derived]
impl ::core::default::Default for SpanInterner {
#[inline]
fn default() -> SpanInterner {
SpanInterner { spans: ::core::default::Default::default() }
}
}Default)]
460pub(crate) struct SpanInterner {
461 spans: FxIndexSet<SpanData>,
462}
463464impl SpanInterner {
465fn intern(&mut self, span_data: &SpanData) -> u32 {
466let (index, _) = self.spans.insert_full(*span_data);
467indexas u32468 }
469}
470471// If an interner exists, return it. Otherwise, prepare a fresh one.
472#[inline]
473fn with_span_interner<T, F: FnOnce(&mut SpanInterner) -> T>(f: F) -> T {
474crate::with_session_globals(|session_globals| f(&mut session_globals.span_interner.lock()))
475}