Skip to content

Commit 220f3ec

Browse files
committed
rustc_span: Remove transmutes from span encoding
1 parent 6fea953 commit 220f3ec

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

compiler/rustc_span/src/span_encoding.rs

+37-15
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use crate::SPAN_TRACK;
44
use crate::{BytePos, SpanData};
55

66
use rustc_data_structures::fx::FxIndexSet;
7+
78
// This code is very hot and uses lots of arithmetic, avoid overflow checks for performance.
89
// See https://github.com/rust-lang/rust/pull/119440#issuecomment-1874255727
910
use rustc_serialize::int_overflow::DebugStrictAdd;
10-
use std::mem::transmute;
1111

1212
/// A compressed span.
1313
///
@@ -105,15 +105,12 @@ struct InlineParent {
105105
#[derive(Clone, Copy)]
106106
struct PartiallyInterned {
107107
index: u32,
108-
_marker1: u16,
109108
ctxt: u16,
110109
}
111110

112111
#[derive(Clone, Copy)]
113112
struct Interned {
114113
index: u32,
115-
_marker1: u16,
116-
_marker2: u16,
117114
}
118115

119116
impl InlineCtxt {
@@ -130,7 +127,13 @@ impl InlineCtxt {
130127
}
131128
#[inline]
132129
fn span(lo: u32, len: u16, ctxt: u16) -> Span {
133-
unsafe { transmute(InlineCtxt { lo, len, ctxt }) }
130+
Span { lo_or_index: lo, len_with_tag_or_marker: len, ctxt_or_parent_or_marker: ctxt }
131+
}
132+
#[inline]
133+
fn from_span(span: Span) -> InlineCtxt {
134+
let (lo, len, ctxt) =
135+
(span.lo_or_index, span.len_with_tag_or_marker, span.ctxt_or_parent_or_marker);
136+
InlineCtxt { lo, len, ctxt }
134137
}
135138
}
136139

@@ -147,8 +150,16 @@ impl InlineParent {
147150
}
148151
}
149152
#[inline]
150-
fn span(lo: u32, len_with_tag: u16, parent: u16) -> Span {
151-
unsafe { transmute(InlineParent { lo, len_with_tag, parent }) }
153+
fn span(lo: u32, len: u16, parent: u16) -> Span {
154+
let (lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker) =
155+
(lo, PARENT_TAG | len, parent);
156+
Span { lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker }
157+
}
158+
#[inline]
159+
fn from_span(span: Span) -> InlineParent {
160+
let (lo, len_with_tag, parent) =
161+
(span.lo_or_index, span.len_with_tag_or_marker, span.ctxt_or_parent_or_marker);
162+
InlineParent { lo, len_with_tag, parent }
152163
}
153164
}
154165

@@ -162,7 +173,13 @@ impl PartiallyInterned {
162173
}
163174
#[inline]
164175
fn span(index: u32, ctxt: u16) -> Span {
165-
unsafe { transmute(PartiallyInterned { index, _marker1: BASE_LEN_INTERNED_MARKER, ctxt }) }
176+
let (lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker) =
177+
(index, BASE_LEN_INTERNED_MARKER, ctxt);
178+
Span { lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker }
179+
}
180+
#[inline]
181+
fn from_span(span: Span) -> PartiallyInterned {
182+
PartiallyInterned { index: span.lo_or_index, ctxt: span.ctxt_or_parent_or_marker }
166183
}
167184
}
168185

@@ -173,8 +190,13 @@ impl Interned {
173190
}
174191
#[inline]
175192
fn span(index: u32) -> Span {
176-
let _marker1 = BASE_LEN_INTERNED_MARKER;
177-
unsafe { transmute(Interned { index, _marker1, _marker2: CTXT_INTERNED_MARKER }) }
193+
let (lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker) =
194+
(index, BASE_LEN_INTERNED_MARKER, CTXT_INTERNED_MARKER);
195+
Span { lo_or_index, len_with_tag_or_marker, ctxt_or_parent_or_marker }
196+
}
197+
#[inline]
198+
fn from_span(span: Span) -> Interned {
199+
Interned { index: span.lo_or_index }
178200
}
179201
}
180202

@@ -192,20 +214,20 @@ macro_rules! match_span_kind {
192214
if $span.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
193215
if $span.len_with_tag_or_marker & PARENT_TAG == 0 {
194216
// Inline-context format.
195-
let $span1: InlineCtxt = unsafe { transmute($span) };
217+
let $span1 = InlineCtxt::from_span($span);
196218
$arm1
197219
} else {
198220
// Inline-parent format.
199-
let $span2: InlineParent = unsafe { transmute($span) };
221+
let $span2 = InlineParent::from_span($span);
200222
$arm2
201223
}
202224
} else if $span.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
203225
// Partially-interned format.
204-
let $span3: PartiallyInterned = unsafe { transmute($span) };
226+
let $span3 = PartiallyInterned::from_span($span);
205227
$arm3
206228
} else {
207229
// Interned format.
208-
let $span4: Interned = unsafe { transmute($span) };
230+
let $span4 = Interned::from_span($span);
209231
$arm4
210232
}
211233
};
@@ -245,7 +267,7 @@ impl Span {
245267
&& let parent32 = parent.local_def_index.as_u32()
246268
&& parent32 <= MAX_CTXT
247269
{
248-
return InlineParent::span(lo.0, PARENT_TAG | len as u16, parent32 as u16);
270+
return InlineParent::span(lo.0, len as u16, parent32 as u16);
249271
}
250272
}
251273

0 commit comments

Comments
 (0)