Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 55cac26

Browse files
committed
Auto merge of #126545 - petrochenkov:spimprove, r=BoxyUwU
rustc_span: Minor improvements Introduce `{IndexNewtype,SyntaxContext}::from_u16` for convenience because small indices are sometimes encoded as `u16`. Use `SpanData::span` instead of `Span::new` where appropriate. Add a clarifying comment about decoding span parents.
2 parents f6236f6 + 14da80c commit 55cac26

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

‎compiler/rustc_index_macros/src/newtype.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ impl Parse for Newtype {
205205
}
206206
}
207207

208+
/// Creates a new index from a given `u16`.
209+
///
210+
/// # Panics
211+
///
212+
/// Will panic if `value` exceeds `MAX`.
213+
#[inline]
214+
#vis const fn from_u16(value: u16) -> Self {
215+
let value = value as u32;
216+
assert!(value <= #max);
217+
// SAFETY: We just checked that `value <= max`.
218+
unsafe {
219+
Self::from_u32_unchecked(value)
220+
}
221+
}
222+
208223
/// Creates a new index from a given `u32`.
209224
///
210225
/// # Safety

‎compiler/rustc_infer/src/infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,13 +881,13 @@ impl<'tcx> InferCtxt<'tcx> {
881881
.collect();
882882
vars.extend(
883883
(0..inner.int_unification_table().len())
884-
.map(|i| ty::IntVid::from_u32(i asu32))
884+
.map(|i| ty::IntVid::from_usize(i))
885885
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_unknown())
886886
.map(|v| Ty::new_int_var(self.tcx, v)),
887887
);
888888
vars.extend(
889889
(0..inner.float_unification_table().len())
890-
.map(|i| ty::FloatVid::from_u32(i asu32))
890+
.map(|i| ty::FloatVid::from_usize(i))
891891
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_unknown())
892892
.map(|v| Ty::new_float_var(self.tcx, v)),
893893
);

‎compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> {
539539
} else {
540540
SpanData::decode(self)
541541
};
542-
Span::new(data.lo, data.hi, data.ctxt, data.parent)
542+
data.span()
543543
}
544544

545545
fn decode_symbol(&mut self) -> Symbol {
@@ -669,7 +669,7 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SpanData {
669669
let lo = lo + source_file.translated_source_file.start_pos;
670670
let hi = hi + source_file.translated_source_file.start_pos;
671671

672-
// Do not try to decode parent for foreign spans.
672+
// Do not try to decode parent for foreign spans (it wasn't encoded in the first place).
673673
SpanData { lo, hi, ctxt, parent: None }
674674
}
675675
}

‎compiler/rustc_span/src/hygiene.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,11 @@ impl SyntaxContext {
691691
SyntaxContext(raw)
692692
}
693693

694+
#[inline]
695+
pub(crate) const fn from_u16(raw: u16) -> SyntaxContext {
696+
SyntaxContext(raw as u32)
697+
}
698+
694699
/// Extend a syntax context with a given expansion and transparency.
695700
pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext {
696701
HygieneData::with(|data| data.apply_mark(self, expn_id, transparency))

‎compiler/rustc_span/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ impl Span {
967967
/// This span, but in a larger context, may switch to the metavariable span if suitable.
968968
pub fn with_neighbor(self, neighbor: Span) -> Span {
969969
match Span::prepare_to_combine(self, neighbor) {
970-
Ok((this, ..)) => Span::new(this.lo, this.hi, this.ctxt, this.parent),
970+
Ok((this, ..)) => this.span(),
971971
Err(_) => self,
972972
}
973973
}
@@ -1352,7 +1352,7 @@ impl fmt::Debug for Span {
13521352

13531353
impl fmt::Debug for SpanData {
13541354
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1355-
fmt::Debug::fmt(&Span::new(self.lo,self.hi,self.ctxt,self.parent), f)
1355+
fmt::Debug::fmt(&self.span(), f)
13561356
}
13571357
}
13581358

‎compiler/rustc_span/src/span_encoding.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl InlineCtxt {
121121
SpanData {
122122
lo: BytePos(self.lo),
123123
hi: BytePos(self.lo.debug_strict_add(len)),
124-
ctxt: SyntaxContext::from_u32(self.ctxtasu32),
124+
ctxt: SyntaxContext::from_u16(self.ctxt),
125125
parent: None,
126126
}
127127
}
@@ -146,7 +146,7 @@ impl InlineParent {
146146
lo: BytePos(self.lo),
147147
hi: BytePos(self.lo.debug_strict_add(len)),
148148
ctxt: SyntaxContext::root(),
149-
parent: Some(LocalDefId { local_def_index: DefIndex::from_u32(self.parentasu32) }),
149+
parent: Some(LocalDefId { local_def_index: DefIndex::from_u16(self.parent) }),
150150
}
151151
}
152152
#[inline]
@@ -167,7 +167,7 @@ impl PartiallyInterned {
167167
#[inline]
168168
fn data(self) -> SpanData {
169169
SpanData {
170-
ctxt: SyntaxContext::from_u32(self.ctxtasu32),
170+
ctxt: SyntaxContext::from_u16(self.ctxt),
171171
..with_span_interner(|interner| interner.spans[self.index as usize])
172172
}
173173
}
@@ -331,8 +331,7 @@ impl Span {
331331
match_span_kind! {
332332
self,
333333
InlineCtxt(span) => {
334-
updated_ctxt32 =
335-
update(SyntaxContext::from_u32(span.ctxt as u32)).as_u32();
334+
updated_ctxt32 = update(SyntaxContext::from_u16(span.ctxt)).as_u32();
336335
// Any small new context including zero will preserve the format.
337336
if updated_ctxt32 <= MAX_CTXT {
338337
return InlineCtxt::span(span.lo, span.len, updated_ctxt32 as u16);
@@ -349,7 +348,7 @@ impl Span {
349348
data = span.data();
350349
},
351350
PartiallyInterned(span) => {
352-
updated_ctxt32 = update(SyntaxContext::from_u32(span.ctxtasu32)).as_u32();
351+
updated_ctxt32 = update(SyntaxContext::from_u16(span.ctxt)).as_u32();
353352
// Any small new context excluding zero will preserve the format.
354353
// Zero may change the format to `InlineParent` if parent and len are small enough.
355354
if updated_ctxt32 <= MAX_CTXT && updated_ctxt32 != 0 {
@@ -373,9 +372,9 @@ impl Span {
373372
fn inline_ctxt(self) -> Result<SyntaxContext, usize> {
374373
match_span_kind! {
375374
self,
376-
InlineCtxt(span) => Ok(SyntaxContext::from_u32(span.ctxtasu32)),
375+
InlineCtxt(span) => Ok(SyntaxContext::from_u16(span.ctxt)),
377376
InlineParent(_span) => Ok(SyntaxContext::root()),
378-
PartiallyInterned(span) => Ok(SyntaxContext::from_u32(span.ctxtasu32)),
377+
PartiallyInterned(span) => Ok(SyntaxContext::from_u16(span.ctxt)),
379378
Interned(span) => Err(span.index as usize),
380379
}
381380
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /