@@ -4,10 +4,10 @@ use crate::SPAN_TRACK;
4
4
use crate :: { BytePos , SpanData } ;
5
5
6
6
use rustc_data_structures:: fx:: FxIndexSet ;
7
+
7
8
// This code is very hot and uses lots of arithmetic, avoid overflow checks for performance.
8
9
// See https://github.com/rust-lang/rust/pull/119440#issuecomment-1874255727
9
10
use rustc_serialize:: int_overflow:: DebugStrictAdd ;
10
- use std:: mem:: transmute;
11
11
12
12
/// A compressed span.
13
13
///
@@ -105,15 +105,12 @@ struct InlineParent {
105
105
#[ derive( Clone , Copy ) ]
106
106
struct PartiallyInterned {
107
107
index : u32 ,
108
- _marker1 : u16 ,
109
108
ctxt : u16 ,
110
109
}
111
110
112
111
#[ derive( Clone , Copy ) ]
113
112
struct Interned {
114
113
index : u32 ,
115
- _marker1 : u16 ,
116
- _marker2 : u16 ,
117
114
}
118
115
119
116
impl InlineCtxt {
@@ -130,7 +127,13 @@ impl InlineCtxt {
130
127
}
131
128
#[ inline]
132
129
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 }
134
137
}
135
138
}
136
139
@@ -147,8 +150,16 @@ impl InlineParent {
147
150
}
148
151
}
149
152
#[ 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 }
152
163
}
153
164
}
154
165
@@ -162,7 +173,13 @@ impl PartiallyInterned {
162
173
}
163
174
#[ inline]
164
175
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 }
166
183
}
167
184
}
168
185
@@ -173,8 +190,13 @@ impl Interned {
173
190
}
174
191
#[ inline]
175
192
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 }
178
200
}
179
201
}
180
202
@@ -192,20 +214,20 @@ macro_rules! match_span_kind {
192
214
if $span. len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
193
215
if $span. len_with_tag_or_marker & PARENT_TAG == 0 {
194
216
// Inline-context format.
195
- let $span1: InlineCtxt = unsafe { transmute ( $span) } ;
217
+ let $span1 = InlineCtxt :: from_span ( $span) ;
196
218
$arm1
197
219
} else {
198
220
// Inline-parent format.
199
- let $span2: InlineParent = unsafe { transmute ( $span) } ;
221
+ let $span2 = InlineParent :: from_span ( $span) ;
200
222
$arm2
201
223
}
202
224
} else if $span. ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
203
225
// Partially-interned format.
204
- let $span3: PartiallyInterned = unsafe { transmute ( $span) } ;
226
+ let $span3 = PartiallyInterned :: from_span ( $span) ;
205
227
$arm3
206
228
} else {
207
229
// Interned format.
208
- let $span4: Interned = unsafe { transmute ( $span) } ;
230
+ let $span4 = Interned :: from_span ( $span) ;
209
231
$arm4
210
232
}
211
233
} ;
@@ -245,7 +267,7 @@ impl Span {
245
267
&& let parent32 = parent. local_def_index . as_u32 ( )
246
268
&& parent32 <= MAX_CTXT
247
269
{
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 ) ;
249
271
}
250
272
}
251
273
0 commit comments